; LLVM IR from tangjz/acm-icpc :: lydsy (1113)

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

5:                                                ; preds = %41, %0
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = add nsw i32 %6, -1
  store i32 %7, ptr @n, align 4, !tbaa !5
  %8 = icmp ne i32 %6, 0
  br i1 %8, label %9, label %42

9:                                                ; preds = %5
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %2, ptr noundef %3)
  br label %11

11:                                               ; preds = %23, %9
  %12 = load i32, ptr @tail, align 4, !tbaa !5
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %14, label %21

14:                                               ; preds = %11
  %15 = load i32, ptr @tail, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [50001 x i32], ptr @q, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = icmp sgt i32 %18, %19
  br label %21

21:                                               ; preds = %14, %11
  %22 = phi i1 [ false, %11 ], [ %20, %14 ]
  br i1 %22, label %23, label %28

23:                                               ; preds = %21
  %24 = load i32, ptr @tail, align 4, !tbaa !5
  %25 = add nsw i32 %24, -1
  store i32 %25, ptr @tail, align 4, !tbaa !5
  %26 = load i32, ptr @ans, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr @ans, align 4, !tbaa !5
  br label %11, !llvm.loop !9

28:                                               ; preds = %21
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = load i32, ptr @tail, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [50001 x i32], ptr @q, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = icmp ne i32 %29, %33
  br i1 %34, label %35, label %41

35:                                               ; preds = %28
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = load i32, ptr @tail, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr @tail, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [50001 x i32], ptr @q, i64 0, i64 %39
  store i32 %36, ptr %40, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %35, %28
  br label %5, !llvm.loop !11

42:                                               ; preds = %5
  %43 = load i32, ptr @ans, align 4, !tbaa !5
  %44 = load i32, ptr @tail, align 4, !tbaa !5
  %45 = add nsw i32 %43, %44
  %46 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %45)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  ret i32 0
}

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

declare i32 @__isoc99_scanf(ptr noundef, ...) #2

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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}


; 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
@q = external dso_local local_unnamed_addr global [50001 x i32], align 16
@tail = 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 [5 x i8], align 1
@.str.2 = 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 = alloca i32, align 4
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  %3 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %4 = load i32, ptr @n, align 4, !tbaa !5
  %5 = add nsw i32 %4, -1
  store i32 %5, ptr @n, align 4, !tbaa !5
  %.not7 = icmp eq i32 %4, 0
  br i1 %.not7, label %.._crit_edge_crit_edge, label %.lr.ph8

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

.lr.ph8:                                          ; preds = %0, %28
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %1, ptr noundef nonnull %2)
  %.pr = load i32, ptr @tail, align 4, !tbaa !5
  %.not24 = icmp eq i32 %.pr, 0
  %.pre = load i32, ptr %2, align 4, !tbaa !5
  br i1 %.not24, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph8
  %7 = sext i32 %.pr to i64
  %8 = getelementptr inbounds [50001 x i32], ptr @q, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp sgt i32 %9, %.pre
  br i1 %10, label %.lr.ph20, label %.critedge.loopexit.split.loop.exit12

.lr.ph20:                                         ; preds = %.lr.ph
  %ans.promoted = load i32, ptr @ans, align 4, !tbaa !5
  br label %15

11:                                               ; preds = %15
  %12 = getelementptr inbounds [50001 x i32], ptr @q, i64 0, i64 %indvars.iv.next
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = icmp sgt i32 %13, %.pre
  br i1 %14, label %15, label %.critedge.loopexit.split.loop.exit12.loopexit, !llvm.loop !9

15:                                               ; preds = %.lr.ph20, %11
  %16 = phi i32 [ %ans.promoted, %.lr.ph20 ], [ %18, %11 ]
  %indvars.iv19 = phi i64 [ %7, %.lr.ph20 ], [ %indvars.iv.next, %11 ]
  %indvars.iv.next = add nsw i64 %indvars.iv19, -1
  %17 = trunc nsw i64 %indvars.iv.next to i32
  %18 = add nsw i32 %16, 1
  %.not2 = icmp eq i64 %indvars.iv.next, 0
  br i1 %.not2, label %.critedge.loopexit, label %11, !llvm.loop !9

.critedge.loopexit.split.loop.exit12.loopexit:    ; preds = %11
  store i32 %17, ptr @tail, align 4, !tbaa !5
  store i32 %18, ptr @ans, align 4, !tbaa !5
  br label %.critedge.loopexit.split.loop.exit12

.critedge.loopexit.split.loop.exit12:             ; preds = %.critedge.loopexit.split.loop.exit12.loopexit, %.lr.ph
  %.lcssa16 = phi i32 [ %.pr, %.lr.ph ], [ %17, %.critedge.loopexit.split.loop.exit12.loopexit ]
  %indvars.iv.lcssa = phi i64 [ %7, %.lr.ph ], [ %indvars.iv.next, %.critedge.loopexit.split.loop.exit12.loopexit ]
  %19 = trunc nsw i64 %indvars.iv.lcssa to i32
  br label %.critedge

.critedge.loopexit:                               ; preds = %15
  store i32 %17, ptr @tail, align 4, !tbaa !5
  store i32 %18, ptr @ans, align 4, !tbaa !5
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit, %.critedge.loopexit.split.loop.exit12, %.lr.ph8
  %20 = phi i32 [ 0, %.lr.ph8 ], [ %.lcssa16, %.critedge.loopexit.split.loop.exit12 ], [ 0, %.critedge.loopexit ]
  %.lcssa = phi i32 [ 0, %.lr.ph8 ], [ %19, %.critedge.loopexit.split.loop.exit12 ], [ 0, %.critedge.loopexit ]
  %21 = sext i32 %.lcssa to i64
  %22 = getelementptr inbounds [50001 x i32], ptr @q, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %.not3 = icmp eq i32 %.pre, %23
  br i1 %.not3, label %28, label %24

24:                                               ; preds = %.critedge
  %25 = add nsw i32 %.lcssa, 1
  store i32 %25, ptr @tail, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [50001 x i32], ptr @q, i64 0, i64 %26
  store i32 %.pre, ptr %27, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %24, %.critedge
  %29 = phi i32 [ %25, %24 ], [ %20, %.critedge ]
  %30 = load i32, ptr @n, align 4, !tbaa !5
  %31 = add nsw i32 %30, -1
  store i32 %31, ptr @n, align 4, !tbaa !5
  %.not = icmp eq i32 %30, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph8, !llvm.loop !11

._crit_edge:                                      ; preds = %28, %.._crit_edge_crit_edge
  %32 = phi i32 [ %.pre10, %.._crit_edge_crit_edge ], [ %29, %28 ]
  %33 = load i32, ptr @ans, align 4, !tbaa !5
  %34 = add nsw i32 %32, %33
  %35 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %34)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  ret i32 0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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}


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