; LLVM IR from brewlin/garbage-collect :: tri-color-marking (tri-color)

; 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_phase = external dso_local global i32, align 4
@max_sweep = external dso_local global i32, align 4
@sweeping = external dso_local global i32, align 4
@gc_heaps_used = external global i64, align 8
@gc_heaps = external global [100000 x %struct.gc_heap], align 16
@.str.2 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [16 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 void @gc_free(ptr noundef) #1

; 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 void @sweep_phase() #2 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %5 = load i32, ptr @sweeping, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  store i64 %6, ptr %1, align 8, !tbaa !9
  br label %7

7:                                                ; preds = %76, %0
  %8 = load i64, ptr %1, align 8, !tbaa !9
  %9 = load i64, ptr @gc_heaps_used, align 8, !tbaa !9
  %10 = icmp ult i64 %8, %9
  br i1 %10, label %11, label %18

11:                                               ; preds = %7
  %12 = load i64, ptr %1, align 8, !tbaa !9
  %13 = load i32, ptr @max_sweep, align 4, !tbaa !5
  %14 = load i32, ptr @sweeping, align 4, !tbaa !5
  %15 = add nsw i32 %13, %14
  %16 = sext i32 %15 to i64
  %17 = icmp ult i64 %12, %16
  br label %18

18:                                               ; preds = %11, %7
  %19 = phi i1 [ false, %7 ], [ %17, %11 ]
  br i1 %19, label %20, label %79

20:                                               ; preds = %18
  %21 = load i64, ptr %1, align 8, !tbaa !9
  %22 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %21
  %23 = getelementptr inbounds %struct.gc_heap, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 16, !tbaa !11
  %25 = ptrtoint ptr %24 to i64
  %26 = load i64, ptr %1, align 8, !tbaa !9
  %27 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %26
  %28 = getelementptr inbounds %struct.gc_heap, ptr %27, i32 0, i32 1
  %29 = load i64, ptr %28, align 8, !tbaa !14
  %30 = add i64 %25, %29
  %31 = inttoptr i64 %30 to ptr
  store ptr %31, ptr %3, align 8, !tbaa !15
  %32 = load i64, ptr %1, align 8, !tbaa !9
  %33 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %32
  %34 = getelementptr inbounds %struct.gc_heap, ptr %33, i32 0, i32 0
  %35 = load ptr, ptr %34, align 16, !tbaa !11
  store ptr %35, ptr %2, align 8, !tbaa !15
  br label %36

36:                                               ; preds = %65, %20
  %37 = load ptr, ptr %2, align 8, !tbaa !15
  %38 = load ptr, ptr %3, align 8, !tbaa !15
  %39 = icmp ult ptr %37, %38
  br i1 %39, label %40, label %75

40:                                               ; preds = %36
  %41 = load ptr, ptr %2, align 8, !tbaa !15
  %42 = getelementptr inbounds %struct.header, ptr %41, i32 0, i32 1
  %43 = load i64, ptr %42, align 8, !tbaa !16
  %44 = and i64 %43, 1
  %45 = icmp ne i64 %44, 0
  br i1 %45, label %46, label %64

46:                                               ; preds = %40
  %47 = load ptr, ptr %2, align 8, !tbaa !15
  %48 = getelementptr inbounds %struct.header, ptr %47, i32 0, i32 1
  %49 = load i64, ptr %48, align 8, !tbaa !16
  %50 = and i64 %49, 2
  %51 = icmp ne i64 %50, 0
  br i1 %51, label %52, label %59

52:                                               ; preds = %46
  %53 = load ptr, ptr %2, align 8, !tbaa !15
  %54 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, ptr noundef %53)
  %55 = load ptr, ptr %2, align 8, !tbaa !15
  %56 = getelementptr inbounds %struct.header, ptr %55, i32 0, i32 1
  %57 = load i64, ptr %56, align 8, !tbaa !16
  %58 = and i64 %57, -3
  store i64 %58, ptr %56, align 8, !tbaa !16
  br label %63

59:                                               ; preds = %46
  %60 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  %61 = load ptr, ptr %2, align 8, !tbaa !15
  %62 = getelementptr inbounds %struct.header, ptr %61, i64 1
  call void @gc_free(ptr noundef %62)
  br label %63

63:                                               ; preds = %59, %52
  br label %64

64:                                               ; preds = %63, %40
  br label %65

65:                                               ; preds = %64
  %66 = load ptr, ptr %2, align 8, !tbaa !15
  %67 = getelementptr inbounds %struct.header, ptr %66, i64 1
  %68 = ptrtoint ptr %67 to i64
  %69 = load ptr, ptr %2, align 8, !tbaa !15
  %70 = getelementptr inbounds %struct.header, ptr %69, i32 0, i32 2
  %71 = load i64, ptr %70, align 8, !tbaa !18
  %72 = sub i64 %71, 48
  %73 = add i64 %68, %72
  %74 = inttoptr i64 %73 to ptr
  store ptr %74, ptr %2, align 8, !tbaa !15
  br label %36, !llvm.loop !19

75:                                               ; preds = %36
  br label %76

76:                                               ; preds = %75
  %77 = load i64, ptr %1, align 8, !tbaa !9
  %78 = add i64 %77, 1
  store i64 %78, ptr %1, align 8, !tbaa !9
  br label %7, !llvm.loop !21

79:                                               ; preds = %18
  %80 = load i64, ptr %1, align 8, !tbaa !9
  %81 = trunc i64 %80 to i32
  store i32 %81, ptr @sweeping, align 4, !tbaa !5
  %82 = load i64, ptr %1, align 8, !tbaa !9
  %83 = load i64, ptr @gc_heaps_used, align 8, !tbaa !9
  %84 = icmp eq i64 %82, %83
  br i1 %84, label %85, label %86

