// #target: macos-x64

module test;
import std::io;
faultdef MY_VAL1, MY_VAL2;

struct Bar
{
    int x;
}

fn void? test1() @maydiscard
{
    Bar? x = MY_VAL1~;
    Bar y = x!;
}

fn void? test2() @maydiscard
{
    Bar? x = {};
    Bar y = x!;
}

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

/* #expect: test.ll

define i64 @test.test1() #0 {
entry:
  %x = alloca %Bar, align 4
  %x.f = alloca i64, align 8
  %y = alloca %Bar, align 4
  %error_var = alloca i64, align 8
  store i64 ptrtoint (ptr @test.MY_VAL1 to i64), ptr %x.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 %error_var, align 8
  br label %guard_block

after_check:                                      ; preds = %entry
  br label %noerr_block

guard_block:                                      ; preds = %assign_optional
  %1 = load i64, ptr %error_var, align 8
  ret i64 %1

noerr_block:                                      ; preds = %after_check
  call void @llvm.memcpy.p0.p0.i32(ptr align 4 %y, ptr align 4 %x, i32 4, i1 false)
  ret i64 0
}


define i64 @test.test2() #0 {
entry:
  %x = alloca %Bar, align 4
  %x.f = alloca i64, align 8
  %y = alloca %Bar, align 4
  %error_var = alloca i64, align 8
  store i32 0, ptr %x, align 4
  store i64 0, ptr %x.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 %error_var, align 8
  br label %guard_block

after_check:                                      ; preds = %entry
  br label %noerr_block

guard_block:                                      ; preds = %assign_optional
  %1 = load i64, ptr %error_var, align 8
  ret i64 %1

noerr_block:                                      ; preds = %after_check
  call void @llvm.memcpy.p0.p0.i32(ptr align 4 %y, ptr align 4 %x, i32 4, i1 false)
  ret i64 0
}


define void @test.main() #0 {
entry:
  %0 = call i64 @test.test1()
  %1 = call i64 @test.test2()
  ret void
}
