; LLVM IR from reyadussalahin/problem-solving :: search/binary_search/codeforces/F1486C2 (Solution)

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

@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z5queryii(i32 noundef, i32 noundef) #0

declare i32 @printf(ptr noundef, ...) #1

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

declare i32 @__isoc99_scanf(ptr noundef, ...) #1

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

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #3 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i8, align 1
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  %11 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %2)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = call noundef i32 @_Z5queryii(i32 noundef 1, i32 noundef %12)
  store i32 %13, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #4
  store i8 1, ptr %4, align 1, !tbaa !9
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = call noundef i32 @_Z5queryii(i32 noundef 1, i32 noundef %14)
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = icmp ne i32 %15, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %0
  store i8 0, ptr %4, align 1, !tbaa !9
  br label %19

19:                                               ; preds = %18, %0
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %20 = load i8, ptr %4, align 1, !tbaa !9, !range !11, !noundef !12
  %21 = trunc i8 %20 to i1
  br i1 %21, label %22, label %24

22:                                               ; preds = %19
  %23 = load i32, ptr %3, align 4, !tbaa !5
  br label %29

24:                                               ; preds = %19
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = sub nsw i32 %25, %26
  %28 = add nsw i32 %27, 1
  br label %29

29:                                               ; preds = %24, %22
  %30 = phi i32 [ %23, %22 ], [ %28, %24 ]
  store i32 %30, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 1, ptr %6, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %34, %29
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = icmp sgt i32 %32, 0
  br i1 %33, label %34, label %40

34:                                               ; preds = %31
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = load i32, ptr %6, align 4, !tbaa !5
  %37 = add nsw i32 %36, %35
  store i32 %37, ptr %6, align 4, !tbaa !5
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = sdiv i32 %38, 2
  store i32 %39, ptr %5, align 4, !tbaa !5
  br label %31, !llvm.loop !13

40:                                               ; preds = %31
  %41 = load i8, ptr %4, align 1, !tbaa !9, !range !11, !noundef !12
  %42 = trunc i8 %41 to i1
  br i1 %42, label %43, label %70

43:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 1, ptr %7, align 4, !tbaa !5
  br label %44

44:                                               ; preds = %62, %43
  %45 = load i32, ptr %6, align 4, !tbaa !5
  %46 = icmp sgt i32 %45, 0
  br i1 %46, label %47, label %65

47:                                               ; preds = %44
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = sdiv i32 %49, 2
  %51 = add nsw i32 %48, %50
  store i32 %51, ptr %8, align 4, !tbaa !5
  %52 = load i32, ptr %8, align 4, !tbaa !5
  %53 = load i32, ptr %3, align 4, !tbaa !5
  %54 = call noundef i32 @_Z5queryii(i32 noundef %52, i32 noundef %53)
  %55 = load i32, ptr %3, align 4, !tbaa !5
  %56 = icmp eq i32 %54, %55
  br i1 %56, label %57, label %62

57:                                               ; preds = %47
  %58 = load i32, ptr %6, align 4, !tbaa !5
  %59 = sdiv i32 %58, 2
  %60 = load i32, ptr %7, align 4, !tbaa !5
  %61 = add nsw i32 %60, %59
  store i32 %61, ptr %7, align 4, !tbaa !5
  br label %62

62:                                               ; preds = %57, %47
  %63 = load i32, ptr %6, align 4, !tbaa !5
  %64 = sdiv i32 %63, 2
  store i32 %64, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %44, !llvm.loop !15

65:                                               ; preds = %44
  %66 = load i32, ptr %7, align 4, !tbaa !5
  %67 = load i32, ptr %6, align 4, !tbaa !5
  %68 = add nsw i32 %66, %67
  %69 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %68)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %98

70:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %71 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %71, ptr %9, align 4, !tbaa !5
  br label %72

72:                                               ; preds = %90, %70
  %73 = load i32, ptr %6, align 4, !tbaa !5
  %74 = icmp sgt i32 %73, 0
  br i1 %74, label %75, label %93

75:                                               ; preds = %72
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %76 = load i32, ptr %9, align 4, !tbaa !5
  %77 = load i32, ptr %6, align 4, !tbaa !5
  %78 = sdiv i32 %77, 2
  %79 = sub nsw i32 %76, %78
  store i32 %79, ptr %10, align 4, !tbaa !5
  %80 = load i32, ptr %3, align 4, !tbaa !5
  %81 = load i32, ptr %10, align 4, !tbaa !5
  %82 = call noundef i32 @_Z5queryii(i32 noundef %80, i32 noundef %81)
  %83 = load i32, ptr %3, align 4, !tbaa !5
  %84 = icmp eq i32 %82, %83
  br i1 %84, label %85, label %90

85:                                               ; preds = %75
  %86 = load i32, ptr %6, align 4, !tbaa !5
  %87 = sdiv i32 %86, 2
  %88 = load i32, ptr %9, align 4, !tbaa !5
  %89 = sub nsw i32 %88, %87
  store i32 %89, ptr %9, align 4, !tbaa !5
  br label %90

90:                                               ; preds = %85, %75
  %91 = load i32, ptr %6, align 4, !tbaa !5
  %92 = sdiv i32 %91, 2
  store i32 %92, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %72, !llvm.loop !16

