// #target: macos-x64
extern fn char*? readLine();
extern fn int? atoi(char*);

extern fn int printf(char* fmt, ...);

fn void main()
{
    char*? line = readLine();
    if (try line)
    {
        int? val = atoi(line);
        if (try val)
        {
            printf("You typed the number %d\n", val);
            return;
        }
    }
    printf("You didn't type an integer :(\n");
}

/* #expect: try_with_unwrap.ll

define void @try_with_unwrap.main() #0 {
entry:
  %line = alloca ptr, align 8
  %line.f = alloca i64, align 8
  %retparam = alloca ptr, align 8
  %val = alloca i32, align 4
  %val.f = alloca i64, align 8
  %retparam1 = alloca i32, align 4
  %0 = call i64 @readLine(ptr %retparam)
  %not_err = icmp eq i64 %0, 0
  %1 = call i1 @llvm.expect.i1(i1 %not_err, i1 true)
  br i1 %1, label %after_check, label %assign_optional

assign_optional:                                  ; preds = %entry
  store i64 %0, ptr %line.f, align 8
  br label %after_assign

after_check:                                      ; preds = %entry
  %2 = load ptr, ptr %retparam, align 8
  store ptr %2, ptr %line, align 8
  store i64 0, ptr %line.f, align 8
  br label %after_assign

after_assign:                                     ; preds = %after_check, %assign_optional
  %load.err = load i64, ptr %line.f, align 8
  %result = icmp eq i64 %load.err, 0
  br i1 %result, label %if.then, label %if.exit9

if.then:                                          ; preds = %after_assign
  %3 = load ptr, ptr %line, align 8
  %4 = call i64 @atoi(ptr %retparam1, ptr %3)
  %not_err2 = icmp eq i64 %4, 0
  %5 = call i1 @llvm.expect.i1(i1 %not_err2, i1 true)
  br i1 %5, label %after_check4, label %assign_optional3

assign_optional3:                                 ; preds = %if.then
  store i64 %4, ptr %val.f, align 8
  br label %after_assign5

after_check4:                                     ; preds = %if.then
  %6 = load i32, ptr %retparam1, align 4
  store i32 %6, ptr %val, align 4
  store i64 0, ptr %val.f, align 8
  br label %after_assign5

after_assign5:                                    ; preds = %after_check4, %assign_optional3
  %load.err6 = load i64, ptr %val.f, align 8
  %result7 = icmp eq i64 %load.err6, 0
  br i1 %result7, label %if.then8, label %if.exit

if.then8:                                         ; preds = %after_assign5
  %7 = load i32, ptr %val, align 4
  %8 = call i32 (ptr, ...) @printf(ptr @.str, i32 %7)
  ret void

if.exit:                                          ; preds = %after_assign5
  br label %if.exit9

if.exit9:                                         ; preds = %if.exit, %after_assign
  %9 = call i32 (ptr, ...) @printf(ptr @.str.1)
  ret void
}

