; LLVM IR from scandum/binary_search :: binary_search (tripletapped_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 @tripletapped_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
  %11 = 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
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %8, align 4, !tbaa !9
  %12 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %12, ptr %10, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %34, %3
  %14 = load i32, ptr %10, align 4, !tbaa !9
  %15 = icmp ugt i32 %14, 3
  br i1 %15, label %16, label %38

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

30:                                               ; preds = %16
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = add i32 %32, %31
  store i32 %33, ptr %8, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %30, %16
  %35 = load i32, ptr %9, align 4, !tbaa !9
  %36 = load i32, ptr %10, align 4, !tbaa !9
  %37 = sub i32 %36, %35
  store i32 %37, ptr %10, align 4, !tbaa !9
  br label %13, !llvm.loop !11

38:                                               ; preds = %13
  br label %39

39:                                               ; preds = %59, %38
  %40 = load i32, ptr %10, align 4, !tbaa !9
  %41 = add i32 %40, -1
  store i32 %41, ptr %10, align 4, !tbaa !9
  %42 = icmp ne i32 %40, 0
  br i1 %42, label %43, label %60

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

55:                                               ; preds = %43
  %56 = load i32, ptr %8, align 4, !tbaa !9
  %57 = load i32, ptr %10, align 4, !tbaa !9
  %58 = add i32 %56, %57
  store i32 %58, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %61

59:                                               ; preds = %43
  br label %39, !llvm.loop !13

60:                                               ; preds = %39
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %61

61:                                               ; preds = %60, %55
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %62 = load i32, ptr %4, align 4
  ret i32 %62
}

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"}
!13 = distinct !{!13, !12}


; 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 @tripletapped_binary_search(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %checks.promoted = load i32, ptr @checks, align 4, !tbaa !5
  %4 = icmp ugt i32 %1, 3
  br i1 %4, label %.lr.ph, label %.preheader

.preheader:                                       ; preds = %.lr.ph, %3
  %checks.promoted25 = phi i32 [ %checks.promoted, %3 ], [ %8, %.lr.ph ]
  %.019.lcssa = phi i32 [ 0, %3 ], [ %spec.select, %.lr.ph ]
  %.0.lcssa = phi i32 [ %1, %3 ], [ %13, %.lr.ph ]
  %5 = zext nneg i32 %.0.lcssa to i64
  br label %15

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

15:                                               ; preds = %.preheader, %17
  %indvars.iv = phi i64 [ %5, %.preheader ], [ %indvars.iv.next, %17 ]
  %16 = phi i32 [ %checks.promoted25, %.preheader ], [ %18, %17 ]
  %.not = icmp eq i64 %indvars.iv, 0
  br i1 %.not, label %24, label %17

17:                                               ; preds = %15
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %indvars = trunc i64 %indvars.iv.next to i32
  %18 = add i32 %16, 1
  store i32 %18, ptr @checks, align 4, !tbaa !5
  %19 = add i32 %.019.lcssa, %indvars
  %20 = zext i32 %19 to i64
  %21 = getelementptr inbounds i32, ptr %0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = icmp eq i32 %22, %2
  br i1 %23, label %24, label %15, !llvm.loop !11

24:                                               ; preds = %15, %17
  %.018 = phi i32 [ %19, %17 ], [ -1, %15 ]
  ret i32 %.018
}

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"}
!11 = distinct !{!11, !10}


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