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

; 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
@pos = external dso_local global [27 x i32], align 16
@f = external dso_local global [5001 x [5001 x i32]], align 16
@str = external dso_local global [5001 x i8], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z7mod_addRii(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z7mod_subRii(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) #0

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

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

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

12:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = sub nsw i32 %13, 1
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [5001 x i8], ptr @str, i64 0, i64 %15
  %17 = load i8, ptr %16, align 1, !tbaa !9
  %18 = sext i8 %17 to i64
  %19 = getelementptr inbounds [27 x i32], ptr @pos, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  store i32 %20, ptr %4, align 4, !tbaa !5
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = sub nsw i32 %22, 1
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [5001 x i8], ptr @str, i64 0, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %27 = sext i8 %26 to i64
  %28 = getelementptr inbounds [27 x i32], ptr @pos, i64 0, i64 %27
  store i32 %21, ptr %28, align 4, !tbaa !5
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %30
  %32 = getelementptr inbounds [5001 x i32], ptr %31, i64 0, i64 0
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %34 = sub nsw i32 %33, 1
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %35
  %37 = getelementptr inbounds [5001 x i32], ptr %36, i64 0, i64 0
  %38 = load i32, ptr @n, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  %40 = sext i32 %39 to i64
  %41 = mul i64 %40, 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %32, ptr align 4 %37, i64 %41, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %42

42:                                               ; preds = %80, %12
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = load i32, ptr @n, align 4, !tbaa !5
  %45 = icmp sle i32 %43, %44
  br i1 %45, label %47, label %46

46:                                               ; preds = %42
  store i32 5, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %83

47:                                               ; preds = %42
  %48 = load i32, ptr %2, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %49
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [5001 x i32], ptr %50, i64 0, i64 %52
  %54 = load i32, ptr %2, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %55
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = sub nsw i32 %57, 1
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [5001 x i32], ptr %56, i64 0, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !5
  call void @_Z7mod_addRii(ptr noundef nonnull align 4 dereferenceable(4) %53, i32 noundef %61)
  %62 = load i32, ptr %4, align 4, !tbaa !5
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %79

64:                                               ; preds = %47
  %65 = load i32, ptr %2, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %66
  %68 = load i32, ptr %5, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [5001 x i32], ptr %67, i64 0, i64 %69
  %71 = load i32, ptr %4, align 4, !tbaa !5
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %72
  %74 = load i32, ptr %5, align 4, !tbaa !5
  %75 = sub nsw i32 %74, 1
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [5001 x i32], ptr %73, i64 0, i64 %76
  %78 = load i32, ptr %77, align 4, !tbaa !5
  call void @_Z7mod_subRii(ptr noundef nonnull align 4 dereferenceable(4) %70, i32 noundef %78)
  br label %79

79:                                               ; preds = %64, %47
  br label %80

80:                                               ; preds = %79
  %81 = load i32, ptr %5, align 4, !tbaa !5
  %82 = add nsw i32 %81, 1
  store i32 %82, ptr %5, align 4, !tbaa !5
  br label %42, !llvm.loop !10

83:                                               ; preds = %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %84

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

87:                                               ; preds = %11
  %88 = load i32, ptr @n, align 4, !tbaa !5
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %89
  %91 = load i32, ptr @n, align 4, !tbaa !5
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds [5001 x i32], ptr %90, i64 0, i64 %92
  %94 = load i32, ptr %93, align 4, !tbaa !5
  %95 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %94)
  ret i32 0
}

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

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

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

attributes #0 = { mustprogress 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 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 #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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}


; 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
@pos = external dso_local local_unnamed_addr global [27 x i32], align 16
@f = external dso_local global [5001 x [5001 x i32]], align 16
@str = external dso_local global [5001 x i8], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z7mod_addRii(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z7mod_subRii(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n, ptr noundef nonnull @str)
  store i32 1, ptr @f, align 16, !tbaa !5
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %.not25 = icmp slt i32 %2, 1
  br i1 %.not25, label %._crit_edge29, label %.lr.ph28.split

._crit_edge29:                                    ; preds = %._crit_edge, %0
  %.lcssa = phi i32 [ %2, %0 ], [ %27, %._crit_edge ]
  %3 = sext i32 %.lcssa to i64
  %4 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %3, i64 %3
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %5)
  ret i32 0

