; LLVM IR from brewlin/garbage-collect :: mark-sweep-multi-free-list (gc)

; 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, ptr }

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

; Function Attrs: nounwind uwtable
define dso_local ptr @add_heap(i64 noundef %0) #0 {
  %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) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %7 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %8 = icmp uge i64 %7, 1000000
  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, ptr noundef %10)
  call void @abort() #7
  unreachable

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

15:                                               ; preds = %12
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = icmp ult i64 %16, 16384
  br i1 %17, label %18, label %19

18:                                               ; preds = %15
  store i64 16384, ptr %3, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %18, %15
  %20 = load i64, ptr %3, align 8, !tbaa !5
  %21 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %20)
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = add i64 %22, 8
  %24 = add i64 %23, 24
  %25 = call ptr @sbrk(i64 noundef %24) #6
  store ptr %25, ptr %4, align 8, !tbaa !9
  %26 = icmp eq ptr %25, inttoptr (i64 -1 to ptr)
  br i1 %26, label %27, label %29

27:                                               ; preds = %19
  %28 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  store ptr null, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %62

29:                                               ; preds = %19
  br label %41

30:                                               ; preds = %12
  %31 = load i64, ptr %3, align 8, !tbaa !5
  %32 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i64 noundef %31)
  %33 = load i64, ptr %3, align 8, !tbaa !5
  %34 = add i64 %33, 24
  %35 = add i64 %34, 8
  %36 = call noalias ptr @malloc(i64 noundef %35) #8
  store ptr %36, ptr %4, align 8, !tbaa !9
  %37 = icmp eq ptr %36, inttoptr (i64 -1 to ptr)
  br i1 %37, label %38, label %40

38:                                               ; preds = %30
  %39 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  store ptr null, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %62

40:                                               ; preds = %30
  br label %41

41:                                               ; preds = %40, %29
  %42 = load ptr, ptr %4, align 8, !tbaa !9
  %43 = ptrtoint ptr %42 to i64
  %44 = add i64 %43, 7
  %45 = and i64 %44, -8
  %46 = inttoptr i64 %45 to ptr
  %47 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %48 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %47
  %49 = getelementptr inbounds %struct.gc_heap, ptr %48, i32 0, i32 0
  store ptr %46, ptr %49, align 16, !tbaa !11
  store ptr %46, ptr %5, align 8, !tbaa !9
  %50 = load i64, ptr %3, align 8, !tbaa !5
  %51 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %52 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %51
  %53 = getelementptr inbounds %struct.gc_heap, ptr %52, i32 0, i32 1
  store i64 %50, ptr %53, align 8, !tbaa !13
  store i64 %50, ptr %3, align 8, !tbaa !5
  %54 = load i64, ptr %3, align 8, !tbaa !5
  %55 = load ptr, ptr %5, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.header, ptr %55, i32 0, i32 1
  store i64 %54, ptr %56, align 8, !tbaa !14
  %57 = load ptr, ptr %5, align 8, !tbaa !9
  %58 = getelementptr inbounds %struct.header, ptr %57, i32 0, i32 2
  store ptr null, ptr %58, align 8, !tbaa !16
  %59 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %60 = add i64 %59, 1
  store i64 %60, ptr @gc_heaps_used, align 8, !tbaa !5
  %61 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %61, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %62

62:                                               ; preds = %41, %38, %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #6
  %63 = load ptr, ptr %2, align 8
  ret ptr %63
}

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

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

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

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

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #5

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { 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 #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 allocsize(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 #6 = { nounwind }
attributes #7 = { noreturn nounwind }
attributes #8 = { nounwind allocsize(0) }

!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 8}
!15 = !{!"header", !6, i64 0, !6, i64 8, !10, i64 16}
!16 = !{!15, !10, i64 16}


; 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
@.str = external hidden unnamed_addr constant [18 x i8], align 1
@stderr = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [28 x i8], align 1
@gc_heaps = external dso_local local_unnamed_addr global [1000000 x %struct.gc_heap], align 16

; Function Attrs: nounwind uwtable
define dso_local ptr @add_heap(i64 noundef %0) local_unnamed_addr #0 {
  %2 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %3 = icmp ugt i64 %2, 999999
  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, ptr noundef %5) #6
  tail call void @abort() #7
  unreachable

7:                                                ; preds = %1
  %8 = icmp ugt i64 %0, 31
  br i1 %8, label %9, label %16

9:                                                ; preds = %7
  %spec.store.select = tail call i64 @llvm.umax.i64(i64 %0, i64 16384)
  %10 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %spec.store.select)
  %11 = add i64 %spec.store.select, 32
  %12 = tail call ptr @sbrk(i64 noundef %11) #8
  %13 = icmp eq ptr %12, inttoptr (i64 -1 to ptr)
  br i1 %13, label %14, label %23

14:                                               ; preds = %9
  %15 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %34

16:                                               ; preds = %7
  %17 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i64 noundef %0)
  %18 = or disjoint i64 %0, 32
  %19 = tail call noalias ptr @malloc(i64 noundef %18) #9
  %20 = icmp eq ptr %19, inttoptr (i64 -1 to ptr)
  br i1 %20, label %21, label %23

21:                                               ; preds = %16
  %22 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4)
  br label %34

23:                                               ; preds = %16, %9
  %.013 = phi i64 [ %spec.store.select, %9 ], [ %0, %16 ]
  %.012 = phi ptr [ %12, %9 ], [ %19, %16 ]
  %24 = ptrtoint ptr %.012 to i64
  %25 = add i64 %24, 7
  %26 = and i64 %25, -8
  %27 = inttoptr i64 %26 to ptr
  %28 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %29 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %28
  store ptr %27, ptr %29, align 16, !tbaa !11
  %30 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %28, i32 1
  store i64 %.013, ptr %30, align 8, !tbaa !13
  %31 = getelementptr inbounds i8, ptr %27, i64 8
  store i64 %.013, ptr %31, align 8, !tbaa !14
  %32 = getelementptr inbounds i8, ptr %27, i64 16
  store ptr null, ptr %32, align 8, !tbaa !16
  %33 = add i64 %28, 1
  store i64 %33, ptr @gc_heaps_used, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %23, %21, %14
  %.0 = phi ptr [ null, %14 ], [ %27, %23 ], [ null, %21 ]
  ret ptr %.0
}

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

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

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

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

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #4

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

attributes #0 = { 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 #1 = { 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 #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 "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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #6 = { cold }
attributes #7 = { noreturn nounwind }
attributes #8 = { nounwind }
attributes #9 = { nounwind allocsize(0) }

!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 8}
!15 = !{!"header", !6, i64 0, !6, i64 8, !10, i64 16}
!16 = !{!15, !10, i64 16}


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