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

; 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"

%struct.Node = type { i32, i32 }

@n = external dso_local global i32, align 4
@sum = external dso_local global i32, align 4
@ans = external dso_local global i32, align 4
@cow = external dso_local global [50001 x %struct.Node], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z3cmpRK4NodeS1_(ptr noundef nonnull align 4 dereferenceable(8), ptr noundef nonnull align 4 dereferenceable(8)) #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #1 {
  %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) #5
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %20, %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) #5
  br label %23

10:                                               ; preds = %5
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [50001 x %struct.Node], ptr @cow, i64 0, i64 %12
  %14 = getelementptr inbounds %struct.Node, ptr %13, i32 0, i32 0
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [50001 x %struct.Node], ptr @cow, i64 0, i64 %16
  %18 = getelementptr inbounds %struct.Node, ptr %17, i32 0, i32 1
  %19 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %14, ptr noundef %18)
  br label %20

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

23:                                               ; preds = %9
  %24 = load i32, ptr @n, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds %struct.Node, ptr @cow, i64 %25
  call void @_ZSt4sortIP4NodePFbRKS0_S3_EEvT_S6_T0_(ptr noundef @cow, ptr noundef %26, ptr noundef @_Z3cmpRK4NodeS1_)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %58, %23
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = load i32, ptr @n, align 4, !tbaa !5
  %30 = icmp slt i32 %28, %29
  br i1 %30, label %32, label %31

31:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  br label %61

32:                                               ; preds = %27
  %33 = load i32, ptr @ans, align 4, !tbaa !5
  %34 = load i32, ptr @sum, align 4, !tbaa !5
  %35 = load i32, ptr %3, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [50001 x %struct.Node], ptr @cow, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.Node, ptr %37, i32 0, i32 1
  %39 = load i32, ptr %38, align 4, !tbaa !11
  %40 = sub nsw i32 %34, %39
  %41 = icmp slt i32 %33, %40
  br i1 %41, label %42, label %50

42:                                               ; preds = %32
  %43 = load i32, ptr @sum, align 4, !tbaa !5
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [50001 x %struct.Node], ptr @cow, i64 0, i64 %45
  %47 = getelementptr inbounds %struct.Node, ptr %46, i32 0, i32 1
  %48 = load i32, ptr %47, align 4, !tbaa !11
  %49 = sub nsw i32 %43, %48
  store i32 %49, ptr @ans, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %42, %32
  %51 = load i32, ptr %3, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [50001 x %struct.Node], ptr @cow, i64 0, i64 %52
  %54 = getelementptr inbounds %struct.Node, ptr %53, i32 0, i32 0
  %55 = load i32, ptr %54, align 8, !tbaa !13
  %56 = load i32, ptr @sum, align 4, !tbaa !5
  %57 = add nsw i32 %56, %55
  store i32 %57, ptr @sum, align 4, !tbaa !5
  br label %58

58:                                               ; preds = %50
  %59 = load i32, ptr %3, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %3, align 4, !tbaa !5
  br label %27, !llvm.loop !14

61:                                               ; preds = %31
  %62 = load i32, ptr @ans, align 4, !tbaa !5
  %63 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %62)
  ret i32 0
}

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

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

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

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

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

attributes #0 = { 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 #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 = { "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 willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !6, i64 4}
!12 = !{!"_ZTS4Node", !6, i64 0, !6, i64 4}
!13 = !{!12, !6, i64 0}
!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"

%struct.Node = type { i32, i32 }

@n = external dso_local global i32, align 4
@sum = external dso_local local_unnamed_addr global i32, align 4
@ans = external dso_local local_unnamed_addr global i32, align 4
@cow = external dso_local global [50001 x %struct.Node], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z3cmpRK4NodeS1_(ptr noundef nonnull align 4 dereferenceable(8), ptr noundef nonnull align 4 dereferenceable(8)) #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 %.._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 ], [ %12, %.lr.ph ]
  %4 = getelementptr inbounds %struct.Node, ptr @cow, i64 %.pre-phi
  tail call void @_ZSt4sortIP4NodePFbRKS0_S3_EEvT_S6_T0_(ptr noundef nonnull @cow, ptr noundef nonnull %4, ptr noundef nonnull @_Z3cmpRK4NodeS1_)
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %ans.promoted = load i32, ptr @ans, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph15.preheader, label %23

.lr.ph15.preheader:                               ; preds = %._crit_edge
  %sum.promoted = load i32, ptr @sum, align 4, !tbaa !5
  %wide.trip.count = zext nneg i32 %5 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %7 = icmp eq i32 %5, 1
  br i1 %7, label %._crit_edge16.unr-lcssa, label %.lr.ph15.preheader.new

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

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