85:                                               ; preds = %79
  store i32 0, ptr @sweeping, align 4, !tbaa !5
  store i32 1, ptr @gc_phase, align 4, !tbaa !5
  br label %86

86:                                               ; preds = %85, %79
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"gc_heap", !13, i64 0, !10, i64 8}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!12, !10, i64 8}
!15 = !{!13, !13, i64 0}
!16 = !{!17, !10, i64 8}
!17 = !{!"header", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !13, i64 32, !13, i64 40}
!18 = !{!17, !10, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}


; 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_phase = external dso_local local_unnamed_addr global i32, align 4
@max_sweep = external dso_local local_unnamed_addr global i32, align 4
@sweeping = external dso_local local_unnamed_addr global i32, align 4
@gc_heaps_used = external local_unnamed_addr global i64, align 8
@gc_heaps = external local_unnamed_addr global [100000 x %struct.gc_heap], align 16
@.str.2 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [16 x i8], align 1

declare void @gc_free(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @sweep_phase() local_unnamed_addr #2 {
  %1 = load i32, ptr @sweeping, align 4, !tbaa !5
  %2 = sext i32 %1 to i64
  %3 = load i64, ptr @gc_heaps_used, align 8, !tbaa !9
  %4 = icmp ugt i64 %3, %2
  br i1 %4, label %.lr.ph21, label %.critedge

.lr.ph21:                                         ; preds = %0, %._crit_edge
  %5 = phi i64 [ %41, %._crit_edge ], [ %3, %0 ]
  %.019 = phi i64 [ %42, %._crit_edge ], [ %2, %0 ]
  %6 = load i32, ptr @max_sweep, align 4, !tbaa !5
  %7 = load i32, ptr @sweeping, align 4, !tbaa !5
  %8 = add nsw i32 %7, %6
  %9 = sext i32 %8 to i64
  %10 = icmp ult i64 %.019, %9
  br i1 %10, label %11, label %.critedge

11:                                               ; preds = %.lr.ph21
  %12 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %.019
  %13 = load ptr, ptr %12, align 16, !tbaa !11
  %14 = ptrtoint ptr %13 to i64
  %15 = getelementptr inbounds i8, ptr %12, i64 8
  %16 = load i64, ptr %15, align 8, !tbaa !14
  %17 = add i64 %16, %14
  %18 = inttoptr i64 %17 to ptr
  %19 = icmp ult ptr %13, %18
  br i1 %19, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %11, %32
  %.01618 = phi ptr [ %39, %32 ], [ %13, %11 ]
  %20 = getelementptr inbounds i8, ptr %.01618, i64 8
  %21 = load i64, ptr %20, align 8, !tbaa !15
  %22 = and i64 %21, 1
  %.not = icmp eq i64 %22, 0
  br i1 %.not, label %32, label %23

23:                                               ; preds = %.lr.ph
  %24 = and i64 %21, 2
  %.not17 = icmp eq i64 %24, 0
  br i1 %.not17, label %29, label %25

25:                                               ; preds = %23
  %26 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, ptr noundef nonnull %.01618)
  %27 = load i64, ptr %20, align 8, !tbaa !15
  %28 = and i64 %27, -3
  store i64 %28, ptr %20, align 8, !tbaa !15
  br label %32

29:                                               ; preds = %23
  %30 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  %31 = getelementptr inbounds i8, ptr %.01618, i64 48
  tail call void @gc_free(ptr noundef nonnull %31) #3
  br label %32

32:                                               ; preds = %.lr.ph, %29, %25
  %33 = getelementptr inbounds i8, ptr %.01618, i64 48
  %34 = ptrtoint ptr %33 to i64
  %35 = getelementptr inbounds i8, ptr %.01618, i64 16
  %36 = load i64, ptr %35, align 8, !tbaa !17
  %37 = add i64 %34, -48
  %38 = add i64 %37, %36
  %39 = inttoptr i64 %38 to ptr
  %40 = icmp ult ptr %39, %18
  br i1 %40, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !18

._crit_edge.loopexit:                             ; preds = %32
  %.pre = load i64, ptr @gc_heaps_used, align 8, !tbaa !9
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %11
  %41 = phi i64 [ %.pre, %._crit_edge.loopexit ], [ %5, %11 ]
  %42 = add nuw i64 %.019, 1
  %43 = icmp ult i64 %42, %41
  br i1 %43, label %.lr.ph21, label %.critedge, !llvm.loop !20

.critedge:                                        ; preds = %.lr.ph21, %._crit_edge, %0
  %.0.lcssa = phi i64 [ %2, %0 ], [ %42, %._crit_edge ], [ %.019, %.lr.ph21 ]
  %.lcssa = phi i64 [ %3, %0 ], [ %41, %._crit_edge ], [ %5, %.lr.ph21 ]
  %44 = trunc i64 %.0.lcssa to i32
  store i32 %44, ptr @sweeping, align 4, !tbaa !5
  %45 = icmp eq i64 %.0.lcssa, %.lcssa
  br i1 %45, label %46, label %47

46:                                               ; preds = %.critedge
  store i32 0, ptr @sweeping, align 4, !tbaa !5
  store i32 1, ptr @gc_phase, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %46, %.critedge
  ret void
}

attributes #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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"gc_heap", !13, i64 0, !10, i64 8}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!12, !10, i64 8}
!15 = !{!16, !10, i64 8}
!16 = !{!"header", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !13, i64 32, !13, i64 40}
!17 = !{!16, !10, i64 16}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass