// #target: macos-x64
module test;
import std::io;

faultdef FOO;

fn void test1()
{
	int? a = 1;
	int? b = a = FOO~;
	if (catch err = a) io::printfn("A err was: %s", err);
	if (catch err = b) io::printfn("B err was: %s", err);
	(void)io::printfn("A was: %s", a);
	(void)io::printfn("B was: %s", b);
}

fn void test2()
{
	int? x = FOO~;
	int? a = 1;
	int? b = a = x;
	if (catch err = a) io::printfn("A err was: %s", err);
	if (catch err = b) io::printfn("B err was: %s", err);
	(void)io::printfn("A was: %s", a);
	(void)io::printfn("B was: %s", b);
}


fn void test3()
{
	int? x = 23;
	int? a = 1;
	int? b = a = x;
	if (catch err = a) io::printfn("A err was: %s", err);
	if (catch err = b) io::printfn("B err was: %s", err);
	(void)io::printfn("A was: %s", a);
	(void)io::printfn("B was: %s", b);
}

fn void main()
{
	test1();
	test2();
	test3();
}

/* #expect: test.ll

define void @test.test1() #0 {
entry:
  %a = alloca i32, align 4
  %a.f = alloca i64, align 8
  %b = alloca i32, align 4
  %b.f = alloca i64, align 8
  %err = alloca i64, align 8
  %varargslots = alloca [1 x %any], align 16
  %retparam = alloca i64, align 8
  %err3 = alloca i64, align 8
  %varargslots12 = alloca [1 x %any], align 16
  %retparam13 = alloca i64, align 8
  %varargslots17 = alloca [1 x %any], align 16
  %retparam21 = alloca i64, align 8
  %varargslots24 = alloca [1 x %any], align 16
  %retparam28 = alloca i64, align 8
  store i32 1, ptr %a, align 4
  store i64 0, ptr %a.f, align 8
  store i64 ptrtoint (ptr @test.FOO to i64), ptr %a.f, align 8
  store i64 ptrtoint (ptr @test.FOO to i64), ptr %b.f, align 8
  br label %after_assign

after_assign:                                     ; preds = %entry
  br label %testblock

testblock:                                        ; preds = %after_assign
  %optval = load i64, ptr %a.f, align 8
  %not_err = icmp eq i64 %optval, 0
  %0 = call i1 @llvm.expect.i1(i1 %not_err, i1 true)
  br i1 %0, label %after_check, label %assign_optional

assign_optional:                                  ; preds = %testblock
  store i64 %optval, ptr %err, align 8
  br label %end_block

after_check:                                      ; preds = %testblock
  store i64 0, ptr %err, align 8
  br label %end_block

end_block:                                        ; preds = %after_check, %assign_optional
  %1 = load i64, ptr %err, align 8
  %i2b = icmp ne i64 %1, 0
  br i1 %i2b, label %if.then, label %if.exit

if.then:                                          ; preds = %end_block
  %2 = insertvalue %any undef, ptr %err, 0
  %3 = insertvalue %any %2, i64 ptrtoint (ptr @"$ct.fault" to i64), 1
  store %any %3, ptr %varargslots, align 16
  %4 = call i64 @std.io.printfn(ptr %retparam, ptr @.str, i64 13, ptr %varargslots, i64 1)
  br label %if.exit

if.exit:                                          ; preds = %if.then, %end_block
  br label %testblock4

testblock4:                                       ; preds = %if.exit
  %optval5 = load i64, ptr %b.f, align 8
  %not_err6 = icmp eq i64 %optval5, 0
  %5 = call i1 @llvm.expect.i1(i1 %not_err6, i1 true)
  br i1 %5, label %after_check8, label %assign_optional7

assign_optional7:                                 ; preds = %testblock4
  store i64 %optval5, ptr %err3, align 8
  br label %end_block9

after_check8:                                     ; preds = %testblock4
  store i64 0, ptr %err3, align 8
  br label %end_block9

end_block9:                                       ; preds = %after_check8, %assign_optional7
  %6 = load i64, ptr %err3, align 8
  %i2b10 = icmp ne i64 %6, 0
  br i1 %i2b10, label %if.then11, label %if.exit16

if.then11:                                        ; preds = %end_block9
  %7 = insertvalue %any undef, ptr %err3, 0
  %8 = insertvalue %any %7, i64 ptrtoint (ptr @"$ct.fault" to i64), 1
  store %any %8, ptr %varargslots12, align 16
  %9 = call i64 @std.io.printfn(ptr %retparam13, ptr @.str.1, i64 13, ptr %varargslots12, i64 1)
  br label %if.exit16

if.exit16:                                        ; preds = %if.then11, %end_block9
  %optval18 = load i64, ptr %a.f, align 8
  %not_err19 = icmp eq i64 %optval18, 0
  %10 = call i1 @llvm.expect.i1(i1 %not_err19, i1 true)
  br i1 %10, label %after_check20, label %after_check23

after_check20:                                    ; preds = %if.exit16
  %11 = insertvalue %any undef, ptr %a, 0
  %12 = insertvalue %any %11, i64 ptrtoint (ptr @"$ct.int" to i64), 1
  store %any %12, ptr %varargslots17, align 16
  %13 = call i64 @std.io.printfn(ptr %retparam21, ptr @.str.2, i64 9, ptr %varargslots17, i64 1)
  %not_err22 = icmp eq i64 %13, 0
  %14 = call i1 @llvm.expect.i1(i1 %not_err22, i1 true)
  br i1 %14, label %after_check23, label %after_check23

after_check23:                                    ; preds = %if.exit16, %after_check20, %after_check20
  %optval25 = load i64, ptr %b.f, align 8
  %not_err26 = icmp eq i64 %optval25, 0
  %15 = call i1 @llvm.expect.i1(i1 %not_err26, i1 true)
  br i1 %15, label %after_check27, label %after_check30

after_check27:                                    ; preds = %after_check23
  %16 = insertvalue %any undef, ptr %b, 0
  %17 = insertvalue %any %16, i64 ptrtoint (ptr @"$ct.int" to i64), 1
  store %any %17, ptr %varargslots24, align 16
  %18 = call i64 @std.io.printfn(ptr %retparam28, ptr @.str.3, i64 9, ptr %varargslots24, i64 1)
  %not_err29 = icmp eq i64 %18, 0
  %19 = call i1 @llvm.expect.i1(i1 %not_err29, i1 true)
  br i1 %19, label %after_check30, label %after_check30

after_check30:                                    ; preds = %after_check23, %after_check27, %after_check27
  ret void
}

; Function Attrs:
define void @test.test2() #0 {
entry:
  %x = alloca i32, align 4
  %x.f = alloca i64, align 8
  %a = alloca i32, align 4
  %a.f = alloca i64, align 8
  %b = alloca i32, align 4
  %b.f = alloca i64, align 8
  %err = alloca i64, align 8
  %varargslots = alloca [1 x %any], align 16
  %retparam = alloca i64, align 8
  %err8 = alloca i64, align 8
  %varargslots17 = alloca [1 x %any], align 16
  %retparam18 = alloca i64, align 8
  %varargslots22 = alloca [1 x %any], align 16
  %retparam26 = alloca i64, align 8
  %varargslots29 = alloca [1 x %any], align 16
  %retparam33 = alloca i64, align 8
  store i64 ptrtoint (ptr @test.FOO to i64), ptr %x.f, align 8
  store i32 1, ptr %a, align 4
  store i64 0, ptr %a.f, align 8
  %optval = load i64, ptr %x.f, align 8
  %not_err = icmp eq i64 %optval, 0
  %0 = call i1 @llvm.expect.i1(i1 %not_err, i1 true)
  br i1 %0, label %after_check, label %assign_optional

assign_optional:                                  ; preds = %entry
  store i64 %optval, ptr %a.f, align 8
  br label %optional_assign_jump

after_check:                                      ; preds = %entry
  %1 = load i32, ptr %x, align 4
  store i32 %1, ptr %a, align 4
  store i64 0, ptr %a.f, align 8
  br label %after_assign

optional_assign_jump:                             ; preds = %assign_optional
  %reload_err = load i64, ptr %a.f, align 8
  store i64 %reload_err, ptr %b.f, align 8
  br label %after_assign1

after_assign:                                     ; preds = %after_check
  store i32 %1, ptr %b, align 4
  store i64 0, ptr %b.f, align 8
  br label %after_assign1

after_assign1:                                    ; preds = %after_assign, %optional_assign_jump
  br label %testblock

testblock:                                        ; preds = %after_assign1
  %optval2 = load i64, ptr %a.f, align 8
  %not_err3 = icmp eq i64 %optval2, 0
  %2 = call i1 @llvm.expect.i1(i1 %not_err3, i1 true)
  br i1 %2, label %after_check5, label %assign_optional4

assign_optional4:                                 ; preds = %testblock
  store i64 %optval2, ptr %err, align 8
  br label %end_block

after_check5:                                     ; preds = %testblock
  store i64 0, ptr %err, align 8
  br label %end_block

end_block:                                        ; preds = %after_check5, %assign_optional4
  %3 = load i64, ptr %err, align 8
  %i2b = icmp ne i64 %3, 0
  br i1 %i2b, label %if.then, label %if.exit

if.then:                                          ; preds = %end_block
  %4 = insertvalue %any undef, ptr %err, 0
  %5 = insertvalue %any %4, i64 ptrtoint (ptr @"$ct.fault" to i64), 1
  store %any %5, ptr %varargslots, align 16
  %6 = call i64 @std.io.printfn(ptr %retparam, ptr @.str.4, i64 13, ptr %varargslots, i64 1)
  br label %if.exit

if.exit:                                          ; preds = %if.then, %end_block
  br label %testblock9

testblock9:                                       ; preds = %if.exit
  %optval10 = load i64, ptr %b.f, align 8
  %not_err11 = icmp eq i64 %optval10, 0
  %7 = call i1 @llvm.expect.i1(i1 %not_err11, i1 true)
  br i1 %7, label %after_check13, label %assign_optional12

assign_optional12:                                ; preds = %testblock9
  store i64 %optval10, ptr %err8, align 8
  br label %end_block14

after_check13:                                    ; preds = %testblock9
  store i64 0, ptr %err8, align 8
  br label %end_block14

end_block14:                                      ; preds = %after_check13, %assign_optional12
  %8 = load i64, ptr %err8, align 8
  %i2b15 = icmp ne i64 %8, 0
  br i1 %i2b15, label %if.then16, label %if.exit21

if.then16:                                        ; preds = %end_block14
  %9 = insertvalue %any undef, ptr %err8, 0
  %10 = insertvalue %any %9, i64 ptrtoint (ptr @"$ct.fault" to i64), 1
  store %any %10, ptr %varargslots17, align 16
  %11 = call i64 @std.io.printfn(ptr %retparam18, ptr @.str.5, i64 13, ptr %varargslots17, i64 1)
  br label %if.exit21

if.exit21:                                        ; preds = %if.then16, %end_block14
  %optval23 = load i64, ptr %a.f, align 8
  %not_err24 = icmp eq i64 %optval23, 0
  %12 = call i1 @llvm.expect.i1(i1 %not_err24, i1 true)
  br i1 %12, label %after_check25, label %after_check28

after_check25:                                    ; preds = %if.exit21
  %13 = insertvalue %any undef, ptr %a, 0
  %14 = insertvalue %any %13, i64 ptrtoint (ptr @"$ct.int" to i64), 1
  store %any %14, ptr %varargslots22, align 16
  %15 = call i64 @std.io.printfn(ptr %retparam26, ptr @.str.6, i64 9, ptr %varargslots22, i64 1)
  %not_err27 = icmp eq i64 %15, 0
  %16 = call i1 @llvm.expect.i1(i1 %not_err27, i1 true)
  br i1 %16, label %after_check28, label %after_check28

after_check28:                                    ; preds = %if.exit21, %after_check25, %after_check25
  %optval30 = load i64, ptr %b.f, align 8
  %not_err31 = icmp eq i64 %optval30, 0
  %17 = call i1 @llvm.expect.i1(i1 %not_err31, i1 true)
  br i1 %17, label %after_check32, label %after_check35

after_check32:                                    ; preds = %after_check28
  %18 = insertvalue %any undef, ptr %b, 0
  %19 = insertvalue %any %18, i64 ptrtoint (ptr @"$ct.int" to i64), 1
  store %any %19, ptr %varargslots29, align 16
  %20 = call i64 @std.io.printfn(ptr %retparam33, ptr @.str.7, i64 9, ptr %varargslots29, i64 1)
  %not_err34 = icmp eq i64 %20, 0
  %21 = call i1 @llvm.expect.i1(i1 %not_err34, i1 true)
  br i1 %21, label %after_check35, label %after_check35

after_check35:                                    ; preds = %after_check28, %after_check32, %after_check32
  ret void
}

; Function Attrs:
define void @test.test3() #0 {
entry:
  %x = alloca i32, align 4
  %x.f = alloca i64, align 8
  %a = alloca i32, align 4
  %a.f = alloca i64, align 8
  %b = alloca i32, align 4
  %b.f = alloca i64, align 8
  %err = alloca i64, align 8
  %varargslots = alloca [1 x %any], align 16
  %retparam = alloca i64, align 8
  %err8 = alloca i64, align 8
  %varargslots17 = alloca [1 x %any], align 16
  %retparam18 = alloca i64, align 8
  %varargslots22 = alloca [1 x %any], align 16
  %retparam26 = alloca i64, align 8
  %varargslots29 = alloca [1 x %any], align 16
  %retparam33 = alloca i64, align 8
  store i32 23, ptr %x, align 4
  store i64 0, ptr %x.f, align 8
  store i32 1, ptr %a, align 4
  store i64 0, ptr %a.f, align 8
  %optval = load i64, ptr %x.f, align 8
  %not_err = icmp eq i64 %optval, 0
  %0 = call i1 @llvm.expect.i1(i1 %not_err, i1 true)
  br i1 %0, label %after_check, label %assign_optional

assign_optional:                                  ; preds = %entry
  store i64 %optval, ptr %a.f, align 8
  br label %optional_assign_jump

after_check:                                      ; preds = %entry
  %1 = load i32, ptr %x, align 4
  store i32 %1, ptr %a, align 4
  store i64 0, ptr %a.f, align 8
  br label %after_assign

optional_assign_jump:                             ; preds = %assign_optional
  %reload_err = load i64, ptr %a.f, align 8
  store i64 %reload_err, ptr %b.f, align 8
  br label %after_assign1

after_assign:                                     ; preds = %after_check
  store i32 %1, ptr %b, align 4
  store i64 0, ptr %b.f, align 8
  br label %after_assign1

after_assign1:                                    ; preds = %after_assign, %optional_assign_jump
  br label %testblock

testblock:                                        ; preds = %after_assign1
  %optval2 = load i64, ptr %a.f, align 8
  %not_err3 = icmp eq i64 %optval2, 0
  %2 = call i1 @llvm.expect.i1(i1 %not_err3, i1 true)
  br i1 %2, label %after_check5, label %assign_optional4

assign_optional4:                                 ; preds = %testblock
  store i64 %optval2, ptr %err, align 8
  br label %end_block

after_check5:                                     ; preds = %testblock
  store i64 0, ptr %err, align 8
  br label %end_block

end_block:                                        ; preds = %after_check5, %assign_optional4
  %3 = load i64, ptr %err, align 8
  %i2b = icmp ne i64 %3, 0
  br i1 %i2b, label %if.then, label %if.exit

if.then:                                          ; preds = %end_block
  %4 = insertvalue %any undef, ptr %err, 0
  %5 = insertvalue %any %4, i64 ptrtoint (ptr @"$ct.fault" to i64), 1
  store %any %5, ptr %varargslots, align 16
  %6 = call i64 @std.io.printfn(ptr %retparam, ptr @.str.8, i64 13, ptr %varargslots, i64 1)
  br label %if.exit

if.exit:                                          ; preds = %if.then, %end_block
  br label %testblock9

testblock9:                                       ; preds = %if.exit
  %optval10 = load i64, ptr %b.f, align 8
  %not_err11 = icmp eq i64 %optval10, 0
  %7 = call i1 @llvm.expect.i1(i1 %not_err11, i1 true)
  br i1 %7, label %after_check13, label %assign_optional12

assign_optional12:                                ; preds = %testblock9
  store i64 %optval10, ptr %err8, align 8
  br label %end_block14

after_check13:                                    ; preds = %testblock9
  store i64 0, ptr %err8, align 8
  br label %end_block14

end_block14:                                      ; preds = %after_check13, %assign_optional12
  %8 = load i64, ptr %err8, align 8
  %i2b15 = icmp ne i64 %8, 0
  br i1 %i2b15, label %if.then16, label %if.exit21

if.then16:                                        ; preds = %end_block14
  %9 = insertvalue %any undef, ptr %err8, 0
  %10 = insertvalue %any %9, i64 ptrtoint (ptr @"$ct.fault" to i64), 1
  store %any %10, ptr %varargslots17, align 16
  %11 = call i64 @std.io.printfn(ptr %retparam18, ptr @.str.9, i64 13, ptr %varargslots17, i64 1)
  br label %if.exit21

if.exit21:                                        ; preds = %if.then16, %end_block14
  %optval23 = load i64, ptr %a.f, align 8
  %not_err24 = icmp eq i64 %optval23, 0
  %12 = call i1 @llvm.expect.i1(i1 %not_err24, i1 true)
  br i1 %12, label %after_check25, label %after_check28

after_check25:                                    ; preds = %if.exit21
  %13 = insertvalue %any undef, ptr %a, 0
  %14 = insertvalue %any %13, i64 ptrtoint (ptr @"$ct.int" to i64), 1
  store %any %14, ptr %varargslots22, align 16
  %15 = call i64 @std.io.printfn(ptr %retparam26, ptr @.str.10, i64 9, ptr %varargslots22, i64 1)
  %not_err27 = icmp eq i64 %15, 0
  %16 = call i1 @llvm.expect.i1(i1 %not_err27, i1 true)
  br i1 %16, label %after_check28, label %after_check28

after_check28:                                    ; preds = %if.exit21, %after_check25, %after_check25
  %optval30 = load i64, ptr %b.f, align 8
  %not_err31 = icmp eq i64 %optval30, 0
  %17 = call i1 @llvm.expect.i1(i1 %not_err31, i1 true)
  br i1 %17, label %after_check32, label %after_check35

after_check32:                                    ; preds = %after_check28
  %18 = insertvalue %any undef, ptr %b, 0
  %19 = insertvalue %any %18, i64 ptrtoint (ptr @"$ct.int" to i64), 1
  store %any %19, ptr %varargslots29, align 16
  %20 = call i64 @std.io.printfn(ptr %retparam33, ptr @.str.11, i64 9, ptr %varargslots29, i64 1)
  %not_err34 = icmp eq i64 %20, 0
  %21 = call i1 @llvm.expect.i1(i1 %not_err34, i1 true)
  br i1 %21, label %after_check35, label %after_check35

after_check35:                                    ; preds = %after_check28, %after_check32, %after_check32
  ret void
}
