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

; 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 [10001 x i32], align 16
@sum = external dso_local global i64, align 8
@ans = 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 [6 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) #4
  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) #4
  br label %18

10:                                               ; preds = %5
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds i32, ptr @a, 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
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i32, ptr @a, i64 %20
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef @a, ptr noundef %21)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %47, %18
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = load i32, ptr @n, align 4, !tbaa !5
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %50

27:                                               ; preds = %22
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = mul nsw i64 %29, %34
  %36 = load i64, ptr @sum, align 8, !tbaa !11
  %37 = sub nsw i64 %35, %36
  %38 = load i64, ptr @ans, align 8, !tbaa !11
  %39 = add nsw i64 %38, %37
  store i64 %39, ptr @ans, align 8, !tbaa !11
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = load i64, ptr @sum, align 8, !tbaa !11
  %46 = add nsw i64 %45, %44
  store i64 %46, ptr @sum, align 8, !tbaa !11
  br label %47

47:                                               ; preds = %27
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %3, align 4, !tbaa !5
  br label %22, !llvm.loop !13

50:                                               ; preds = %26
  %51 = load i64, ptr @ans, align 8, !tbaa !11
  %52 = mul nsw i64 %51, 2
  %53 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %52)
  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}


; 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 [10001 x i32], align 16
@sum = external dso_local local_unnamed_addr global i64, align 8
@ans = 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 [6 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 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %.._crit_edge_crit_edge

.._crit_edge_crit_edge:                           ; preds = %0
  %.pre = sext i32 %2 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.._crit_edge_crit_edge
  %.pre-phi = phi i64 [ %.pre, %.._crit_edge_crit_edge ], [ %11, %.lr.ph ]
  %4 = getelementptr inbounds i32, ptr @a, i64 %.pre-phi
  tail call void @_ZSt4sortIPiEvT_S1_(ptr noundef nonnull @a, ptr noundef nonnull %4)
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %ans.promoted = load i64, ptr @ans, align 8, !tbaa !9
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph13.preheader, label %22

.lr.ph13.preheader:                               ; preds = %._crit_edge
  %sum.promoted = load i64, ptr @sum, align 8, !tbaa !9
  %wide.trip.count = zext nneg i32 %5 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %7 = icmp ult i32 %5, 4
  br i1 %7, label %._crit_edge14.unr-lcssa, label %.lr.ph13.preheader.new

.lr.ph13.preheader.new:                           ; preds = %.lr.ph13.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483644
  br label %.lr.ph13

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

._crit_edge14.unr-lcssa:                          ; preds = %.lr.ph13, %.lr.ph13.preheader
  %.lcssa28.ph = phi i64 [ poison, %.lr.ph13.preheader ], [ %54, %.lr.ph13 ]
  %.lcssa.ph = phi i64 [ poison, %.lr.ph13.preheader ], [ %55, %.lr.ph13 ]
  %indvars.iv21.unr = phi i64 [ 0, %.lr.ph13.preheader ], [ %indvars.iv.next22.3, %.lr.ph13 ]
  %.unr = phi i64 [ %sum.promoted, %.lr.ph13.preheader ], [ %55, %.lr.ph13 ]
  %.unr30 = phi i64 [ %ans.promoted, %.lr.ph13.preheader ], [ %54, %.lr.ph13 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge14, label %.lr.ph13.epil

.lr.ph13.epil:                                    ; preds = %._crit_edge14.unr-lcssa, %.lr.ph13.epil
  %indvars.iv21.epil = phi i64 [ %indvars.iv.next22.epil, %.lr.ph13.epil ], [ %indvars.iv21.unr, %._crit_edge14.unr-lcssa ]
  %13 = phi i64 [ %21, %.lr.ph13.epil ], [ %.unr, %._crit_edge14.unr-lcssa ]
  %14 = phi i64 [ %20, %.lr.ph13.epil ], [ %.unr30, %._crit_edge14.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph13.epil ], [ 0, %._crit_edge14.unr-lcssa ]
  %15 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %indvars.iv21.epil
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = mul nsw i64 %indvars.iv21.epil, %17
  %19 = sub i64 %18, %13
  %20 = add nsw i64 %19, %14
  %21 = add nsw i64 %13, %17
  %indvars.iv.next22.epil = add nuw nsw i64 %indvars.iv21.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge14, label %.lr.ph13.epil, !llvm.loop !13

._crit_edge14:                                    ; preds = %.lr.ph13.epil, %._crit_edge14.unr-lcssa
  %.lcssa28 = phi i64 [ %.lcssa28.ph, %._crit_edge14.unr-lcssa ], [ %20, %.lr.ph13.epil ]
  %.lcssa = phi i64 [ %.lcssa.ph, %._crit_edge14.unr-lcssa ], [ %21, %.lr.ph13.epil ]
  store i64 %.lcssa28, ptr @ans, align 8, !tbaa !9
  store i64 %.lcssa, ptr @sum, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %._crit_edge14, %._crit_edge
  %23 = phi i64 [ %.lcssa28, %._crit_edge14 ], [ %ans.promoted, %._crit_edge ]
  %24 = shl nsw i64 %23, 1
  %25 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %24)
  ret i32 0

.lr.ph13:                                         ; preds = %.lr.ph13, %.lr.ph13.preheader.new
  %indvars.iv21 = phi i64 [ 0, %.lr.ph13.preheader.new ], [ %indvars.iv.next22.3, %.lr.ph13 ]
  %26 = phi i64 [ %sum.promoted, %.lr.ph13.preheader.new ], [ %55, %.lr.ph13 ]
  %27 = phi i64 [ %ans.promoted, %.lr.ph13.preheader.new ], [ %54, %.lr.ph13 ]
  %niter = phi i64 [ 0, %.lr.ph13.preheader.new ], [ %niter.next.3, %.lr.ph13 ]
  %28 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %indvars.iv21
  %29 = load i32, ptr %28, align 16, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = mul nsw i64 %indvars.iv21, %30
  %32 = sub i64 %31, %26
  %33 = add nsw i64 %32, %27
  %34 = add nsw i64 %26, %30
  %indvars.iv.next22 = or disjoint i64 %indvars.iv21, 1
  %35 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %indvars.iv.next22
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = mul nsw i64 %indvars.iv.next22, %37
  %39 = sub i64 %38, %34
  %40 = add nsw i64 %39, %33
  %41 = add nsw i64 %34, %37
  %indvars.iv.next22.1 = or disjoint i64 %indvars.iv21, 2
  %42 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %indvars.iv.next22.1
  %43 = load i32, ptr %42, align 8, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = mul nsw i64 %indvars.iv.next22.1, %44
  %46 = sub i64 %45, %41
  %47 = add nsw i64 %46, %40
  %48 = add nsw i64 %41, %44
  %indvars.iv.next22.2 = or disjoint i64 %indvars.iv21, 3
  %49 = getelementptr inbounds [10001 x i32], ptr @a, i64 0, i64 %indvars.iv.next22.2
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = mul nsw i64 %indvars.iv.next22.2, %51
  %53 = sub i64 %52, %48
  %54 = add nsw i64 %53, %47
  %55 = add nsw i64 %48, %51
  %indvars.iv.next22.3 = add nuw nsw i64 %indvars.iv21, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge14.unr-lcssa, label %.lr.ph13, !llvm.loop !15
}

; 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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.unroll.disable"}
!15 = distinct !{!15, !12}


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