._crit_edge16.unr-lcssa:                          ; preds = %43, %.lr.ph15.preheader
  %.lcssa28.ph = phi i32 [ poison, %.lr.ph15.preheader ], [ %44, %43 ]
  %.lcssa.ph = phi i32 [ poison, %.lr.ph15.preheader ], [ %46, %43 ]
  %indvars.iv21.unr = phi i64 [ 0, %.lr.ph15.preheader ], [ %indvars.iv.next22.1, %43 ]
  %.unr = phi i32 [ %ans.promoted, %.lr.ph15.preheader ], [ %44, %43 ]
  %.unr30 = phi i32 [ %sum.promoted, %.lr.ph15.preheader ], [ %46, %43 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge16, label %.lr.ph15.epil

.lr.ph15.epil:                                    ; preds = %._crit_edge16.unr-lcssa
  %14 = getelementptr inbounds [50001 x %struct.Node], ptr @cow, i64 0, i64 %indvars.iv21.unr
  %15 = getelementptr inbounds i8, ptr %14, i64 4
  %16 = load i32, ptr %15, align 4, !tbaa !11
  %17 = sub nsw i32 %.unr30, %16
  %18 = icmp slt i32 %.unr, %17
  br i1 %18, label %19, label %._crit_edge16.epilog-lcssa

19:                                               ; preds = %.lr.ph15.epil
  store i32 %17, ptr @ans, align 4, !tbaa !5
  br label %._crit_edge16.epilog-lcssa

._crit_edge16.epilog-lcssa:                       ; preds = %19, %.lr.ph15.epil
  %20 = phi i32 [ %17, %19 ], [ %.unr, %.lr.ph15.epil ]
  %21 = load i32, ptr %14, align 8, !tbaa !13
  %22 = add nsw i32 %21, %.unr30
  br label %._crit_edge16

._crit_edge16:                                    ; preds = %._crit_edge16.unr-lcssa, %._crit_edge16.epilog-lcssa
  %.lcssa28 = phi i32 [ %.lcssa28.ph, %._crit_edge16.unr-lcssa ], [ %20, %._crit_edge16.epilog-lcssa ]
  %.lcssa = phi i32 [ %.lcssa.ph, %._crit_edge16.unr-lcssa ], [ %22, %._crit_edge16.epilog-lcssa ]
  store i32 %.lcssa, ptr @sum, align 4, !tbaa !5
  br label %23

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

.lr.ph15:                                         ; preds = %43, %.lr.ph15.preheader.new
  %indvars.iv21 = phi i64 [ 0, %.lr.ph15.preheader.new ], [ %indvars.iv.next22.1, %43 ]
  %26 = phi i32 [ %ans.promoted, %.lr.ph15.preheader.new ], [ %44, %43 ]
  %27 = phi i32 [ %sum.promoted, %.lr.ph15.preheader.new ], [ %46, %43 ]
  %niter = phi i64 [ 0, %.lr.ph15.preheader.new ], [ %niter.next.1, %43 ]
  %28 = getelementptr inbounds [50001 x %struct.Node], ptr @cow, i64 0, i64 %indvars.iv21
  %29 = getelementptr inbounds i8, ptr %28, i64 4
  %30 = load i32, ptr %29, align 4, !tbaa !11
  %31 = sub nsw i32 %27, %30
  %32 = icmp slt i32 %26, %31
  br i1 %32, label %33, label %.lr.ph15.1

33:                                               ; preds = %.lr.ph15
  store i32 %31, ptr @ans, align 4, !tbaa !5
  br label %.lr.ph15.1

.lr.ph15.1:                                       ; preds = %33, %.lr.ph15
  %34 = phi i32 [ %31, %33 ], [ %26, %.lr.ph15 ]
  %35 = load i32, ptr %28, align 16, !tbaa !13
  %36 = add nsw i32 %35, %27
  %indvars.iv.next22 = or disjoint i64 %indvars.iv21, 1
  %37 = getelementptr inbounds [50001 x %struct.Node], ptr @cow, i64 0, i64 %indvars.iv.next22
  %38 = getelementptr inbounds i8, ptr %37, i64 4
  %39 = load i32, ptr %38, align 4, !tbaa !11
  %40 = sub nsw i32 %36, %39
  %41 = icmp slt i32 %34, %40
  br i1 %41, label %42, label %43

42:                                               ; preds = %.lr.ph15.1
  store i32 %40, ptr @ans, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %42, %.lr.ph15.1
  %44 = phi i32 [ %40, %42 ], [ %34, %.lr.ph15.1 ]
  %45 = load i32, ptr %37, align 8, !tbaa !13
  %46 = add nsw i32 %45, %36
  %indvars.iv.next22.1 = add nuw nsw i64 %indvars.iv21, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge16.unr-lcssa, label %.lr.ph15, !llvm.loop !14
}

; 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 @_ZSt4sortIP4NodePFbRKS0_S3_EEvT_S6_T0_(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

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

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

!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, !6, i64 4}
!12 = !{!"_ZTS4Node", !6, i64 0, !6, i64 4}
!13 = !{!12, !6, i64 0}
!14 = distinct !{!14, !10}


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