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

@gc_phase = external dso_local global i32, align 4
@stack = external dso_local global %struct.stack_header, align 8
@root_used = external global i64, align 8
@roots = external global [100000 x %struct.root_], align 16

; 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 ptr @is_pointer_to_heap(ptr noundef) #1

declare ptr @get_header(ptr noundef, ptr noundef) #1

declare void @push(ptr noundef, ptr noundef) #1

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

6:                                                ; preds = %51, %0
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = load i64, ptr @root_used, align 8, !tbaa !9
  %10 = icmp ult i64 %8, %9
  br i1 %10, label %12, label %11

11:                                               ; preds = %6
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  br label %54

12:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %13 = load i32, ptr %1, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [100000 x %struct.root_], ptr @roots, i64 0, i64 %14
  %16 = getelementptr inbounds %struct.root_, ptr %15, i32 0, i32 0
  %17 = load ptr, ptr %16, align 16, !tbaa !11
  store ptr %17, ptr %3, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %18 = load ptr, ptr %3, align 8, !tbaa !14
  %19 = call ptr @is_pointer_to_heap(ptr noundef %18)
  store ptr %19, ptr %4, align 8, !tbaa !14
  %20 = icmp ne ptr %19, null
  br i1 %20, label %22, label %21

21:                                               ; preds = %12
  store i32 4, ptr %2, align 4
  br label %48

22:                                               ; preds = %12
  %23 = load ptr, ptr %4, align 8, !tbaa !14
  %24 = load ptr, ptr %3, align 8, !tbaa !14
  %25 = call ptr @get_header(ptr noundef %23, ptr noundef %24)
  store ptr %25, ptr %5, align 8, !tbaa !14
  %26 = icmp ne ptr %25, null
  br i1 %26, label %28, label %27

27:                                               ; preds = %22
  store i32 4, ptr %2, align 4
  br label %48

28:                                               ; preds = %22
  %29 = load ptr, ptr %5, align 8, !tbaa !14
  %30 = getelementptr inbounds %struct.header, ptr %29, i32 0, i32 1
  %31 = load i64, ptr %30, align 8, !tbaa !15
  %32 = and i64 %31, 1
  %33 = icmp ne i64 %32, 0
  br i1 %33, label %35, label %34

34:                                               ; preds = %28
  store i32 4, ptr %2, align 4
  br label %48

35:                                               ; preds = %28
  %36 = load ptr, ptr %5, align 8, !tbaa !14
  %37 = getelementptr inbounds %struct.header, ptr %36, i32 0, i32 1
  %38 = load i64, ptr %37, align 8, !tbaa !15
  %39 = and i64 %38, 2
  %40 = icmp ne i64 %39, 0
  br i1 %40, label %41, label %42

41:                                               ; preds = %35
  store i32 4, ptr %2, align 4
  br label %48

42:                                               ; preds = %35
  %43 = load ptr, ptr %5, align 8, !tbaa !14
  %44 = getelementptr inbounds %struct.header, ptr %43, i32 0, i32 1
  %45 = load i64, ptr %44, align 8, !tbaa !15
  %46 = or i64 %45, 2
  store i64 %46, ptr %44, align 8, !tbaa !15
  %47 = load ptr, ptr %3, align 8, !tbaa !14
  call void @push(ptr noundef @stack, ptr noundef %47)
  store i32 0, ptr %2, align 4
  br label %48

48:                                               ; preds = %42, %41, %34, %27, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %49 = load i32, ptr %2, align 4
  switch i32 %49, label %55 [
    i32 0, label %50
    i32 4, label %51
  ]

50:                                               ; preds = %48
  br label %51

51:                                               ; preds = %50, %48
  %52 = load i32, ptr %1, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !17

54:                                               ; preds = %11
  store i32 2, ptr @gc_phase, align 4, !tbaa !5
  ret void

55:                                               ; preds = %48
  unreachable
}

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 = !{!"root_", !13, i64 0, !13, i64 8}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!13, !13, i64 0}
!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 = 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"

%struct.stack_header = type { ptr, ptr }
%struct.root_ = type { ptr, ptr }

@gc_phase = external dso_local local_unnamed_addr global i32, align 4
@stack = external dso_local global %struct.stack_header, align 8
@root_used = external local_unnamed_addr global i64, align 8
@roots = external local_unnamed_addr global [100000 x %struct.root_], align 16

declare ptr @is_pointer_to_heap(ptr noundef) local_unnamed_addr #0

declare ptr @get_header(ptr noundef, ptr noundef) local_unnamed_addr #0

declare void @push(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @root_scan_phase() local_unnamed_addr #1 {
  %1 = load i64, ptr @root_used, align 8, !tbaa !5
  %.not16 = icmp eq i64 %1, 0
  br i1 %.not16, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %13, %0
  store i32 2, ptr @gc_phase, align 4, !tbaa !9
  ret void

.lr.ph:                                           ; preds = %0, %13
  %indvars.iv = phi i64 [ %indvars.iv.next, %13 ], [ 0, %0 ]
  %2 = getelementptr inbounds [100000 x %struct.root_], ptr @roots, i64 0, i64 %indvars.iv
  %3 = load ptr, ptr %2, align 16, !tbaa !11
  %4 = tail call ptr @is_pointer_to_heap(ptr noundef %3) #2
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %13, label %5

5:                                                ; preds = %.lr.ph
  %6 = tail call ptr @get_header(ptr noundef nonnull %4, ptr noundef %3) #2
  %.not12 = icmp eq ptr %6, null
  br i1 %.not12, label %13, label %7

7:                                                ; preds = %5
  %8 = getelementptr inbounds i8, ptr %6, i64 8
  %9 = load i64, ptr %8, align 8, !tbaa !14
  %10 = and i64 %9, 3
  %or.cond = icmp eq i64 %10, 1
  br i1 %or.cond, label %11, label %13

11:                                               ; preds = %7
  %12 = or disjoint i64 %9, 2
  store i64 %12, ptr %8, align 8, !tbaa !14
  tail call void @push(ptr noundef nonnull @stack, ptr noundef %3) #2
  br label %13

13:                                               ; preds = %7, %5, %.lr.ph, %11
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %14 = load i64, ptr @root_used, align 8, !tbaa !5
  %15 = icmp ugt i64 %14, %indvars.iv.next
  br i1 %15, label %.lr.ph, label %._crit_edge, !llvm.loop !16
}

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


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