; LLVM IR from brewlin/garbage-collect :: gc (add_heap)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.gc_heap = type { ptr, i64 }
%struct.header = type { i64, i64, i64, i64, ptr, ptr }

@gc_heaps_used = external dso_local global i64, align 8
@stderr = external global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [25 x i8], align 1
@gc_heaps = external dso_local global [100000 x %struct.gc_heap], align 16
@.str.3 = external hidden unnamed_addr constant [25 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

declare i32 @fputs(ptr noundef, ptr noundef) #1

; Function Attrs: noreturn nounwind
declare void @abort() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @add_heap(i64 noundef %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %7 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %8 = icmp uge i64 %7, 100000
  br i1 %8, label %9, label %12

9:                                                ; preds = %1
  %10 = load ptr, ptr @stderr, align 8, !tbaa !9
  %11 = call i32 @fputs(ptr noundef @.str.1, ptr noundef %10)
  call void @abort() #6
  unreachable

12:                                               ; preds = %1
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = icmp ult i64 %13, 4096
  br i1 %14, label %15, label %16

15:                                               ; preds = %12
  store i64 4096, ptr %3, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %15, %12
  %17 = load i64, ptr %3, align 8, !tbaa !5
  %18 = add i64 %17, 8
  %19 = call ptr @sbrk(i64 noundef %18) #5
  store ptr %19, ptr %4, align 8, !tbaa !9
  %20 = icmp eq ptr %19, inttoptr (i64 -1 to ptr)
  br i1 %20, label %21, label %23

21:                                               ; preds = %16
  %22 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  store ptr null, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %47

23:                                               ; preds = %16
  %24 = load ptr, ptr %4, align 8, !tbaa !9
  %25 = ptrtoint ptr %24 to i64
  %26 = add i64 %25, 7
  %27 = and i64 %26, -8
  %28 = inttoptr i64 %27 to ptr
  %29 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %30 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %29
  %31 = getelementptr inbounds %struct.gc_heap, ptr %30, i32 0, i32 0
  store ptr %28, ptr %31, align 16, !tbaa !11
  store ptr %28, ptr %5, align 8, !tbaa !9
  %32 = load i64, ptr %3, align 8, !tbaa !5
  %33 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %34 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %33
  %35 = getelementptr inbounds %struct.gc_heap, ptr %34, i32 0, i32 1
  store i64 %32, ptr %35, align 8, !tbaa !13
  store i64 %32, ptr %3, align 8, !tbaa !5
  %36 = load i64, ptr %3, align 8, !tbaa !5
  %37 = load ptr, ptr %5, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.header, ptr %37, i32 0, i32 2
  store i64 %36, ptr %38, align 8, !tbaa !14
  %39 = load ptr, ptr %5, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.header, ptr %39, i32 0, i32 4
  store ptr null, ptr %40, align 8, !tbaa !16
  %41 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %42 = add i64 %41, 1
  store i64 %42, ptr @gc_heaps_used, align 8, !tbaa !5
  %43 = load i64, ptr %3, align 8, !tbaa !5
  %44 = load ptr, ptr %5, align 8, !tbaa !9
  %45 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i64 noundef %43, ptr noundef %44)
  %46 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %46, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %47

47:                                               ; preds = %23, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  %48 = load ptr, ptr %2, align 8
  ret ptr %48
}

; Function Attrs: nounwind
declare ptr @sbrk(i64 noundef) #4

declare i32 @printf(ptr noundef, ...) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { noreturn nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #5 = { nounwind }
attributes #6 = { noreturn nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"gc_heap", !10, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = !{!15, !6, i64 16}
!15 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !10, i64 40}
!16 = !{!15, !10, i64 32}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.gc_heap = type { ptr, i64 }

@gc_heaps_used = external dso_local local_unnamed_addr global i64, align 8
@stderr = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [25 x i8], align 1
@gc_heaps = external dso_local local_unnamed_addr global [100000 x %struct.gc_heap], align 16
@.str.3 = external hidden unnamed_addr constant [25 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fputs(ptr nocapture noundef readonly, ptr nocapture noundef) local_unnamed_addr #0

; Function Attrs: noreturn nounwind
declare void @abort() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @add_heap(i64 noundef %0) local_unnamed_addr #2 {
  %2 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %3 = icmp ugt i64 %2, 99999
  br i1 %3, label %4, label %7

4:                                                ; preds = %1
  %5 = load ptr, ptr @stderr, align 8, !tbaa !9
  %6 = tail call i32 @fputs(ptr noundef nonnull @.str.1, ptr noundef %5) #5
  tail call void @abort() #6
  unreachable

7:                                                ; preds = %1
  %spec.store.select = tail call i64 @llvm.umax.i64(i64 %0, i64 4096)
  %8 = add i64 %spec.store.select, 8
  %9 = tail call ptr @sbrk(i64 noundef %8) #7
  %10 = icmp eq ptr %9, inttoptr (i64 -1 to ptr)
  br i1 %10, label %11, label %13

11:                                               ; preds = %7
  %12 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %25

13:                                               ; preds = %7
  %14 = ptrtoint ptr %9 to i64
  %15 = add i64 %14, 7
  %16 = and i64 %15, -8
  %17 = inttoptr i64 %16 to ptr
  %18 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %19 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %18
  store ptr %17, ptr %19, align 16, !tbaa !11
  %20 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %18, i32 1
  store i64 %spec.store.select, ptr %20, align 8, !tbaa !13
  %21 = getelementptr inbounds i8, ptr %17, i64 16
  store i64 %spec.store.select, ptr %21, align 8, !tbaa !14
  %22 = getelementptr inbounds i8, ptr %17, i64 32
  store ptr null, ptr %22, align 8, !tbaa !16
  %23 = add i64 %18, 1
  store i64 %23, ptr @gc_heaps_used, align 8, !tbaa !5
  %24 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i64 noundef %spec.store.select, ptr noundef %17)
  br label %25

25:                                               ; preds = %13, %11
  %.0 = phi ptr [ null, %11 ], [ %17, %13 ]
  ret ptr %.0
}

; Function Attrs: nounwind
declare ptr @sbrk(i64 noundef) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #4

attributes #0 = { nofree nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { noreturn nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { cold }
attributes #6 = { noreturn nounwind }
attributes #7 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"gc_heap", !10, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = !{!15, !6, i64 16}
!15 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !10, i64 40}
!16 = !{!15, !10, i64 32}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass