; 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_heaps_used = external global i64, align 8
@gc_heaps = external global [100000 x %struct.gc_heap], align 16
@free_list = external global ptr, align 8

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

6:                                                ; preds = %62, %1
  %7 = load i64, ptr %3, align 8, !tbaa !5
  %8 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %9 = icmp ult i64 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #6
  br label %65

11:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #6
  %12 = load i64, ptr %2, align 8, !tbaa !5
  %13 = add i64 %12, 8
  %14 = call ptr @sbrk(i64 noundef %13) #6
  store ptr %14, ptr %4, align 8, !tbaa !9
  %15 = load ptr, ptr %4, align 8, !tbaa !9
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %18

17:                                               ; preds = %11
  call void @exit(i32 noundef -1) #7
  unreachable

18:                                               ; preds = %11
  %19 = load ptr, ptr %4, align 8, !tbaa !9
  %20 = ptrtoint ptr %19 to i64
  %21 = add i64 %20, 7
  %22 = and i64 %21, -8
  %23 = inttoptr i64 %22 to ptr
  %24 = load i64, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %24
  %26 = getelementptr inbounds %struct.gc_heap, ptr %25, i32 0, i32 0
  store ptr %23, ptr %26, align 16, !tbaa !11
  %27 = load i64, ptr %2, align 8, !tbaa !5
  %28 = load i64, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %28
  %30 = getelementptr inbounds %struct.gc_heap, ptr %29, i32 0, i32 1
  store i64 %27, ptr %30, align 8, !tbaa !13
  %31 = load i64, ptr %2, align 8, !tbaa !5
  %32 = load i64, ptr %3, align 8, !tbaa !5
  %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
  %36 = getelementptr inbounds %struct.header, ptr %35, i32 0, i32 2
  store i64 %31, ptr %36, align 8, !tbaa !14
  %37 = load i64, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %37
  %39 = getelementptr inbounds %struct.gc_heap, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 16, !tbaa !11
  %41 = getelementptr inbounds %struct.header, ptr %40, i32 0, i32 4
  store ptr null, ptr %41, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %42 = load i64, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %42
  %44 = getelementptr inbounds %struct.gc_heap, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %44, align 16, !tbaa !11
  store ptr %45, ptr %5, align 8, !tbaa !9
  %46 = load ptr, ptr @free_list, align 8, !tbaa !9
  %47 = icmp eq ptr %46, null
  br i1 %47, label %48, label %58

48:                                               ; preds = %18
  %49 = load ptr, ptr %5, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.header, ptr %49, i64 1
  %51 = load ptr, ptr %5, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.header, ptr %51, i32 0, i32 2
  %53 = load i64, ptr %52, align 8, !tbaa !14
  %54 = sub i64 %53, 48
  call void @llvm.memset.p0.i64(ptr align 8 %50, i8 0, i64 %54, i1 false)
  %55 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %55, ptr @free_list, align 8, !tbaa !9
  %56 = load ptr, ptr %5, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.header, ptr %56, i32 0, i32 1
  store i64 0, ptr %57, align 8, !tbaa !17
  br label %61

58:                                               ; preds = %18
  %59 = load ptr, ptr %5, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.header, ptr %59, i64 1
  call void @gc_free(ptr noundef %60)
  br label %61

61:                                               ; preds = %58, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #6
  br label %62

62:                                               ; preds = %61
  %63 = load i64, ptr %3, align 8, !tbaa !5
  %64 = add i64 %63, 1
  store i64 %64, ptr %3, align 8, !tbaa !5
  br label %6, !llvm.loop !18

65:                                               ; preds = %10
  ret void
}

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

; Function Attrs: nounwind
declare ptr @sbrk(i64 noundef) #2

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #3

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

declare void @gc_free(ptr noundef) #5

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { noreturn 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { "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 #6 = { nounwind }
attributes #7 = { noreturn 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, !10, i64 0}
!12 = !{!"gc_heap", !10, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = !{!15, !6, i64 16}
!15 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !10, i64 40}
!16 = !{!15, !10, i64 32}
!17 = !{!15, !6, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"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.gc_heap = type { ptr, i64 }

@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
@free_list = external local_unnamed_addr global ptr, align 8

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

.lr.ph:                                           ; preds = %1
  %3 = add i64 %0, 8
  %4 = add i64 %0, -48
  br label %5

._crit_edge:                                      ; preds = %24, %1
  ret void

5:                                                ; preds = %.lr.ph, %24
  %.017 = phi i64 [ 0, %.lr.ph ], [ %25, %24 ]
  %6 = tail call ptr @sbrk(i64 noundef %3) #5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %9

8:                                                ; preds = %5
  tail call void @exit(i32 noundef -1) #6
  unreachable

9:                                                ; preds = %5
  %10 = ptrtoint ptr %6 to i64
  %11 = add i64 %10, 7
  %12 = and i64 %11, -8
  %13 = inttoptr i64 %12 to ptr
  %14 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %.017
  store ptr %13, ptr %14, align 16, !tbaa !9
  %15 = getelementptr inbounds i8, ptr %14, i64 8
  store i64 %0, ptr %15, align 8, !tbaa !12
  %16 = getelementptr inbounds i8, ptr %13, i64 16
  store i64 %0, ptr %16, align 8, !tbaa !13
  %17 = getelementptr inbounds i8, ptr %13, i64 32
  store ptr null, ptr %17, align 8, !tbaa !15
  %18 = load ptr, ptr @free_list, align 8, !tbaa !16
  %19 = icmp eq ptr %18, null
  %20 = getelementptr inbounds i8, ptr %13, i64 48
  br i1 %19, label %21, label %23

21:                                               ; preds = %9
  tail call void @llvm.memset.p0.i64(ptr nonnull align 8 %20, i8 0, i64 %4, i1 false)
  store ptr %13, ptr @free_list, align 8, !tbaa !16
  %22 = getelementptr inbounds i8, ptr %13, i64 8
  store i64 0, ptr %22, align 8, !tbaa !17
  br label %24

23:                                               ; preds = %9
  tail call void @gc_free(ptr noundef nonnull %20) #5
  br label %24

24:                                               ; preds = %23, %21
  %25 = add nuw i64 %.017, 1
  %26 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %27 = icmp ult i64 %25, %26
  br i1 %27, label %5, label %._crit_edge, !llvm.loop !18
}

; Function Attrs: nounwind
declare ptr @sbrk(i64 noundef) local_unnamed_addr #1

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

declare void @gc_free(ptr noundef) local_unnamed_addr #4

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 = { 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 = { noreturn 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { "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 #5 = { nounwind }
attributes #6 = { noreturn 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, !11, i64 0}
!10 = !{!"gc_heap", !11, i64 0, !6, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!14, !6, i64 16}
!14 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !11, i64 40}
!15 = !{!14, !11, i64 32}
!16 = !{!11, !11, i64 0}
!17 = !{!14, !6, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


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