; LLVM IR from tangjz/acm-icpc :: usaco/dec14-bronze (C)

; 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
@v = external dso_local global [100010 x i32], align 16
@now = external dso_local global i32, align 4
@ans = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [10 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.6 = 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
  store i32 0, ptr %1, align 4
  %4 = load ptr, ptr @stdin, align 8, !tbaa !5
  %5 = call ptr @freopen(ptr noundef @.str, ptr noundef @.str.1, ptr noundef %4)
  %6 = load ptr, ptr @stdout, align 8, !tbaa !5
  %7 = call ptr @freopen(ptr noundef @.str.2, ptr noundef @.str.3, ptr noundef %6)
  %8 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.4, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !9
  br label %9

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

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

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

19:                                               ; preds = %14
  %20 = load i32, ptr %2, align 4, !tbaa !9
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %2, align 4, !tbaa !9
  br label %9, !llvm.loop !11

22:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %23 = load i32, ptr @n, align 4, !tbaa !9
  %24 = sub nsw i32 %23, 1
  store i32 %24, ptr %3, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %44, %22
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = icmp sge i32 %26, 0
  br i1 %27, label %29, label %28

28:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %47

29:                                               ; preds = %25
  %30 = load i32, ptr %3, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [100010 x i32], ptr @v, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !9
  %34 = load i32, ptr @now, align 4, !tbaa !9
  %35 = icmp sle i32 %33, %34
  br i1 %35, label %36, label %43

36:                                               ; preds = %29
  %37 = load i32, ptr @ans, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr @ans, align 4, !tbaa !9
  %39 = load i32, ptr %3, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [100010 x i32], ptr @v, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !9
  store i32 %42, ptr @now, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %36, %29
  br label %44

44:                                               ; preds = %43
  %45 = load i32, ptr %3, align 4, !tbaa !9
  %46 = add nsw i32 %45, -1
  store i32 %46, ptr %3, align 4, !tbaa !9
  br label %25, !llvm.loop !13

47:                                               ; preds = %28
  %48 = load i32, ptr @ans, align 4, !tbaa !9
  %49 = call i32 (ptr, ...) @printf(ptr noundef @.str.6, i32 noundef %48)
  %50 = load ptr, ptr @stdin, align 8, !tbaa !5
  %51 = call i32 @fclose(ptr noundef %50)
  %52 = load ptr, ptr @stdout, align 8, !tbaa !5
  %53 = call i32 @fclose(ptr noundef %52)
  ret i32 0
}

declare ptr @freopen(ptr noundef, ptr noundef, ptr noundef) #1

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

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

declare i32 @fclose(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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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
@v = external dso_local global [100010 x i32], align 16
@now = 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 [10 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external local_unnamed_addr global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = load ptr, ptr @stdin, align 8, !tbaa !5
  %2 = tail call ptr @freopen(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, ptr noundef %1)
  %3 = load ptr, ptr @stdout, align 8, !tbaa !5
  %4 = tail call ptr @freopen(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.3, ptr noundef %3)
  %5 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.4, ptr noundef nonnull @n)
  %6 = load i32, ptr @n, align 4, !tbaa !9
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %.preheader.thread

.preheader.thread:                                ; preds = %0
  %ans.promoted19 = load i32, ptr @ans, align 4, !tbaa !9
  br label %._crit_edge

.preheader:                                       ; preds = %.lr.ph
  %ans.promoted = load i32, ptr @ans, align 4, !tbaa !9
  %8 = icmp sgt i32 %17, 0
  br i1 %8, label %.lr.ph12.preheader, label %._crit_edge

.lr.ph12.preheader:                               ; preds = %.preheader
  %now.promoted = load i32, ptr @now, align 4, !tbaa !9
  %9 = zext nneg i32 %17 to i64
  %xtraiter = and i64 %9, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph12.prol.loopexit, label %.lr.ph12.prol

.lr.ph12.prol:                                    ; preds = %.lr.ph12.preheader
  %indvars.iv.next16.prol = add nsw i64 %9, -1
  %10 = getelementptr inbounds [100010 x i32], ptr @v, i64 0, i64 %indvars.iv.next16.prol
  %11 = load i32, ptr %10, align 4, !tbaa !9
  %.not.prol = icmp sgt i32 %11, %now.promoted
  br i1 %.not.prol, label %.lr.ph12.prol.loopexit, label %12

12:                                               ; preds = %.lr.ph12.prol
  %13 = add nsw i32 %ans.promoted, 1
  store i32 %13, ptr @ans, align 4, !tbaa !9
  store i32 %11, ptr @now, align 4, !tbaa !9
  br label %.lr.ph12.prol.loopexit

.lr.ph12.prol.loopexit:                           ; preds = %.lr.ph12.prol, %12, %.lr.ph12.preheader
  %indvars.iv15.unr = phi i64 [ %9, %.lr.ph12.preheader ], [ %indvars.iv.next16.prol, %12 ], [ %indvars.iv.next16.prol, %.lr.ph12.prol ]
  %.unr = phi i32 [ %now.promoted, %.lr.ph12.preheader ], [ %now.promoted, %.lr.ph12.prol ], [ %11, %12 ]
  %.unr23 = phi i32 [ %ans.promoted, %.lr.ph12.preheader ], [ %ans.promoted, %.lr.ph12.prol ], [ %13, %12 ]
  %.lcssa.unr = phi i32 [ poison, %.lr.ph12.preheader ], [ %ans.promoted, %.lr.ph12.prol ], [ %13, %12 ]
  %14 = icmp eq i32 %17, 1
  br i1 %14, label %._crit_edge, label %.lr.ph12

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

._crit_edge:                                      ; preds = %.lr.ph12.prol.loopexit, %38, %.preheader.thread, %.preheader
  %20 = phi i32 [ %ans.promoted, %.preheader ], [ %ans.promoted19, %.preheader.thread ], [ %.lcssa.unr, %.lr.ph12.prol.loopexit ], [ %39, %38 ]
  %21 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6, i32 noundef %20)
  %22 = load ptr, ptr @stdin, align 8, !tbaa !5
  %23 = tail call i32 @fclose(ptr noundef %22)
  %24 = load ptr, ptr @stdout, align 8, !tbaa !5
  %25 = tail call i32 @fclose(ptr noundef %24)
  ret i32 0

.lr.ph12:                                         ; preds = %.lr.ph12.prol.loopexit, %38
  %indvars.iv15 = phi i64 [ %indvars.iv.next16.1, %38 ], [ %indvars.iv15.unr, %.lr.ph12.prol.loopexit ]
  %26 = phi i32 [ %40, %38 ], [ %.unr, %.lr.ph12.prol.loopexit ]
  %27 = phi i32 [ %39, %38 ], [ %.unr23, %.lr.ph12.prol.loopexit ]
  %indvars.iv.next16 = add nsw i64 %indvars.iv15, -1
  %28 = getelementptr inbounds [100010 x i32], ptr @v, i64 0, i64 %indvars.iv.next16
  %29 = load i32, ptr %28, align 4, !tbaa !9
  %.not = icmp sgt i32 %29, %26
  br i1 %.not, label %.lr.ph12.1, label %30

30:                                               ; preds = %.lr.ph12
  %31 = add nsw i32 %27, 1
  store i32 %31, ptr @ans, align 4, !tbaa !9
  store i32 %29, ptr @now, align 4, !tbaa !9
  br label %.lr.ph12.1

.lr.ph12.1:                                       ; preds = %.lr.ph12, %30
  %32 = phi i32 [ %27, %.lr.ph12 ], [ %31, %30 ]
  %33 = phi i32 [ %26, %.lr.ph12 ], [ %29, %30 ]
  %indvars.iv.next16.1 = add nsw i64 %indvars.iv15, -2
  %34 = getelementptr inbounds [100010 x i32], ptr @v, i64 0, i64 %indvars.iv.next16.1
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %.not.1 = icmp sgt i32 %35, %33
  br i1 %.not.1, label %38, label %36

36:                                               ; preds = %.lr.ph12.1
  %37 = add nsw i32 %32, 1
  store i32 %37, ptr @ans, align 4, !tbaa !9
  store i32 %35, ptr @now, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %36, %.lr.ph12.1
  %39 = phi i32 [ %32, %.lr.ph12.1 ], [ %37, %36 ]
  %40 = phi i32 [ %33, %.lr.ph12.1 ], [ %35, %36 ]
  %41 = icmp ugt i64 %indvars.iv.next16, 1
  br i1 %41, label %.lr.ph12, label %._crit_edge, !llvm.loop !13
}

declare ptr @freopen(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

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

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

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #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 = { "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" }

!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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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