; LLVM IR from nickmqb/muon :: bootstrap/mu.ll_RangeFinder (find__)

; 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.tag_____IAllocator__ = type { ptr, ptr, ptr, ptr }
%struct.tag_____IntRange__ = type { i32, i32 }
%struct.Node__ = type { i32, ptr }
%struct.tag_____RangeFinderState__ = type { i32, i32 }

@top_____currentAllocator__ = external dso_local thread_local global %struct.tag_____IAllocator__, align 8
@top_____abandonFn__ = external dso_local thread_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 speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #1

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nounwind uwtable
define dso_local i64 @RangeFinder__find__(i32 %0, ptr %1) #4 {
  %3 = alloca %struct.tag_____IntRange__, align 4
  %4 = alloca %struct.Node__, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.tag_____RangeFinderState__, align 4
  %7 = alloca ptr, align 8
  %8 = getelementptr inbounds { i32, ptr }, ptr %4, i32 0, i32 0
  store i32 %0, ptr %8, align 8
  %9 = getelementptr inbounds { i32, ptr }, ptr %4, i32 0, i32 1
  store ptr %1, ptr %9, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %10 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____currentAllocator__)
  %11 = getelementptr inbounds %struct.tag_____IAllocator__, ptr %10, i32 0, i32 1
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %13 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____currentAllocator__)
  %14 = getelementptr inbounds %struct.tag_____IAllocator__, ptr %13, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8, !tbaa !10
  %16 = call ptr %12(ptr noundef %15, i64 noundef 8)
  store ptr %16, ptr %5, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  call void @llvm.memset.p0.i64(ptr align 4 %6, i8 0, i64 8, i1 false)
  %17 = getelementptr inbounds %struct.tag_____RangeFinderState__, ptr %6, i32 0, i32 0
  store i32 2147483647, ptr %17, align 4, !tbaa !12
  %18 = getelementptr inbounds %struct.tag_____RangeFinderState__, ptr %6, i32 0, i32 1
  store i32 -2147483648, ptr %18, align 4, !tbaa !15
  %19 = load ptr, ptr %5, align 8, !tbaa !11
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %19, ptr align 4 %6, i64 8, i1 false), !tbaa.struct !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %20 = load ptr, ptr %5, align 8, !tbaa !11
  store ptr %20, ptr %7, align 8, !tbaa !11
  %21 = load ptr, ptr %7, align 8, !tbaa !11
  %22 = getelementptr inbounds { i32, ptr }, ptr %4, i32 0, i32 0
  %23 = load i32, ptr %22, align 8
  %24 = getelementptr inbounds { i32, ptr }, ptr %4, i32 0, i32 1
  %25 = load ptr, ptr %24, align 8
  call void @RangeFinder__checkAny__(ptr noundef %21, i32 %23, ptr %25)
  %26 = load ptr, ptr %7, align 8, !tbaa !11
  %27 = getelementptr inbounds %struct.tag_____RangeFinderState__, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %27, align 4, !tbaa !12
  %29 = load ptr, ptr %7, align 8, !tbaa !11
  %30 = getelementptr inbounds %struct.tag_____RangeFinderState__, ptr %29, i32 0, i32 1
  %31 = load i32, ptr %30, align 4, !tbaa !15
  %32 = icmp sle i32 %28, %31
  br i1 %32, label %36, label %33

33:                                               ; preds = %2
  %34 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  call void %35(i32 noundef -2)
  br label %36

36:                                               ; preds = %33, %2
  %37 = load ptr, ptr %7, align 8, !tbaa !11
  %38 = getelementptr inbounds %struct.tag_____RangeFinderState__, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 4, !tbaa !12
  %40 = load ptr, ptr %7, align 8, !tbaa !11
  %41 = getelementptr inbounds %struct.tag_____RangeFinderState__, ptr %40, i32 0, i32 1
  %42 = load i32, ptr %41, align 4, !tbaa !15
  %43 = call i64 @IntRange__cons__(i32 noundef %39, i32 noundef %42)
  store i64 %43, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  %44 = load i64, ptr %3, align 4
  ret i64 %44
}

; Function Attrs: nounwind uwtable
declare dso_local i64 @IntRange__cons__(i32 noundef, i32 noundef) #4

; Function Attrs: nounwind uwtable
declare dso_local void @RangeFinder__checkAny__(ptr noundef, i32, ptr) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { 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, !7, i64 8}
!6 = !{!"tag_____IAllocator__", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !14, i64 0}
!13 = !{!"tag_____RangeFinderState__", !14, i64 0, !14, i64 4}
!14 = !{!"int", !8, i64 0}
!15 = !{!13, !14, i64 4}
!16 = !{i64 0, i64 4, !17, i64 4, i64 4, !17}
!17 = !{!14, !14, i64 0}


; 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.tag_____IAllocator__ = type { ptr, ptr, ptr, ptr }

@top_____currentAllocator__ = external dso_local thread_local local_unnamed_addr global %struct.tag_____IAllocator__, align 8
@top_____abandonFn__ = external dso_local thread_local local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0

; Function Attrs: nounwind uwtable
define dso_local i64 @RangeFinder__find__(i32 %0, ptr %1) local_unnamed_addr #1 {
  %3 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____currentAllocator__)
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8, !tbaa !10
  %7 = tail call ptr %5(ptr noundef %6, i64 noundef 8) #2
  store i32 2147483647, ptr %7, align 4, !tbaa !11
  %.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %7, i64 4
  store i32 -2147483648, ptr %.sroa.5.0..sroa_idx, align 4, !tbaa !11
  tail call void @RangeFinder__checkAny__(ptr noundef nonnull %7, i32 %0, ptr %1)
  %8 = load i32, ptr %7, align 4, !tbaa !13
  %9 = load i32, ptr %.sroa.5.0..sroa_idx, align 4, !tbaa !15
  %.not = icmp sgt i32 %8, %9
  br i1 %.not, label %10, label %13

10:                                               ; preds = %2
  %11 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  %12 = load ptr, ptr %11, align 8, !tbaa !16
  tail call void %12(i32 noundef -2) #2
  %.pre = load i32, ptr %7, align 4, !tbaa !13
  %.pre9 = load i32, ptr %.sroa.5.0..sroa_idx, align 4, !tbaa !15
  br label %13

13:                                               ; preds = %10, %2
  %14 = phi i32 [ %.pre9, %10 ], [ %9, %2 ]
  %15 = phi i32 [ %.pre, %10 ], [ %8, %2 ]
  %16 = tail call i64 @IntRange__cons__(i32 noundef %15, i32 noundef %14)
  ret i64 %16
}

; Function Attrs: nounwind uwtable
declare dso_local i64 @IntRange__cons__(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @RangeFinder__checkAny__(ptr noundef, i32, ptr) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !7, i64 8}
!6 = !{!"tag_____IAllocator__", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"tag_____RangeFinderState__", !12, i64 0, !12, i64 4}
!15 = !{!14, !12, i64 4}
!16 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,TailCallElimPass