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

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

@free_list = external dso_local global ptr, align 8
@auto_gc = external dso_local global i32, align 4
@auto_grow = external dso_local global i32, align 4
@.str.5 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [19 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

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

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @gc_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
  store i64 %0, ptr %3, align 8, !tbaa !5
  %8 = load i64, ptr %3, align 8, !tbaa !5
  %9 = call i32 (ptr, ...) @printf(ptr noundef @.str.6, i64 noundef %8)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store i64 0, ptr %6, align 8, !tbaa !5
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = add i64 %10, 48
  store i64 %11, ptr %3, align 8, !tbaa !5
  %12 = load i64, ptr %3, align 8, !tbaa !5
  %13 = add i64 %12, 7
  %14 = and i64 %13, -8
  store i64 %14, ptr %3, align 8, !tbaa !5
  %15 = load i64, ptr %3, align 8, !tbaa !5
  %16 = icmp ule i64 %15, 0
  br i1 %16, label %17, label %18

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

18:                                               ; preds = %1
  br label %19

19:                                               ; preds = %107, %98, %18
  %20 = load ptr, ptr @free_list, align 8, !tbaa !9
  store ptr %20, ptr %5, align 8, !tbaa !9
  %21 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %21, ptr %4, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %87, %19
  %23 = load ptr, ptr %4, align 8, !tbaa !9
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %92

25:                                               ; preds = %22
  %26 = load ptr, ptr %4, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.header, ptr %26, i32 0, i32 2
  %28 = load i64, ptr %27, align 8, !tbaa !11
  %29 = load i64, ptr %3, align 8, !tbaa !5
  %30 = icmp uge i64 %28, %29
  br i1 %30, label %31, label %86

31:                                               ; preds = %25
  %32 = load ptr, ptr %4, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.header, ptr %32, i32 0, i32 2
  %34 = load i64, ptr %33, align 8, !tbaa !11
  %35 = load i64, ptr %3, align 8, !tbaa !5
  %36 = icmp eq i64 %34, %35
  br i1 %36, label %37, label %52

37:                                               ; preds = %31
  %38 = load ptr, ptr %4, align 8, !tbaa !9
  %39 = load ptr, ptr %5, align 8, !tbaa !9
  %40 = icmp eq ptr %38, %39
  br i1 %40, label %41, label %45

41:                                               ; preds = %37
  %42 = load ptr, ptr %4, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.header, ptr %42, i32 0, i32 4
  %44 = load ptr, ptr %43, align 8, !tbaa !13
  store ptr %44, ptr %5, align 8, !tbaa !9
  store ptr %44, ptr @free_list, align 8, !tbaa !9
  br label %51

45:                                               ; preds = %37
  %46 = load ptr, ptr %4, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.header, ptr %46, i32 0, i32 4
  %48 = load ptr, ptr %47, align 8, !tbaa !13
  %49 = load ptr, ptr %5, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.header, ptr %49, i32 0, i32 4
  store ptr %48, ptr %50, align 8, !tbaa !13
  br label %51

51:                                               ; preds = %45, %41
  br label %65

52:                                               ; preds = %31
  %53 = load ptr, ptr %5, align 8, !tbaa !9
  %54 = load i64, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds i8, ptr %53, i64 %54
  store ptr %55, ptr %5, align 8, !tbaa !9
  %56 = load ptr, ptr %5, align 8, !tbaa !9
  %57 = load ptr, ptr %4, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %56, ptr align 8 %57, i64 48, i1 false)
  %58 = load ptr, ptr %4, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.header, ptr %58, i32 0, i32 2
  %60 = load i64, ptr %59, align 8, !tbaa !11
  %61 = load i64, ptr %3, align 8, !tbaa !5
  %62 = sub i64 %60, %61
  %63 = load ptr, ptr %5, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.header, ptr %63, i32 0, i32 2
  store i64 %62, ptr %64, align 8, !tbaa !11
  br label %65

65:                                               ; preds = %52, %51
  %66 = load i64, ptr %3, align 8, !tbaa !5
  %67 = load ptr, ptr %4, align 8, !tbaa !9
  %68 = getelementptr inbounds %struct.header, ptr %67, i32 0, i32 2
  store i64 %66, ptr %68, align 8, !tbaa !11
  %69 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %69, ptr @free_list, align 8, !tbaa !9
  %70 = load ptr, ptr %4, align 8, !tbaa !9
  %71 = call i32 (ptr, ...) @printf(ptr noundef @.str.5, ptr noundef %70)
  %72 = load ptr, ptr %4, align 8, !tbaa !9
  %73 = getelementptr inbounds %struct.header, ptr %72, i32 0, i32 1
  store i64 0, ptr %73, align 8, !tbaa !14
  %74 = load ptr, ptr %4, align 8, !tbaa !9
  %75 = getelementptr inbounds %struct.header, ptr %74, i32 0, i32 0
  store i64 1, ptr %75, align 8, !tbaa !15
  %76 = load ptr, ptr %4, align 8, !tbaa !9
  %77 = getelementptr inbounds %struct.header, ptr %76, i32 0, i32 1
  %78 = load i64, ptr %77, align 8, !tbaa !14
  %79 = or i64 %78, 1
  store i64 %79, ptr %77, align 8, !tbaa !14
  %80 = load ptr, ptr %4, align 8, !tbaa !9
  %81 = getelementptr inbounds %struct.header, ptr %80, i32 0, i32 3
  store i64 0, ptr %81, align 8, !tbaa !16
  %82 = load ptr, ptr %4, align 8, !tbaa !9
  %83 = getelementptr inbounds %struct.header, ptr %82, i32 0, i32 5
  store ptr null, ptr %83, align 8, !tbaa !17
  %84 = load ptr, ptr %4, align 8, !tbaa !9
  %85 = getelementptr inbounds %struct.header, ptr %84, i64 1
  store ptr %85, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %111

86:                                               ; preds = %25
  br label %87

87:                                               ; preds = %86
  %88 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %88, ptr %5, align 8, !tbaa !9
  %89 = load ptr, ptr %4, align 8, !tbaa !9
  %90 = getelementptr inbounds %struct.header, ptr %89, i32 0, i32 4
  %91 = load ptr, ptr %90, align 8, !tbaa !13
  store ptr %91, ptr %4, align 8, !tbaa !9
  br label %22, !llvm.loop !18

92:                                               ; preds = %22
  %93 = load i64, ptr %6, align 8, !tbaa !5
  %94 = icmp ne i64 %93, 0
  br i1 %94, label %99, label %95

95:                                               ; preds = %92
  %96 = load i32, ptr @auto_gc, align 4, !tbaa !20
  %97 = icmp ne i32 %96, 0
  br i1 %97, label %98, label %99

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

99:                                               ; preds = %95, %92
  %100 = load i32, ptr @auto_grow, align 4, !tbaa !20
  %101 = icmp ne i32 %100, 0
  br i1 %101, label %102, label %109

102:                                              ; preds = %99
  %103 = load i64, ptr %3, align 8, !tbaa !5
  %104 = call ptr @gc_grow(i64 noundef %103)
  store ptr %104, ptr %4, align 8, !tbaa !9
  %105 = load ptr, ptr %4, align 8, !tbaa !9
  %106 = icmp ne ptr %105, null
  br i1 %106, label %107, label %108

107:                                              ; preds = %102
  br label %19

108:                                              ; preds = %102
  br label %109

109:                                              ; preds = %108, %99
  br label %110

110:                                              ; preds = %109
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %111

111:                                              ; preds = %110, %65, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %112 = load ptr, ptr %2, align 8
  ret ptr %112
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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, !6, i64 16}
!12 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !10, i64 40}
!13 = !{!12, !10, i64 32}
!14 = !{!12, !6, i64 8}
!15 = !{!12, !6, i64 0}
!16 = !{!12, !6, i64 24}
!17 = !{!12, !10, i64 40}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!21, !21, i64 0}
!21 = !{!"int", !7, i64 0}


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

