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

; 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
@num = external dso_local global [50001 x i32], align 16
@tmp = 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 uwtable
declare dso_local void @_Z3dfsi(i32 noundef) #0

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

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

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  %3 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %4

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

8:                                                ; preds = %4
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i32, ptr @num, i64 %10
  %12 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %11)
  br label %13

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

16:                                               ; preds = %4
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %51, %16
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %21, label %54

21:                                               ; preds = %17
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [50001 x i32], ptr @num, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = load i32, ptr @tmp, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr @tmp, align 4, !tbaa !5
  %28 = sext i32 %26 to i64
  %29 = getelementptr inbounds [50001 x i32], ptr @num, i64 0, i64 %28
  store i32 %25, ptr %29, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %47, %21
  %31 = load i32, ptr @tmp, align 4, !tbaa !5
  %32 = icmp sge i32 %31, 3
  br i1 %32, label %33, label %45

33:                                               ; preds = %30
  %34 = load i32, ptr @tmp, align 4, !tbaa !5
  %35 = sub nsw i32 %34, 3
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [50001 x i32], ptr @num, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = load i32, ptr @tmp, align 4, !tbaa !5
  %40 = sub nsw i32 %39, 1
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [50001 x i32], ptr @num, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = icmp sle i32 %38, %43
  br label %45

45:                                               ; preds = %33, %30
  %46 = phi i1 [ false, %30 ], [ %44, %33 ]
  br i1 %46, label %47, label %50

47:                                               ; preds = %45
  %48 = load i32, ptr @tmp, align 4, !tbaa !5
  %49 = sub nsw i32 %48, 2
  call void @_Z3dfsi(i32 noundef %49)
  br label %30, !llvm.loop !11

50:                                               ; preds = %45
  br label %51

51:                                               ; preds = %50
  %52 = load i32, ptr %2, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %2, align 4, !tbaa !5
  br label %17, !llvm.loop !12

54:                                               ; preds = %17
  br label %55

55:                                               ; preds = %58, %54
  %56 = load i32, ptr @tmp, align 4, !tbaa !5
  %57 = icmp sgt i32 %56, 1
  br i1 %57, label %58, label %61

58:                                               ; preds = %55
  %59 = load i32, ptr @tmp, align 4, !tbaa !5
  %60 = sub nsw i32 %59, 1
  call void @_Z3dfsi(i32 noundef %60)
  br label %55, !llvm.loop !13

61:                                               ; preds = %55
  %62 = load i32, ptr @ans, align 4, !tbaa !5
  %63 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %62)
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  ret i32 0
}

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

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { "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}
!13 = distinct !{!13, !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
@num = external dso_local global [50001 x i32], align 16
@tmp = 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 [4 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3dfsi(i32 noundef) 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 %.preheader8.thread

.preheader8.thread:                               ; preds = %0
  %.pre2021 = load i32, ptr @tmp, align 4, !tbaa !5
  br label %.preheader

.preheader8:                                      ; preds = %.lr.ph
  %4 = icmp sgt i32 %7, 1
  %.pre20 = load i32, ptr @tmp, align 4, !tbaa !5
  br i1 %4, label %.lr.ph13, label %.preheader

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

.preheader:                                       ; preds = %.critedge, %.preheader8.thread, %.preheader8
  %10 = phi i32 [ %.pre20, %.preheader8 ], [ %.pre2021, %.preheader8.thread ], [ %34, %.critedge ]
  %11 = icmp sgt i32 %10, 1
  br i1 %11, label %.lr.ph14, label %._crit_edge

.lr.ph13:                                         ; preds = %.preheader8, %.critedge
  %12 = phi i32 [ %33, %.critedge ], [ %7, %.preheader8 ]
  %13 = phi i32 [ %34, %.critedge ], [ %.pre20, %.preheader8 ]
  %indvars.iv16 = phi i64 [ %indvars.iv.next17, %.critedge ], [ 1, %.preheader8 ]
  %14 = getelementptr inbounds [50001 x i32], ptr @num, i64 0, i64 %indvars.iv16
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = add nsw i32 %13, 1
  store i32 %16, ptr @tmp, align 4, !tbaa !5
  %17 = sext i32 %13 to i64
  %18 = getelementptr inbounds [50001 x i32], ptr @num, i64 0, i64 %17
  store i32 %15, ptr %18, align 4, !tbaa !5
  %19 = icmp sgt i32 %13, 1
  br i1 %19, label %.lr.ph10, label %.critedge

.lr.ph10:                                         ; preds = %.lr.ph13, %29
  %20 = phi i32 [ %.pr, %29 ], [ %16, %.lr.ph13 ]
  %21 = add nsw i32 %20, -3
  %22 = zext nneg i32 %21 to i64
  %23 = getelementptr inbounds [50001 x i32], ptr @num, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = add nsw i32 %20, -1
  %26 = zext nneg i32 %25 to i64
  %27 = getelementptr inbounds [50001 x i32], ptr @num, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %.not = icmp sgt i32 %24, %28
  br i1 %.not, label %.critedge.loopexit, label %29

29:                                               ; preds = %.lr.ph10
  %30 = add nsw i32 %20, -2
  tail call void @_Z3dfsi(i32 noundef %30)
  %.pr = load i32, ptr @tmp, align 4, !tbaa !5
  %31 = icmp sgt i32 %.pr, 2
  br i1 %31, label %.lr.ph10, label %.critedge.loopexit, !llvm.loop !11

.critedge.loopexit:                               ; preds = %.lr.ph10, %29
  %32 = phi i32 [ %20, %.lr.ph10 ], [ %.pr, %29 ]
  %.pre19 = load i32, ptr @n, align 4, !tbaa !5
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit, %.lr.ph13
  %33 = phi i32 [ %.pre19, %.critedge.loopexit ], [ %12, %.lr.ph13 ]
  %34 = phi i32 [ %32, %.critedge.loopexit ], [ %16, %.lr.ph13 ]
  %indvars.iv.next17 = add nuw nsw i64 %indvars.iv16, 1
  %35 = sext i32 %33 to i64
  %36 = icmp slt i64 %indvars.iv.next17, %35
  br i1 %36, label %.lr.ph13, label %.preheader, !llvm.loop !12

.lr.ph14:                                         ; preds = %.preheader, %.lr.ph14
  %37 = phi i32 [ %39, %.lr.ph14 ], [ %10, %.preheader ]
  %38 = add nsw i32 %37, -1
  tail call void @_Z3dfsi(i32 noundef %38)
  %39 = load i32, ptr @tmp, align 4, !tbaa !5
  %40 = icmp sgt i32 %39, 1
  br i1 %40, label %.lr.ph14, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph14, %.preheader
  %41 = load i32, ptr @ans, align 4, !tbaa !5
  %42 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %41)
  ret i32 0
}

; 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

attributes #0 = { 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 #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" }

!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}
!13 = distinct !{!13, !10}


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