; LLVM IR from jermp/2i_bench :: external/CRoaring/src (array_util)

; 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: inlinehint nounwind uwtable
define dso_local i32 @binarySearch(ptr noundef %0, i32 noundef %1, i16 noundef zeroext %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i16, align 2
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i16, align 2
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i16 %2, ptr %7, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = sub nsw i32 %13, 1
  store i32 %14, ptr %9, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %52, %3
  %16 = load i32, ptr %8, align 4, !tbaa !9
  %17 = load i32, ptr %9, align 4, !tbaa !9
  %18 = icmp sle i32 %16, %17
  br i1 %18, label %19, label %53

19:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = load i32, ptr %9, align 4, !tbaa !9
  %22 = add nsw i32 %20, %21
  %23 = ashr i32 %22, 1
  store i32 %23, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #2
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = load i32, ptr %10, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i16, ptr %24, i64 %26
  %28 = load i16, ptr %27, align 2, !tbaa !11
  store i16 %28, ptr %11, align 2, !tbaa !11
  %29 = load i16, ptr %11, align 2, !tbaa !11
  %30 = zext i16 %29 to i32
  %31 = load i16, ptr %7, align 2, !tbaa !11
  %32 = zext i16 %31 to i32
  %33 = icmp slt i32 %30, %32
  br i1 %33, label %34, label %37

34:                                               ; preds = %19
  %35 = load i32, ptr %10, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %8, align 4, !tbaa !9
  br label %49

37:                                               ; preds = %19
  %38 = load i16, ptr %11, align 2, !tbaa !11
  %39 = zext i16 %38 to i32
  %40 = load i16, ptr %7, align 2, !tbaa !11
  %41 = zext i16 %40 to i32
  %42 = icmp sgt i32 %39, %41
  br i1 %42, label %43, label %46

43:                                               ; preds = %37
  %44 = load i32, ptr %10, align 4, !tbaa !9
  %45 = sub nsw i32 %44, 1
  store i32 %45, ptr %9, align 4, !tbaa !9
  br label %48

46:                                               ; preds = %37
  %47 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %47, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %50

48:                                               ; preds = %43
  br label %49

49:                                               ; preds = %48, %34
  store i32 0, ptr %12, align 4
  br label %50

50:                                               ; preds = %49, %46
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %51 = load i32, ptr %12, align 4
  switch i32 %51, label %57 [
    i32 0, label %52
  ]

52:                                               ; preds = %50
  br label %15, !llvm.loop !13

53:                                               ; preds = %15
  %54 = load i32, ptr %8, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  %56 = sub nsw i32 0, %55
  store i32 %56, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %57

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

; 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 = { inlinehint 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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

; Function Attrs: inlinehint nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local i32 @binarySearch(ptr nocapture noundef readonly %0, i32 noundef %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  %.not30 = icmp slt i32 %1, 1
  br i1 %.not30, label %.thread, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %4 = add nsw i32 %1, -1
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %17
  %.01732 = phi i32 [ %.219, %17 ], [ 0, %.lr.ph.preheader ]
  %.02031 = phi i32 [ %.222, %17 ], [ %4, %.lr.ph.preheader ]
  %5 = add nuw nsw i32 %.01732, %.02031
  %6 = lshr i32 %5, 1
  %7 = zext nneg i32 %6 to i64
  %8 = getelementptr inbounds i16, ptr %0, i64 %7
  %9 = load i16, ptr %8, align 2, !tbaa !5
  %10 = icmp ult i16 %9, %2
  br i1 %10, label %11, label %13

11:                                               ; preds = %.lr.ph
  %12 = add nuw nsw i32 %6, 1
  br label %17

13:                                               ; preds = %.lr.ph
  %14 = icmp ugt i16 %9, %2
  br i1 %14, label %15, label %.thread

15:                                               ; preds = %13
  %16 = add nsw i32 %6, -1
  br label %17

17:                                               ; preds = %11, %15
  %.222 = phi i32 [ %.02031, %11 ], [ %16, %15 ]
  %.219 = phi i32 [ %12, %11 ], [ %.01732, %15 ]
  %.not = icmp sgt i32 %.219, %.222
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !9

._crit_edge.loopexit:                             ; preds = %17
  %18 = xor i32 %.219, -1
  br label %.thread

.thread:                                          ; preds = %13, %3, %._crit_edge.loopexit
  %.2 = phi i32 [ -1, %3 ], [ %18, %._crit_edge.loopexit ], [ %6, %13 ]
  ret i32 %.2
}

attributes #0 = { inlinehint nofree norecurse nosync nounwind 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 = !{!"short", !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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass