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

@gc_phase = external dso_local global i32, align 4
@max_mark = external dso_local global i32, align 4
@stack = external dso_local global %struct.stack_header, align 8

; 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

; Function Attrs: nounwind uwtable
declare dso_local void @gc_mark(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local void @mark_phase() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 0, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %47, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = load i32, ptr @max_mark, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %12, label %11

11:                                               ; preds = %7
  store i32 2, ptr %3, align 4
  br label %50

12:                                               ; preds = %7
  %13 = call i32 @empty(ptr noundef @stack)
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %22

15:                                               ; preds = %12
  %16 = load i32, ptr %1, align 4, !tbaa !5
  %17 = icmp sge i32 %16, 1
  br i1 %17, label %18, label %19

18:                                               ; preds = %15
  store i32 3, ptr @gc_phase, align 4, !tbaa !5
  store i32 2, ptr %3, align 4
  br label %50

19:                                               ; preds = %15
  call void @root_scan_phase()
  %20 = load i32, ptr %1, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %1, align 4, !tbaa !5
  br label %47

22:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %23 = call ptr @pop(ptr noundef @stack)
  store ptr %23, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %24 = load ptr, ptr %4, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.header, ptr %24, i64 -1
  store ptr %25, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %26 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %26, ptr %6, align 8, !tbaa !9
  br label %27

27:                                               ; preds = %43, %22
  %28 = load ptr, ptr %6, align 8, !tbaa !9
  %29 = load ptr, ptr %5, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.header, ptr %29, i64 1
  %31 = ptrtoint ptr %30 to i64
  %32 = load ptr, ptr %5, 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 = sub i64 %34, 48
  %36 = add i64 %31, %35
  %37 = inttoptr i64 %36 to ptr
  %38 = icmp ult ptr %28, %37
  br i1 %38, label %40, label %39

39:                                               ; preds = %27
  store i32 5, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %46

40:                                               ; preds = %27
  %41 = load ptr, ptr %6, align 8, !tbaa !9
  %42 = load ptr, ptr %41, align 8, !tbaa !9
  call void @gc_mark(ptr noundef %42)
  br label %43

43:                                               ; preds = %40
  %44 = load ptr, ptr %6, align 8, !tbaa !9
  %45 = getelementptr inbounds i8, ptr %44, i32 1
  store ptr %45, ptr %6, align 8, !tbaa !9
  br label %27, !llvm.loop !14

46:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %47

47:                                               ; preds = %46, %19
  %48 = load i32, ptr %2, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %2, align 4, !tbaa !5
  br label %7, !llvm.loop !16

50:                                               ; preds = %18, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %51

51:                                               ; preds = %50
  %52 = call i32 @empty(ptr noundef @stack)
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %55

54:                                               ; preds = %51
  store i32 3, ptr @gc_phase, align 4, !tbaa !5
  br label %55

55:                                               ; preds = %54, %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

declare i32 @empty(ptr noundef) #2

declare ptr @pop(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !13, i64 16}
!12 = !{!"header", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !10, i64 32, !10, i64 40}
!13 = !{!"long", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

@gc_phase = external dso_local local_unnamed_addr global i32, align 4
@max_mark = external dso_local local_unnamed_addr global i32, align 4
@stack = external dso_local global %struct.stack_header, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @gc_mark(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @root_scan_phase() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @mark_phase() local_unnamed_addr #0 {
  %1 = load i32, ptr @max_mark, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph17, label %.loopexit13

.lr.ph17:                                         ; preds = %0, %.loopexit
  %.01016 = phi i32 [ %.1, %.loopexit ], [ 0, %0 ]
  %.01115 = phi i32 [ %23, %.loopexit ], [ 0, %0 ]
  %3 = tail call i32 @empty(ptr noundef nonnull @stack) #2
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %8, label %4

4:                                                ; preds = %.lr.ph17
  %5 = icmp sgt i32 %.01016, 0
  br i1 %5, label %6, label %7

6:                                                ; preds = %4
  store i32 3, ptr @gc_phase, align 4, !tbaa !5
  br label %.loopexit13

7:                                                ; preds = %4
  tail call void @root_scan_phase()
  br label %.loopexit

8:                                                ; preds = %.lr.ph17
  %9 = tail call ptr @pop(ptr noundef nonnull @stack) #2
  %10 = ptrtoint ptr %9 to i64
  %11 = getelementptr inbounds i8, ptr %9, i64 -32
  %12 = add i64 %10, -48
  %13 = load i64, ptr %11, align 8, !tbaa !9
  %14 = add i64 %12, %13
  %15 = inttoptr i64 %14 to ptr
  %16 = icmp ult ptr %9, %15
  br i1 %16, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %8, %.lr.ph
  %.014 = phi ptr [ %18, %.lr.ph ], [ %9, %8 ]
  %17 = load ptr, ptr %.014, align 8, !tbaa !13
  tail call void @gc_mark(ptr noundef %17)
  %18 = getelementptr inbounds i8, ptr %.014, i64 1
  %19 = load i64, ptr %11, align 8, !tbaa !9
  %20 = add i64 %12, %19
  %21 = inttoptr i64 %20 to ptr
  %22 = icmp ult ptr %18, %21
  br i1 %22, label %.lr.ph, label %.loopexit, !llvm.loop !14

.loopexit:                                        ; preds = %.lr.ph, %8, %7
  %.1 = phi i32 [ 1, %7 ], [ %.01016, %8 ], [ %.01016, %.lr.ph ]
  %23 = add nuw nsw i32 %.01115, 1
  %24 = load i32, ptr @max_mark, align 4, !tbaa !5
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %.lr.ph17, label %.loopexit13, !llvm.loop !16

.loopexit13:                                      ; preds = %.loopexit, %0, %6
  %26 = tail call i32 @empty(ptr noundef nonnull @stack) #2
  %.not12 = icmp eq i32 %26, 0
  br i1 %.not12, label %28, label %27

27:                                               ; preds = %.loopexit13
  store i32 3, ptr @gc_phase, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %27, %.loopexit13
  ret void
}

declare i32 @empty(ptr noundef) local_unnamed_addr #1

declare ptr @pop(ptr noundef) local_unnamed_addr #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 = { "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 }

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


; Effective O3 passes: GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass