; LLVM IR from MostafaTwfiq/C-DataStructures-And-Algorithms :: Algorithms/Searching/Sources (BinarySearch)

; 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: 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 @binarySearchHelper(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef %5) #1 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store i32 %2, ptr %10, align 4, !tbaa !9
  store i32 %3, ptr %11, align 4, !tbaa !9
  store i32 %4, ptr %12, align 4, !tbaa !9
  store ptr %5, ptr %13, align 8, !tbaa !5
  %16 = load i32, ptr %10, align 4, !tbaa !9
  %17 = load i32, ptr %11, align 4, !tbaa !9
  %18 = icmp sgt i32 %16, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %6
  store i32 -1, ptr %7, align 4
  br label %67

20:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  %21 = load i32, ptr %10, align 4, !tbaa !9
  %22 = load i32, ptr %11, align 4, !tbaa !9
  %23 = add nsw i32 %21, %22
  %24 = sdiv i32 %23, 2
  store i32 %24, ptr %14, align 4, !tbaa !9
  %25 = load ptr, ptr %13, align 8, !tbaa !5
  %26 = load ptr, ptr %9, align 8, !tbaa !5
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = load i32, ptr %14, align 4, !tbaa !9
  %29 = load i32, ptr %12, align 4, !tbaa !9
  %30 = mul nsw i32 %28, %29
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i8, ptr %27, i64 %31
  %33 = call i32 %25(ptr noundef %26, ptr noundef %32)
  %34 = icmp eq i32 %33, 0
  br i1 %34, label %35, label %37

35:                                               ; preds = %20
  %36 = load i32, ptr %14, align 4, !tbaa !9
  store i32 %36, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %66

37:                                               ; preds = %20
  %38 = load ptr, ptr %13, align 8, !tbaa !5
  %39 = load ptr, ptr %9, align 8, !tbaa !5
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = load i32, ptr %14, align 4, !tbaa !9
  %42 = load i32, ptr %12, align 4, !tbaa !9
  %43 = mul nsw i32 %41, %42
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i8, ptr %40, i64 %44
  %46 = call i32 %38(ptr noundef %39, ptr noundef %45)
  %47 = icmp slt i32 %46, 0
  br i1 %47, label %48, label %57

48:                                               ; preds = %37
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  %50 = load ptr, ptr %9, align 8, !tbaa !5
  %51 = load i32, ptr %10, align 4, !tbaa !9
  %52 = load i32, ptr %14, align 4, !tbaa !9
  %53 = sub nsw i32 %52, 1
  %54 = load i32, ptr %12, align 4, !tbaa !9
  %55 = load ptr, ptr %13, align 8, !tbaa !5
  %56 = call i32 @binarySearchHelper(ptr noundef %49, ptr noundef %50, i32 noundef %51, i32 noundef %53, i32 noundef %54, ptr noundef %55)
  store i32 %56, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %66

57:                                               ; preds = %37
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = load ptr, ptr %9, align 8, !tbaa !5
  %60 = load i32, ptr %14, align 4, !tbaa !9
  %61 = add nsw i32 %60, 1
  %62 = load i32, ptr %11, align 4, !tbaa !9
  %63 = load i32, ptr %12, align 4, !tbaa !9
  %64 = load ptr, ptr %13, align 8, !tbaa !5
  %65 = call i32 @binarySearchHelper(ptr noundef %58, ptr noundef %59, i32 noundef %61, i32 noundef %62, i32 noundef %63, ptr noundef %64)
  store i32 %65, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %66

66:                                               ; preds = %57, %48, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  br label %67

67:                                               ; preds = %66, %19
  %68 = load i32, ptr %7, align 4
  ret i32 %68
}

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}


; 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: nounwind uwtable
define dso_local range(i32 -1073741824, 1073741824) i32 @binarySearchHelper(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr nocapture noundef readonly %5) local_unnamed_addr #0 {
  %7 = icmp sgt i32 %2, %3
  br i1 %7, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6, %tailrecurse.outer
  %.tr31.ph39 = phi i32 [ %.tr3135, %tailrecurse.outer ], [ %3, %6 ]
  %.tr30.ph38 = phi i32 [ %20, %tailrecurse.outer ], [ %2, %6 ]
  br label %8

8:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr3135 = phi i32 [ %.tr31.ph39, %.lr.ph ], [ %19, %tailrecurse ]
  %9 = add nsw i32 %.tr3135, %.tr30.ph38
  %10 = sdiv i32 %9, 2
  %11 = mul nsw i32 %10, %4
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds i8, ptr %0, i64 %12
  %14 = tail call i32 %5(ptr noundef %1, ptr noundef %13) #1
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %tailrecurse.outer._crit_edge, label %16

16:                                               ; preds = %8
  %17 = tail call i32 %5(ptr noundef %1, ptr noundef %13) #1
  %18 = icmp slt i32 %17, 0
  br i1 %18, label %tailrecurse, label %tailrecurse.outer

tailrecurse:                                      ; preds = %16
  %19 = add nsw i32 %10, -1
  %.not41 = icmp slt i32 %.tr30.ph38, %10
  br i1 %.not41, label %8, label %tailrecurse.outer._crit_edge

tailrecurse.outer:                                ; preds = %16
  %20 = add nsw i32 %10, 1
  %.not = icmp slt i32 %10, %.tr3135
  br i1 %.not, label %.lr.ph, label %tailrecurse.outer._crit_edge

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %tailrecurse, %8, %6
  %.1 = phi i32 [ -1, %6 ], [ %10, %8 ], [ -1, %tailrecurse ], [ -1, %tailrecurse.outer ]
  ret i32 %.1
}

attributes #0 = { 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 #1 = { 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)"}


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