; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/VK Cup 2015 Finals/Problem F (F)

; 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 [1001000 x i32], align 16
@x = external dso_local global i32, align 4
@up = 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
  %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) #4
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %20, %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) #4
  br label %23

13:                                               ; preds = %8
  %14 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @x)
  %15 = load i32, ptr @x, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [1001000 x i32], ptr @a, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr %17, align 4, !tbaa !5
  br label %20

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

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

24:                                               ; preds = %66, %23
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = icmp sge i32 %25, 1
  br i1 %26, label %28, label %27

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

28:                                               ; preds = %24
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [1001000 x i32], ptr @a, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = icmp sgt i32 %32, 0
  br i1 %33, label %34, label %65

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

38:                                               ; preds = %49, %34
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = load i32, ptr @up, align 4, !tbaa !5
  %41 = icmp sle 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) #4
  br label %53

43:                                               ; preds = %38
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [1001000 x i32], ptr @a, i64 0, i64 %45
  %47 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %5, ptr noundef nonnull align 4 dereferenceable(4) %46)
  %48 = load i32, ptr %47, align 4, !tbaa !5
  store i32 %48, ptr %5, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %43
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = add nsw i32 %51, %50
  store i32 %52, ptr %6, align 4, !tbaa !5
  br label %38, !llvm.loop !11

53:                                               ; preds = %42
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = load i32, ptr %3, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [1001000 x i32], ptr @a, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = add nsw i32 %58, %54
  store i32 %59, ptr %57, align 4, !tbaa !5
  %60 = load i32, ptr %3, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [1001000 x i32], ptr @a, i64 0, i64 %61
  %63 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) @ans, ptr noundef nonnull align 4 dereferenceable(4) %62)
  %64 = load i32, ptr %63, align 4, !tbaa !5
  store i32 %64, ptr @ans, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %65

65:                                               ; preds = %53, %28
  br label %66

66:                                               ; preds = %65
  %67 = load i32, ptr %3, align 4, !tbaa !5
  %68 = add nsw i32 %67, -1
  store i32 %68, ptr %3, align 4, !tbaa !5
  br label %24, !llvm.loop !12

69:                                               ; preds = %27
  %70 = load i32, ptr @ans, align 4, !tbaa !5
  %71 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %70)
  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

; 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

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
@a = external dso_local global [1001000 x i32], align 16
@x = external dso_local global i32, align 4
@up = external dso_local local_unnamed_addr 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 nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %.preheader.preheader

.preheader.preheader:                             ; preds = %.lr.ph, %0
  br label %.preheader

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

14:                                               ; preds = %34
  %15 = load i32, ptr @ans, align 4, !tbaa !5
  %16 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %15)
  ret i32 0

.preheader:                                       ; preds = %.preheader.preheader, %34
  %indvars.iv21 = phi i64 [ %indvars.iv.next22, %34 ], [ 1000000, %.preheader.preheader ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %34 ], [ 2000000, %.preheader.preheader ]
  %17 = getelementptr inbounds [1001000 x i32], ptr @a, i64 0, i64 %indvars.iv21
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = icmp sgt i32 %18, 0
  br i1 %19, label %20, label %34

20:                                               ; preds = %.preheader
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #4
  store i32 0, ptr %1, align 4, !tbaa !5
  %21 = shl nuw nsw i64 %indvars.iv21, 1
  %22 = load i32, ptr @up, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %.not16 = icmp sgt i64 %21, %23
  br i1 %.not16, label %._crit_edge, label %.lr.ph18

._crit_edge.loopexit:                             ; preds = %.lr.ph18
  %.pre = load i32, ptr %17, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %20
  %24 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ %18, %20 ]
  %25 = phi i32 [ %31, %._crit_edge.loopexit ], [ 0, %20 ]
  %26 = add nsw i32 %24, %25
  store i32 %26, ptr %17, align 4, !tbaa !5
  %27 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) @ans, ptr noundef nonnull align 4 dereferenceable(4) %17)
  %28 = load i32, ptr %27, align 4, !tbaa !5
  store i32 %28, ptr @ans, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #4
  br label %34

.lr.ph18:                                         ; preds = %20, %.lr.ph18
  %indvars.iv23 = phi i64 [ %indvars.iv.next24, %.lr.ph18 ], [ %indvars.iv, %20 ]
  %29 = getelementptr inbounds [1001000 x i32], ptr @a, i64 0, i64 %indvars.iv23
  %30 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %1, ptr noundef nonnull align 4 dereferenceable(4) %29)
  %31 = load i32, ptr %30, align 4, !tbaa !5
  store i32 %31, ptr %1, align 4, !tbaa !5
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, %indvars.iv21
  %32 = load i32, ptr @up, align 4, !tbaa !5
  %33 = trunc nuw i64 %indvars.iv.next24 to i32
  %.not = icmp slt i32 %32, %33
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph18, !llvm.loop !11

34:                                               ; preds = %.preheader, %._crit_edge
  %indvars.iv.next22 = add nsw i64 %indvars.iv21, -1
  %35 = icmp ugt i64 %indvars.iv21, 1
  %indvars.iv.next = add nsw i64 %indvars.iv, -2
  br i1 %35, label %.preheader, label %14, !llvm.loop !12
}

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

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.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)) local_unnamed_addr #3

; 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 = { mustprogress 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}


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