; LLVM IR from sbshah97/DS-Algo-Handbook :: Algorithms/Search_Algorithms/Binary_Search (BinarySearch_Recursive)

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z12binarySearchPxxxx(ptr noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !9
  store i64 %3, ptr %9, align 8, !tbaa !9
  %12 = load i64, ptr %8, align 8, !tbaa !9
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = icmp sge i64 %12, %13
  br i1 %14, label %15, label %58

15:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = load i64, ptr %8, align 8, !tbaa !9
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = sub nsw i64 %17, %18
  %20 = sdiv i64 %19, 2
  %21 = add nsw i64 %16, %20
  %22 = trunc i64 %21 to i32
  store i32 %22, ptr %10, align 4, !tbaa !11
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load i32, ptr %10, align 4, !tbaa !11
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i64, ptr %23, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !9
  %28 = load i64, ptr %9, align 8, !tbaa !9
  %29 = icmp eq i64 %27, %28
  br i1 %29, label %30, label %33

30:                                               ; preds = %15
  %31 = load i32, ptr %10, align 4, !tbaa !11
  %32 = sext i32 %31 to i64
  store i64 %32, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %57

33:                                               ; preds = %15
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = load i32, ptr %10, align 4, !tbaa !11
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i64, ptr %34, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !9
  %39 = load i64, ptr %9, align 8, !tbaa !9
  %40 = icmp sgt i64 %38, %39
  br i1 %40, label %41, label %49

41:                                               ; preds = %33
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = load i32, ptr %10, align 4, !tbaa !11
  %45 = sub nsw i32 %44, 1
  %46 = sext i32 %45 to i64
  %47 = load i64, ptr %9, align 8, !tbaa !9
  %48 = call noundef i64 @_Z12binarySearchPxxxx(ptr noundef %42, i64 noundef %43, i64 noundef %46, i64 noundef %47)
  store i64 %48, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %57

49:                                               ; preds = %33
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = load i32, ptr %10, align 4, !tbaa !11
  %52 = add nsw i32 %51, 1
  %53 = sext i32 %52 to i64
  %54 = load i64, ptr %8, align 8, !tbaa !9
  %55 = load i64, ptr %9, align 8, !tbaa !9
  %56 = call noundef i64 @_Z12binarySearchPxxxx(ptr noundef %50, i64 noundef %53, i64 noundef %54, i64 noundef %55)
  store i64 %56, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %57

57:                                               ; preds = %49, %41, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %59

58:                                               ; preds = %4
  store i64 -1, ptr %5, align 8
  br label %59

59:                                               ; preds = %58, %57
  %60 = load i64, ptr %5, align 8
  ret i64 %60
}

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

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

attributes #0 = { mustprogress 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, 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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef range(i64 -2147483648, 2147483648) i64 @_Z12binarySearchPxxxx(ptr nocapture noundef readonly %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %.not3034 = icmp slt i64 %2, %1
  br i1 %.not3034, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %tailrecurse.outer
  %.tr27.ph36 = phi i64 [ %.tr2731, %tailrecurse.outer ], [ %2, %4 ]
  %.tr26.ph35 = phi i64 [ %16, %tailrecurse.outer ], [ %1, %4 ]
  br label %5

5:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr2731 = phi i64 [ %.tr27.ph36, %.lr.ph ], [ %15, %tailrecurse ]
  %6 = sub nsw i64 %.tr2731, %.tr26.ph35
  %7 = sdiv i64 %6, 2
  %8 = add nsw i64 %7, %.tr26.ph35
  %sext = shl i64 %8, 32
  %9 = ashr exact i64 %sext, 32
  %10 = getelementptr inbounds i64, ptr %0, i64 %9
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %12 = icmp eq i64 %11, %3
  br i1 %12, label %tailrecurse.outer._crit_edge, label %13

13:                                               ; preds = %5
  %14 = icmp sgt i64 %11, %3
  br i1 %14, label %tailrecurse, label %tailrecurse.outer

tailrecurse:                                      ; preds = %13
  %sext25 = add i64 %sext, -4294967296
  %15 = ashr exact i64 %sext25, 32
  %.not = icmp slt i64 %15, %.tr26.ph35
  br i1 %.not, label %tailrecurse.outer._crit_edge, label %5

tailrecurse.outer:                                ; preds = %13
  %sext24 = add i64 %sext, 4294967296
  %16 = ashr exact i64 %sext24, 32
  %.not30 = icmp slt i64 %.tr2731, %16
  br i1 %.not30, label %tailrecurse.outer._crit_edge, label %.lr.ph

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %5, %tailrecurse, %4
  %.1 = phi i64 [ -1, %4 ], [ -1, %tailrecurse ], [ %9, %5 ], [ -1, %tailrecurse.outer ]
  ret i64 %.1
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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 long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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