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

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

5:                                                ; preds = %21, %0
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = load i32, ptr @n, align 4, !tbaa !5
  %8 = icmp sle i32 %6, %7
  br i1 %8, label %9, label %24

9:                                                ; preds = %5
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %3)
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = sub nsw i32 %11, 1
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [1000001 x i64], ptr @sum, i64 0, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !9
  %16 = load i64, ptr %3, align 8, !tbaa !9
  %17 = add nsw i64 %15, %16
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [1000001 x i64], ptr @sum, i64 0, i64 %19
  store i64 %17, ptr %20, align 8, !tbaa !9
  br label %21

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

24:                                               ; preds = %5
  %25 = load i32, ptr @n, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [1000001 x i64], ptr @sum, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !9
  %29 = load i32, ptr @n, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = sdiv i64 %28, %30
  store i64 %31, ptr %3, align 8, !tbaa !9
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %46, %24
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %34 = load i32, ptr @n, align 4, !tbaa !5
  %35 = icmp sle i32 %33, %34
  br i1 %35, label %36, label %49

36:                                               ; preds = %32
  %37 = load i64, ptr %3, align 8, !tbaa !9
  %38 = load i32, ptr %2, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = mul nsw i64 %37, %39
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [1000001 x i64], ptr @sum, i64 0, i64 %42
  %44 = load i64, ptr %43, align 8, !tbaa !9
  %45 = sub nsw i64 %44, %40
  store i64 %45, ptr %43, align 8, !tbaa !9
  br label %46

46:                                               ; preds = %36
  %47 = load i32, ptr %2, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %2, align 4, !tbaa !5
  br label %32, !llvm.loop !13

49:                                               ; preds = %32
  %50 = load i32, ptr @n, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i64, ptr @sum, i64 %51
  %53 = getelementptr inbounds i64, ptr %52, i64 1
  call void @_ZSt4sortIPxEvT_S1_(ptr noundef getelementptr inbounds (i64, ptr @sum, i64 1), ptr noundef %53)
  %54 = load i32, ptr @n, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  %56 = ashr i32 %55, 1
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [1000001 x i64], ptr @sum, i64 0, i64 %57
  %59 = load i64, ptr %58, align 8, !tbaa !9
  store i64 %59, ptr %3, align 8, !tbaa !9
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %60

60:                                               ; preds = %74, %49
  %61 = load i32, ptr %2, align 4, !tbaa !5
  %62 = load i32, ptr @n, align 4, !tbaa !5
  %63 = icmp sle i32 %61, %62
  br i1 %63, label %64, label %77

64:                                               ; preds = %60
  %65 = load i32, ptr %2, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [1000001 x i64], ptr @sum, i64 0, i64 %66
  %68 = load i64, ptr %67, align 8, !tbaa !9
  %69 = load i64, ptr %3, align 8, !tbaa !9
  %70 = sub nsw i64 %68, %69
  %71 = call noundef i64 @_ZSt3absx(i64 noundef %70)
  %72 = load i64, ptr @ans, align 8, !tbaa !9
  %73 = add nsw i64 %72, %71
  store i64 %73, ptr @ans, align 8, !tbaa !9
  br label %74

74:                                               ; preds = %64
  %75 = load i32, ptr %2, align 4, !tbaa !5
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %2, align 4, !tbaa !5
  br label %60, !llvm.loop !14

77:                                               ; preds = %60
  %78 = load i64, ptr @ans, align 8, !tbaa !9
  %79 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %78)
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPxEvT_S1_(ptr noundef, ptr noundef) #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZSt3absx(i64 noundef) #4

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


; 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
@sum = external dso_local global [1000001 x i64], align 16
@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 [5 x i8], align 1
@.str.2 = 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 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #5
  %2 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %.not15 = icmp slt i32 %3, 1
  br i1 %.not15, label %._crit_edge.thread, label %.lr.ph

._crit_edge.thread:                               ; preds = %0
  %.pre35 = sext i32 %3 to i64
  %4 = getelementptr inbounds [1000001 x i64], ptr @sum, i64 0, i64 %.pre35
  %5 = load i64, ptr %4, align 8, !tbaa !9
  %6 = sdiv i64 %5, %.pre35
  store i64 %6, ptr %1, align 8, !tbaa !9
  br label %._crit_edge21

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

._crit_edge:                                      ; preds = %.lr.ph
  %16 = getelementptr inbounds [1000001 x i64], ptr @sum, i64 0, i64 %15
  %17 = load i64, ptr %16, align 8, !tbaa !9
  %18 = sdiv i64 %17, %15
  store i64 %18, ptr %1, align 8, !tbaa !9
  %.not1317 = icmp slt i32 %14, 1
  br i1 %.not1317, label %._crit_edge21, label %.lr.ph20.preheader

.lr.ph20.preheader:                               ; preds = %._crit_edge
  %19 = add nuw i32 %14, 1
  %wide.trip.count = zext i32 %19 to i64
  %20 = zext nneg i32 %14 to i64
  %min.iters.check = icmp ult i32 %14, 4
  br i1 %min.iters.check, label %.lr.ph20.preheader45, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph20.preheader
  %n.vec = and i64 %20, 2147483644
  %ind.end = or disjoint i64 %n.vec, 1
  %broadcast.splatinsert = insertelement <2 x i64> poison, i64 %18, i64 0
  %broadcast.splat = shufflevector <2 x i64> %broadcast.splatinsert, <2 x i64> poison, <2 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <2 x i64> [ <i64 1, i64 2>, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %step.add = add <2 x i64> %vec.ind, <i64 2, i64 2>
  %offset.idx = or disjoint i64 %index, 1
  %21 = mul nsw <2 x i64> %broadcast.splat, %vec.ind
  %22 = mul nsw <2 x i64> %broadcast.splat, %step.add
  %23 = getelementptr inbounds [1000001 x i64], ptr @sum, i64 0, i64 %offset.idx
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  %wide.load = load <2 x i64>, ptr %23, align 8, !tbaa !9
  %wide.load44 = load <2 x i64>, ptr %24, align 8, !tbaa !9
  %25 = sub nsw <2 x i64> %wide.load, %21
  %26 = sub nsw <2 x i64> %wide.load44, %22
  store <2 x i64> %25, ptr %23, align 8, !tbaa !9
  store <2 x i64> %26, ptr %24, align 8, !tbaa !9
  %index.next = add nuw i64 %index, 4
  %vec.ind.next = add <2 x i64> %vec.ind, <i64 4, i64 4>
  %27 = icmp eq i64 %index.next, %n.vec
  br i1 %27, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %20
  br i1 %cmp.n, label %._crit_edge21, label %.lr.ph20.preheader45

.lr.ph20.preheader45:                             ; preds = %middle.block, %.lr.ph20.preheader
  %indvars.iv29.ph = phi i64 [ 1, %.lr.ph20.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph20

.lr.ph20:                                         ; preds = %.lr.ph20.preheader45, %.lr.ph20
  %indvars.iv29 = phi i64 [ %indvars.iv.next30, %.lr.ph20 ], [ %indvars.iv29.ph, %.lr.ph20.preheader45 ]
  %28 = mul nsw i64 %18, %indvars.iv29
  %29 = getelementptr inbounds [1000001 x i64], ptr @sum, i64 0, i64 %indvars.iv29
  %30 = load i64, ptr %29, align 8, !tbaa !9
  %31 = sub nsw i64 %30, %28
  store i64 %31, ptr %29, align 8, !tbaa !9
  %indvars.iv.next30 = add nuw nsw i64 %indvars.iv29, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next30, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge21, label %.lr.ph20, !llvm.loop !16

._crit_edge21:                                    ; preds = %.lr.ph20, %middle.block, %._crit_edge.thread, %._crit_edge
  %32 = phi ptr [ %4, %._crit_edge.thread ], [ %16, %._crit_edge ], [ %16, %middle.block ], [ %16, %.lr.ph20 ]
  %33 = getelementptr inbounds i8, ptr %32, i64 8
  call void @_ZSt4sortIPxEvT_S1_(ptr noundef nonnull getelementptr inbounds (i8, ptr @sum, i64 8), ptr noundef nonnull %33)
  %34 = load i32, ptr @n, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  %36 = ashr i32 %35, 1
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [1000001 x i64], ptr @sum, i64 0, i64 %37
  %39 = load i64, ptr %38, align 8, !tbaa !9
  store i64 %39, ptr %1, align 8, !tbaa !9
  %.not1422 = icmp slt i32 %34, 1
  br i1 %.not1422, label %._crit_edge21.._crit_edge26_crit_edge, label %.lr.ph25

._crit_edge21.._crit_edge26_crit_edge:            ; preds = %._crit_edge21
  %.pre = load i64, ptr @ans, align 8, !tbaa !9
  br label %._crit_edge26

.lr.ph25:                                         ; preds = %._crit_edge21, %.lr.ph25
  %indvars.iv32 = phi i64 [ %indvars.iv.next33, %.lr.ph25 ], [ 1, %._crit_edge21 ]
  %40 = getelementptr inbounds [1000001 x i64], ptr @sum, i64 0, i64 %indvars.iv32
  %41 = load i64, ptr %40, align 8, !tbaa !9
  %42 = load i64, ptr %1, align 8, !tbaa !9
  %43 = sub nsw i64 %41, %42
  %44 = call noundef i64 @_ZSt3absx(i64 noundef %43)
  %45 = load i64, ptr @ans, align 8, !tbaa !9
  %46 = add nsw i64 %45, %44
  store i64 %46, ptr @ans, align 8, !tbaa !9
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %47 = load i32, ptr @n, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %.not14.not = icmp slt i64 %indvars.iv32, %48
  br i1 %.not14.not, label %.lr.ph25, label %._crit_edge26, !llvm.loop !17

._crit_edge26:                                    ; preds = %.lr.ph25, %._crit_edge21.._crit_edge26_crit_edge
  %49 = phi i64 [ %.pre, %._crit_edge21.._crit_edge26_crit_edge ], [ %46, %.lr.ph25 ]
  %50 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %49)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #5
  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: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPxEvT_S1_(ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZSt3absx(i64 noundef) local_unnamed_addr #4

; 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 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 = { 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 = { 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 = { 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 #5 = { 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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12, !14, !15}
!14 = !{!"llvm.loop.isvectorized", i32 1}
!15 = !{!"llvm.loop.unroll.runtime.disable"}
!16 = distinct !{!16, !12, !15, !14}
!17 = distinct !{!17, !12}


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