; LLVM IR from brewlin/garbage-collect :: gc-python (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.arena_object = type { i64, ptr, ptr, i32, i32, ptr, ptr, ptr }
%struct.pool_header = type { %union.anon, ptr, ptr, ptr, i32, i32, i32, i32 }
%union.anon = type { ptr }
%struct.header = type { i64, ptr }

@arenas = external global ptr, align 8

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

; 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() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %10 = load ptr, ptr @arenas, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.arena_object, ptr %10, i64 0
  store ptr %11, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %12 = load ptr, ptr %1, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.arena_object, ptr %12, i32 0, i32 1
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  store ptr %14, ptr %4, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %71, %0
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = load ptr, ptr %1, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.arena_object, ptr %17, i32 0, i32 2
  %19 = load ptr, ptr %18, align 8, !tbaa !14
  %20 = icmp ult ptr %16, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %15
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %74

22:                                               ; preds = %15
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %23, ptr %2, align 8, !tbaa !5
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.pool_header, ptr %24, i32 0, i32 5
  %26 = load i32, ptr %25, align 4, !tbaa !15
  %27 = add i32 %26, 1
  %28 = shl i32 %27, 3
  store i32 %28, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %29, i64 48
  store ptr %30, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i64 4096
  store ptr %32, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %33, ptr %8, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %65, %22
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = icmp ult ptr %35, %36
  br i1 %37, label %39, label %38

38:                                               ; preds = %34
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %70

39:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %40, ptr %9, align 8, !tbaa !5
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.header, ptr %41, i32 0, i32 0
  %43 = load i64, ptr %42, align 8, !tbaa !17
  %44 = and i64 %43, 1
  %45 = icmp ne i64 %44, 0
  br i1 %45, label %46, label %64

46:                                               ; preds = %39
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.header, ptr %47, i32 0, i32 0
  %49 = load i64, ptr %48, align 8, !tbaa !17
  %50 = and i64 %49, 2
  %51 = icmp ne i64 %50, 0
  br i1 %51, label %52, label %57

52:                                               ; preds = %46
  %53 = load ptr, ptr %9, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.header, ptr %53, i32 0, i32 0
  %55 = load i64, ptr %54, align 8, !tbaa !17
  %56 = and i64 %55, -3
  store i64 %56, ptr %54, align 8, !tbaa !17
  br label %63

57:                                               ; preds = %46
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.header, ptr %58, i32 0, i32 0
  %60 = load i64, ptr %59, align 8, !tbaa !17
  %61 = and i64 %60, -2
  store i64 %61, ptr %59, align 8, !tbaa !17
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  call void @Free(ptr noundef %62)
  br label %63

63:                                               ; preds = %57, %52
  br label %64

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

65:                                               ; preds = %64
  %66 = load i32, ptr %3, align 4, !tbaa !9
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = zext i32 %66 to i64
  %69 = getelementptr inbounds i8, ptr %67, i64 %68
  store ptr %69, ptr %8, align 8, !tbaa !5
  br label %34, !llvm.loop !19

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

71:                                               ; preds = %70
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = getelementptr inbounds i8, ptr %72, i64 4096
  store ptr %73, ptr %4, align 8, !tbaa !5
  br label %15, !llvm.loop !21

74:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 4, 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 @Free(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !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, !6, i64 8}
!12 = !{!"arena_object", !13, i64 0, !6, i64 8, !6, i64 16, !10, i64 24, !10, i64 28, !6, i64 32, !6, i64 40, !6, i64 48}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !6, i64 16}
!15 = !{!16, !10, i64 36}
!16 = !{!"pool_header", !7, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44}
!17 = !{!18, !13, i64 0}
!18 = !{!"header", !13, i64 0, !6, i64 8}
!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"

@arenas = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @gc_sweep() local_unnamed_addr #0 {
  %1 = load ptr, ptr @arenas, align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 8
  %3 = load ptr, ptr %2, align 8, !tbaa !9
  %4 = getelementptr inbounds i8, ptr %1, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !13
  %6 = icmp ult ptr %3, %5
  br i1 %6, label %.lr.ph, label %._crit_edge

.loopexit:                                        ; preds = %24
  %7 = getelementptr inbounds i8, ptr %.01825, i64 4096
  %8 = load ptr, ptr %4, align 8, !tbaa !13
  %9 = icmp ult ptr %7, %8
  br i1 %9, label %.lr.ph, label %._crit_edge, !llvm.loop !14

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

.lr.ph:                                           ; preds = %0, %.loopexit
  %.01825 = phi ptr [ %7, %.loopexit ], [ %3, %0 ]
  %10 = getelementptr inbounds i8, ptr %.01825, i64 36
  %11 = load i32, ptr %10, align 4, !tbaa !16
  %12 = shl i32 %11, 3
  %13 = add i32 %12, 8
  %.0.ptr22 = getelementptr inbounds i8, ptr %.01825, i64 48
  %14 = zext i32 %13 to i64
  br label %15

15:                                               ; preds = %.lr.ph, %24
  %.0.ptr24 = phi ptr [ %.0.ptr22, %.lr.ph ], [ %.0.ptr, %24 ]
  %.0.idx23 = phi i64 [ 48, %.lr.ph ], [ %.0.add, %24 ]
  %16 = load i64, ptr %.0.ptr24, align 8, !tbaa !18
  %17 = and i64 %16, 1
  %.not = icmp eq i64 %17, 0
  br i1 %.not, label %24, label %18

18:                                               ; preds = %15
  %19 = and i64 %16, 2
  %.not21 = icmp eq i64 %19, 0
  br i1 %.not21, label %22, label %20

20:                                               ; preds = %18
  %21 = and i64 %16, -3
  store i64 %21, ptr %.0.ptr24, align 8, !tbaa !18
  br label %24

22:                                               ; preds = %18
  %23 = and i64 %16, -4
  store i64 %23, ptr %.0.ptr24, align 8, !tbaa !18
  tail call void @Free(ptr noundef nonnull %.0.ptr24) #2
  br label %24

24:                                               ; preds = %20, %22, %15
  %.0.add = add nuw nsw i64 %.0.idx23, %14
  %.0.ptr = getelementptr inbounds i8, ptr %.01825, i64 %.0.add
  %25 = icmp ult i64 %.0.add, 4096
  br i1 %25, label %15, label %.loopexit, !llvm.loop !20
}

declare void @Free(ptr noundef) local_unnamed_addr #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 = { "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 }

!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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"arena_object", !11, i64 0, !6, i64 8, !6, i64 16, !12, i64 24, !12, i64 28, !6, i64 32, !6, i64 40, !6, i64 48}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !6, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !12, i64 36}
!17 = !{!"pool_header", !7, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !12, i64 32, !12, i64 36, !12, i64 40, !12, i64 44}
!18 = !{!19, !11, i64 0}
!19 = !{!"header", !11, i64 0, !6, i64 8}
!20 = distinct !{!20, !15}


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