; LLVM IR from tangjz/acm-icpc :: codeforces/complete (305C)

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

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

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

10:                                               ; preds = %5
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100001 x i32], ptr @a, i64 0, i64 %12
  %14 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %13)
  br label %15

15:                                               ; preds = %10
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr %2, align 4, !tbaa !5
  br label %5, !llvm.loop !9

18:                                               ; preds = %9
  store i32 -1, ptr @top, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = sub nsw i32 %19, 1
  store i32 %20, ptr %3, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %58, %18
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = icmp sge i32 %22, 0
  br i1 %23, label %25, label %24

24:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %61

25:                                               ; preds = %21
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [100001 x i32], ptr @a, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = load i32, ptr @top, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr @top, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100001 x i32], ptr @stack, i64 0, i64 %32
  store i32 %29, ptr %33, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %50, %25
  %35 = load i32, ptr @top, align 4, !tbaa !5
  %36 = icmp sgt i32 %35, 0
  br i1 %36, label %37, label %48

37:                                               ; preds = %34
  %38 = load i32, ptr @top, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [100001 x i32], ptr @stack, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = load i32, ptr @top, align 4, !tbaa !5
  %43 = sub nsw i32 %42, 1
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [100001 x i32], ptr @stack, i64 0, i64 %44
  %46 = load i32, ptr %45, align 4, !tbaa !5
  %47 = icmp eq i32 %41, %46
  br label %48

48:                                               ; preds = %37, %34
  %49 = phi i1 [ false, %34 ], [ %47, %37 ]
  br i1 %49, label %50, label %57

50:                                               ; preds = %48
  %51 = load i32, ptr @top, align 4, !tbaa !5
  %52 = add nsw i32 %51, -1
  store i32 %52, ptr @top, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [100001 x i32], ptr @stack, i64 0, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %54, align 4, !tbaa !5
  br label %34, !llvm.loop !11

57:                                               ; preds = %48
  br label %58

58:                                               ; preds = %57
  %59 = load i32, ptr %3, align 4, !tbaa !5
  %60 = add nsw i32 %59, -1
  store i32 %60, ptr %3, align 4, !tbaa !5
  br label %21, !llvm.loop !12

61:                                               ; preds = %24
  %62 = load i32, ptr @stack, align 16, !tbaa !5
  %63 = load i32, ptr @top, align 4, !tbaa !5
  %64 = sub nsw i32 %62, %63
  %65 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %64)
  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

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 = { 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
@a = external dso_local global [100001 x i32], align 16
@top = external dso_local local_unnamed_addr global i32, align 4
@stack = external dso_local local_unnamed_addr global [100001 x i32], align 16
@.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 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)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %.sink.split

._crit_edge:                                      ; preds = %.lr.ph
  store i32 -1, ptr @top, align 4, !tbaa !5
  %4 = icmp sgt i32 %8, 0
  br i1 %4, label %.lr.ph19.preheader, label %11

.lr.ph19.preheader:                               ; preds = %._crit_edge
  %5 = zext nneg i32 %8 to i64
  br label %.lr.ph19

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

.sink.split:                                      ; preds = %.critedge, %0
  %.sink = phi i32 [ -1, %0 ], [ %33, %.critedge ]
  store i32 %.sink, ptr @top, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %.sink.split, %._crit_edge
  %12 = phi i32 [ -1, %._crit_edge ], [ %.sink, %.sink.split ]
  %13 = load i32, ptr @stack, align 16, !tbaa !5
  %14 = sub nsw i32 %13, %12
  %15 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %14)
  ret i32 0

.lr.ph19:                                         ; preds = %.lr.ph19.preheader, %.critedge
  %indvars.iv29 = phi i64 [ %5, %.lr.ph19.preheader ], [ %indvars.iv.next30, %.critedge ]
  %16 = phi i32 [ -1, %.lr.ph19.preheader ], [ %33, %.critedge ]
  %indvars.iv.next30 = add nsw i64 %indvars.iv29, -1
  %17 = getelementptr inbounds [100001 x i32], ptr @a, i64 0, i64 %indvars.iv.next30
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %16, 1
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [100001 x i32], ptr @stack, i64 0, i64 %20
  store i32 %18, ptr %21, align 4, !tbaa !5
  %22 = icmp sgt i32 %16, -1
  br i1 %22, label %.lr.ph10.preheader, label %.critedge

.lr.ph10.preheader:                               ; preds = %.lr.ph19
  %.phi.trans.insert = zext nneg i32 %19 to i64
  %.phi.trans.insert32 = getelementptr inbounds [100001 x i32], ptr @stack, i64 0, i64 %.phi.trans.insert
  %.pre = load i32, ptr %.phi.trans.insert32, align 4, !tbaa !5
  br label %.lr.ph10

.lr.ph10:                                         ; preds = %.lr.ph10.preheader, %30
  %23 = phi i32 [ %31, %30 ], [ %.pre, %.lr.ph10.preheader ]
  %24 = phi i32 [ %25, %30 ], [ %19, %.lr.ph10.preheader ]
  %25 = add nsw i32 %24, -1
  %26 = zext nneg i32 %25 to i64
  %27 = getelementptr inbounds [100001 x i32], ptr @stack, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = icmp eq i32 %23, %28
  br i1 %29, label %30, label %.critedge

30:                                               ; preds = %.lr.ph10
  %31 = add nsw i32 %23, 1
  store i32 %31, ptr %27, align 4, !tbaa !5
  %32 = icmp ugt i32 %24, 1
  br i1 %32, label %.lr.ph10, label %.critedge, !llvm.loop !11

.critedge:                                        ; preds = %30, %.lr.ph10, %.lr.ph19
  %33 = phi i32 [ %19, %.lr.ph19 ], [ %24, %.lr.ph10 ], [ 0, %30 ]
  %34 = icmp sgt i64 %indvars.iv29, 1
  br i1 %34, label %.lr.ph19, label %.sink.split, !llvm.loop !12
}

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

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

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

!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: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass