; LLVM IR from tangjz/acm-icpc :: buaa/oj3rd (1243)

; 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 [500001 x i32], align 16
@L = external dso_local global i32, align 4
@R = external dso_local global i32, align 4
@M = external dso_local global i32, align 4
@cnt = external dso_local global i64, align 8
@.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
  store i32 0, ptr %1, align 4
  br label %5

5:                                                ; preds = %92, %0
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  %7 = icmp eq i32 %6, 1
  br i1 %7, label %8, label %95

8:                                                ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %19, %8
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = load i32, ptr @n, align 4, !tbaa !5
  %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) #4
  br label %22

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

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

22:                                               ; preds = %13
  %23 = load i32, ptr @n, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i32, ptr @a, i64 %24
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef @a, ptr noundef %25)
  store i32 0, ptr @L, align 4, !tbaa !5
  store i32 2147483647, ptr @R, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %91, %22
  %27 = load i32, ptr @L, align 4, !tbaa !5
  %28 = load i32, ptr @R, align 4, !tbaa !5
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %30, label %92

30:                                               ; preds = %26
  %31 = load i32, ptr @L, align 4, !tbaa !5
  %32 = load i32, ptr @R, align 4, !tbaa !5
  %33 = load i32, ptr @L, align 4, !tbaa !5
  %34 = sub nsw i32 %32, %33
  %35 = ashr i32 %34, 1
  %36 = add nsw i32 %31, %35
  store i32 %36, ptr @M, align 4, !tbaa !5
  store i64 0, ptr @cnt, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %72, %30
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = load i32, ptr @n, align 4, !tbaa !5
  %40 = icmp slt i32 %38, %39
  br i1 %40, label %42, label %41

41:                                               ; preds = %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %75

42:                                               ; preds = %37
  br label %43

43:                                               ; preds = %62, %42
  %44 = load i32, ptr %4, align 4, !tbaa !5
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %47, label %59

47:                                               ; preds = %43
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [500001 x i32], ptr @a, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [500001 x i32], ptr @a, i64 0, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !5
  %56 = sub nsw i32 %51, %55
  %57 = load i32, ptr @M, align 4, !tbaa !5
  %58 = icmp sgt i32 %56, %57
  br label %59

59:                                               ; preds = %47, %43
  %60 = phi i1 [ false, %43 ], [ %58, %47 ]
  br i1 %60, label %61, label %65

61:                                               ; preds = %59
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %4, align 4, !tbaa !5
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %4, align 4, !tbaa !5
  br label %43, !llvm.loop !13

65:                                               ; preds = %59
  %66 = load i32, ptr %3, align 4, !tbaa !5
  %67 = load i32, ptr %4, align 4, !tbaa !5
  %68 = sub nsw i32 %66, %67
  %69 = sext i32 %68 to i64
  %70 = load i64, ptr @cnt, align 8, !tbaa !11
  %71 = add nsw i64 %70, %69
  store i64 %71, ptr @cnt, align 8, !tbaa !11
  br label %72

72:                                               ; preds = %65
  %73 = load i32, ptr %3, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %3, align 4, !tbaa !5
  br label %37, !llvm.loop !14

75:                                               ; preds = %41
  %76 = load i64, ptr @cnt, align 8, !tbaa !11
  %77 = load i32, ptr @n, align 4, !tbaa !5
  %78 = sext i32 %77 to i64
  %79 = load i32, ptr @n, align 4, !tbaa !5
  %80 = sext i32 %79 to i64
  %81 = sub nsw i64 %80, 1
  %82 = mul nsw i64 %78, %81
  %83 = add nsw i64 %82, 2
  %84 = ashr i64 %83, 2
  %85 = icmp sge i64 %76, %84
  br i1 %85, label %86, label %88

86:                                               ; preds = %75
  %87 = load i32, ptr @M, align 4, !tbaa !5
  store i32 %87, ptr @R, align 4, !tbaa !5
  br label %91

88:                                               ; preds = %75
  %89 = load i32, ptr @M, align 4, !tbaa !5
  %90 = add nsw i32 %89, 1
  store i32 %90, ptr @L, align 4, !tbaa !5
  br label %91

91:                                               ; preds = %88, %86
  br label %26, !llvm.loop !15

92:                                               ; preds = %26
  %93 = load i32, ptr @L, align 4, !tbaa !5
  %94 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %93)
  br label %5, !llvm.loop !16

95:                                               ; preds = %5
  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 uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) #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 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 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}
!16 = distinct !{!16, !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 [500001 x i32], align 16
@L = external dso_local local_unnamed_addr global i32, align 4
@R = external dso_local local_unnamed_addr global i32, align 4
@M = external dso_local local_unnamed_addr global i32, align 4
@cnt = external dso_local local_unnamed_addr global i64, align 8
@.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() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %2 = icmp eq i32 %1, 1
  br i1 %2, label %.preheader19, label %._crit_edge50

.preheader19:                                     ; preds = %0, %.loopexit
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %.preheader19.._crit_edge_crit_edge

.preheader19.._crit_edge_crit_edge:               ; preds = %.preheader19
  %.pre67 = sext i32 %3 to i64
  br label %.lr.ph36

.lr.ph36:                                         ; preds = %.lr.ph, %.preheader19.._crit_edge_crit_edge
  %.pre-phi68 = phi i64 [ %.pre67, %.preheader19.._crit_edge_crit_edge ], [ %42, %.lr.ph ]
  %5 = getelementptr inbounds i32, ptr @a, i64 %.pre-phi68
  tail call void @_ZSt4sortIPiEvT_S1_(ptr noundef nonnull @a, ptr noundef nonnull %5)
  store i32 0, ptr @L, align 4, !tbaa !5
  store i32 2147483647, ptr @R, align 4, !tbaa !5
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = icmp sgt i32 %6, 0
  %8 = sext i32 %6 to i64
  %9 = add nsw i64 %8, -1
  %10 = mul nsw i64 %9, %8
  %11 = add nsw i64 %10, 2
  %12 = ashr i64 %11, 2
  br i1 %7, label %.preheader.lr.ph.us.preheader, label %.lr.ph36.split

.preheader.lr.ph.us.preheader:                    ; preds = %.lr.ph36
  %wide.trip.count = zext nneg i32 %6 to i64
  br label %.preheader.lr.ph.us

.preheader.lr.ph.us:                              ; preds = %.preheader.lr.ph.us.preheader, %21
  %13 = phi i32 [ %23, %21 ], [ 0, %.preheader.lr.ph.us.preheader ]
  %14 = phi i32 [ %22, %21 ], [ 2147483647, %.preheader.lr.ph.us.preheader ]
  %15 = sub nsw i32 %14, %13
  %16 = ashr i32 %15, 1
  %17 = add nsw i32 %16, %13
  br label %.preheader.us

18:                                               ; preds = %._crit_edge30.us
  store i32 %17, ptr @R, align 4, !tbaa !5
  br label %21

19:                                               ; preds = %._crit_edge30.us
  %20 = add nsw i32 %17, 1
  store i32 %20, ptr @L, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %19, %18
  %22 = phi i32 [ %14, %19 ], [ %17, %18 ]
  %23 = phi i32 [ %20, %19 ], [ %13, %18 ]
  %24 = icmp slt i32 %23, %22
  br i1 %24, label %.preheader.lr.ph.us, label %.loopexit, !llvm.loop !9

25:                                               ; preds = %.lr.ph24.us, %32
  %indvars.iv61 = phi i64 [ %35, %.lr.ph24.us ], [ %indvars.iv.next62, %32 ]
  %26 = getelementptr inbounds [500001 x i32], ptr @a, i64 0, i64 %indvars.iv61
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = sub nsw i32 %38, %27
  %29 = icmp sgt i32 %28, %17
  br i1 %29, label %32, label %.critedge.us.loopexit

.critedge.us.loopexit:                            ; preds = %32, %25
  %.1.lcssa.us.ph.in = phi i64 [ %indvars.iv61, %25 ], [ %indvars.iv64, %32 ]
  %.1.lcssa.us.ph = trunc i64 %.1.lcssa.us.ph.in to i32
  %sext = shl i64 %.1.lcssa.us.ph.in, 32
  %.pre = ashr exact i64 %sext, 32
  br label %.critedge.us

.critedge.us:                                     ; preds = %.critedge.us.loopexit, %.preheader.us
  %.pre-phi = phi i64 [ %.pre, %.critedge.us.loopexit ], [ %35, %.preheader.us ]
  %.1.lcssa.us = phi i32 [ %.1.lcssa.us.ph, %.critedge.us.loopexit ], [ %.029.us, %.preheader.us ]
  %30 = sub nsw i64 %indvars.iv64, %.pre-phi
  %31 = add nsw i64 %34, %30
  %indvars.iv.next65 = add nuw nsw i64 %indvars.iv64, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next65, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge30.us, label %.preheader.us, !llvm.loop !11

32:                                               ; preds = %25
  %indvars.iv.next62 = add nsw i64 %indvars.iv61, 1
  %33 = icmp slt i64 %indvars.iv.next62, %indvars.iv64
  br i1 %33, label %25, label %.critedge.us.loopexit, !llvm.loop !12

.preheader.us:                                    ; preds = %.preheader.lr.ph.us, %.critedge.us
  %indvars.iv64 = phi i64 [ 0, %.preheader.lr.ph.us ], [ %indvars.iv.next65, %.critedge.us ]
  %.029.us = phi i32 [ 0, %.preheader.lr.ph.us ], [ %.1.lcssa.us, %.critedge.us ]
  %34 = phi i64 [ 0, %.preheader.lr.ph.us ], [ %31, %.critedge.us ]
  %35 = sext i32 %.029.us to i64
  %36 = icmp sgt i64 %indvars.iv64, %35
  br i1 %36, label %.lr.ph24.us, label %.critedge.us

.lr.ph24.us:                                      ; preds = %.preheader.us
  %37 = getelementptr inbounds [500001 x i32], ptr @a, i64 0, i64 %indvars.iv64
  %38 = load i32, ptr %37, align 4, !tbaa !5
  br label %25

._crit_edge30.us:                                 ; preds = %.critedge.us
  %.not.us = icmp slt i64 %31, %12
  br i1 %.not.us, label %19, label %18

.lr.ph36.split:                                   ; preds = %.lr.ph36
  %.not = icmp sgt i64 %12, 0
  br i1 %.not, label %.lr.ph36.split.split.us.preheader, label %.lr.ph36.split.split.preheader

.lr.ph36.split.split.preheader:                   ; preds = %.lr.ph36.split
  store i32 0, ptr @R, align 4, !tbaa !5
  br label %.loopexit

.lr.ph36.split.split.us.preheader:                ; preds = %.lr.ph36.split
  store i32 2147483647, ptr @L, align 4, !tbaa !5
  br label %.loopexit

.lr.ph:                                           ; preds = %.preheader19, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %.preheader19 ]
  %39 = getelementptr inbounds i32, ptr @a, i64 %indvars.iv
  %40 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %39)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %41 = load i32, ptr @n, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = icmp slt i64 %indvars.iv.next, %42
  br i1 %43, label %.lr.ph, label %.lr.ph36, !llvm.loop !13

.loopexit:                                        ; preds = %21, %.lr.ph36.split.split.us.preheader, %.lr.ph36.split.split.preheader
  %.us-phi = phi i64 [ 0, %.lr.ph36.split.split.us.preheader ], [ 0, %.lr.ph36.split.split.preheader ], [ %31, %21 ]
  %.us-phi40 = phi i32 [ 2147483646, %.lr.ph36.split.split.us.preheader ], [ 0, %.lr.ph36.split.split.preheader ], [ %17, %21 ]
  %.us-phi41 = phi i32 [ 2147483647, %.lr.ph36.split.split.us.preheader ], [ 0, %.lr.ph36.split.split.preheader ], [ %23, %21 ]
  store i32 %.us-phi40, ptr @M, align 4, !tbaa !5
  store i64 %.us-phi, ptr @cnt, align 8, !tbaa !14
  %44 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %.us-phi41)
  %45 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %46 = icmp eq i32 %45, 1
  br i1 %46, label %.preheader19, label %._crit_edge50, !llvm.loop !16

._crit_edge50:                                    ; preds = %.loopexit, %0
  ret i32 0
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) local_unnamed_addr #2

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

!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}
!14 = !{!15, !15, i64 0}
!15 = !{!"long long", !7, i64 0}
!16 = distinct !{!16, !10}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass