; LLVM IR from scandum/binary_search :: binary_search (boundless_binary_search)

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

@checks = external dso_local global i32, align 4

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

13:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %54

14:                                               ; preds = %3
  store i32 0, ptr %9, align 4, !tbaa !9
  %15 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %15, ptr %8, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %38, %14
  %17 = load i32, ptr %8, align 4, !tbaa !9
  %18 = icmp ugt i32 %17, 1
  br i1 %18, label %19, label %41

19:                                               ; preds = %16
  %20 = load i32, ptr @checks, align 4, !tbaa !9
  %21 = add i32 %20, 1
  store i32 %21, ptr @checks, align 4, !tbaa !9
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = load i32, ptr %8, align 4, !tbaa !9
  %26 = udiv i32 %25, 2
  %27 = add i32 %24, %26
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds i32, ptr %23, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = icmp sge i32 %22, %30
  br i1 %31, label %32, label %38

32:                                               ; preds = %19
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = add i32 %33, 1
  store i32 %34, ptr %8, align 4, !tbaa !9
  %35 = udiv i32 %33, 2
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = add i32 %36, %35
  store i32 %37, ptr %9, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %32, %19
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = udiv i32 %39, 2
  store i32 %40, ptr %8, align 4, !tbaa !9
  br label %16, !llvm.loop !11

41:                                               ; preds = %16
  %42 = load i32, ptr @checks, align 4, !tbaa !9
  %43 = add i32 %42, 1
  store i32 %43, ptr @checks, align 4, !tbaa !9
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = load i32, ptr %9, align 4, !tbaa !9
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds i32, ptr %45, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !9
  %50 = icmp eq i32 %44, %49
  br i1 %50, label %51, label %53

51:                                               ; preds = %41
  %52 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %52, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %54

53:                                               ; preds = %41
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %54

54:                                               ; preds = %53, %51, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %55 = load i32, ptr %4, align 4
  ret i32 %55
}

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 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@checks = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local i32 @boundless_binary_search(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq i32 %1, 0
  br i1 %4, label %21, label %.preheader

.preheader:                                       ; preds = %3
  %checks.promoted = load i32, ptr @checks, align 4, !tbaa !5
  %.not21 = icmp eq i32 %1, 1
  br i1 %.not21, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.020 = phi i32 [ %spec.select18, %.lr.ph ], [ 0, %.preheader ]
  %.01419 = phi i32 [ %13, %.lr.ph ], [ %1, %.preheader ]
  %5 = phi i32 [ %6, %.lr.ph ], [ %checks.promoted, %.preheader ]
  %6 = add i32 %5, 1
  store i32 %6, ptr @checks, align 4, !tbaa !5
  %7 = lshr i32 %.01419, 1
  %8 = add i32 %.020, %7
  %9 = zext i32 %8 to i64
  %10 = getelementptr inbounds i32, ptr %0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %.not = icmp sle i32 %11, %2
  %12 = zext i1 %.not to i32
  %spec.select = add i32 %.01419, %12
  %spec.select18 = select i1 %.not, i32 %8, i32 %.020
  %13 = lshr i32 %spec.select, 1
  %14 = icmp ugt i32 %spec.select, 3
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %15 = phi i32 [ %checks.promoted, %.preheader ], [ %6, %.lr.ph ]
  %.0.lcssa = phi i32 [ 0, %.preheader ], [ %spec.select18, %.lr.ph ]
  %16 = add i32 %15, 1
  store i32 %16, ptr @checks, align 4, !tbaa !5
  %17 = zext i32 %.0.lcssa to i64
  %18 = getelementptr inbounds i32, ptr %0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = icmp eq i32 %19, %2
  %.0. = select i1 %20, i32 %.0.lcssa, i32 -1
  br label %21

21:                                               ; preds = %._crit_edge, %3
  %.016 = phi i32 [ -1, %3 ], [ %.0., %._crit_edge ]
  ret i32 %.016
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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