; LLVM IR from reyadussalahin/problem-solving :: ad_hoc/constructive_algorithm/codeforces/F1350D (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"

@b = external dso_local global [100010 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = 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 i8, align 1
  %9 = alloca i8, align 1
  %10 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %11 = 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 %12

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

16:                                               ; preds = %12
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %92

17:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  %18 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %5, ptr noundef %6)
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #3
  store i8 0, ptr %8, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #3
  store i8 0, ptr %9, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %69, %17
  %20 = load i32, ptr %10, align 4, !tbaa !5
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %72

24:                                               ; preds = %19
  %25 = load i32, ptr %10, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100010 x i32], ptr @b, i64 0, i64 %26
  %28 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %27)
  %29 = load i32, ptr %10, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [100010 x i32], ptr @b, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = icmp eq i32 %32, %33
  %35 = zext i1 %34 to i32
  %36 = load i8, ptr %9, align 1, !tbaa !9, !range !11, !noundef !12
  %37 = trunc i8 %36 to i1
  %38 = zext i1 %37 to i32
  %39 = or i32 %38, %35
  %40 = icmp ne i32 %39, 0
  %41 = zext i1 %40 to i8
  store i8 %41, ptr %9, align 1, !tbaa !9
  %42 = load i32, ptr %10, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [100010 x i32], ptr @b, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = load i32, ptr %6, align 4, !tbaa !5
  %47 = icmp sge i32 %45, %46
  %48 = zext i1 %47 to i32
  %49 = load i32, ptr %7, align 4, !tbaa !5
  %50 = add nsw i32 %49, %48
  store i32 %50, ptr %7, align 4, !tbaa !5
  %51 = load i32, ptr %10, align 4, !tbaa !5
  %52 = icmp sge i32 %51, 3
  br i1 %52, label %53, label %64

53:                                               ; preds = %24
  %54 = load i32, ptr %10, align 4, !tbaa !5
  %55 = sub nsw i32 %54, 3
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [100010 x i32], ptr @b, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = load i32, ptr %6, align 4, !tbaa !5
  %60 = icmp sge i32 %58, %59
  %61 = zext i1 %60 to i32
  %62 = load i32, ptr %7, align 4, !tbaa !5
  %63 = sub nsw i32 %62, %61
  store i32 %63, ptr %7, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %53, %24
  %65 = load i32, ptr %7, align 4, !tbaa !5
  %66 = icmp sge i32 %65, 2
  br i1 %66, label %67, label %68

67:                                               ; preds = %64
  store i8 1, ptr %8, align 1, !tbaa !9
  br label %68

68:                                               ; preds = %67, %64
  br label %69

69:                                               ; preds = %68
  %70 = load i32, ptr %10, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %10, align 4, !tbaa !5
  br label %19, !llvm.loop !13

72:                                               ; preds = %23
  %73 = load i8, ptr %9, align 1, !tbaa !9, !range !11, !noundef !12
  %74 = trunc i8 %73 to i1
  br i1 %74, label %75, label %86

75:                                               ; preds = %72
  %76 = load i8, ptr %8, align 1, !tbaa !9, !range !11, !noundef !12
  %77 = trunc i8 %76 to i1
  br i1 %77, label %84, label %78

78:                                               ; preds = %75
  %79 = load i32, ptr %5, align 4, !tbaa !5
  %80 = icmp eq i32 %79, 1
  br i1 %80, label %81, label %86

81:                                               ; preds = %78
  %82 = load i32, ptr %7, align 4, !tbaa !5
  %83 = icmp eq i32 %82, 1
  br i1 %83, label %84, label %86

84:                                               ; preds = %81, %75
  %85 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  br label %88

86:                                               ; preds = %81, %78, %72
  %87 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  br label %88

88:                                               ; preds = %86, %84
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 1, 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
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %89

89:                                               ; preds = %88
  %90 = load i32, ptr %3, align 4, !tbaa !5
  %91 = add nsw i32 %90, 1
  store i32 %91, ptr %3, align 4, !tbaa !5
  br label %12, !llvm.loop !15

92:                                               ; preds = %16
  store i32 0, ptr %1, align 4
  store i32 1, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  %93 = load i32, ptr %1, align 4
  ret i32 %93
}

; 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 = !{!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}


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

@b = external dso_local global [100010 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = 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
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  %4 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph33, label %._crit_edge34

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

.lr.ph33:                                         ; preds = %0, %64
  %.02231 = phi i32 [ %66, %64 ], [ 0, %0 ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #3
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %2, ptr noundef nonnull %3)
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %10, label %._crit_edge.thread

10:                                               ; preds = %.lr.ph33
  %11 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @b)
  %12 = load i32, ptr @b, align 16, !tbaa !5
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = icmp eq i32 %12, %13
  %15 = icmp sge i32 %12, %13
  %16 = zext i1 %15 to i32
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = icmp sgt i32 %17, 1
  br i1 %18, label %19, label %._crit_edge.thread54

19:                                               ; preds = %10
  %20 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull getelementptr inbounds (i8, ptr @b, i64 4))
  %21 = load i32, ptr getelementptr inbounds (i8, ptr @b, i64 4), align 4, !tbaa !5
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = icmp eq i32 %21, %22
  %24 = or i1 %14, %23
  %25 = icmp sge i32 %21, %22
  %26 = zext i1 %25 to i32
  %27 = add nuw nsw i32 %16, %26
  %28 = and i1 %15, %25
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = icmp sgt i32 %29, 2
  br i1 %30, label %31, label %._crit_edge

31:                                               ; preds = %19
  %32 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull getelementptr inbounds (i8, ptr @b, i64 8))
  %33 = load i32, ptr getelementptr inbounds (i8, ptr @b, i64 8), align 8, !tbaa !5
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = icmp eq i32 %33, %34
  %36 = or i1 %24, %35
  %37 = icmp sge i32 %33, %34
  %38 = zext i1 %37 to i32
  %39 = add nuw nsw i32 %27, %38
  %40 = icmp ugt i32 %39, 1
  %spec.select.peel47 = select i1 %40, i1 true, i1 %28
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = icmp sgt i32 %41, 3
  br i1 %42, label %.lr.ph.peel.next43, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.peel.next43, %19, %31
  %.020.lcssa = phi i32 [ %27, %19 ], [ %39, %31 ], [ %56, %.lr.ph.peel.next43 ]
  %.019.lcssa = phi i1 [ %28, %19 ], [ %spec.select.peel47, %31 ], [ %spec.select, %.lr.ph.peel.next43 ]
  %.018.lcssa = phi i1 [ %24, %19 ], [ %36, %31 ], [ %48, %.lr.ph.peel.next43 ]
  %.lcssa = phi i32 [ %29, %19 ], [ %41, %31 ], [ %58, %.lr.ph.peel.next43 ]
  br i1 %.018.lcssa, label %61, label %._crit_edge.thread

._crit_edge.thread54:                             ; preds = %10
  br i1 %14, label %.thread, label %._crit_edge.thread

.lr.ph.peel.next43:                               ; preds = %31, %.lr.ph.peel.next43
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph.peel.next43 ], [ 3, %31 ]
  %.01826 = phi i1 [ %48, %.lr.ph.peel.next43 ], [ %36, %31 ]
  %.01925 = phi i1 [ %spec.select, %.lr.ph.peel.next43 ], [ %spec.select.peel47, %31 ]
  %.02024 = phi i32 [ %56, %.lr.ph.peel.next43 ], [ %39, %31 ]
  %43 = getelementptr inbounds [100010 x i32], ptr @b, i64 0, i64 %indvars.iv
  %44 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %43)
  %45 = load i32, ptr %43, align 4, !tbaa !5
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = icmp eq i32 %45, %46
  %48 = or i1 %.01826, %47
  %49 = icmp sge i32 %45, %46
  %50 = zext i1 %49 to i32
  %51 = add nsw i32 %.02024, %50
  %52 = add nsw i64 %indvars.iv, -3
  %53 = getelementptr inbounds [100010 x i32], ptr @b, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = icmp sge i32 %54, %46
  %.neg = sext i1 %55 to i32
  %56 = add i32 %51, %.neg
  %57 = icmp sgt i32 %56, 1
  %spec.select = select i1 %57, i1 true, i1 %.01925
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %58 = load i32, ptr %2, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = icmp slt i64 %indvars.iv.next, %59
  br i1 %60, label %.lr.ph.peel.next43, label %._crit_edge, !llvm.loop !9

61:                                               ; preds = %._crit_edge
  br i1 %.019.lcssa, label %64, label %.thread

.thread:                                          ; preds = %._crit_edge.thread54, %61
  %.020.lcssa5966 = phi i32 [ %.020.lcssa, %61 ], [ %16, %._crit_edge.thread54 ]
  %.lcssa6165 = phi i32 [ %.lcssa, %61 ], [ %17, %._crit_edge.thread54 ]
  %62 = icmp eq i32 %.lcssa6165, 1
  %63 = icmp eq i32 %.020.lcssa5966, 1
  %or.cond = and i1 %63, %62
  br i1 %or.cond, label %64, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %.lr.ph33, %._crit_edge.thread54, %.thread, %._crit_edge
  br label %64

64:                                               ; preds = %61, %.thread, %._crit_edge.thread
  %.str.3.sink = phi ptr [ @.str.3, %._crit_edge.thread ], [ @.str.2, %.thread ], [ @.str.2, %61 ]
  %65 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) %.str.3.sink)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  %66 = add nuw nsw i32 %.02231, 1
  %67 = load i32, ptr %1, align 4, !tbaa !5
  %68 = icmp slt i32 %66, %67
  br i1 %68, label %.lr.ph33, label %._crit_edge34, !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, !11}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.peeled.count", i32 3}
!12 = distinct !{!12, !10}


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