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

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

@new_free_p = external dso_local global ptr, align 8
@.str.5 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [13 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
define dso_local ptr @minor_malloc(i64 noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %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.5, i64 noundef %8)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 0, ptr %5, 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 %6, align 4
  br label %61

18:                                               ; preds = %1
  br label %19

19:                                               ; preds = %28, %18
  %20 = load ptr, ptr @new_free_p, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.header, ptr %20, i32 0, i32 2
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = load i64, ptr %3, align 8, !tbaa !5
  %24 = icmp ult i64 %22, %23
  br i1 %24, label %25, label %31

25:                                               ; preds = %19
  %26 = load i64, ptr %5, align 8, !tbaa !5
  %27 = icmp ne i64 %26, 0
  br i1 %27, label %29, label %28

28:                                               ; preds = %25
  store i64 1, ptr %5, align 8, !tbaa !5
  call void @minor_gc()
  br label %19

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

31:                                               ; preds = %19
  %32 = load ptr, ptr @new_free_p, align 8, !tbaa !9
  store ptr %32, ptr %4, align 8, !tbaa !9
  %33 = load ptr, ptr @new_free_p, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.header, ptr %33, i32 0, i32 2
  %35 = load i64, ptr %34, align 8, !tbaa !11
  %36 = load i64, ptr %3, align 8, !tbaa !5
  %37 = sub i64 %35, %36
  %38 = trunc i64 %37 to i32
  store i32 %38, ptr %7, align 4, !tbaa !13
  %39 = load ptr, ptr @new_free_p, align 8, !tbaa !9
  %40 = load i64, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %39, i64 %40
  store ptr %41, ptr @new_free_p, align 8, !tbaa !9
  %42 = load i32, ptr %7, align 4, !tbaa !13
  %43 = sext i32 %42 to i64
  %44 = load ptr, ptr @new_free_p, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.header, ptr %44, i32 0, i32 2
  store i64 %43, ptr %45, align 8, !tbaa !11
  %46 = load i64, ptr %3, align 8, !tbaa !5
  %47 = load ptr, ptr %4, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.header, ptr %47, i32 0, i32 2
  store i64 %46, ptr %48, align 8, !tbaa !11
  %49 = load ptr, ptr %4, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.header, ptr %49, i32 0, i32 1
  store i64 0, ptr %50, align 8, !tbaa !15
  %51 = load ptr, ptr %4, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.header, ptr %51, i32 0, i32 1
  %53 = load i64, ptr %52, align 8, !tbaa !15
  %54 = or i64 %53, 1
  store i64 %54, ptr %52, align 8, !tbaa !15
  %55 = load ptr, ptr %4, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.header, ptr %55, i32 0, i32 3
  store i64 0, ptr %56, align 8, !tbaa !16
  %57 = load ptr, ptr %4, align 8, !tbaa !9
  %58 = getelementptr inbounds %struct.header, ptr %57, i32 0, i32 5
  store ptr null, ptr %58, align 8, !tbaa !17
  %59 = load ptr, ptr %4, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.header, ptr %59, i64 1
  store ptr %60, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %61

61:                                               ; preds = %31, %29, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %62 = load ptr, ptr %2, align 8
  ret ptr %62
}

; Function Attrs: nounwind uwtable
declare dso_local void @minor_gc() #2

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 = !{!"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 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !6, i64 8}
!16 = !{!12, !6, i64 24}
!17 = !{!12, !10, i64 40}


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

@new_free_p = external dso_local local_unnamed_addr global ptr, align 8
@.str.5 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [13 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local ptr @minor_malloc(i64 noundef %0) local_unnamed_addr #1 {
  %2 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, i64 noundef %0)
  %3 = add i64 %0, 55
  %4 = and i64 %3, -8
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %24, label %.preheader

.preheader:                                       ; preds = %1
  %6 = load ptr, ptr @new_free_p, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 16
  %8 = load i64, ptr %7, align 8, !tbaa !9
  %9 = icmp ult i64 %8, %4
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  tail call void @minor_gc()
  %10 = load ptr, ptr @new_free_p, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  %12 = load i64, ptr %11, align 8, !tbaa !9
  %13 = icmp ult i64 %12, %4
  br i1 %13, label %.critedge, label %._crit_edge

.critedge:                                        ; preds = %.lr.ph
  %14 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6)
  br label %24

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.lcssa24 = phi ptr [ %6, %.preheader ], [ %10, %.lr.ph ]
  %.lcssa = phi i64 [ %8, %.preheader ], [ %12, %.lr.ph ]
  %15 = getelementptr inbounds i8, ptr %.lcssa24, i64 16
  %16 = sub i64 %.lcssa, %4
  %17 = getelementptr inbounds i8, ptr %.lcssa24, i64 %4
  store ptr %17, ptr @new_free_p, align 8, !tbaa !5
  %sext = shl i64 %16, 32
  %18 = ashr exact i64 %sext, 32
  %19 = getelementptr inbounds i8, ptr %17, i64 16
  store i64 %18, ptr %19, align 8, !tbaa !9
  store i64 %4, ptr %15, align 8, !tbaa !9
  %20 = getelementptr inbounds i8, ptr %.lcssa24, i64 8
  store i64 1, ptr %20, align 8, !tbaa !12
  %21 = getelementptr inbounds i8, ptr %.lcssa24, i64 24
  store i64 0, ptr %21, align 8, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %.lcssa24, i64 40
  store ptr null, ptr %22, align 8, !tbaa !14
  %23 = getelementptr inbounds i8, ptr %.lcssa24, i64 48
  br label %24

24:                                               ; preds = %1, %._crit_edge, %.critedge
  %.0 = phi ptr [ null, %.critedge ], [ %23, %._crit_edge ], [ null, %1 ]
  ret ptr %.0
}

; Function Attrs: nounwind uwtable
declare dso_local void @minor_gc() local_unnamed_addr #1

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

!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 = !{!10, !11, i64 8}
!13 = !{!10, !11, i64 24}
!14 = !{!10, !6, i64 40}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass,TailCallElimPass