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

; 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
@from = external global i32, align 4
@to = external global i32, align 4
@gc_heaps = external global [100000 x %struct.gc_heap], align 16
@.str.1 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.2 = 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 i32 @printf(ptr noundef, ...) #1

; 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
define dso_local void @gc_sweep() #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
  store i64 0, ptr %1, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %76, %0
  %6 = load i64, ptr %1, 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 %9, label %79

9:                                                ; preds = %5
  %10 = load i64, ptr %1, align 8, !tbaa !5
  %11 = load i32, ptr @from, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = icmp eq i64 %10, %12
  br i1 %13, label %19, label %14

14:                                               ; preds = %9
  %15 = load i64, ptr %1, align 8, !tbaa !5
  %16 = load i32, ptr @to, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = icmp eq i64 %15, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %14, %9
  br label %76

20:                                               ; preds = %14
  %21 = load i64, ptr %1, align 8, !tbaa !5
  %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 !5
  %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 !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
  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.1, 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.2)
  %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, %19
  %77 = load i64, ptr %1, align 8, !tbaa !5
  %78 = add i64 %77, 1
  store i64 %78, ptr %1, align 8, !tbaa !5
  br label %5, !llvm.loop !21

79:                                               ; preds = %5
  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
}

declare void @gc_free(ptr noundef) #1

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 = !{!"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 = !{!"gc_heap", !13, i64 0, !6, i64 8}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!12, !6, i64 8}
!15 = !{!13, !13, i64 0}
!16 = !{!17, !6, i64 8}
!17 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !13, i64 32, !13, i64 40}
!18 = !{!17, !6, 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_heaps_used = external local_unnamed_addr global i64, align 8
@from = external local_unnamed_addr global i32, align 4
@to = external local_unnamed_addr global i32, align 4
@gc_heaps = external local_unnamed_addr global [100000 x %struct.gc_heap], align 16
@.str.1 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [16 x i8], align 1

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

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

.lr.ph21:                                         ; preds = %0, %.loopexit
  %2 = phi i64 [ %39, %.loopexit ], [ %1, %0 ]
  %.020 = phi i64 [ %40, %.loopexit ], [ 0, %0 ]
  %3 = load i32, ptr @from, align 4, !tbaa !9
  %4 = sext i32 %3 to i64
  %5 = icmp eq i64 %.020, %4
  %6 = load i32, ptr @to, align 4
  %7 = sext i32 %6 to i64
  %8 = icmp eq i64 %.020, %7
  %or.cond = select i1 %5, i1 true, i1 %8
  br i1 %or.cond, label %.loopexit, label %9

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

.lr.ph:                                           ; preds = %9, %30
  %.01519 = phi ptr [ %37, %30 ], [ %11, %9 ]
  %18 = getelementptr inbounds i8, ptr %.01519, i64 8
  %19 = load i64, ptr %18, align 8, !tbaa !15
  %20 = and i64 %19, 1
  %.not = icmp eq i64 %20, 0
  br i1 %.not, label %30, label %21

21:                                               ; preds = %.lr.ph
  %22 = and i64 %19, 2
  %.not16 = icmp eq i64 %22, 0
  br i1 %.not16, label %27, label %23

23:                                               ; preds = %21
  %24 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, ptr noundef nonnull %.01519)
  %25 = load i64, ptr %18, align 8, !tbaa !15
  %26 = and i64 %25, -3
  store i64 %26, ptr %18, align 8, !tbaa !15
  br label %30

27:                                               ; preds = %21
  %28 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  %29 = getelementptr inbounds i8, ptr %.01519, i64 48
  tail call void @gc_free(ptr noundef nonnull %29) #3
  br label %30

30:                                               ; preds = %.lr.ph, %27, %23
  %31 = getelementptr inbounds i8, ptr %.01519, i64 48
  %32 = ptrtoint ptr %31 to i64
  %33 = getelementptr inbounds i8, ptr %.01519, i64 16
  %34 = load i64, ptr %33, align 8, !tbaa !17
  %35 = add i64 %32, -48
  %36 = add i64 %35, %34
  %37 = inttoptr i64 %36 to ptr
  %38 = icmp ult ptr %37, %16
  br i1 %38, label %.lr.ph, label %.loopexit.loopexit, !llvm.loop !18

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

.loopexit:                                        ; preds = %.loopexit.loopexit, %9, %.lr.ph21
  %39 = phi i64 [ %.pre, %.loopexit.loopexit ], [ %2, %9 ], [ %2, %.lr.ph21 ]
  %40 = add nuw i64 %.020, 1
  %41 = icmp ult i64 %40, %39
  br i1 %41, label %.lr.ph21, label %._crit_edge, !llvm.loop !20

._crit_edge:                                      ; preds = %.loopexit, %0
  ret void
}

declare void @gc_free(ptr noundef) local_unnamed_addr #2

attributes #0 = { 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 #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 = !{!"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 = !{!"gc_heap", !13, i64 0, !6, i64 8}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!12, !6, i64 8}
!15 = !{!16, !6, i64 8}
!16 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !13, i64 32, !13, i64 40}
!17 = !{!16, !6, 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,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass