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

; 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 [101 x i64], align 16
@l = external dso_local global i64, align 8
@r = external dso_local global i64, align 8
@mid = external dso_local global i64, align 8
@sum = 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
  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)
  %4 = load i32, ptr @n, align 4, !tbaa !5
  %5 = add nsw i32 %4, 1
  store i32 %5, ptr @n, align 4, !tbaa !5
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %6

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

10:                                               ; preds = %6
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [101 x i64], ptr @num, i64 0, i64 %12
  %14 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, 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 %6, !llvm.loop !9

18:                                               ; preds = %6
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i64, ptr @num, i64 %20
  call void @_ZSt4sortIPxEvT_S1_(ptr noundef @num, ptr noundef %21)
  store i64 0, ptr @l, align 8, !tbaa !11
  store i64 2147483647, ptr @r, align 8, !tbaa !11
  br label %22

22:                                               ; preds = %64, %18
  %23 = load i64, ptr @l, align 8, !tbaa !11
  %24 = add nsw i64 %23, 1
  %25 = load i64, ptr @r, align 8, !tbaa !11
  %26 = icmp slt i64 %24, %25
  br i1 %26, label %27, label %65

27:                                               ; preds = %22
  %28 = load i64, ptr @l, align 8, !tbaa !11
  %29 = load i64, ptr @r, align 8, !tbaa !11
  %30 = add nsw i64 %28, %29
  %31 = ashr i64 %30, 1
  store i64 %31, ptr @mid, align 8, !tbaa !11
  store i64 0, ptr @sum, align 8, !tbaa !11
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %53, %27
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %34 = load i32, ptr @n, align 4, !tbaa !5
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %36, label %56

36:                                               ; preds = %32
  %37 = load i32, ptr %2, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [101 x i64], ptr @num, i64 0, i64 %38
  %40 = load i64, ptr %39, align 8, !tbaa !11
  %41 = load i64, ptr @mid, align 8, !tbaa !11
  %42 = icmp slt i64 %40, %41
  br i1 %42, label %43, label %52

43:                                               ; preds = %36
  %44 = load i64, ptr @mid, align 8, !tbaa !11
  %45 = load i32, ptr %2, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [101 x i64], ptr @num, i64 0, i64 %46
  %48 = load i64, ptr %47, align 8, !tbaa !11
  %49 = sub nsw i64 %44, %48
  %50 = load i64, ptr @sum, align 8, !tbaa !11
  %51 = add nsw i64 %50, %49
  store i64 %51, ptr @sum, align 8, !tbaa !11
  br label %52

52:                                               ; preds = %43, %36
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr %2, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %2, align 4, !tbaa !5
  br label %32, !llvm.loop !13

56:                                               ; preds = %32
  %57 = load i64, ptr @sum, align 8, !tbaa !11
  %58 = load i64, ptr @mid, align 8, !tbaa !11
  %59 = icmp sle i64 %57, %58
  br i1 %59, label %60, label %62

60:                                               ; preds = %56
  %61 = load i64, ptr @mid, align 8, !tbaa !11
  store i64 %61, ptr @l, align 8, !tbaa !11
  br label %64

62:                                               ; preds = %56
  %63 = load i64, ptr @mid, align 8, !tbaa !11
  store i64 %63, ptr @r, align 8, !tbaa !11
  br label %64

64:                                               ; preds = %62, %60
  br label %22, !llvm.loop !14

65:                                               ; preds = %22
  %66 = load i64, ptr @l, align 8, !tbaa !11
  %67 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %66)
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  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

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 = { 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}


; 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 [101 x i64], align 16
@l = external dso_local local_unnamed_addr global i64, align 8
@r = external dso_local local_unnamed_addr global i64, align 8
@mid = external dso_local local_unnamed_addr global i64, align 8
@sum = 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 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = add nsw i32 %2, 1
  store i32 %3, ptr @n, align 4, !tbaa !5
  %4 = icmp sgt i32 %2, -1
  br i1 %4, label %.lr.ph, label %.._crit_edge_crit_edge

.._crit_edge_crit_edge:                           ; preds = %0
  %.pre = sext i32 %3 to i64
  br label %.lr.ph23

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %5 = getelementptr inbounds [101 x i64], ptr @num, i64 0, i64 %indvars.iv
  %6 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, 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 %.lr.ph23, !llvm.loop !9

.lr.ph23:                                         ; preds = %.lr.ph, %.._crit_edge_crit_edge
  %.pre-phi = phi i64 [ %.pre, %.._crit_edge_crit_edge ], [ %8, %.lr.ph ]
  %10 = getelementptr inbounds i64, ptr @num, i64 %.pre-phi
  tail call void @_ZSt4sortIPxEvT_S1_(ptr noundef nonnull @num, ptr noundef nonnull %10)
  store i64 0, ptr @l, align 8, !tbaa !11
  store i64 2147483647, ptr @r, align 8, !tbaa !11
  %11 = load i32, ptr @n, align 4, !tbaa !5
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %.lr.ph17.us.preheader, label %.lr.ph23.split

.lr.ph17.us.preheader:                            ; preds = %.lr.ph23
  %wide.trip.count = zext nneg i32 %11 to i64
  %min.iters.check = icmp ult i32 %11, 4
  %n.vec = and i64 %wide.trip.count, 2147483644
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br label %.lr.ph17.us

.lr.ph17.us:                                      ; preds = %.lr.ph17.us.preheader, %._crit_edge18.us
  %13 = phi i64 [ %.48, %._crit_edge18.us ], [ 0, %.lr.ph17.us.preheader ]
  %14 = phi i64 [ %., %._crit_edge18.us ], [ 2147483647, %.lr.ph17.us.preheader ]
  %15 = add nsw i64 %14, %13
  %16 = ashr i64 %15, 1
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph17.us
  %broadcast.splatinsert = insertelement <2 x i64> poison, i64 %16, 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.phi = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %25, %vector.body ]
  %vec.phi57 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %26, %vector.body ]
  %17 = getelementptr inbounds [101 x i64], ptr @num, i64 0, i64 %index
  %18 = getelementptr inbounds i8, ptr %17, i64 16
  %wide.load = load <2 x i64>, ptr %17, align 16, !tbaa !11
  %wide.load58 = load <2 x i64>, ptr %18, align 16, !tbaa !11
  %19 = icmp slt <2 x i64> %wide.load, %broadcast.splat
  %20 = icmp slt <2 x i64> %wide.load58, %broadcast.splat
  %21 = sub <2 x i64> %broadcast.splat, %wide.load
  %22 = sub <2 x i64> %broadcast.splat, %wide.load58
  %23 = select <2 x i1> %19, <2 x i64> %21, <2 x i64> zeroinitializer
  %24 = select <2 x i1> %20, <2 x i64> %22, <2 x i64> zeroinitializer
  %25 = add <2 x i64> %vec.phi, %23
  %26 = add <2 x i64> %vec.phi57, %24
  %index.next = add nuw i64 %index, 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
  %bin.rdx = add <2 x i64> %26, %25
  %28 = tail call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  br i1 %cmp.n, label %._crit_edge18.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph17.us
  %indvars.iv39.ph = phi i64 [ 0, %.lr.ph17.us ], [ %n.vec, %middle.block ]
  %.ph = phi i64 [ 0, %.lr.ph17.us ], [ %28, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv39 = phi i64 [ %indvars.iv.next40, %scalar.ph ], [ %indvars.iv39.ph, %scalar.ph.preheader ]
  %29 = phi i64 [ %35, %scalar.ph ], [ %.ph, %scalar.ph.preheader ]
  %30 = getelementptr inbounds [101 x i64], ptr @num, i64 0, i64 %indvars.iv39
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = icmp slt i64 %31, %16
  %33 = sub i64 %16, %31
  %34 = select i1 %32, i64 %33, i64 0
  %35 = add nsw i64 %29, %34
  %indvars.iv.next40 = add nuw nsw i64 %indvars.iv39, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next40, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge18.us, label %scalar.ph, !llvm.loop !16

._crit_edge18.us:                                 ; preds = %scalar.ph, %middle.block
  %.lcssa = phi i64 [ %28, %middle.block ], [ %35, %scalar.ph ]
  %.not.us = icmp sgt i64 %.lcssa, %16
  %r.l = select i1 %.not.us, ptr @r, ptr @l
  %. = select i1 %.not.us, i64 %16, i64 %14
  %.48 = select i1 %.not.us, i64 %13, i64 %16
  store i64 %16, ptr %r.l, align 8, !tbaa !11
  %36 = add nsw i64 %.48, 1
  %37 = icmp slt i64 %36, %.
  br i1 %37, label %.lr.ph17.us, label %.loopexit, !llvm.loop !17

.lr.ph23.split:                                   ; preds = %.lr.ph23, %.lr.ph23.split
  %38 = phi i64 [ %.52, %.lr.ph23.split ], [ 0, %.lr.ph23 ]
  %39 = phi i64 [ %.51, %.lr.ph23.split ], [ 2147483647, %.lr.ph23 ]
  %40 = add nsw i64 %39, %38
  %41 = ashr i64 %40, 1
  %.not = icmp slt i64 %41, 0
  %r.l50 = select i1 %.not, ptr @r, ptr @l
  %.51 = select i1 %.not, i64 %41, i64 %39
  %.52 = select i1 %.not, i64 %38, i64 %41
  store i64 %41, ptr %r.l50, align 8, !tbaa !11
  %42 = add nsw i64 %.52, 1
  %43 = icmp slt i64 %42, %.51
  br i1 %43, label %.lr.ph23.split, label %.loopexit, !llvm.loop !17

.loopexit:                                        ; preds = %.lr.ph23.split, %._crit_edge18.us
  %.us-phi = phi i64 [ %.lcssa, %._crit_edge18.us ], [ 0, %.lr.ph23.split ]
  %.us-phi29 = phi i64 [ %16, %._crit_edge18.us ], [ %41, %.lr.ph23.split ]
  %.us-phi30 = phi i64 [ %.48, %._crit_edge18.us ], [ %.52, %.lr.ph23.split ]
  store i64 %.us-phi29, ptr @mid, align 8, !tbaa !11
  store i64 %.us-phi, ptr @sum, align 8, !tbaa !11
  %44 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %.us-phi30)
  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 @_ZSt4sortIPxEvT_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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v2i64(<2 x i64>) #3

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" }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !15}
!14 = !{!"llvm.loop.isvectorized", i32 1}
!15 = !{!"llvm.loop.unroll.runtime.disable"}
!16 = distinct !{!16, !10, !15, !14}
!17 = distinct !{!17, !10}


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