; LLVM IR from tangjz/acm-icpc :: codeforces/complete (526D)

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

@n = external dso_local global i32, align 4
@kk = external dso_local global i32, align 4
@f = external dso_local global [1000010 x i32], align 16
@s = external dso_local global [1000010 x i8], align 16
@.str = external hidden unnamed_addr constant [7 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
  store i32 0, ptr %1, align 4
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n, ptr noundef @kk, ptr noundef @s)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 1, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  br label %7

7:                                                ; preds = %59, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %7
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [1000010 x i32], ptr @f, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  store i32 %16, ptr %3, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %34, %12
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %20, label %32

20:                                               ; preds = %17
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [1000010 x i8], ptr @s, i64 0, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !9
  %25 = sext i8 %24 to i32
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [1000010 x i8], ptr @s, i64 0, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !9
  %30 = sext i8 %29 to i32
  %31 = icmp ne i32 %25, %30
  br label %32

32:                                               ; preds = %20, %17
  %33 = phi i1 [ false, %17 ], [ %31, %20 ]
  br i1 %33, label %34, label %39

34:                                               ; preds = %32
  %35 = load i32, ptr %3, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1000010 x i32], ptr @f, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  store i32 %38, ptr %3, align 4, !tbaa !5
  br label %17, !llvm.loop !10

39:                                               ; preds = %32
  %40 = load i32, ptr %2, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [1000010 x i8], ptr @s, i64 0, i64 %41
  %43 = load i8, ptr %42, align 1, !tbaa !9
  %44 = sext i8 %43 to i32
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [1000010 x i8], ptr @s, i64 0, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !9
  %49 = sext i8 %48 to i32
  %50 = icmp eq i32 %44, %49
  br i1 %50, label %51, label %58

51:                                               ; preds = %39
  %52 = load i32, ptr %3, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  %54 = load i32, ptr %2, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [1000010 x i32], ptr @f, i64 0, i64 %56
  store i32 %53, ptr %57, align 4, !tbaa !5
  br label %58

58:                                               ; preds = %51, %39
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %2, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %2, align 4, !tbaa !5
  br label %7, !llvm.loop !12

62:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %63

63:                                               ; preds = %106, %62
  %64 = load i32, ptr %4, align 4, !tbaa !5
  %65 = load i32, ptr @n, align 4, !tbaa !5
  %66 = icmp slt i32 %64, %65
  br i1 %66, label %68, label %67

67:                                               ; preds = %63
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %109

68:                                               ; preds = %63
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %69 = load i32, ptr %4, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  %71 = load i32, ptr %4, align 4, !tbaa !5
  %72 = add nsw i32 %71, 1
  %73 = load i32, ptr %4, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [1000010 x i32], ptr @f, i64 0, i64 %75
  %77 = load i32, ptr %76, align 4, !tbaa !5
  %78 = sub nsw i32 %72, %77
  %79 = sdiv i32 %70, %78
  store i32 %79, ptr %5, align 4, !tbaa !5
  %80 = load i32, ptr %5, align 4, !tbaa !5
  %81 = load i32, ptr @kk, align 4, !tbaa !5
  %82 = sdiv i32 %80, %81
  %83 = load i32, ptr %4, align 4, !tbaa !5
  %84 = add nsw i32 %83, 1
  %85 = load i32, ptr %4, align 4, !tbaa !5
  %86 = add nsw i32 %85, 1
  %87 = load i32, ptr %4, align 4, !tbaa !5
  %88 = add nsw i32 %87, 1
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [1000010 x i32], ptr @f, i64 0, i64 %89
  %91 = load i32, ptr %90, align 4, !tbaa !5
  %92 = sub nsw i32 %86, %91
  %93 = srem i32 %84, %92
  %94 = icmp eq i32 %93, 0
  %95 = zext i1 %94 to i32
  %96 = add nsw i32 %82, %95
  %97 = load i32, ptr %5, align 4, !tbaa !5
  %98 = load i32, ptr @kk, align 4, !tbaa !5
  %99 = srem i32 %97, %98
  %100 = icmp sgt i32 %96, %99
  br i1 %100, label %101, label %103

101:                                              ; preds = %68
  %102 = call i32 @putchar(i32 noundef 49)
  br label %105

103:                                              ; preds = %68
  %104 = call i32 @putchar(i32 noundef 48)
  br label %105

105:                                              ; preds = %103, %101
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %106

106:                                              ; preds = %105
  %107 = load i32, ptr %4, align 4, !tbaa !5
  %108 = add nsw i32 %107, 1
  store i32 %108, ptr %4, align 4, !tbaa !5
  br label %63, !llvm.loop !13

109:                                              ; preds = %67
  ret i32 0
}

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

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

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

; Function Attrs: inlinehint mustprogress uwtable
declare i32 @putchar(i32 noundef) #3

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 = { "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 = { inlinehint 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 #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 = !{!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"

@n = external dso_local global i32, align 4
@kk = external dso_local global i32, align 4
@f = external dso_local local_unnamed_addr global [1000010 x i32], align 16
@s = external dso_local global [1000010 x i8], align 16
@.str = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n, ptr noundef nonnull @kk, ptr noundef nonnull @s)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 1
  br i1 %3, label %.lr.ph.preheader, label %.preheader

.lr.ph.preheader:                                 ; preds = %0
  %wide.trip.count = zext nneg i32 %2 to i64
  %.pre31 = load i8, ptr @s, align 16
  br label %.lr.ph

.preheader:                                       ; preds = %15, %0
  %4 = icmp sgt i32 %2, 0
  br i1 %4, label %.lr.ph26, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph.preheader, %15
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next.pre-phi, %15 ]
  %5 = getelementptr inbounds [1000010 x i8], ptr @s, i64 0, i64 %indvars.iv
  %.pre.pre = load i8, ptr %5, align 1, !tbaa !9
  br label %6

6:                                                ; preds = %7, %.lr.ph
  %.pn = phi i64 [ %indvars.iv, %.lr.ph ], [ %8, %7 ]
  %.022.in = getelementptr inbounds [1000010 x i32], ptr @f, i64 0, i64 %.pn
  %.022 = load i32, ptr %.022.in, align 4, !tbaa !5
  %.not = icmp eq i32 %.022, 0
  br i1 %.not, label %.critedge, label %7

7:                                                ; preds = %6
  %8 = sext i32 %.022 to i64
  %9 = getelementptr inbounds [1000010 x i8], ptr @s, i64 0, i64 %8
  %10 = load i8, ptr %9, align 1, !tbaa !9
  %.not23 = icmp eq i8 %.pre.pre, %10
  br i1 %.not23, label %.critedge.thread, label %6, !llvm.loop !10

.critedge:                                        ; preds = %6
  %11 = icmp eq i8 %.pre.pre, %.pre31
  br i1 %11, label %.critedge.thread, label %.critedge._crit_edge

.critedge._crit_edge:                             ; preds = %.critedge
  %.pre = add nuw nsw i64 %indvars.iv, 1
  br label %15

.critedge.thread:                                 ; preds = %7, %.critedge
  %12 = add nsw i32 %.022, 1
  %13 = add nuw nsw i64 %indvars.iv, 1
  %14 = getelementptr inbounds [1000010 x i32], ptr @f, i64 0, i64 %13
  store i32 %12, ptr %14, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %.critedge._crit_edge, %.critedge.thread
  %indvars.iv.next.pre-phi = phi i64 [ %.pre, %.critedge._crit_edge ], [ %13, %.critedge.thread ]
  %exitcond.not = icmp eq i64 %indvars.iv.next.pre-phi, %wide.trip.count
  br i1 %exitcond.not, label %.preheader, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph26, %.preheader
  ret i32 0

.lr.ph26:                                         ; preds = %.preheader, %.lr.ph26
  %indvars.iv28 = phi i64 [ %indvars.iv.next29, %.lr.ph26 ], [ 0, %.preheader ]
  %indvars.iv.next29 = add nuw nsw i64 %indvars.iv28, 1
  %16 = getelementptr inbounds [1000010 x i32], ptr @f, i64 0, i64 %indvars.iv.next29
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = trunc nuw nsw i64 %indvars.iv.next29 to i32
  %19 = sub nsw i32 %18, %17
  %20 = sdiv i32 %18, %19
  %21 = load i32, ptr @kk, align 4, !tbaa !5
  %22 = sdiv i32 %20, %21
  %23 = srem i32 %18, %19
  %24 = icmp eq i32 %23, 0
  %25 = zext i1 %24 to i32
  %26 = add nsw i32 %22, %25
  %27 = srem i32 %20, %21
  %28 = icmp sgt i32 %26, %27
  %. = select i1 %28, i32 49, i32 48
  %29 = tail call i32 @putchar(i32 noundef %.)
  %30 = load i32, ptr @n, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = icmp slt i64 %indvars.iv.next29, %31
  br i1 %32, label %.lr.ph26, label %._crit_edge, !llvm.loop !13
}

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

; Function Attrs: inlinehint mustprogress nofree nounwind uwtable
declare noundef i32 @putchar(i32 noundef) 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 = { 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 = { inlinehint mustprogress nofree 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" }

!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}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass