; LLVM IR from brewlin/garbage-collect :: gc (is_pointer_to_heap)

; 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 [100000 x %struct.gc_heap], align 16

; 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
  store i64 0, ptr %4, align 8, !tbaa !9
  br label %6

6:                                                ; preds = %35, %1
  %7 = load i64, ptr %4, align 8, !tbaa !9
  %8 = load i64, ptr @gc_heaps_used, align 8, !tbaa !9
  %9 = icmp ult i64 %7, %8
  br i1 %9, label %10, label %38

10:                                               ; preds = %6
  %11 = load i64, ptr %4, align 8, !tbaa !9
  %12 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %11
  %13 = getelementptr inbounds %struct.gc_heap, ptr %12, i32 0, i32 0
  %14 = load ptr, ptr %13, align 16, !tbaa !11
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = icmp ule ptr %14, %15
  br i1 %16, label %17, label %34

17:                                               ; preds = %10
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = ptrtoint ptr %18 to i64
  %20 = load i64, ptr %4, align 8, !tbaa !9
  %21 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %20
  %22 = getelementptr inbounds %struct.gc_heap, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 16, !tbaa !11
  %24 = ptrtoint ptr %23 to i64
  %25 = load i64, ptr %4, align 8, !tbaa !9
  %26 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %25
  %27 = getelementptr inbounds %struct.gc_heap, ptr %26, i32 0, i32 1
  %28 = load i64, ptr %27, align 8, !tbaa !13
  %29 = add i64 %24, %28
  %30 = icmp ult i64 %19, %29
  br i1 %30, label %31, label %34

31:                                               ; preds = %17
  %32 = load i64, ptr %4, align 8, !tbaa !9
  %33 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %32
  store ptr %33, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %39

34:                                               ; preds = %17, %10
  br label %35

35:                                               ; preds = %34
  %36 = load i64, ptr %4, align 8, !tbaa !9
  %37 = add i64 %36, 1
  store i64 %37, ptr %4, align 8, !tbaa !9
  br label %6, !llvm.loop !14

38:                                               ; preds = %6
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %39

39:                                               ; preds = %38, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %40 = load ptr, ptr %2, align 8
  ret ptr %40
}

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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"gc_heap", !6, i64 0, !10, i64 8}
!13 = !{!12, !10, i64 8}
!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 [100000 x %struct.gc_heap], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef ptr @is_pointer_to_heap(ptr noundef %0) local_unnamed_addr #0 {
  %2 = ptrtoint ptr %0 to i64
  %3 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %.not14 = icmp eq i64 %3, 0
  br i1 %.not14, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %12
  %.011 = phi i64 [ %13, %12 ], [ 0, %1 ]
  %4 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %.011
  %5 = load ptr, ptr %4, align 16, !tbaa !9
  %.not = icmp ugt ptr %5, %0
  br i1 %.not, label %12, label %6

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

12:                                               ; preds = %.lr.ph, %6
  %13 = add nuw i64 %.011, 1
  %exitcond.not = icmp eq i64 %13, %3
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %6, %12, %1
  %.08 = phi ptr [ null, %1 ], [ null, %12 ], [ %4, %6 ]
  ret ptr %.08
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: none, 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 = !{!"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 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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