; 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.header = type { i64, i64, ptr }

@auto_gc = external dso_local global i32, align 4
@free_list = external dso_local global [33 x ptr], align 16
@.str.6 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [24 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 @printf(ptr noundef, ...) #1

; 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
declare dso_local ptr @grow(i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @gc_malloc(i64 noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  %9 = load i64, ptr %3, align 8, !tbaa !5
  %10 = call i32 (ptr, ...) @printf(ptr noundef @.str.6, i64 noundef %9)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 0, ptr %6, align 8, !tbaa !5
  %11 = load i64, ptr %3, align 8, !tbaa !5
  %12 = icmp ule i64 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %115

14:                                               ; preds = %1
  %15 = load i64, ptr %3, align 8, !tbaa !5
  %16 = add i64 %15, 7
  %17 = and i64 %16, -8
  store i64 %17, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = sub i64 %18, 1
  %20 = lshr i64 %19, 3
  %21 = trunc i64 %20 to i32
  store i32 %21, ptr %8, align 4, !tbaa !9
  %22 = load i32, ptr %8, align 4, !tbaa !9
  %23 = icmp sgt i32 %22, 31
  br i1 %23, label %24, label %25

24:                                               ; preds = %14
  store i32 32, ptr %8, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %24, %14
  %26 = load i32, ptr %8, align 4, !tbaa !9
  %27 = load i64, ptr %3, align 8, !tbaa !5
  %28 = call i32 (ptr, ...) @printf(ptr noundef @.str.7, i32 noundef %26, i64 noundef %27)
  br label %29

29:                                               ; preds = %111, %106, %25
  %30 = load i32, ptr %8, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [33 x ptr], ptr @free_list, i64 0, i64 %31
  %33 = load ptr, ptr %32, align 8, !tbaa !11
  store ptr %33, ptr %5, align 8, !tbaa !11
  %34 = load ptr, ptr %5, align 8, !tbaa !11
  store ptr %34, ptr %4, align 8, !tbaa !11
  br label %35

35:                                               ; preds = %95, %29
  %36 = load ptr, ptr %4, align 8, !tbaa !11
  %37 = icmp ne ptr %36, null
  br i1 %37, label %38, label %100

38:                                               ; preds = %35
  %39 = load ptr, ptr %4, align 8, !tbaa !11
  %40 = getelementptr inbounds %struct.header, ptr %39, i32 0, i32 1
  %41 = load i64, ptr %40, align 8, !tbaa !13
  %42 = load i64, ptr %3, align 8, !tbaa !5
  %43 = icmp uge i64 %41, %42
  br i1 %43, label %44, label %94

44:                                               ; preds = %38
  %45 = load ptr, ptr %4, align 8, !tbaa !11
  %46 = getelementptr inbounds %struct.header, ptr %45, i32 0, i32 1
  %47 = load i64, ptr %46, align 8, !tbaa !13
  %48 = load i64, ptr %3, align 8, !tbaa !5
  %49 = icmp eq i64 %47, %48
  br i1 %49, label %50, label %65

50:                                               ; preds = %44
  %51 = load ptr, ptr %4, align 8, !tbaa !11
  %52 = load ptr, ptr %5, align 8, !tbaa !11
  %53 = icmp eq ptr %51, %52
  br i1 %53, label %54, label %58

54:                                               ; preds = %50
  %55 = load ptr, ptr %4, align 8, !tbaa !11
  %56 = getelementptr inbounds %struct.header, ptr %55, i32 0, i32 2
  %57 = load ptr, ptr %56, align 8, !tbaa !15
  store ptr %57, ptr %5, align 8, !tbaa !11
  br label %64

58:                                               ; preds = %50
  %59 = load ptr, ptr %4, align 8, !tbaa !11
  %60 = getelementptr inbounds %struct.header, ptr %59, i32 0, i32 2
  %61 = load ptr, ptr %60, align 8, !tbaa !15
  %62 = load ptr, ptr %5, align 8, !tbaa !11
  %63 = getelementptr inbounds %struct.header, ptr %62, i32 0, i32 2
  store ptr %61, ptr %63, align 8, !tbaa !15
  br label %64

64:                                               ; preds = %58, %54
  br label %83

65:                                               ; preds = %44
  %66 = load i64, ptr %3, align 8, !tbaa !5
  %67 = add i64 %66, 24
  %68 = load ptr, ptr %4, align 8, !tbaa !11
  %69 = getelementptr inbounds %struct.header, ptr %68, i32 0, i32 1
  %70 = load i64, ptr %69, align 8, !tbaa !13
  %71 = sub i64 %70, %67
  store i64 %71, ptr %69, align 8, !tbaa !13
  %72 = load ptr, ptr %4, align 8, !tbaa !11
  %73 = getelementptr inbounds %struct.header, ptr %72, i64 1
  %74 = ptrtoint ptr %73 to i64
  %75 = load ptr, ptr %4, align 8, !tbaa !11
  %76 = getelementptr inbounds %struct.header, ptr %75, i32 0, i32 1
  %77 = load i64, ptr %76, align 8, !tbaa !13
  %78 = add i64 %74, %77
  %79 = inttoptr i64 %78 to ptr
  store ptr %79, ptr %4, align 8, !tbaa !11
  %80 = load i64, ptr %3, align 8, !tbaa !5
  %81 = load ptr, ptr %4, align 8, !tbaa !11
  %82 = getelementptr inbounds %struct.header, ptr %81, i32 0, i32 1
  store i64 %80, ptr %82, align 8, !tbaa !13
  br label %83

83:                                               ; preds = %65, %64
  %84 = load ptr, ptr %5, align 8, !tbaa !11
  %85 = load i32, ptr %8, align 4, !tbaa !9
  %86 = sext i32 %85 to i64
  %87 = getelementptr inbounds [33 x ptr], ptr @free_list, i64 0, i64 %86
  store ptr %84, ptr %87, align 8, !tbaa !11
  %88 = load ptr, ptr %4, align 8, !tbaa !11
  %89 = getelementptr inbounds %struct.header, ptr %88, i32 0, i32 0
  %90 = load i64, ptr %89, align 8, !tbaa !16
  %91 = or i64 %90, 1
  store i64 %91, ptr %89, align 8, !tbaa !16
  %92 = load ptr, ptr %4, align 8, !tbaa !11
  %93 = getelementptr inbounds %struct.header, ptr %92, i64 1
  store ptr %93, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %114

94:                                               ; preds = %38
  br label %95

95:                                               ; preds = %94
  %96 = load ptr, ptr %4, align 8, !tbaa !11
  store ptr %96, ptr %5, align 8, !tbaa !11
  %97 = load ptr, ptr %4, align 8, !tbaa !11
  %98 = getelementptr inbounds %struct.header, ptr %97, i32 0, i32 2
  %99 = load ptr, ptr %98, align 8, !tbaa !15
  store ptr %99, ptr %4, align 8, !tbaa !11
  br label %35, !llvm.loop !17

100:                                              ; preds = %35
  %101 = load i64, ptr %6, align 8, !tbaa !5
  %102 = icmp ne i64 %101, 0
  br i1 %102, label %107, label %103

103:                                              ; preds = %100
  %104 = load i32, ptr @auto_gc, align 4, !tbaa !9
  %105 = icmp ne i32 %104, 0
  br i1 %105, label %106, label %107

106:                                              ; preds = %103
  call void @gc()
  store i64 1, ptr %6, align 8, !tbaa !5
  br label %29

107:                                              ; preds = %103, %100
  %108 = load i64, ptr %3, align 8, !tbaa !5
  %109 = call ptr @grow(i64 noundef %108)
  store ptr %109, ptr %4, align 8, !tbaa !11
  %110 = icmp ne ptr %109, null
  br i1 %110, label %111, label %112

111:                                              ; preds = %107
  br label %29

112:                                              ; preds = %107
  br label %113

113:                                              ; preds = %112
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %114

114:                                              ; preds = %113, %83
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %115

115:                                              ; preds = %114, %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %116 = load ptr, ptr %2, align 8
  ret ptr %116
}

declare void @gc() #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 = { 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 }

!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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"header", !6, i64 0, !6, i64 8, !12, i64 16}
!15 = !{!14, !12, i64 16}
!16 = !{!14, !6, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; 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"

@auto_gc = external dso_local local_unnamed_addr global i32, align 4
@free_list = external dso_local local_unnamed_addr global [33 x ptr], align 16
@.str.6 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [24 x i8], align 1

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @grow(i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @gc_malloc(i64 noundef %0) local_unnamed_addr #1 {
  %2 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6, i64 noundef %0)
  %3 = icmp eq i64 %0, 0
  br i1 %3, label %.loopexit, label %.outer.split.preheader

.outer.split.preheader:                           ; preds = %1
  %4 = add i64 %0, 7
  %5 = and i64 %4, -8
  %6 = add i64 %5, 34359738367
  %7 = lshr i64 %6, 3
  %8 = trunc i64 %7 to i32
  %spec.store.select = tail call i32 @llvm.smin.i32(i32 %8, i32 32)
  %9 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7, i32 noundef %spec.store.select, i64 noundef %5)
  %10 = sext i32 %spec.store.select to i64
  %11 = getelementptr inbounds [33 x ptr], ptr @free_list, i64 0, i64 %10
  br label %.outer.split

12:                                               ; preds = %._crit_edge.us, %.outer
  %13 = load ptr, ptr %11, align 8, !tbaa !5
  %.not51.us = icmp eq ptr %13, null
  br i1 %.not51.us, label %._crit_edge.us, label %.lr.ph.us.preheader

.lr.ph.us.preheader:                              ; preds = %12
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  %15 = load i64, ptr %14, align 8, !tbaa !9
  %.not43.us162 = icmp ult i64 %15, %5
  br i1 %.not43.us162, label %.lr.ph164, label %.split.us

.lr.ph.us:                                        ; preds = %.lr.ph164
  %16 = getelementptr inbounds i8, ptr %19, i64 8
  %17 = load i64, ptr %16, align 8, !tbaa !9
  %.not43.us = icmp ult i64 %17, %5
  br i1 %.not43.us, label %.lr.ph164, label %.split.us, !llvm.loop !12

.lr.ph164:                                        ; preds = %.lr.ph.us.preheader, %.lr.ph.us
  %.03552.us163 = phi ptr [ %19, %.lr.ph.us ], [ %13, %.lr.ph.us.preheader ]
  %18 = getelementptr inbounds i8, ptr %.03552.us163, i64 16
  %19 = load ptr, ptr %18, align 8, !tbaa !14
  %.not.us = icmp eq ptr %19, null
  br i1 %.not.us, label %._crit_edge.us, label %.lr.ph.us, !llvm.loop !12

._crit_edge.us:                                   ; preds = %.lr.ph164, %12
  %20 = tail call ptr @grow(i64 noundef %5)
  %.not42.us = icmp eq ptr %20, null
  br i1 %.not42.us, label %.loopexit, label %12

.outer.split:                                     ; preds = %.outer.split.preheader, %49
  %21 = load ptr, ptr %11, align 8, !tbaa !5
  %.not51 = icmp eq ptr %21, null
  br i1 %.not51, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.outer.split
  %22 = getelementptr inbounds i8, ptr %21, i64 8
  %23 = load i64, ptr %22, align 8, !tbaa !9
  %.not4397 = icmp ult i64 %23, %5
  br i1 %.not4397, label %.lr.ph99, label %.split.us

24:                                               ; preds = %.lr.ph99
  %25 = getelementptr inbounds i8, ptr %47, i64 8
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %.not43 = icmp ult i64 %26, %5
  br i1 %.not43, label %.lr.ph99, label %.split.us, !llvm.loop !12

.split.us:                                        ; preds = %.lr.ph, %24, %.lr.ph.us.preheader, %.lr.ph.us
  %.us-phi55 = phi ptr [ %19, %.lr.ph.us ], [ %13, %.lr.ph.us.preheader ], [ %47, %24 ], [ %21, %.lr.ph ]
  %.us-phi56 = phi i64 [ %17, %.lr.ph.us ], [ %15, %.lr.ph.us.preheader ], [ %26, %24 ], [ %23, %.lr.ph ]
  %.us-phi58 = phi ptr [ %.03552.us163, %.lr.ph.us ], [ %13, %.lr.ph.us.preheader ], [ %.0355298, %24 ], [ %21, %.lr.ph ]
  %27 = icmp eq i64 %.us-phi56, %5
  br i1 %27, label %28, label %34

28:                                               ; preds = %.split.us
  %29 = icmp eq ptr %.us-phi55, %.us-phi58
  %30 = getelementptr inbounds i8, ptr %.us-phi55, i64 16
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  br i1 %29, label %42, label %32

32:                                               ; preds = %28
  %33 = getelementptr inbounds i8, ptr %.us-phi58, i64 16
  store ptr %31, ptr %33, align 8, !tbaa !14
  br label %42

34:                                               ; preds = %.split.us
  %35 = getelementptr inbounds i8, ptr %.us-phi55, i64 8
  %reass.sub = sub i64 %.us-phi56, %5
  %36 = add i64 %reass.sub, -24
  store i64 %36, ptr %35, align 8, !tbaa !9
  %37 = getelementptr inbounds i8, ptr %.us-phi55, i64 24
  %38 = ptrtoint ptr %37 to i64
  %39 = add i64 %36, %38
  %40 = inttoptr i64 %39 to ptr
  %41 = getelementptr inbounds i8, ptr %40, i64 8
  store i64 %5, ptr %41, align 8, !tbaa !9
  br label %42

42:                                               ; preds = %28, %32, %34
  %.136 = phi ptr [ %.us-phi55, %32 ], [ %40, %34 ], [ %.us-phi55, %28 ]
  %.1 = phi ptr [ %.us-phi58, %32 ], [ %.us-phi58, %34 ], [ %31, %28 ]
  store ptr %.1, ptr %11, align 8, !tbaa !5
  %43 = load i64, ptr %.136, align 8, !tbaa !15
  %44 = or i64 %43, 1
  store i64 %44, ptr %.136, align 8, !tbaa !15
  %45 = getelementptr inbounds i8, ptr %.136, i64 24
  br label %.loopexit

.lr.ph99:                                         ; preds = %.lr.ph, %24
  %.0355298 = phi ptr [ %47, %24 ], [ %21, %.lr.ph ]
  %46 = getelementptr inbounds i8, ptr %.0355298, i64 16
  %47 = load ptr, ptr %46, align 8, !tbaa !14
  %.not = icmp eq ptr %47, null
  br i1 %.not, label %._crit_edge, label %24, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph99, %.outer.split
  %48 = load i32, ptr @auto_gc, align 4
  %.not110 = icmp eq i32 %48, 0
  br i1 %.not110, label %49, label %.outer

.outer:                                           ; preds = %._crit_edge
  tail call void @gc() #4
  br label %12

49:                                               ; preds = %._crit_edge
  %50 = tail call ptr @grow(i64 noundef %5)
  %.not42 = icmp eq ptr %50, null
  br i1 %.not42, label %.loopexit, label %.outer.split

.loopexit:                                        ; preds = %49, %._crit_edge.us, %42, %1
  %.138 = phi ptr [ null, %1 ], [ %45, %42 ], [ null, %._crit_edge.us ], [ null, %49 ]
  ret ptr %.138
}

declare void @gc() local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #3

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 = { 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 #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 8}
!10 = !{!"header", !11, i64 0, !11, i64 8, !6, i64 16}
!11 = !{!"long", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!10, !6, i64 16}
!15 = !{!10, !11, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass