; LLVM IR from brewlin/garbage-collect :: generational (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 }

@oldg = external global i32, align 4
@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
  %5 = load i32, ptr @oldg, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %6
  %8 = getelementptr inbounds %struct.gc_heap, ptr %7, i32 0, i32 0
  %9 = load ptr, ptr %8, align 16, !tbaa !9
  store ptr %9, ptr %2, align 8, !tbaa !13
  br label %10

10:                                               ; preds = %51, %0
  %11 = load ptr, ptr %2, align 8, !tbaa !13
  %12 = load i32, ptr @oldg, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %13
  %15 = getelementptr inbounds %struct.gc_heap, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 16, !tbaa !9
  %17 = ptrtoint ptr %16 to i64
  %18 = load i32, ptr @oldg, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %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 1
  %22 = load i64, ptr %21, align 8, !tbaa !14
  %23 = add i64 %17, %22
  %24 = inttoptr i64 %23 to ptr
  %25 = icmp ult ptr %11, %24
  br i1 %25, label %26, label %61

26:                                               ; preds = %10
  %27 = load ptr, ptr %2, align 8, !tbaa !13
  %28 = getelementptr inbounds %struct.header, ptr %27, i32 0, i32 1
  %29 = load i64, ptr %28, align 8, !tbaa !15
  %30 = and i64 %29, 1
  %31 = icmp ne i64 %30, 0
  br i1 %31, label %32, label %50

32:                                               ; preds = %26
  %33 = load ptr, ptr %2, align 8, !tbaa !13
  %34 = getelementptr inbounds %struct.header, ptr %33, i32 0, i32 1
  %35 = load i64, ptr %34, align 8, !tbaa !15
  %36 = and i64 %35, 2
  %37 = icmp ne i64 %36, 0
  br i1 %37, label %38, label %45

38:                                               ; preds = %32
  %39 = load ptr, ptr %2, align 8, !tbaa !13
  %40 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, ptr noundef %39)
  %41 = load ptr, ptr %2, align 8, !tbaa !13
  %42 = getelementptr inbounds %struct.header, ptr %41, i32 0, i32 1
  %43 = load i64, ptr %42, align 8, !tbaa !15
  %44 = and i64 %43, -3
  store i64 %44, ptr %42, align 8, !tbaa !15
  br label %49

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

49:                                               ; preds = %45, %38
  br label %50

50:                                               ; preds = %49, %26
  br label %51

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

61:                                               ; preds = %10
  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 = !{!"int", !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, !12, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !12, i64 8}
!15 = !{!16, !12, i64 8}
!16 = !{!"header", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !11, i64 32, !11, i64 40}
!17 = !{!16, !12, i64 16}
!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 }

@oldg = external local_unnamed_addr global i32, align 4
@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 i32, ptr @oldg, align 4, !tbaa !5
  %2 = sext i32 %1 to i64
  %3 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %2
  %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 !13
  %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 = %0, %23
  %.09 = phi ptr [ %30, %23 ], [ %4, %0 ]
  %11 = getelementptr inbounds i8, ptr %.09, i64 8
  %12 = load i64, ptr %11, align 8, !tbaa !14
  %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
  %.not8 = icmp eq i64 %15, 0
  br i1 %.not8, label %20, label %16

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

20:                                               ; preds = %14
  %21 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  %22 = getelementptr inbounds i8, ptr %.09, 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 %.09, i64 48
  %25 = ptrtoint ptr %24 to i64
  %26 = getelementptr inbounds i8, ptr %.09, i64 16
  %27 = load i64, ptr %26, align 8, !tbaa !16
  %28 = add i64 %25, -48
  %29 = add i64 %28, %27
  %30 = inttoptr i64 %29 to ptr
  %31 = load i32, ptr @oldg, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %32
  %34 = load ptr, ptr %33, align 16, !tbaa !9
  %35 = ptrtoint ptr %34 to i64
  %36 = getelementptr inbounds i8, ptr %33, i64 8
  %37 = load i64, ptr %36, align 8, !tbaa !13
  %38 = add i64 %37, %35
  %39 = inttoptr i64 %38 to ptr
  %40 = icmp ult ptr %30, %39
  br i1 %40, label %.lr.ph, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %23, %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 = !{!"int", !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, !12, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!10, !12, i64 8}
!14 = !{!15, !12, i64 8}
!15 = !{!"header", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !11, i64 32, !11, i64 40}
!16 = !{!15, !12, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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