; LLVM IR from brewlin/garbage-collect :: copying-or-mark (copying)

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

@auto_grow = external global i32, align 4
@gc_heaps_used = external global i64, align 8
@gc_heaps = external global [100000 x %struct.gc_heap], align 16

; 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
  store i64 %0, ptr %2, align 8, !tbaa !5
  store i32 0, ptr @auto_grow, align 4, !tbaa !9
  store i64 3, ptr @gc_heaps_used, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %5

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

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

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

39:                                               ; preds = %10
  %40 = load i64, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %40
  %42 = getelementptr inbounds %struct.gc_heap, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 16, !tbaa !13
  %44 = getelementptr inbounds %struct.header, ptr %43, i64 1
  call void @gc_free(ptr noundef %44)
  br label %45

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

46:                                               ; preds = %45
  %47 = load i64, ptr %3, align 8, !tbaa !5
  %48 = add i64 %47, 1
  store i64 %48, ptr %3, align 8, !tbaa !5
  br label %5, !llvm.loop !19

49:                                               ; preds = %9
  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

declare void @gc_free(ptr noundef) #3

; 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 = { "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 = { 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, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"gc_heap", !12, i64 0, !6, i64 8}
!15 = !{!14, !6, i64 8}
!16 = !{!17, !6, i64 16}
!17 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !12, i64 32, !12, i64 40}
!18 = !{!17, !12, i64 32}
!19 = distinct !{!19, !20}
!20 = !{!"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 }

@auto_grow = external 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

; Function Attrs: nounwind uwtable
define dso_local void @gc_init(i64 noundef %0) local_unnamed_addr #0 {
  store i32 0, ptr @auto_grow, align 4, !tbaa !5
  store i64 3, ptr @gc_heaps_used, align 8, !tbaa !9
  %2 = add i64 %0, 8
  %3 = tail call ptr @sbrk(i64 noundef %2) #3
  %4 = ptrtoint ptr %3 to i64
  %5 = add i64 %4, 7
  %6 = and i64 %5, -8
  %7 = inttoptr i64 %6 to ptr
  store ptr %7, ptr @gc_heaps, align 16, !tbaa !11
  store i64 %0, ptr getelementptr inbounds (i8, ptr @gc_heaps, i64 8), align 8, !tbaa !14
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  store i64 %0, ptr %8, align 8, !tbaa !15
  %9 = getelementptr inbounds i8, ptr %7, i64 32
  store ptr null, ptr %9, align 8, !tbaa !17
  %.pre = load i64, ptr @gc_heaps_used, align 8, !tbaa !9
  %10 = icmp ugt i64 %.pre, 1
  br i1 %10, label %.peel.next, label %._crit_edge

._crit_edge:                                      ; preds = %.peel.next, %1
  ret void

.peel.next:                                       ; preds = %1, %.peel.next
  %.011 = phi i64 [ %21, %.peel.next ], [ 1, %1 ]
  %11 = tail call ptr @sbrk(i64 noundef %2) #3
  %12 = ptrtoint ptr %11 to i64
  %13 = add i64 %12, 7
  %14 = and i64 %13, -8
  %15 = inttoptr i64 %14 to ptr
  %16 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %.011
  store ptr %15, ptr %16, align 16, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %16, i64 8
  store i64 %0, ptr %17, align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %15, i64 16
  store i64 %0, ptr %18, align 8, !tbaa !15
  %19 = getelementptr inbounds i8, ptr %15, i64 32
  store ptr null, ptr %19, align 8, !tbaa !17
  %20 = getelementptr inbounds i8, ptr %15, i64 48
  tail call void @gc_free(ptr noundef nonnull %20) #3
  %21 = add nuw i64 %.011, 1
  %22 = load i64, ptr @gc_heaps_used, align 8, !tbaa !9
  %23 = icmp ult i64 %21, %22
  br i1 %23, label %.peel.next, label %._crit_edge, !llvm.loop !18
}

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

declare void @gc_free(ptr noundef) local_unnamed_addr #2

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 = { "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 16}
!16 = !{!"header", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !13, i64 32, !13, i64 40}
!17 = !{!16, !13, i64 32}
!18 = distinct !{!18, !19, !20}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!"llvm.loop.peeled.count", i32 1}


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