; LLVM IR from eyangch/competitive-programming :: Codeforces/1324/d.ll (Z7binsrchxPxxx)

; 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 nounwind uwtable
define dso_local noundef i64 @_Z7binsrchxPxxx(i64 noundef %0, ptr 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 i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  store i64 %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !5
  store i64 %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store i64 0, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %13 = load i64, ptr %8, align 8, !tbaa !5
  store i64 %13, ptr %10, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %36, %4
  %15 = load i64, ptr %9, align 8, !tbaa !5
  %16 = load i64, ptr %10, align 8, !tbaa !5
  %17 = icmp slt i64 %15, %16
  br i1 %17, label %18, label %37

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

30:                                               ; preds = %18
  %31 = load i64, ptr %11, align 8, !tbaa !5
  %32 = sub nsw i64 %31, 1
  store i64 %32, ptr %10, align 8, !tbaa !5
  br label %36

33:                                               ; preds = %18
  %34 = load i64, ptr %11, align 8, !tbaa !5
  %35 = add nsw i64 %34, 1
  store i64 %35, ptr %9, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %33, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  br label %14, !llvm.loop !11

37:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %38 = load i64, ptr %9, align 8, !tbaa !5
  store i64 %38, ptr %12, align 8, !tbaa !5
  br label %39

39:                                               ; preds = %53, %37
  %40 = load ptr, ptr %6, align 8, !tbaa !9
  %41 = load i64, ptr %12, align 8, !tbaa !5
  %42 = getelementptr inbounds i64, ptr %40, i64 %41
  %43 = load i64, ptr %42, align 8, !tbaa !5
  %44 = load i64, ptr %7, align 8, !tbaa !5
  %45 = add nsw i64 %43, %44
  %46 = icmp sle i64 %45, 0
  br i1 %46, label %47, label %51

47:                                               ; preds = %39
  %48 = load i64, ptr %12, align 8, !tbaa !5
  %49 = load i64, ptr %5, align 8, !tbaa !5
  %50 = icmp slt i64 %48, %49
  br label %51

51:                                               ; preds = %47, %39
  %52 = phi i1 [ false, %39 ], [ %50, %47 ]
  br i1 %52, label %53, label %56

53:                                               ; preds = %51
  %54 = load i64, ptr %12, align 8, !tbaa !5
  %55 = add nsw i64 %54, 1
  store i64 %55, ptr %12, align 8, !tbaa !5
  br label %39, !llvm.loop !13

56:                                               ; preds = %51
  %57 = load i64, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  ret i64 %57
}

; 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 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef i64 @_Z7binsrchxPxxx(i64 noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = icmp sgt i64 %3, 0
  br i1 %5, label %.lr.ph, label %.preheader.preheader

.lr.ph:                                           ; preds = %4, %.lr.ph
  %.01721 = phi i64 [ %.1, %.lr.ph ], [ 0, %4 ]
  %.01820 = phi i64 [ %.119, %.lr.ph ], [ %3, %4 ]
  %6 = add nuw nsw i64 %.01721, %.01820
  %7 = lshr i64 %6, 1
  %8 = getelementptr inbounds i64, ptr %1, i64 %7
  %9 = load i64, ptr %8, align 8, !tbaa !5
  %10 = add nsw i64 %9, %2
  %11 = icmp sgt i64 %10, 0
  %12 = add nsw i64 %7, -1
  %13 = add nuw nsw i64 %7, 1
  %.119 = select i1 %11, i64 %12, i64 %.01820
  %.1 = select i1 %11, i64 %.01721, i64 %13
  %14 = icmp slt i64 %.1, %.119
  br i1 %14, label %.lr.ph, label %.preheader.preheader, !llvm.loop !9

.preheader.preheader:                             ; preds = %.lr.ph, %4
  %.0.ph = phi i64 [ 0, %4 ], [ %.1, %.lr.ph ]
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.preheader
  %.0 = phi i64 [ %21, %.preheader ], [ %.0.ph, %.preheader.preheader ]
  %15 = getelementptr inbounds i64, ptr %1, i64 %.0
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = add nsw i64 %16, %2
  %18 = icmp slt i64 %17, 1
  %19 = icmp slt i64 %.0, %0
  %20 = and i1 %19, %18
  %21 = add nuw nsw i64 %.0, 1
  br i1 %20, label %.preheader, label %22, !llvm.loop !11

22:                                               ; preds = %.preheader
  ret i64 %.0
}

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"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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