; LLVM IR from brewlin/garbage-collect :: gc-try (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
@gc_heaps = external global [100000 x %struct.gc_heap], align 16
@.str.2 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.3 = 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 = %65, %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 %68

9:                                                ; preds = %5
  %10 = load i64, ptr %1, align 8, !tbaa !5
  %11 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %10
  %12 = getelementptr inbounds %struct.gc_heap, ptr %11, i32 0, i32 0
  %13 = load ptr, ptr %12, align 16, !tbaa !9
  %14 = ptrtoint ptr %13 to i64
  %15 = load i64, ptr %1, align 8, !tbaa !5
  %16 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %15
  %17 = getelementptr inbounds %struct.gc_heap, ptr %16, i32 0, i32 1
  %18 = load i64, ptr %17, align 8, !tbaa !12
  %19 = add i64 %14, %18
  %20 = inttoptr i64 %19 to ptr
  store ptr %20, ptr %3, align 8, !tbaa !13
  %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 !9
  store ptr %24, ptr %2, align 8, !tbaa !13
  br label %25

25:                                               ; preds = %54, %9
  %26 = load ptr, ptr %2, align 8, !tbaa !13
  %27 = load ptr, ptr %3, align 8, !tbaa !13
  %28 = icmp ult ptr %26, %27
  br i1 %28, label %29, label %64

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

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

41:                                               ; preds = %35
  %42 = load ptr, ptr %2, align 8, !tbaa !13
  %43 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, ptr noundef %42)
  %44 = load ptr, ptr %2, align 8, !tbaa !13
  %45 = getelementptr inbounds %struct.header, ptr %44, i32 0, i32 1
  %46 = load i64, ptr %45, align 8, !tbaa !14
  %47 = and i64 %46, -3
  store i64 %47, ptr %45, align 8, !tbaa !14
  br label %52

48:                                               ; preds = %35
  %49 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  %50 = load ptr, ptr %2, align 8, !tbaa !13
  %51 = getelementptr inbounds %struct.header, ptr %50, i64 1
  call void @gc_free(ptr noundef %51)
  br label %52

52:                                               ; preds = %48, %41
  br label %53

53:                                               ; preds = %52, %29
  br label %54

54:                                               ; preds = %53
  %55 = load ptr, ptr %2, align 8, !tbaa !13
  %56 = getelementptr inbounds %struct.header, ptr %55, i64 1
  %57 = ptrtoint ptr %56 to i64
  %58 = load ptr, ptr %2, align 8, !tbaa !13
  %59 = getelementptr inbounds %struct.header, ptr %58, i32 0, i32 2
  %60 = load i64, ptr %59, align 8, !tbaa !16
  %61 = sub i64 %60, 48
  %62 = add i64 %57, %61
  %63 = inttoptr i64 %62 to ptr
  store ptr %63, ptr %2, align 8, !tbaa !13
  br label %25, !llvm.loop !17

64:                                               ; preds = %25
  br label %65

65:                                               ; preds = %64
  %66 = load i64, ptr %1, align 8, !tbaa !5
  %67 = add i64 %66, 1
  store i64 %67, ptr %1, align 8, !tbaa !5
  br label %5, !llvm.loop !19

68:                                               ; 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, !11, i64 0}
!10 = !{!"gc_heap", !11, i64 0, !6, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!11, !11, i64 0}
!14 = !{!15, !6, i64 8}
!15 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !11, i64 40}
!16 = !{!15, !6, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


; 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
@.str.2 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.3 = 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
  %.not20 = icmp eq i64 %1, 0
  br i1 %.not20, label %._crit_edge19, label %.lr.ph18

.lr.ph18:                                         ; preds = %0, %._crit_edge
  %2 = phi i64 [ %32, %._crit_edge ], [ %1, %0 ]
  %.016 = phi i64 [ %33, %._crit_edge ], [ 0, %0 ]
  %3 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %.016
  %4 = load ptr, ptr %3, align 16, !tbaa !9
  %5 = ptrtoint ptr %4 to i64
  %6 = getelementptr inbounds i8, ptr %3, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !12
  %8 = add i64 %7, %5
  %9 = inttoptr i64 %8 to ptr
  %10 = icmp ult ptr %4, %9
  br i1 %10, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph18, %23
  %.01315 = phi ptr [ %30, %23 ], [ %4, %.lr.ph18 ]
  %11 = getelementptr inbounds i8, ptr %.01315, i64 8
  %12 = load i64, ptr %11, align 8, !tbaa !13
  %13 = and i64 %12, 1
  %.not = icmp eq i64 %13, 0
  br i1 %.not, label %23, label %14

14:                                               ; preds = %.lr.ph
  %15 = and i64 %12, 2
  %.not14 = icmp eq i64 %15, 0
  br i1 %.not14, label %20, label %16

16:                                               ; preds = %14
  %17 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, ptr noundef nonnull %.01315)
  %18 = load i64, ptr %11, align 8, !tbaa !13
  %19 = and i64 %18, -3
  store i64 %19, ptr %11, align 8, !tbaa !13
  br label %23

20:                                               ; preds = %14
  %21 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  %22 = getelementptr inbounds i8, ptr %.01315, i64 48
  tail call void @gc_free(ptr noundef nonnull %22) #3
  br label %23

23:                                               ; preds = %.lr.ph, %20, %16
  %24 = getelementptr inbounds i8, ptr %.01315, i64 48
  %25 = ptrtoint ptr %24 to i64
  %26 = getelementptr inbounds i8, ptr %.01315, i64 16
  %27 = load i64, ptr %26, align 8, !tbaa !15
  %28 = add i64 %25, -48
  %29 = add i64 %28, %27
  %30 = inttoptr i64 %29 to ptr
  %31 = icmp ult ptr %30, %9
  br i1 %31, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !16

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

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.lr.ph18
  %32 = phi i64 [ %.pre, %._crit_edge.loopexit ], [ %2, %.lr.ph18 ]
  %33 = add nuw i64 %.016, 1
  %34 = icmp ult i64 %33, %32
  br i1 %34, label %.lr.ph18, label %._crit_edge19, !llvm.loop !18

._crit_edge19:                                    ; preds = %._crit_edge, %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, !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 8}
!14 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !11, i64 40}
!15 = !{!14, !6, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


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