// #target: macos-x64

extern fn char*? readLine();
extern fn int? atoi(char*);

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

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

/* #expect: try_with_chained_unwrap.ll


define void @try_with_chained_unwrap.main() #0 {
entry:
  %val = alloca i32, align 4
  %retparam = alloca ptr, align 8
  %retparam1 = alloca i32, align 4
  store i32 0, ptr %val, 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 %catch_landing

after_check:                                      ; preds = %entry
  %2 = load ptr, ptr %retparam, align 8
  %3 = call i64 @atoi(ptr %retparam1, ptr %2)
  %not_err2 = icmp eq i64 %3, 0
  %4 = call i1 @llvm.expect.i1(i1 %not_err2, i1 true)
  br i1 %4, label %after_check3, label %catch_landing

after_check3:                                     ; preds = %after_check
  %5 = load i32, ptr %retparam1, align 4
  store i32 %5, ptr %val, align 4
  br label %phi_try_catch

catch_landing:                                    ; preds = %after_check, %entry
  br label %phi_try_catch

phi_try_catch:                                    ; preds = %catch_landing, %after_check3
  %val4 = phi i1 [ true, %after_check3 ], [ false, %catch_landing ]
  br i1 %val4, label %if.then, label %if.exit

if.then:                                          ; preds = %phi_try_catch
  %6 = load i32, ptr %val, align 4
  %7 = call i32 (ptr, ...) @printf(ptr @.str, i32 %6)
  ret void

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