; LLVM IR from brewlin/garbage-collect :: mark-sweep-multi-free-list (gc)

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

@gc_heaps_used = external dso_local global i64, align 8
@gc_heaps = external dso_local global [1000000 x %struct.gc_heap], align 16
@hit_cache = external dso_local 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 ptr @is_pointer_to_heap(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %6 = load ptr, ptr @hit_cache, align 8, !tbaa !5
  %7 = icmp ne ptr %6, null
  br i1 %7, label %8, label %28

8:                                                ; preds = %1
  %9 = load ptr, ptr @hit_cache, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.gc_heap, ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = icmp ule ptr %11, %12
  br i1 %13, label %14, label %28

14:                                               ; preds = %8
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = ptrtoint ptr %15 to i64
  %17 = load ptr, ptr @hit_cache, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.gc_heap, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !9
  %20 = ptrtoint ptr %19 to i64
  %21 = load ptr, ptr @hit_cache, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.gc_heap, ptr %21, i32 0, i32 1
  %23 = load i64, ptr %22, align 8, !tbaa !12
  %24 = add i64 %20, %23
  %25 = icmp ult i64 %16, %24
  br i1 %25, label %26, label %28

26:                                               ; preds = %14
  %27 = load ptr, ptr @hit_cache, align 8, !tbaa !5
  store ptr %27, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %65

28:                                               ; preds = %14, %8, %1
  store i64 0, ptr %4, align 8, !tbaa !13
  br label %29

29:                                               ; preds = %61, %28
  %30 = load i64, ptr %4, align 8, !tbaa !13
  %31 = load i64, ptr @gc_heaps_used, align 8, !tbaa !13
  %32 = icmp ult i64 %30, %31
  br i1 %32, label %33, label %64

33:                                               ; preds = %29
  %34 = load i64, ptr %4, align 8, !tbaa !13
  %35 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.gc_heap, ptr %35, i32 0, i32 0
  %37 = load ptr, ptr %36, align 16, !tbaa !9
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = icmp ule ptr %37, %38
  br i1 %39, label %40, label %60

40:                                               ; preds = %33
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = ptrtoint ptr %41 to i64
  %43 = load i64, ptr %4, align 8, !tbaa !13
  %44 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %43
  %45 = getelementptr inbounds %struct.gc_heap, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 16, !tbaa !9
  %47 = ptrtoint ptr %46 to i64
  %48 = add i64 %47, 24
  %49 = load i64, ptr %4, align 8, !tbaa !13
  %50 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %49
  %51 = getelementptr inbounds %struct.gc_heap, ptr %50, i32 0, i32 1
  %52 = load i64, ptr %51, align 8, !tbaa !12
  %53 = add i64 %48, %52
  %54 = icmp ult i64 %42, %53
  br i1 %54, label %55, label %60

55:                                               ; preds = %40
  %56 = load i64, ptr %4, align 8, !tbaa !13
  %57 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %56
  store ptr %57, ptr @hit_cache, align 8, !tbaa !5
  %58 = load i64, ptr %4, align 8, !tbaa !13
  %59 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %58
  store ptr %59, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %65

60:                                               ; preds = %40, %33
  br label %61

61:                                               ; preds = %60
  %62 = load i64, ptr %4, align 8, !tbaa !13
  %63 = add i64 %62, 1
  store i64 %63, ptr %4, align 8, !tbaa !13
  br label %29, !llvm.loop !14

64:                                               ; preds = %29
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %65

65:                                               ; preds = %64, %55, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %66 = load ptr, ptr %2, align 8
  ret ptr %66
}

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 = { 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 0}
!10 = !{!"gc_heap", !6, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!11, !11, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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 }

@gc_heaps_used = external dso_local local_unnamed_addr global i64, align 8
@gc_heaps = external dso_local global [1000000 x %struct.gc_heap], align 16
@hit_cache = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local ptr @is_pointer_to_heap(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @hit_cache, align 8, !tbaa !5
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %12, label %3

3:                                                ; preds = %1
  %4 = load ptr, ptr %2, align 8, !tbaa !9
  %.not16 = icmp ugt ptr %4, %0
  br i1 %.not16, label %12, label %5

5:                                                ; preds = %3
  %6 = ptrtoint ptr %0 to i64
  %7 = ptrtoint ptr %4 to i64
  %8 = getelementptr inbounds i8, ptr %2, i64 8
  %9 = load i64, ptr %8, align 8, !tbaa !12
  %10 = add i64 %9, %7
  %11 = icmp ugt i64 %10, %6
  br i1 %11, label %.loopexit, label %12

12:                                               ; preds = %5, %3, %1
  %13 = load i64, ptr @gc_heaps_used, align 8, !tbaa !13
  %.not19 = icmp eq i64 %13, 0
  br i1 %.not19, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %12
  %14 = ptrtoint ptr %0 to i64
  br label %15

15:                                               ; preds = %.lr.ph, %26
  %.018 = phi i64 [ 0, %.lr.ph ], [ %27, %26 ]
  %16 = getelementptr inbounds [1000000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %.018
  %17 = load ptr, ptr %16, align 16, !tbaa !9
  %.not17 = icmp ugt ptr %17, %0
  br i1 %.not17, label %26, label %18

18:                                               ; preds = %15
  %19 = ptrtoint ptr %17 to i64
  %20 = add i64 %19, 24
  %21 = getelementptr inbounds i8, ptr %16, i64 8
  %22 = load i64, ptr %21, align 8, !tbaa !12
  %23 = add i64 %20, %22
  %24 = icmp ugt i64 %23, %14
  br i1 %24, label %25, label %26

25:                                               ; preds = %18
  store ptr %16, ptr @hit_cache, align 8, !tbaa !5
  br label %.loopexit

26:                                               ; preds = %15, %18
  %27 = add nuw i64 %.018, 1
  %exitcond.not = icmp eq i64 %27, %13
  br i1 %exitcond.not, label %.loopexit, label %15, !llvm.loop !14

.loopexit:                                        ; preds = %26, %12, %5, %25
  %.011 = phi ptr [ %16, %25 ], [ %2, %5 ], [ null, %12 ], [ null, %26 ]
  ret ptr %.011
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) 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" }

!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 0}
!10 = !{!"gc_heap", !6, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!11, !11, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass