; LLVM IR from div-bargali/Data-Structures-and-Algorithms :: C/Algorithms/Searching-Algorithms (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"

@low = external dso_local global i32, align 4
@high = external dso_local global i32, align 4
@Index = external dso_local global i32, align 4
@mid = external dso_local global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @Binary_search(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %6 = load i32, ptr @high, align 4, !tbaa !9
  %7 = load i32, ptr @low, align 4, !tbaa !9
  %8 = icmp sgt i32 %6, %7
  br i1 %8, label %9, label %46

9:                                                ; preds = %2
  %10 = load i32, ptr @low, align 4, !tbaa !9
  %11 = load i32, ptr @high, align 4, !tbaa !9
  %12 = add nsw i32 %10, %11
  %13 = sdiv i32 %12, 2
  store i32 %13, ptr @mid, align 4, !tbaa !9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = load i32, ptr @mid, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds i32, ptr %14, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !9
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = icmp eq i32 %18, %19
  br i1 %20, label %21, label %23

21:                                               ; preds = %9
  %22 = load i32, ptr @mid, align 4, !tbaa !9
  store i32 %22, ptr @Index, align 4, !tbaa !9
  br label %44

23:                                               ; preds = %9
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = load i32, ptr @mid, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %24, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = icmp slt i32 %28, %29
  br i1 %30, label %31, label %37

31:                                               ; preds = %23
  %32 = load i32, ptr @mid, align 4, !tbaa !9
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr @low, align 4, !tbaa !9
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = load i32, ptr %5, align 4, !tbaa !9
  %36 = call i32 @Binary_search(ptr noundef %34, i32 noundef %35)
  store i32 %36, ptr @Index, align 4, !tbaa !9
  br label %43

37:                                               ; preds = %23
  %38 = load i32, ptr @mid, align 4, !tbaa !9
  %39 = sub nsw i32 %38, 1
  store i32 %39, ptr @high, align 4, !tbaa !9
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = load i32, ptr %5, align 4, !tbaa !9
  %42 = call i32 @Binary_search(ptr noundef %40, i32 noundef %41)
  store i32 %42, ptr @Index, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %37, %31
  br label %44

44:                                               ; preds = %43, %21
  %45 = load i32, ptr @Index, align 4, !tbaa !9
  store i32 %45, ptr %3, align 4
  br label %48

46:                                               ; preds = %2
  %47 = load i32, ptr @Index, align 4, !tbaa !9
  store i32 %47, ptr %3, align 4
  br label %48

48:                                               ; preds = %46, %44
  %49 = load i32, ptr %3, align 4
  ret i32 %49
}

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

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

@low = external dso_local local_unnamed_addr global i32, align 4
@high = external dso_local local_unnamed_addr global i32, align 4
@Index = external dso_local local_unnamed_addr global i32, align 4
@mid = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: nofree nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local i32 @Binary_search(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @high, align 4, !tbaa !5
  %4 = load i32, ptr @low, align 4, !tbaa !5
  %5 = icmp sgt i32 %3, %4
  br i1 %5, label %6, label %._crit_edge

._crit_edge:                                      ; preds = %2
  %.0.pre = load i32, ptr @Index, align 4, !tbaa !5
  br label %common.ret

6:                                                ; preds = %2
  %7 = add nsw i32 %4, %3
  %8 = sdiv i32 %7, 2
  store i32 %8, ptr @mid, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds i32, ptr %0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = icmp eq i32 %11, %1
  br i1 %12, label %13, label %14

13:                                               ; preds = %6
  store i32 %8, ptr @Index, align 4, !tbaa !5
  br label %common.ret

14:                                               ; preds = %6
  %15 = icmp slt i32 %11, %1
  br i1 %15, label %16, label %19

common.ret:                                       ; preds = %13, %._crit_edge, %19, %16
  %common.ret.op = phi i32 [ %18, %16 ], [ %21, %19 ], [ %.0.pre, %._crit_edge ], [ %8, %13 ]
  ret i32 %common.ret.op

16:                                               ; preds = %14
  %17 = add nsw i32 %8, 1
  store i32 %17, ptr @low, align 4, !tbaa !5
  %18 = tail call i32 @Binary_search(ptr noundef nonnull %0, i32 noundef %1)
  store i32 %18, ptr @Index, align 4, !tbaa !5
  br label %common.ret

19:                                               ; preds = %14
  %20 = add nsw i32 %8, -1
  store i32 %20, ptr @high, align 4, !tbaa !5
  %21 = tail call i32 @Binary_search(ptr noundef nonnull %0, i32 noundef %1)
  store i32 %21, ptr @Index, align 4, !tbaa !5
  br label %common.ret
}

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass