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

; 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
@a = external dso_local global [10001 x i32], align 16
@f = external dso_local global [10000 x [10000 x i16]], align 16
@ans = external dso_local global i16, align 2
@.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 nounwind uwtable
declare dso_local void @_Z3updRss(ptr noundef nonnull align 2 dereferenceable(2), i16 noundef signext) #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
  %6 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %8

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

12:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  br label %21

13:                                               ; preds = %8
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds i32, ptr @a, i64 %15
  %17 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %16)
  br label %18

18:                                               ; preds = %13
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %2, align 4, !tbaa !5
  br label %8, !llvm.loop !9

21:                                               ; preds = %12
  %22 = load i32, ptr @n, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i32, ptr @a, i64 %23
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef @a, ptr noundef %24)
  %25 = load i32, ptr @n, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %26
  store i32 2147483647, ptr %27, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %123, %21
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = load i32, ptr @n, align 4, !tbaa !5
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %33, label %32

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

33:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = add nsw i32 %36, 2
  store i32 %37, ptr %6, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %119, %33
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = load i32, ptr @n, align 4, !tbaa !5
  %41 = icmp slt i32 %39, %40
  br i1 %41, label %43, label %42

42:                                               ; preds = %38
  store i32 8, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %122

43:                                               ; preds = %38
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [10000 x [10000 x i16]], ptr @f, i64 0, i64 %45
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [10000 x i16], ptr %46, i64 0, i64 %48
  call void @_Z3updRss(ptr noundef nonnull align 2 dereferenceable(2) %49, i16 noundef signext 2)
  br label %50

50:                                               ; preds = %71, %43
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = load i32, ptr %3, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = sub nsw i32 %54, %58
  %60 = load i32, ptr %6, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !5
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %65
  %67 = load i32, ptr %66, align 4, !tbaa !5
  %68 = sub nsw i32 %63, %67
  %69 = icmp sgt i32 %59, %68
  br i1 %69, label %70, label %74

70:                                               ; preds = %50
  br label %71

71:                                               ; preds = %70
  %72 = load i32, ptr %6, align 4, !tbaa !5
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %6, align 4, !tbaa !5
  br label %50, !llvm.loop !11

74:                                               ; preds = %50
  %75 = load i32, ptr %5, align 4, !tbaa !5
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %76
  %78 = load i32, ptr %77, align 4, !tbaa !5
  %79 = load i32, ptr %3, align 4, !tbaa !5
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %80
  %82 = load i32, ptr %81, align 4, !tbaa !5
  %83 = sub nsw i32 %78, %82
  %84 = load i32, ptr %6, align 4, !tbaa !5
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %85
  %87 = load i32, ptr %86, align 4, !tbaa !5
  %88 = load i32, ptr %5, align 4, !tbaa !5
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %89
  %91 = load i32, ptr %90, align 4, !tbaa !5
  %92 = sub nsw i32 %87, %91
  %93 = icmp eq i32 %83, %92
  br i1 %93, label %94, label %111

94:                                               ; preds = %74
  %95 = load i32, ptr %5, align 4, !tbaa !5
  %96 = sext i32 %95 to i64
  %97 = getelementptr inbounds [10000 x [10000 x i16]], ptr @f, i64 0, i64 %96
  %98 = load i32, ptr %6, align 4, !tbaa !5
  %99 = sext i32 %98 to i64
  %100 = getelementptr inbounds [10000 x i16], ptr %97, i64 0, i64 %99
  %101 = load i32, ptr %3, align 4, !tbaa !5
  %102 = sext i32 %101 to i64
  %103 = getelementptr inbounds [10000 x [10000 x i16]], ptr @f, i64 0, i64 %102
  %104 = load i32, ptr %5, align 4, !tbaa !5
  %105 = sext i32 %104 to i64
  %106 = getelementptr inbounds [10000 x i16], ptr %103, i64 0, i64 %105
  %107 = load i16, ptr %106, align 2, !tbaa !12
  %108 = sext i16 %107 to i32
  %109 = add nsw i32 %108, 1
  %110 = trunc i32 %109 to i16
  call void @_Z3updRss(ptr noundef nonnull align 2 dereferenceable(2) %100, i16 noundef signext %110)
  br label %111

111:                                              ; preds = %94, %74
  %112 = load i32, ptr %3, align 4, !tbaa !5
  %113 = sext i32 %112 to i64
  %114 = getelementptr inbounds [10000 x [10000 x i16]], ptr @f, i64 0, i64 %113
  %115 = load i32, ptr %5, align 4, !tbaa !5
  %116 = sext i32 %115 to i64
  %117 = getelementptr inbounds [10000 x i16], ptr %114, i64 0, i64 %116
  %118 = load i16, ptr %117, align 2, !tbaa !12
  call void @_Z3updRss(ptr noundef nonnull align 2 dereferenceable(2) @ans, i16 noundef signext %118)
  br label %119

119:                                              ; preds = %111
  %120 = load i32, ptr %5, align 4, !tbaa !5
  %121 = add nsw i32 %120, 1
  store i32 %121, ptr %5, align 4, !tbaa !5
  br label %38, !llvm.loop !14

122:                                              ; preds = %42
  br label %123

123:                                              ; preds = %122
  %124 = load i32, ptr %3, align 4, !tbaa !5
  %125 = add nsw i32 %124, 1
  store i32 %125, ptr %3, align 4, !tbaa !5
  br label %28, !llvm.loop !15

