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

fn int foo(int x) => @pool()
{
	String s = string::tformat("%d", x);
	io::printn(s);
	return 2;
}

fn void main()
{
	foo(3);
}

/* #expect: test.ll

entry:
  %state = alloca ptr, align 8
  %s = alloca %"char[]", align 8
  %varargslots = alloca [1 x %any], align 16
  %taddr = alloca i32, align 4
  %result = alloca %"char[]", align 8
  %x = alloca %"char[]", align 8
  %x1 = alloca %"char[]", align 8
  %len = alloca i64, align 8
  %error_var = alloca i64, align 8
  %x2 = alloca %"char[]", align 8
  %retparam = alloca i64, align 8
  %error_var4 = alloca i64, align 8
  %error_var10 = alloca i64, align 8
  %1 = call ptr @std.core.mem.allocator.push_pool(i64 0) #3
  store ptr %1, ptr %state, align 8
  store i32 %0, 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 { ptr, i64 } @std.core.string.tformat(ptr @.str, i64 2, ptr %varargslots, i64 1)
  store { ptr, i64 } %4, ptr %result, align 8
  call void @llvm.memcpy.p0.p0.i32(ptr align 8 %s, ptr align 8 %result, i32 16, i1 false)
  call void @llvm.memcpy.p0.p0.i32(ptr align 8 %x, ptr align 8 %s, i32 16, i1 false)
  %5 = call ptr @std.io.stdout()
  call void @llvm.memcpy.p0.p0.i32(ptr align 8 %x1, ptr align 8 %x, i32 16, i1 false)
  call void @llvm.memcpy.p0.p0.i32(ptr align 8 %x2, ptr align 8 %x1, i32 16, i1 false)
  %lo = load ptr, ptr %x2, align 8
  %ptradd = getelementptr inbounds i8, ptr %x2, i64 8
  %hi = load i64, ptr %ptradd, align 8
  %6 = call i64 @std.io.File.write(ptr %retparam, ptr %5, ptr %lo, i64 %hi)
  %not_err = icmp eq i64 %6, 0
  %7 = call i1 @llvm.expect.i1(i1 %not_err, i1 true)
  br i1 %7, label %after_check, label %assign_optional

assign_optional:                                  ; preds = %entry
  store i64 %6, ptr %error_var, align 8
  br label %guard_block

after_check:                                      ; preds = %entry
  br label %noerr_block

guard_block:                                      ; preds = %assign_optional
  br label %voiderr

noerr_block:                                      ; preds = %after_check
  %8 = load i64, ptr %retparam, align 8
  store i64 %8, ptr %len, align 8
  %9 = call i64 @std.io.File.write_byte(ptr %5, i8 zeroext 10)
  %not_err5 = icmp eq i64 %9, 0
  %10 = call i1 @llvm.expect.i1(i1 %not_err5, i1 true)
  br i1 %10, label %after_check7, label %assign_optional6

assign_optional6:                                 ; preds = %noerr_block
  store i64 %9, ptr %error_var4, align 8
  br label %guard_block8

after_check7:                                     ; preds = %noerr_block
  br label %noerr_block9

guard_block8:                                     ; preds = %assign_optional6
  br label %voiderr

noerr_block9:                                     ; preds = %after_check7
  %11 = call i64 @std.io.File.flush(ptr %5)
  %not_err11 = icmp eq i64 %11, 0
  %12 = call i1 @llvm.expect.i1(i1 %not_err11, i1 true)
  br i1 %12, label %after_check13, label %assign_optional12

assign_optional12:                                ; preds = %noerr_block9
  store i64 %11, ptr %error_var10, align 8
  br label %guard_block14

after_check13:                                    ; preds = %noerr_block9
  br label %noerr_block15

guard_block14:                                    ; preds = %assign_optional12
  br label %voiderr

noerr_block15:                                    ; preds = %after_check13
  %13 = load i64, ptr %len, align 8
  %add = add i64 %13, 1
  br label %voiderr

voiderr:                                          ; preds = %noerr_block15, %guard_block14, %guard_block8, %guard_block
  %14 = load ptr, ptr %state, align 8
  call void @std.core.mem.allocator.pop_pool(ptr %14) #3
  ret i32 2
}
