; LLVM IR from reyadussalahin/problem-solving :: search/binary_search/codeforces/F1345B (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 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %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
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %77, %0
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %80

16:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %17 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %4)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %73, %16
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = icmp sgt i32 %19, 0
  br i1 %20, label %21, label %74

21:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 1000000000, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %51, %21
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = icmp sle i32 %23, %24
  br i1 %25, label %26, label %52

26:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = load i32, ptr %7, align 4, !tbaa !5
  %29 = add nsw i32 %27, %28
  %30 = sdiv i32 %29, 2
  store i32 %30, ptr %9, align 4, !tbaa !5
  %31 = load i32, ptr %9, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = mul nsw i64 3, %32
  %34 = load i32, ptr %9, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = mul nsw i64 %33, %35
  %37 = load i32, ptr %9, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = add nsw i64 %36, %38
  %40 = sdiv i64 %39, 2
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = icmp sle i64 %40, %42
  br i1 %43, label %44, label %48

44:                                               ; preds = %26
  %45 = load i32, ptr %9, align 4, !tbaa !5
  store i32 %45, ptr %8, align 4, !tbaa !5
  %46 = load i32, ptr %9, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %6, align 4, !tbaa !5
  br label %51

48:                                               ; preds = %26
  %49 = load i32, ptr %9, align 4, !tbaa !5
  %50 = sub nsw i32 %49, 1
  store i32 %50, ptr %7, align 4, !tbaa !5
  br label %51

51:                                               ; preds = %48, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %22, !llvm.loop !9

52:                                               ; preds = %22
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %72

55:                                               ; preds = %52
  %56 = load i32, ptr %4, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = load i32, ptr %8, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = mul nsw i64 3, %59
  %61 = load i32, ptr %8, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = mul nsw i64 %60, %62
  %64 = load i32, ptr %8, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = add nsw i64 %63, %65
  %67 = sdiv i64 %66, 2
  %68 = sub nsw i64 %57, %67
  %69 = trunc i64 %68 to i32
  store i32 %69, ptr %4, align 4, !tbaa !5
  %70 = load i32, ptr %5, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %5, align 4, !tbaa !5
  br label %73

72:                                               ; preds = %52
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %73

73:                                               ; preds = %72, %55
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %18, !llvm.loop !11

74:                                               ; preds = %18
  %75 = load i32, ptr %5, align 4, !tbaa !5
  %76 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %75)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %77

77:                                               ; preds = %74
  %78 = load i32, ptr %3, align 4, !tbaa !5
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %3, align 4, !tbaa !5
  br label %11, !llvm.loop !12

80:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  ret i32 0
}

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

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

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

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 #3 = { 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}


; 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 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  %3 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %4 = load i32, ptr %1, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph, label %._crit_edge33

._crit_edge33:                                    ; preds = %._crit_edge, %0
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  ret i32 0

.lr.ph:                                           ; preds = %0, %._crit_edge
  %.031 = phi i32 [ %32, %._crit_edge ], [ 0, %0 ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %2)
  %.promoted = load i32, ptr %2, align 4, !tbaa !5
  %7 = icmp sgt i32 %.promoted, 0
  br i1 %7, label %.preheader, label %._crit_edge

.preheader:                                       ; preds = %.lr.ph, %21
  %.02330 = phi i32 [ %29, %21 ], [ 0, %.lr.ph ]
  %8 = phi i32 [ %28, %21 ], [ %.promoted, %.lr.ph ]
  %9 = zext nneg i32 %8 to i64
  br label %10

10:                                               ; preds = %.preheader, %10
  %.01829 = phi i32 [ 0, %.preheader ], [ %.1, %10 ]
  %.01928 = phi i32 [ 1000000000, %.preheader ], [ %.120, %10 ]
  %.02127 = phi i32 [ 0, %.preheader ], [ %.122, %10 ]
  %11 = add nuw nsw i32 %.01928, %.02127
  %12 = lshr i32 %11, 1
  %13 = zext nneg i32 %12 to i64
  %14 = mul nuw nsw i64 %13, 3
  %15 = add nuw nsw i64 %14, 1
  %16 = mul nuw i64 %15, %13
  %17 = lshr i64 %16, 1
  %.not26 = icmp ugt i64 %17, %9
  %18 = add nuw nsw i32 %12, 1
  %19 = add nsw i32 %12, -1
  %.122 = select i1 %.not26, i32 %.02127, i32 %18
  %.120 = select i1 %.not26, i32 %19, i32 %.01928
  %.1 = select i1 %.not26, i32 %.01829, i32 %12
  %.not = icmp sgt i32 %.122, %.120
  br i1 %.not, label %20, label %10, !llvm.loop !9

20:                                               ; preds = %10
  %.not25 = icmp eq i32 %.1, 0
  br i1 %.not25, label %.thread, label %21

.thread:                                          ; preds = %20
  store i32 0, ptr %2, align 4
  br label %._crit_edge

21:                                               ; preds = %20
  %22 = zext nneg i32 %.1 to i64
  %23 = mul nuw nsw i64 %22, 3
  %24 = add nuw nsw i64 %23, 1
  %25 = mul nuw i64 %24, %22
  %26 = lshr i64 %25, 1
  %27 = trunc i64 %26 to i32
  %28 = sub i32 %8, %27
  %29 = add nuw nsw i32 %.02330, 1
  store i32 %28, ptr %2, align 4
  %30 = icmp sgt i32 %28, 0
  br i1 %30, label %.preheader, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %21, %.thread, %.lr.ph
  %.023.lcssa = phi i32 [ 0, %.lr.ph ], [ %.02330, %.thread ], [ %29, %21 ]
  %31 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %.023.lcssa)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  %32 = add nuw nsw i32 %.031, 1
  %33 = load i32, ptr %1, align 4, !tbaa !5
  %34 = icmp slt i32 %32, %33
  br i1 %34, label %.lr.ph, label %._crit_edge33, !llvm.loop !12
}

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

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

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

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

attributes #0 = { mustprogress nofree norecurse 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass