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

@s = external dso_local global [300030 x i8], align 16
@b = external dso_local global [300030 x i8], align 16
@c = external dso_local global [300030 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 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 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 = %134, %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
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %137

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

20:                                               ; preds = %84, %16
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %87

25:                                               ; preds = %20
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [300030 x i8], ptr @s, i64 0, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !9
  %30 = sext i8 %29 to i32
  %31 = sub nsw i32 %30, 48
  %32 = trunc i32 %31 to i8
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [300030 x i8], ptr @s, i64 0, i64 %34
  store i8 %32, ptr %35, align 1, !tbaa !9
  %36 = load i32, ptr %6, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [300030 x i8], ptr @b, i64 0, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !9
  %40 = sext i8 %39 to i32
  %41 = sub nsw i32 %40, 48
  %42 = trunc i32 %41 to i8
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [300030 x i8], ptr @b, i64 0, i64 %44
  store i8 %42, ptr %45, align 1, !tbaa !9
  %46 = load i32, ptr %6, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [300030 x i8], ptr @s, i64 0, i64 %47
  %49 = load i8, ptr %48, align 1, !tbaa !9
  %50 = sext i8 %49 to i32
  %51 = icmp eq i32 %50, 0
  br i1 %51, label %52, label %68

52:                                               ; preds = %25
  %53 = load i32, ptr %6, align 4, !tbaa !5
  %54 = icmp sgt i32 %53, 0
  br i1 %54, label %55, label %61

55:                                               ; preds = %52
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = sub nsw i32 %56, 1
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [300030 x i32], ptr @c, i64 0, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !5
  br label %62

61:                                               ; preds = %52
  br label %62

62:                                               ; preds = %61, %55
  %63 = phi i32 [ %60, %55 ], [ 0, %61 ]
  %64 = add nsw i32 %63, 1
  %65 = load i32, ptr %6, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [300030 x i32], ptr @c, i64 0, i64 %66
  store i32 %64, ptr %67, align 4, !tbaa !5
  br label %83

68:                                               ; preds = %25
  %69 = load i32, ptr %6, align 4, !tbaa !5
  %70 = icmp sgt i32 %69, 0
  br i1 %70, label %71, label %77

71:                                               ; preds = %68
  %72 = load i32, ptr %6, align 4, !tbaa !5
  %73 = sub nsw i32 %72, 1
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds [300030 x i32], ptr @c, i64 0, i64 %74
  %76 = load i32, ptr %75, align 4, !tbaa !5
  br label %78

77:                                               ; preds = %68
  br label %78

78:                                               ; preds = %77, %71
  %79 = phi i32 [ %76, %71 ], [ 0, %77 ]
  %80 = load i32, ptr %6, align 4, !tbaa !5
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds [300030 x i32], ptr @c, i64 0, i64 %81
  store i32 %79, ptr %82, align 4, !tbaa !5
  br label %83

83:                                               ; preds = %78, %62
  br label %84

84:                                               ; preds = %83
  %85 = load i32, ptr %6, align 4, !tbaa !5
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %6, align 4, !tbaa !5
  br label %20, !llvm.loop !10

87:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 1, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %88 = load i32, ptr %5, align 4, !tbaa !5
  %89 = sub nsw i32 %88, 1
  store i32 %89, ptr %9, align 4, !tbaa !5
  br label %90

90:                                               ; preds = %122, %87
  %91 = load i32, ptr %9, align 4, !tbaa !5
  %92 = icmp sge i32 %91, 0
  br i1 %92, label %94, label %93

93:                                               ; preds = %90
  store i32 8, ptr %4, align 4
  br label %125

94:                                               ; preds = %90
  %95 = load i32, ptr %9, align 4, !tbaa !5
  %96 = sext i32 %95 to i64
  %97 = getelementptr inbounds [300030 x i8], ptr @s, i64 0, i64 %96
  %98 = load i8, ptr %97, align 1, !tbaa !9
  %99 = sext i8 %98 to i32
  %100 = load i32, ptr %7, align 4, !tbaa !5
  %101 = xor i32 %99, %100
  %102 = load i32, ptr %9, align 4, !tbaa !5
  %103 = sext i32 %102 to i64
  %104 = getelementptr inbounds [300030 x i8], ptr @b, i64 0, i64 %103
  %105 = load i8, ptr %104, align 1, !tbaa !9
  %106 = sext i8 %105 to i32
  %107 = icmp eq i32 %101, %106
  br i1 %107, label %108, label %109

108:                                              ; preds = %94
  br label %122

109:                                              ; preds = %94
  %110 = load i32, ptr %9, align 4, !tbaa !5
  %111 = sext i32 %110 to i64
  %112 = getelementptr inbounds [300030 x i32], ptr @c, i64 0, i64 %111
  %113 = load i32, ptr %112, align 4, !tbaa !5
  %114 = mul nsw i32 %113, 2
  %115 = load i32, ptr %9, align 4, !tbaa !5
  %116 = add nsw i32 %115, 1
  %117 = icmp ne i32 %114, %116
  br i1 %117, label %118, label %119

118:                                              ; preds = %109
  store i32 0, ptr %8, align 4, !tbaa !5
  store i32 8, ptr %4, align 4
  br label %125

119:                                              ; preds = %109
  %120 = load i32, ptr %7, align 4, !tbaa !5
  %121 = xor i32 %120, 1
  store i32 %121, ptr %7, align 4, !tbaa !5
  br label %122

122:                                              ; preds = %119, %108
  %123 = load i32, ptr %9, align 4, !tbaa !5
  %124 = add nsw i32 %123, -1
  store i32 %124, ptr %9, align 4, !tbaa !5
  br label %90, !llvm.loop !12

125:                                              ; preds = %118, %93
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %126

126:                                              ; preds = %125
  %127 = load i32, ptr %8, align 4, !tbaa !5
  %128 = icmp ne i32 %127, 0
  br i1 %128, label %129, label %131

129:                                              ; preds = %126
  %130 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  br label %133

131:                                              ; preds = %126
  %132 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  br label %133

133:                                              ; preds = %131, %129
  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 %5) #3
  br label %134

134:                                              ; preds = %133
  %135 = load i32, ptr %3, align 4, !tbaa !5
  %136 = add nsw i32 %135, 1
  store i32 %136, ptr %3, align 4, !tbaa !5
  br label %11, !llvm.loop !13

137:                                              ; preds = %15
  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
  %138 = load i32, ptr %1, align 4
  ret i32 %138
}

; 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
!13 = distinct !{!13, !11}


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

@s = external dso_local global [300030 x i8], align 16
@b = external dso_local global [300030 x i8], align 16
@c = external dso_local local_unnamed_addr global [300030 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 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
  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.ph41, label %._crit_edge

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

.lr.ph41:                                         ; preds = %0, %.critedge
  %.02840 = phi i32 [ %44, %.critedge ], [ 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)
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @s)
  %8 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @b)
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = icmp sgt i32 %9, 0
  br i1 %10, label %.lr.ph.preheader, label %.critedge

.lr.ph.preheader:                                 ; preds = %.lr.ph41
  %wide.trip.count = zext nneg i32 %9 to i64
  %11 = load i8, ptr @s, align 16, !tbaa !9
  %12 = add i8 %11, -48
  store i8 %12, ptr @s, align 16, !tbaa !9
  %13 = load i8, ptr @b, align 16, !tbaa !9
  %14 = add i8 %13, -48
  store i8 %14, ptr @b, align 16, !tbaa !9
  %15 = icmp eq i8 %12, 0
  %spec.select = zext i1 %15 to i32
  store i32 %spec.select, ptr @c, align 16, !tbaa !5
  %exitcond.peel.not = icmp eq i32 %9, 1
  br i1 %exitcond.peel.not, label %.lr.ph39.preheader, label %.lr.ph.preheader51

.lr.ph.preheader51:                               ; preds = %.lr.ph.preheader
  %load_initial = load i32, ptr @c, align 16
  br label %.lr.ph

.preheader:                                       ; preds = %.lr.ph
  br i1 %10, label %.lr.ph39.preheader, label %.critedge

.lr.ph39.preheader:                               ; preds = %.lr.ph.preheader, %.preheader
  %16 = zext nneg i32 %9 to i64
  br label %.lr.ph39

.lr.ph:                                           ; preds = %.lr.ph.preheader51, %.lr.ph
  %store_forwarded = phi i32 [ %load_initial, %.lr.ph.preheader51 ], [ %.sink, %.lr.ph ]
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader51 ], [ %indvars.iv.next, %.lr.ph ]
  %17 = getelementptr inbounds [300030 x i8], ptr @s, i64 0, i64 %indvars.iv
  %18 = load i8, ptr %17, align 1, !tbaa !9
  %19 = add i8 %18, -48
  store i8 %19, ptr %17, align 1, !tbaa !9
  %20 = getelementptr inbounds [300030 x i8], ptr @b, i64 0, i64 %indvars.iv
  %21 = load i8, ptr %20, align 1, !tbaa !9
  %22 = add i8 %21, -48
  store i8 %22, ptr %20, align 1, !tbaa !9
  %23 = icmp eq i8 %19, 0
  %24 = zext i1 %23 to i32
  %.sink = add nsw i32 %store_forwarded, %24
  %25 = getelementptr inbounds [300030 x i32], ptr @c, i64 0, i64 %indvars.iv
  store i32 %.sink, ptr %25, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.preheader, label %.lr.ph, !llvm.loop !10

.lr.ph39:                                         ; preds = %.lr.ph39.preheader, %41
  %indvars.iv44 = phi i64 [ %16, %.lr.ph39.preheader ], [ %indvars.iv.next45, %41 ]
  %.02636 = phi i32 [ 0, %.lr.ph39.preheader ], [ %.1, %41 ]
  %indvars.iv.next45 = add nsw i64 %indvars.iv44, -1
  %26 = getelementptr inbounds [300030 x i8], ptr @s, i64 0, i64 %indvars.iv.next45
  %27 = load i8, ptr %26, align 1, !tbaa !9
  %28 = sext i8 %27 to i32
  %29 = xor i32 %.02636, %28
  %30 = getelementptr inbounds [300030 x i8], ptr @b, i64 0, i64 %indvars.iv.next45
  %31 = load i8, ptr %30, align 1, !tbaa !9
  %32 = sext i8 %31 to i32
  %33 = icmp eq i32 %29, %32
  br i1 %33, label %41, label %34

34:                                               ; preds = %.lr.ph39
  %35 = getelementptr inbounds [300030 x i32], ptr @c, i64 0, i64 %indvars.iv.next45
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = shl nsw i32 %36, 1
  %38 = zext i32 %37 to i64
  %.not = icmp eq i64 %indvars.iv44, %38
  br i1 %.not, label %39, label %.critedge

39:                                               ; preds = %34
  %40 = xor i32 %.02636, 1
  br label %41

41:                                               ; preds = %.lr.ph39, %39
  %.1 = phi i32 [ %.02636, %.lr.ph39 ], [ %40, %39 ]
  %42 = icmp ugt i64 %indvars.iv44, 1
  br i1 %42, label %.lr.ph39, label %.critedge, !llvm.loop !13

.critedge:                                        ; preds = %34, %41, %.preheader, %.lr.ph41
  %.str.3.sink = phi ptr [ @.str.2, %.lr.ph41 ], [ @.str.2, %.preheader ], [ @.str.2, %41 ], [ @.str.3, %34 ]
  %43 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) %.str.3.sink)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  %44 = add nuw nsw i32 %.02840, 1
  %45 = load i32, ptr %1, align 4, !tbaa !5
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %.lr.ph41, label %._crit_edge, !llvm.loop !14
}

; 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11, !12}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!"llvm.loop.peeled.count", i32 1}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !11}


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