93:                                               ; preds = %72
  %94 = load i32, ptr %9, align 4, !tbaa !5
  %95 = load i32, ptr %6, align 4, !tbaa !5
  %96 = sub nsw i32 %94, %95
  %97 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %96)
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %98

98:                                               ; preds = %93, %65
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  ret i32 0
}

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 = { "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress norecurse 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 #4 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


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

@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z5queryii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #3 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #4
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %1)
  %3 = load i32, ptr %1, align 4, !tbaa !5
  %4 = call noundef i32 @_Z5queryii(i32 noundef 1, i32 noundef %3)
  %5 = call noundef i32 @_Z5queryii(i32 noundef 1, i32 noundef %4)
  %.not = icmp eq i32 %5, %4
  %6 = load i32, ptr %1, align 4
  %reass.sub = sub i32 %6, %4
  %7 = add i32 %reass.sub, 1
  %8 = select i1 %.not, i32 %4, i32 %7
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %.lr.ph, label %._crit_edge.thread

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.03341 = phi i32 [ %11, %.lr.ph ], [ %8, %0 ]
  %.03640 = phi i32 [ %10, %.lr.ph ], [ 1, %0 ]
  %10 = shl nsw i32 %.03640, 1
  %11 = lshr i32 %.03341, 1
  %.not54 = icmp ult i32 %.03341, 2
  br i1 %.not54, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph
  %12 = icmp sgt i32 %.03640, 0
  br i1 %.not, label %.preheader, label %.preheader39

._crit_edge.thread:                               ; preds = %0
  br i1 %.not, label %.lr.ph50.preheader, label %.lr.ph44.preheader

.preheader39:                                     ; preds = %._crit_edge
  br i1 %12, label %.lr.ph44.preheader, label %._crit_edge45

.lr.ph44.preheader:                               ; preds = %._crit_edge.thread, %.preheader39
  %.242.ph = phi i32 [ %10, %.preheader39 ], [ 1, %._crit_edge.thread ]
  br label %.lr.ph44

.preheader:                                       ; preds = %._crit_edge
  br i1 %12, label %.lr.ph50.preheader, label %._crit_edge51

.lr.ph50.preheader:                               ; preds = %._crit_edge.thread, %.preheader
  %.13748.ph = phi i32 [ %10, %.preheader ], [ 1, %._crit_edge.thread ]
  br label %.lr.ph50

.lr.ph50:                                         ; preds = %.lr.ph50.preheader, %.lr.ph50
  %.03449 = phi i32 [ %spec.select, %.lr.ph50 ], [ 1, %.lr.ph50.preheader ]
  %.13748 = phi i32 [ %13, %.lr.ph50 ], [ %.13748.ph, %.lr.ph50.preheader ]
  %13 = lshr i32 %.13748, 1
  %14 = add nuw nsw i32 %.03449, %13
  %15 = call noundef i32 @_Z5queryii(i32 noundef %14, i32 noundef %4)
  %16 = icmp eq i32 %15, %4
  %spec.select = select i1 %16, i32 %14, i32 %.03449
  %.not56 = icmp ult i32 %.13748, 2
  br i1 %.not56, label %._crit_edge51, label %.lr.ph50, !llvm.loop !11

._crit_edge51:                                    ; preds = %.lr.ph50, %.preheader
  %.137.lcssa = phi i32 [ %10, %.preheader ], [ %13, %.lr.ph50 ]
  %.034.lcssa = phi i32 [ 1, %.preheader ], [ %spec.select, %.lr.ph50 ]
  %17 = add nsw i32 %.034.lcssa, %.137.lcssa
  br label %23

.lr.ph44:                                         ; preds = %.lr.ph44.preheader, %.lr.ph44
  %.03243 = phi i32 [ %spec.select38, %.lr.ph44 ], [ %6, %.lr.ph44.preheader ]
  %.242 = phi i32 [ %18, %.lr.ph44 ], [ %.242.ph, %.lr.ph44.preheader ]
  %18 = lshr i32 %.242, 1
  %19 = sub nsw i32 %.03243, %18
  %20 = call noundef i32 @_Z5queryii(i32 noundef %4, i32 noundef %19)
  %21 = icmp eq i32 %20, %4
  %spec.select38 = select i1 %21, i32 %19, i32 %.03243
  %.not55 = icmp ult i32 %.242, 2
  br i1 %.not55, label %._crit_edge45, label %.lr.ph44, !llvm.loop !12

._crit_edge45:                                    ; preds = %.lr.ph44, %.preheader39
  %.2.lcssa = phi i32 [ %10, %.preheader39 ], [ %18, %.lr.ph44 ]
  %.032.lcssa = phi i32 [ %6, %.preheader39 ], [ %spec.select38, %.lr.ph44 ]
  %22 = sub nsw i32 %.032.lcssa, %.2.lcssa
  br label %23

23:                                               ; preds = %._crit_edge45, %._crit_edge51
  %.sink = phi i32 [ %22, %._crit_edge45 ], [ %17, %._crit_edge51 ]
  %24 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %.sink)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #4
  ret i32 0
}

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 = { nofree nounwind "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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress norecurse 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 #4 = { 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 = !{!"int", !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}
!12 = distinct !{!12, !10}


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