.lr.ph28.split:                                   ; preds = %0, %._crit_edge
  %7 = phi i32 [ %27, %._crit_edge ], [ %2, %0 ]
  %indvars.iv37 = phi i64 [ %indvars.iv.next38, %._crit_edge ], [ 1, %0 ]
  %8 = add nsw i64 %indvars.iv37, -1
  %9 = getelementptr inbounds [5001 x i8], ptr @str, i64 0, i64 %8
  %10 = load i8, ptr %9, align 1, !tbaa !9
  %11 = sext i8 %10 to i64
  %12 = getelementptr inbounds [27 x i32], ptr @pos, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = trunc nuw nsw i64 %indvars.iv37 to i32
  store i32 %14, ptr %12, align 4, !tbaa !5
  %15 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %indvars.iv37
  %16 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %8
  %17 = add nsw i32 %7, 1
  %18 = sext i32 %17 to i64
  %19 = shl nsw i64 %18, 2
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 4 %15, ptr nonnull align 4 %16, i64 %19, i1 false)
  %.not2123 = icmp slt i32 %7, 1
  br i1 %.not2123, label %.lr.ph28.split.._crit_edge_crit_edge, label %.lr.ph

.lr.ph28.split.._crit_edge_crit_edge:             ; preds = %.lr.ph28.split
  %.pre = sext i32 %7 to i64
  br label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph28.split
  %.fr = freeze i32 %13
  %.not22 = icmp eq i32 %.fr, 0
  %20 = sext i32 %.fr to i64
  br i1 %.not22, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %.lr.ph.split.us
  %indvars.iv34 = phi i64 [ %indvars.iv.next35, %.lr.ph.split.us ], [ 1, %.lr.ph ]
  %21 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %indvars.iv37, i64 %indvars.iv34
  %22 = add nsw i64 %indvars.iv34, -1
  %23 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %indvars.iv37, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  tail call void @_Z7mod_addRii(ptr noundef nonnull align 4 dereferenceable(4) %21, i32 noundef %24)
  %indvars.iv.next35 = add nuw nsw i64 %indvars.iv34, 1
  %25 = load i32, ptr @n, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %.not21.us.not = icmp slt i64 %indvars.iv34, %26
  br i1 %.not21.us.not, label %.lr.ph.split.us, label %._crit_edge, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph.split, %.lr.ph.split.us, %.lr.ph28.split.._crit_edge_crit_edge
  %.pre-phi = phi i64 [ %.pre, %.lr.ph28.split.._crit_edge_crit_edge ], [ %26, %.lr.ph.split.us ], [ %35, %.lr.ph.split ]
  %27 = phi i32 [ %7, %.lr.ph28.split.._crit_edge_crit_edge ], [ %25, %.lr.ph.split.us ], [ %34, %.lr.ph.split ]
  %indvars.iv.next38 = add nuw nsw i64 %indvars.iv37, 1
  %.not.not = icmp slt i64 %indvars.iv37, %.pre-phi
  br i1 %.not.not, label %.lr.ph28.split, label %._crit_edge29, !llvm.loop !12

.lr.ph.split:                                     ; preds = %.lr.ph, %.lr.ph.split
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph.split ], [ 1, %.lr.ph ]
  %28 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %indvars.iv37, i64 %indvars.iv
  %29 = add nsw i64 %indvars.iv, -1
  %30 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %indvars.iv37, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  tail call void @_Z7mod_addRii(ptr noundef nonnull align 4 dereferenceable(4) %28, i32 noundef %31)
  %32 = getelementptr inbounds [5001 x [5001 x i32]], ptr @f, i64 0, i64 %20, i64 %29
  %33 = load i32, ptr %32, align 4, !tbaa !5
  tail call void @_Z7mod_subRii(ptr noundef nonnull align 4 dereferenceable(4) %28, i32 noundef %33)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %34 = load i32, ptr @n, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %.not21.not = icmp slt i64 %indvars.iv, %35
  br i1 %.not21.not, label %.lr.ph.split, label %._crit_edge, !llvm.loop !10
}

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

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

!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}
!13 = !{!"llvm.loop.unswitch.partial.disable"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopFullUnrollPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass,TailCallElimPass