126:                                              ; preds = %32
  %127 = load i16, ptr @ans, align 2, !tbaa !12
  %128 = sext i16 %127 to i32
  %129 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %128)
  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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #3

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) #4

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 = { 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 #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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !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
@a = external dso_local global [10001 x i32], align 16
@f = external dso_local global [10000 x [10000 x i16]], align 16
@ans = external dso_local global i16, align 2
@.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 nounwind uwtable
declare dso_local void @_Z3updRss(ptr noundef nonnull align 2 dereferenceable(2), i16 noundef signext) local_unnamed_addr #0

; Function Attrs: mustprogress norecurse 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)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %.._crit_edge_crit_edge

.._crit_edge_crit_edge:                           ; preds = %0
  %.pre58 = sext i32 %2 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.._crit_edge_crit_edge
  %.pre-phi59 = phi i64 [ %.pre58, %.._crit_edge_crit_edge ], [ %12, %.lr.ph ]
  %4 = getelementptr inbounds i32, ptr @a, i64 %.pre-phi59
  tail call void @_ZSt4sortIPiEvT_S1_(ptr noundef nonnull @a, ptr noundef nonnull %4)
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %6
  store i32 2147483647, ptr %7, align 4, !tbaa !5
  %8 = icmp sgt i32 %5, 0
  br i1 %8, label %.lr.ph42, label %._crit_edge43

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %9 = getelementptr inbounds i32, ptr @a, i64 %indvars.iv
  %10 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %9)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %11 = load i32, ptr @n, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = icmp slt i64 %indvars.iv.next, %12
  br i1 %13, label %.lr.ph, label %._crit_edge, !llvm.loop !9

.loopexit.loopexit:                               ; preds = %42
  %.pre = sext i32 %44 to i64
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %.lr.ph42
  %.pre-phi = phi i64 [ %.pre, %.loopexit.loopexit ], [ %20, %.lr.ph42 ]
  %14 = phi i32 [ %44, %.loopexit.loopexit ], [ %19, %.lr.ph42 ]
  %15 = icmp slt i64 %indvars.iv.next56, %.pre-phi
  %indvars.iv.next51 = add nuw nsw i64 %indvars.iv50, 1
  br i1 %15, label %.lr.ph42, label %._crit_edge43, !llvm.loop !11

._crit_edge43:                                    ; preds = %.loopexit, %._crit_edge
  %16 = load i16, ptr @ans, align 2, !tbaa !12
  %17 = sext i16 %16 to i32
  %18 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %17)
  ret i32 0

.lr.ph42:                                         ; preds = %._crit_edge, %.loopexit
  %19 = phi i32 [ %14, %.loopexit ], [ %5, %._crit_edge ]
  %indvars.iv55 = phi i64 [ %indvars.iv.next56, %.loopexit ], [ 0, %._crit_edge ]
  %indvars.iv50 = phi i64 [ %indvars.iv.next51, %.loopexit ], [ 1, %._crit_edge ]
  %indvars.iv.next56 = add nuw nsw i64 %indvars.iv55, 1
  %20 = sext i32 %19 to i64
  %21 = icmp slt i64 %indvars.iv.next56, %20
  br i1 %21, label %.lr.ph39, label %.loopexit

.lr.ph39:                                         ; preds = %.lr.ph42
  %22 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %indvars.iv55
  %23 = add nuw nsw i64 %indvars.iv55, 2
  br label %24

24:                                               ; preds = %.lr.ph39, %42
  %indvars.iv52 = phi i64 [ %indvars.iv50, %.lr.ph39 ], [ %indvars.iv.next53, %42 ]
  %.037 = phi i64 [ %23, %.lr.ph39 ], [ %indvars.iv47, %42 ]
  %25 = getelementptr inbounds [10000 x [10000 x i16]], ptr @f, i64 0, i64 %indvars.iv55, i64 %indvars.iv52
  tail call void @_Z3updRss(ptr noundef nonnull align 2 dereferenceable(2) %25, i16 noundef signext 2)
  %26 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %indvars.iv52
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = load i32, ptr %22, align 4, !tbaa !5
  %29 = sub nsw i32 %27, %28
  %sext = shl i64 %.037, 32
  %30 = ashr exact i64 %sext, 32
  br label %31

31:                                               ; preds = %31, %24
  %indvars.iv47 = phi i64 [ %indvars.iv.next48, %31 ], [ %30, %24 ]
  %32 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %indvars.iv47
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = sub nsw i32 %33, %27
  %35 = icmp sgt i32 %29, %34
  %indvars.iv.next48 = add nsw i64 %indvars.iv47, 1
  br i1 %35, label %31, label %36, !llvm.loop !14

36:                                               ; preds = %31
  %37 = icmp eq i32 %29, %34
  br i1 %37, label %38, label %42

38:                                               ; preds = %36
  %39 = getelementptr inbounds [10000 x [10000 x i16]], ptr @f, i64 0, i64 %indvars.iv52, i64 %indvars.iv47
  %40 = load i16, ptr %25, align 2, !tbaa !12
  %41 = add i16 %40, 1
  tail call void @_Z3updRss(ptr noundef nonnull align 2 dereferenceable(2) %39, i16 noundef signext %41)
  br label %42

42:                                               ; preds = %38, %36
  %43 = load i16, ptr %25, align 2, !tbaa !12
  tail call void @_Z3updRss(ptr noundef nonnull align 2 dereferenceable(2) @ans, i16 noundef signext %43)
  %indvars.iv.next53 = add nuw nsw i64 %indvars.iv52, 1
  %44 = load i32, ptr @n, align 4, !tbaa !5
  %45 = trunc nuw i64 %indvars.iv.next53 to i32
  %46 = icmp sgt i32 %44, %45
  br i1 %46, label %24, label %.loopexit.loopexit, !llvm.loop !15
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) local_unnamed_addr #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 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 = { 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" }

!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 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}


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