// #target: macos-x64
module test;
import std::io;
alias Func = fn int(int y);
alias FuncOther = fn bool(char*);
alias FuncSame = fn int(int z = 444);

fn int test(int a) { return a; }
fn int test2(int b = 3) { return b; }

fn int? test3(int b = 3) { return b; }
fn int? test4(int b = 3) { return b; }

fn void main()
{
	Func a = &test;
	Func b = &test2;
	io::printfn("%d", a(y: 123));
	io::printfn("%d", (&test2)());
	FuncSame z = &test2;
	io::printfn("%d", z());
	io::printfn("%s", $typeof(b ? &test2 : &test).qnameof);
	io::printfn("%s", $typeof(b ? &test2 : &test).nameof);
	io::printfn("%s", $typeof(&test2).qnameof);
	io::printfn("%s", $typeof(&test2).nameof);
	io::printfn("%s", $typeof(b ? &test4 : &test3).nameof);

	$typeof(b ? &test2 : &test) y = &test2;
	typeid zfoke = $typeof(y).typeid;
}

/* #expect: test.ll

@.str = private unnamed_addr constant [3 x i8] c"%d\00", align 1
@"$ct.int" = linkonce global %.introspect { i8 2, i64 0, ptr null, i64 4, i64 0, i64 0, [0 x i64] zeroinitializer }, align 8
@.str.1 = private unnamed_addr constant [3 x i8] c"%d\00", align 1
@.str.2 = private unnamed_addr constant [3 x i8] c"%d\00", align 1
@.str.3 = private unnamed_addr constant [3 x i8] c"%s\00", align 1
@.str.4 = private unnamed_addr constant [12 x i8] c"fn int(int)\00", align 1
@"$ct.String" = linkonce global %.introspect { i8 18, i64 ptrtoint (ptr @"$ct.sa$char" to i64), ptr null, i64 16, i64 ptrtoint (ptr @"$ct.sa$char" to i64), i64 0, [0 x i64] zeroinitializer }, align 8
@"$ct.sa$char" = linkonce global %.introspect { i8 16, i64 0, ptr null, i64 16, i64 ptrtoint (ptr @"$ct.char" to i64), i64 0, [0 x i64] zeroinitializer }, align 8
@"$ct.char" = linkonce global %.introspect { i8 3, i64 0, ptr null, i64 1, i64 0, i64 0, [0 x i64] zeroinitializer }, align 8
@.str.5 = private unnamed_addr constant [3 x i8] c"%s\00", align 1
@.str.6 = private unnamed_addr constant [12 x i8] c"fn int(int)\00", align 1
@.str.7 = private unnamed_addr constant [3 x i8] c"%s\00", align 1
@.str.8 = private unnamed_addr constant [12 x i8] c"fn int(int)\00", align 1
@.str.9 = private unnamed_addr constant [3 x i8] c"%s\00", align 1
@.str.10 = private unnamed_addr constant [12 x i8] c"fn int(int)\00", align 1
@.str.11 = private unnamed_addr constant [3 x i8] c"%s\00", align 1
@.str.12 = private unnamed_addr constant [13 x i8] c"fn int?(int)\00", align 1
@"$ct.fn$int$int$" = linkonce global %.introspect { i8 13, i64 0, ptr null, i64 8, i64 0, i64 0, [0 x i64] zeroinitializer }, align 8

define void @test.main() #0 {
entry:
  %a = alloca ptr, align 8
  %b = alloca ptr, align 8
  %varargslots = alloca [1 x %any], align 16
  %taddr = alloca i32, align 4
  %retparam = alloca i64, align 8
  %varargslots1 = alloca [1 x %any], align 16
  %taddr2 = alloca i32, align 4
  %retparam3 = alloca i64, align 8
  %z = alloca ptr, align 8
  %varargslots4 = alloca [1 x %any], align 16
  %taddr5 = alloca i32, align 4
  %retparam6 = alloca i64, align 8
  %varargslots7 = alloca [1 x %any], align 16
  %taddr8 = alloca %"char[]", align 8
  %retparam9 = alloca i64, align 8
  %varargslots10 = alloca [1 x %any], align 16
  %taddr11 = alloca %"char[]", align 8
  %retparam12 = alloca i64, align 8
  %varargslots13 = alloca [1 x %any], align 16
  %taddr14 = alloca %"char[]", align 8
  %retparam15 = alloca i64, align 8
  %varargslots16 = alloca [1 x %any], align 16
  %taddr17 = alloca %"char[]", align 8
  %retparam18 = alloca i64, align 8
  %varargslots19 = alloca [1 x %any], align 16
  %taddr20 = alloca %"char[]", align 8
  %retparam21 = alloca i64, align 8
  %y = alloca ptr, align 8
  %zfoke = alloca i64, align 8
  store ptr @test.test, ptr %a, align 8
  store ptr @test.test2, ptr %b, align 8
  %0 = load ptr, ptr %a, align 8
  %1 = call i32 %0(i32 123)
  store i32 %1, ptr %taddr, align 4
  %2 = insertvalue %any undef, ptr %taddr, 0
  %3 = insertvalue %any %2, i64 ptrtoint (ptr @"$ct.int" to i64), 1
  store %any %3, ptr %varargslots, align 16
  %4 = call i64 @std.io.printfn(ptr %retparam, ptr @.str, i64 2, ptr %varargslots, i64 1)
  %5 = call i32 @test.test2(i32 3)
  store i32 %5, ptr %taddr2, align 4
  %6 = insertvalue %any undef, ptr %taddr2, 0
  %7 = insertvalue %any %6, i64 ptrtoint (ptr @"$ct.int" to i64), 1
  store %any %7, ptr %varargslots1, align 16
  %8 = call i64 @std.io.printfn(ptr %retparam3, ptr @.str.1, i64 2, ptr %varargslots1, i64 1)
  store ptr @test.test2, ptr %z, align 8
  %9 = load ptr, ptr %z, align 8
  %10 = call i32 %9(i32 444)
  store i32 %10, ptr %taddr5, align 4
  %11 = insertvalue %any undef, ptr %taddr5, 0
  %12 = insertvalue %any %11, i64 ptrtoint (ptr @"$ct.int" to i64), 1
  store %any %12, ptr %varargslots4, align 16
  %13 = call i64 @std.io.printfn(ptr %retparam6, ptr @.str.2, i64 2, ptr %varargslots4, i64 1)
  store %"char[]" { ptr @.str.4, i64 11 }, ptr %taddr8, align 8
  %14 = insertvalue %any undef, ptr %taddr8, 0
  %15 = insertvalue %any %14, i64 ptrtoint (ptr @"$ct.String" to i64), 1
  store %any %15, ptr %varargslots7, align 16
  %16 = call i64 @std.io.printfn(ptr %retparam9, ptr @.str.3, i64 2, ptr %varargslots7, i64 1)
  store %"char[]" { ptr @.str.6, i64 11 }, ptr %taddr11, align 8
  %17 = insertvalue %any undef, ptr %taddr11, 0
  %18 = insertvalue %any %17, i64 ptrtoint (ptr @"$ct.String" to i64), 1
  store %any %18, ptr %varargslots10, align 16
  %19 = call i64 @std.io.printfn(ptr %retparam12, ptr @.str.5, i64 2, ptr %varargslots10, i64 1)
  store %"char[]" { ptr @.str.8, i64 11 }, ptr %taddr14, align 8
  %20 = insertvalue %any undef, ptr %taddr14, 0
  %21 = insertvalue %any %20, i64 ptrtoint (ptr @"$ct.String" to i64), 1
  store %any %21, ptr %varargslots13, align 16
  %22 = call i64 @std.io.printfn(ptr %retparam15, ptr @.str.7, i64 2, ptr %varargslots13, i64 1)
  store %"char[]" { ptr @.str.10, i64 11 }, ptr %taddr17, align 8
  %23 = insertvalue %any undef, ptr %taddr17, 0
  %24 = insertvalue %any %23, i64 ptrtoint (ptr @"$ct.String" to i64), 1
  store %any %24, ptr %varargslots16, align 16
  %25 = call i64 @std.io.printfn(ptr %retparam18, ptr @.str.9, i64 2, ptr %varargslots16, i64 1)
  store %"char[]" { ptr @.str.12, i64 12 }, ptr %taddr20, align 8
  %26 = insertvalue %any undef, ptr %taddr20, 0
  %27 = insertvalue %any %26, i64 ptrtoint (ptr @"$ct.String" to i64), 1
  store %any %27, ptr %varargslots19, align 16
  %28 = call i64 @std.io.printfn(ptr %retparam21, ptr @.str.11, i64 2, ptr %varargslots19, i64 1)
  store ptr @test.test2, ptr %y, align 8
  store i64 ptrtoint (ptr @"$ct.fn$int$int$" to i64), ptr %zfoke, align 8
  ret void
}