@free_list = external dso_local local_unnamed_addr global ptr, align 8
@auto_gc = external dso_local local_unnamed_addr global i32, align 4
@auto_grow = external dso_local local_unnamed_addr global i32, align 4
@.str.5 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [19 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 @gc_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 = add i64 %0, 55
  %4 = and i64 %3, -8
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %.loopexit, label %.outer.split

6:                                                ; preds = %15, %.outer
  %7 = load ptr, ptr @free_list, align 8, !tbaa !5
  %.not49.us = icmp eq ptr %7, null
  br i1 %.not49.us, label %._crit_edge.us, label %.lr.ph.us.preheader

.lr.ph.us.preheader:                              ; preds = %6
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  %9 = load i64, ptr %8, align 8, !tbaa !9
  %.not45.us146 = icmp ult i64 %9, %4
  br i1 %.not45.us146, label %.lr.ph148, label %.split.us

.lr.ph.us:                                        ; preds = %.lr.ph148
  %10 = getelementptr inbounds i8, ptr %13, i64 16
  %11 = load i64, ptr %10, align 8, !tbaa !9
  %.not45.us = icmp ult i64 %11, %4
  br i1 %.not45.us, label %.lr.ph148, label %.split.us, !llvm.loop !12

.lr.ph148:                                        ; preds = %.lr.ph.us.preheader, %.lr.ph.us
  %.03950.us147 = phi ptr [ %13, %.lr.ph.us ], [ %7, %.lr.ph.us.preheader ]
  %12 = getelementptr inbounds i8, ptr %.03950.us147, i64 32
  %13 = load ptr, ptr %12, align 8, !tbaa !14
  %.not.us = icmp eq ptr %13, null
  br i1 %.not.us, label %._crit_edge.us, label %.lr.ph.us, !llvm.loop !12

._crit_edge.us:                                   ; preds = %.lr.ph148, %6
  %14 = load i32, ptr @auto_grow, align 4, !tbaa !15
  %.not43.us = icmp eq i32 %14, 0
  br i1 %.not43.us, label %.loopexit, label %15

15:                                               ; preds = %._crit_edge.us
  %16 = tail call ptr @gc_grow(i64 noundef %4)
  %.not44.us = icmp eq ptr %16, null
  br i1 %.not44.us, label %.loopexit, label %6

.outer.split:                                     ; preds = %1, %47
  %17 = load ptr, ptr @free_list, align 8, !tbaa !5
  %.not49 = icmp eq ptr %17, null
  br i1 %.not49, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.outer.split
  %18 = getelementptr inbounds i8, ptr %17, i64 16
  %19 = load i64, ptr %18, align 8, !tbaa !9
  %.not4583 = icmp ult i64 %19, %4
  br i1 %.not4583, label %.lr.ph85, label %.split.us

20:                                               ; preds = %.lr.ph85
  %21 = getelementptr inbounds i8, ptr %43, i64 16
  %22 = load i64, ptr %21, align 8, !tbaa !9
  %.not45 = icmp ult i64 %22, %4
  br i1 %.not45, label %.lr.ph85, label %.split.us, !llvm.loop !12

.split.us:                                        ; preds = %.lr.ph, %20, %.lr.ph.us.preheader, %.lr.ph.us
  %.us-phi = phi ptr [ %13, %.lr.ph.us ], [ %7, %.lr.ph.us.preheader ], [ %43, %20 ], [ %17, %.lr.ph ]
  %.us-phi52 = phi i64 [ %11, %.lr.ph.us ], [ %9, %.lr.ph.us.preheader ], [ %22, %20 ], [ %19, %.lr.ph ]
  %.us-phi54 = phi ptr [ %.03950.us147, %.lr.ph.us ], [ %7, %.lr.ph.us.preheader ], [ %.0395084, %20 ], [ %17, %.lr.ph ]
  %23 = getelementptr inbounds i8, ptr %.us-phi, i64 16
  %24 = icmp eq i64 %.us-phi52, %4
  br i1 %24, label %25, label %31

25:                                               ; preds = %.split.us
  %26 = icmp eq ptr %.us-phi, %.us-phi54
  %27 = getelementptr inbounds i8, ptr %.us-phi, i64 32
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  br i1 %26, label %36, label %29

29:                                               ; preds = %25
  %30 = getelementptr inbounds i8, ptr %.us-phi54, i64 32
  store ptr %28, ptr %30, align 8, !tbaa !14
  br label %36

31:                                               ; preds = %.split.us
  %32 = getelementptr inbounds i8, ptr %.us-phi54, i64 %4
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(48) %32, ptr noundef nonnull align 8 dereferenceable(48) %.us-phi, i64 48, i1 false)
  %33 = load i64, ptr %23, align 8, !tbaa !9
  %34 = sub i64 %33, %4
  %35 = getelementptr inbounds i8, ptr %32, i64 16
  store i64 %34, ptr %35, align 8, !tbaa !9
  br label %36

36:                                               ; preds = %25, %29, %31
  %.1 = phi ptr [ %.us-phi54, %29 ], [ %32, %31 ], [ %28, %25 ]
  store i64 %4, ptr %23, align 8, !tbaa !9
  store ptr %.1, ptr @free_list, align 8, !tbaa !5
  %37 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, ptr noundef nonnull %.us-phi)
  %38 = getelementptr inbounds i8, ptr %.us-phi, i64 8
  store i64 1, ptr %.us-phi, align 8, !tbaa !17
  store i64 1, ptr %38, align 8, !tbaa !18
  %39 = getelementptr inbounds i8, ptr %.us-phi, i64 24
  store i64 0, ptr %39, align 8, !tbaa !19
  %40 = getelementptr inbounds i8, ptr %.us-phi, i64 40
  store ptr null, ptr %40, align 8, !tbaa !20
  %41 = getelementptr inbounds i8, ptr %.us-phi, i64 48
  br label %.loopexit

.lr.ph85:                                         ; preds = %.lr.ph, %20
  %.0395084 = phi ptr [ %43, %20 ], [ %17, %.lr.ph ]
  %42 = getelementptr inbounds i8, ptr %.0395084, i64 32
  %43 = load ptr, ptr %42, align 8, !tbaa !14
  %.not = icmp eq ptr %43, null
  br i1 %.not, label %._crit_edge, label %20, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph85, %.outer.split
  %44 = load i32, ptr @auto_gc, align 4
  %.not94 = icmp eq i32 %44, 0
  br i1 %.not94, label %45, label %.outer

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

45:                                               ; preds = %._crit_edge
  %46 = load i32, ptr @auto_grow, align 4, !tbaa !15
  %.not43 = icmp eq i32 %46, 0
  br i1 %.not43, label %.loopexit, label %47

47:                                               ; preds = %45
  %48 = tail call ptr @gc_grow(i64 noundef %4)
  %.not44 = icmp eq ptr %48, null
  br i1 %.not44, label %.loopexit, label %.outer.split

.loopexit:                                        ; preds = %47, %45, %15, %._crit_edge.us, %1, %36
  %.040 = phi ptr [ %41, %36 ], [ null, %1 ], [ null, %._crit_edge.us ], [ null, %15 ], [ null, %45 ], [ null, %47 ]
  ret ptr %.040
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

declare void @gc() local_unnamed_addr #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 }

!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 16}
!10 = !{!"header", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !6, i64 32, !6, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!10, !6, i64 32}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{!10, !11, i64 0}
!18 = !{!10, !11, i64 8}
!19 = !{!10, !11, i64 24}
!20 = !{!10, !6, i64 40}


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