; LLVM IR from tangjz/acm-icpc :: 51nod (1002)

; 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
@f = external dso_local global [2 x [501 x i32]], align 16
@cur = external dso_local global i32, align 4
@nxt = external dso_local global i32, align 4
@ans = external dso_local global i32, align 4
@.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
  store i32 0, ptr %1, align 4
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %54, %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) #4
  br label %61

12:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %50, %12
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = icmp sle i32 %14, %15
  br i1 %16, label %18, label %17

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

18:                                               ; preds = %13
  %19 = load i32, ptr @nxt, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %20
  %22 = getelementptr inbounds [501 x i32], ptr %21, i64 0, i64 0
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i32, ptr %22, i64 %24
  %26 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %25)
  %27 = load i32, ptr @cur, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %28
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = sub nsw i32 %30, 1
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [501 x i32], ptr %29, i64 0, i64 %32
  %34 = load i32, ptr @cur, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %35
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [501 x i32], ptr %36, i64 0, i64 %38
  %40 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %33, ptr noundef nonnull align 4 dereferenceable(4) %39)
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = load i32, ptr @nxt, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %43
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [501 x i32], ptr %44, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = add nsw i32 %48, %41
  store i32 %49, ptr %47, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %18
  %51 = load i32, ptr %4, align 4, !tbaa !5
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %4, align 4, !tbaa !5
  br label %13, !llvm.loop !9

53:                                               ; preds = %17
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %2, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %2, align 4, !tbaa !5
  %57 = load i32, ptr @cur, align 4, !tbaa !5
  %58 = xor i32 %57, 1
  store i32 %58, ptr @cur, align 4, !tbaa !5
  %59 = load i32, ptr @nxt, align 4, !tbaa !5
  %60 = xor i32 %59, 1
  store i32 %60, ptr @nxt, align 4, !tbaa !5
  br label %7, !llvm.loop !11

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

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

66:                                               ; preds = %62
  store i32 8, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %89

67:                                               ; preds = %62
  %68 = load i32, ptr @ans, align 4, !tbaa !5
  %69 = load i32, ptr @cur, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %70
  %72 = load i32, ptr %5, align 4, !tbaa !5
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds [501 x i32], ptr %71, i64 0, i64 %73
  %75 = load i32, ptr %74, align 4, !tbaa !5
  %76 = icmp slt i32 %68, %75
  br i1 %76, label %77, label %85

77:                                               ; preds = %67
  %78 = load i32, ptr @cur, align 4, !tbaa !5
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %79
  %81 = load i32, ptr %5, align 4, !tbaa !5
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds [501 x i32], ptr %80, i64 0, i64 %82
  %84 = load i32, ptr %83, align 4, !tbaa !5
  store i32 %84, ptr @ans, align 4, !tbaa !5
  br label %85

85:                                               ; preds = %77, %67
  br label %86

86:                                               ; preds = %85
  %87 = load i32, ptr %5, align 4, !tbaa !5
  %88 = add nsw i32 %87, 1
  store i32 %88, ptr %5, align 4, !tbaa !5
  br label %62, !llvm.loop !12

89:                                               ; preds = %66
  %90 = load i32, ptr @ans, align 4, !tbaa !5
  %91 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %90)
  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: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #3

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

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

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 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 #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 = 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"

@n = external dso_local global i32, align 4
@f = external dso_local global [2 x [501 x i32]], align 16
@cur = external dso_local local_unnamed_addr global i32, align 4
@nxt = external dso_local local_unnamed_addr global i32, align 4
@ans = external dso_local local_unnamed_addr global i32, align 4
@.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 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)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %.not20 = icmp slt i32 %2, 1
  br i1 %.not20, label %.preheader.._crit_edge_crit_edge, label %.preheader18.preheader

.preheader18.preheader:                           ; preds = %0
  %.pre.pre = load i32, ptr @nxt, align 4, !tbaa !5
  br label %.preheader18

.preheader18:                                     ; preds = %.preheader18.preheader, %7
  %.pre = phi i32 [ %.pre.pre, %.preheader18.preheader ], [ %11, %7 ]
  %indvars.iv28 = phi i64 [ 2, %.preheader18.preheader ], [ %indvars.iv.next29, %7 ]
  %.01421 = phi i32 [ 1, %.preheader18.preheader ], [ %8, %7 ]
  br label %13

.preheader:                                       ; preds = %7
  %.not1622 = icmp slt i32 %12, 1
  br i1 %.not1622, label %.preheader.._crit_edge_crit_edge, label %.lr.ph

.preheader.._crit_edge_crit_edge:                 ; preds = %0, %.preheader
  %.pre35 = load i32, ptr @ans, align 4, !tbaa !5
  br label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %ans.promoted = load i32, ptr @ans, align 4, !tbaa !5
  %3 = load i32, ptr @cur, align 4, !tbaa !5
  %4 = sext i32 %3 to i64
  %5 = zext nneg i32 %12 to i64
  %xtraiter = and i64 %5, 1
  %6 = icmp eq i32 %12, 1
  br i1 %6, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %5, 2147483646
  br label %37

7:                                                ; preds = %13
  %8 = add nuw nsw i32 %.01421, 1
  %9 = load i32, ptr @cur, align 4, !tbaa !5
  %10 = xor i32 %9, 1
  store i32 %10, ptr @cur, align 4, !tbaa !5
  %11 = xor i32 %26, 1
  store i32 %11, ptr @nxt, align 4, !tbaa !5
  %12 = load i32, ptr @n, align 4, !tbaa !5
  %.not.not = icmp slt i32 %.01421, %12
  %indvars.iv.next29 = add nuw nsw i64 %indvars.iv28, 1
  br i1 %.not.not, label %.preheader18, label %.preheader, !llvm.loop !9

13:                                               ; preds = %.preheader18, %13
  %14 = phi i32 [ %.pre, %.preheader18 ], [ %26, %13 ]
  %indvars.iv = phi i64 [ 1, %.preheader18 ], [ %indvars.iv.next, %13 ]
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %15
  %17 = getelementptr inbounds i32, ptr %16, i64 %indvars.iv
  %18 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %17)
  %19 = load i32, ptr @cur, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = add nsw i64 %indvars.iv, -1
  %22 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %20, i64 %21
  %23 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %20, i64 %indvars.iv
  %24 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %22, ptr noundef nonnull align 4 dereferenceable(4) %23)
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = load i32, ptr @nxt, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %27, i64 %indvars.iv
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = add nsw i32 %29, %25
  store i32 %30, ptr %28, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %indvars.iv28
  br i1 %exitcond.not, label %7, label %13, !llvm.loop !11

._crit_edge.loopexit.unr-lcssa:                   ; preds = %49, %.lr.ph
  %.lcssa.ph = phi i32 [ poison, %.lr.ph ], [ %50, %49 ]
  %indvars.iv31.unr = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next32.1, %49 ]
  %.unr = phi i32 [ %ans.promoted, %.lr.ph ], [ %50, %49 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.loopexit.unr-lcssa
  %31 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %4, i64 %indvars.iv31.unr
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = icmp slt i32 %.unr, %32
  br i1 %33, label %34, label %._crit_edge

34:                                               ; preds = %.epil.preheader
  store i32 %32, ptr @ans, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %34, %.epil.preheader, %.preheader.._crit_edge_crit_edge
  %35 = phi i32 [ %.pre35, %.preheader.._crit_edge_crit_edge ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.unr, %.epil.preheader ], [ %32, %34 ]
  %36 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %35)
  ret i32 0

37:                                               ; preds = %49, %.lr.ph.new
  %indvars.iv31 = phi i64 [ 1, %.lr.ph.new ], [ %indvars.iv.next32.1, %49 ]
  %38 = phi i32 [ %ans.promoted, %.lr.ph.new ], [ %50, %49 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %49 ]
  %39 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %4, i64 %indvars.iv31
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = icmp slt i32 %38, %40
  br i1 %41, label %42, label %43

42:                                               ; preds = %37
  store i32 %40, ptr @ans, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %37, %42
  %44 = phi i32 [ %38, %37 ], [ %40, %42 ]
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %45 = getelementptr inbounds [2 x [501 x i32]], ptr @f, i64 0, i64 %4, i64 %indvars.iv.next32
  %46 = load i32, ptr %45, align 4, !tbaa !5
  %47 = icmp slt i32 %44, %46
  br i1 %47, label %48, label %49

48:                                               ; preds = %43
  store i32 %46, ptr @ans, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %48, %43
  %50 = phi i32 [ %44, %43 ], [ %46, %48 ]
  %indvars.iv.next32.1 = add nuw nsw i64 %indvars.iv31, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %37, !llvm.loop !12
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

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

attributes #0 = { 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 #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 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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