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

; 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 { i64, i64 }

@n = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@ans = external dso_local global i64, align 8
@s = external dso_local global [1001 x %struct.Node], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 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 i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  br label %6

6:                                                ; preds = %80, %0
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n, ptr noundef @k)
  %8 = icmp eq i32 %7, 2
  br i1 %8, label %9, label %83

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

10:                                               ; preds = %21, %9
  %11 = load i32, ptr %2, align 4, !tbaa !9
  %12 = load i32, ptr @n, align 4, !tbaa !9
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  br label %24

15:                                               ; preds = %10
  %16 = load i32, ptr %2, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [1001 x %struct.Node], ptr @s, i64 0, i64 %17
  %19 = getelementptr inbounds %struct.Node, ptr %18, i32 0, i32 0
  %20 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %19)
  br label %21

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

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

25:                                               ; preds = %36, %24
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = load i32, ptr @n, align 4, !tbaa !9
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %39

30:                                               ; preds = %25
  %31 = load i32, ptr %3, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [1001 x %struct.Node], ptr @s, i64 0, i64 %32
  %34 = getelementptr inbounds %struct.Node, ptr %33, i32 0, i32 1
  %35 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %34)
  br label %36

36:                                               ; preds = %30
  %37 = load i32, ptr %3, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %3, align 4, !tbaa !9
  br label %25, !llvm.loop !13

39:                                               ; preds = %29
  %40 = load i32, ptr @n, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds %struct.Node, ptr @s, i64 %41
  call void @_ZSt4sortIP4NodeEvT_S2_(ptr noundef @s, ptr noundef %42)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %76, %39
  %44 = load i32, ptr %4, align 4, !tbaa !9
  %45 = load i32, ptr @n, align 4, !tbaa !9
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %48, label %47

47:                                               ; preds = %43
  store i32 10, ptr %5, align 4
  br label %79

48:                                               ; preds = %43
  %49 = load i32, ptr %4, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [1001 x %struct.Node], ptr @s, i64 0, i64 %50
  %52 = getelementptr inbounds %struct.Node, ptr %51, i32 0, i32 0
  %53 = load i64, ptr %52, align 16, !tbaa !14
  %54 = load i32, ptr @k, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = icmp sle i64 %53, %55
  br i1 %56, label %57, label %74

57:                                               ; preds = %48
  %58 = load i32, ptr %4, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [1001 x %struct.Node], ptr @s, i64 0, i64 %59
  %61 = getelementptr inbounds %struct.Node, ptr %60, i32 0, i32 0
  %62 = load i64, ptr %61, align 16, !tbaa !14
  %63 = load i32, ptr @k, align 4, !tbaa !9
  %64 = sext i32 %63 to i64
  %65 = sub nsw i64 %64, %62
  %66 = trunc i64 %65 to i32
  store i32 %66, ptr @k, align 4, !tbaa !9
  %67 = load i32, ptr %4, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [1001 x %struct.Node], ptr @s, i64 0, i64 %68
  %70 = getelementptr inbounds %struct.Node, ptr %69, i32 0, i32 1
  %71 = load i64, ptr %70, align 8, !tbaa !16
  %72 = load i64, ptr @ans, align 8, !tbaa !5
  %73 = add nsw i64 %72, %71
  store i64 %73, ptr @ans, align 8, !tbaa !5
  br label %75

74:                                               ; preds = %48
  store i32 10, ptr %5, align 4
  br label %79

75:                                               ; preds = %57
  br label %76

76:                                               ; preds = %75
  %77 = load i32, ptr %4, align 4, !tbaa !9
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %4, align 4, !tbaa !9
  br label %43, !llvm.loop !17

79:                                               ; preds = %74, %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %80

80:                                               ; preds = %79
  %81 = load i64, ptr @ans, align 8, !tbaa !5
  %82 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %81)
  br label %6, !llvm.loop !18

83:                                               ; preds = %6
  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 @_ZSt4sortIP4NodeEvT_S2_(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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = !{!15, !6, i64 0}
!15 = !{!"_ZTS4Node", !6, i64 0, !6, i64 8}
!16 = !{!15, !6, i64 8}
!17 = distinct !{!17, !12}
!18 = distinct !{!18, !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"

%struct.Node = type { i64, i64 }

@n = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@ans = external dso_local local_unnamed_addr global i64, align 8
@s = external dso_local global [1001 x %struct.Node], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 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, ptr noundef nonnull @k)
  %2 = icmp eq i32 %1, 2
  br i1 %2, label %.lr.ph25, label %._crit_edge26

.lr.ph25:                                         ; preds = %0, %._crit_edge22
  store i64 0, ptr @ans, align 8, !tbaa !5
  %3 = load i32, ptr @n, align 4, !tbaa !9
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %.preheader.._crit_edge_crit_edge

.preheader:                                       ; preds = %.lr.ph
  %5 = icmp sgt i32 %9, 0
  br i1 %5, label %.lr.ph18, label %.preheader.._crit_edge_crit_edge

.preheader.._crit_edge_crit_edge:                 ; preds = %.lr.ph25, %.preheader
  %6 = phi i32 [ %9, %.preheader ], [ %3, %.lr.ph25 ]
  %.pre = sext i32 %6 to i64
  br label %._crit_edge

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

._crit_edge:                                      ; preds = %.lr.ph18, %.preheader.._crit_edge_crit_edge
  %.pre-phi = phi i64 [ %.pre, %.preheader.._crit_edge_crit_edge ], [ %18, %.lr.ph18 ]
  %12 = getelementptr inbounds %struct.Node, ptr @s, i64 %.pre-phi
  tail call void @_ZSt4sortIP4NodeEvT_S2_(ptr noundef nonnull @s, ptr noundef nonnull %12)
  %13 = load i32, ptr @n, align 4, !tbaa !9
  %ans.promoted = load i64, ptr @ans, align 8, !tbaa !5
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %.lr.ph21.preheader, label %._crit_edge22

.lr.ph21.preheader:                               ; preds = %._crit_edge
  %k.promoted = load i32, ptr @k, align 4, !tbaa !9
  %wide.trip.count = zext nneg i32 %13 to i64
  br label %.lr.ph21

.lr.ph18:                                         ; preds = %.preheader, %.lr.ph18
  %indvars.iv29 = phi i64 [ %indvars.iv.next30, %.lr.ph18 ], [ 0, %.preheader ]
  %15 = getelementptr inbounds [1001 x %struct.Node], ptr @s, i64 0, i64 %indvars.iv29, i32 1
  %16 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %15)
  %indvars.iv.next30 = add nuw nsw i64 %indvars.iv29, 1
  %17 = load i32, ptr @n, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = icmp slt i64 %indvars.iv.next30, %18
  br i1 %19, label %.lr.ph18, label %._crit_edge, !llvm.loop !13

.lr.ph21:                                         ; preds = %.lr.ph21.preheader, %25
  %indvars.iv32 = phi i64 [ 0, %.lr.ph21.preheader ], [ %indvars.iv.next33, %25 ]
  %20 = phi i32 [ %k.promoted, %.lr.ph21.preheader ], [ %27, %25 ]
  %21 = phi i64 [ %ans.promoted, %.lr.ph21.preheader ], [ %30, %25 ]
  %22 = getelementptr inbounds [1001 x %struct.Node], ptr @s, i64 0, i64 %indvars.iv32
  %23 = load i64, ptr %22, align 16, !tbaa !14
  %24 = sext i32 %20 to i64
  %.not = icmp sgt i64 %23, %24
  br i1 %.not, label %._crit_edge22, label %25

25:                                               ; preds = %.lr.ph21
  %26 = trunc i64 %23 to i32
  %27 = sub i32 %20, %26
  store i32 %27, ptr @k, align 4, !tbaa !9
  %28 = getelementptr inbounds i8, ptr %22, i64 8
  %29 = load i64, ptr %28, align 8, !tbaa !16
  %30 = add nsw i64 %21, %29
  store i64 %30, ptr @ans, align 8, !tbaa !5
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next33, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge22, label %.lr.ph21, !llvm.loop !17

._crit_edge22:                                    ; preds = %25, %.lr.ph21, %._crit_edge
  %31 = phi i64 [ %ans.promoted, %._crit_edge ], [ %30, %25 ], [ %21, %.lr.ph21 ]
  %32 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %31)
  %33 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n, ptr noundef nonnull @k)
  %34 = icmp eq i32 %33, 2
  br i1 %34, label %.lr.ph25, label %._crit_edge26, !llvm.loop !18

._crit_edge26:                                    ; preds = %._crit_edge22, %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 @_ZSt4sortIP4NodeEvT_S2_(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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = !{!15, !6, i64 0}
!15 = !{!"_ZTS4Node", !6, i64 0, !6, i64 8}
!16 = !{!15, !6, i64 8}
!17 = distinct !{!17, !12}
!18 = distinct !{!18, !12}


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