; LLVM IR from tangjz/acm-icpc :: codeforces/complete (492C)

; 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
@r = external dso_local global i32, align 4
@avg = external dso_local global i32, align 4
@res = external dso_local global i64, align 8
@ans = external dso_local global i64, align 8
@e = external dso_local global [233333 x %struct.Node], align 16
@.str = external hidden unnamed_addr constant [7 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 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 i64, align 8
  %5 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n, ptr noundef @r, ptr noundef @avg)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %7

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

11:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  br label %44

12:                                               ; preds = %7
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [233333 x %struct.Node], ptr @e, i64 0, i64 %14
  %16 = getelementptr inbounds %struct.Node, ptr %15, i32 0, i32 0
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [233333 x %struct.Node], ptr @e, i64 0, i64 %18
  %20 = getelementptr inbounds %struct.Node, ptr %19, i32 0, i32 1
  %21 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %16, ptr noundef %20)
  %22 = load i32, ptr @avg, align 4, !tbaa !5
  %23 = load i32, ptr %2, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [233333 x %struct.Node], ptr @e, i64 0, i64 %24
  %26 = getelementptr inbounds %struct.Node, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 8, !tbaa !9
  %28 = sub nsw i32 %22, %27
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [233333 x %struct.Node], ptr @e, i64 0, i64 %30
  %32 = getelementptr inbounds %struct.Node, ptr %31, i32 0, i32 0
  store i32 %28, ptr %32, align 8, !tbaa !9
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [233333 x %struct.Node], ptr @e, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.Node, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 8, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = load i64, ptr @res, align 8, !tbaa !11
  %40 = add nsw i64 %39, %38
  store i64 %40, ptr @res, align 8, !tbaa !11
  br label %41

41:                                               ; preds = %12
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %2, align 4, !tbaa !5
  br label %7, !llvm.loop !13

44:                                               ; preds = %11
  %45 = load i32, ptr @n, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds %struct.Node, ptr @e, i64 %46
  call void @_ZSt4sortIP4NodeEvT_S2_(ptr noundef @e, ptr noundef %47)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %48

48:                                               ; preds = %98, %44
  %49 = load i32, ptr %3, align 4, !tbaa !5
  %50 = load i32, ptr @n, align 4, !tbaa !5
  %51 = icmp slt i32 %49, %50
  br i1 %51, label %52, label %55

52:                                               ; preds = %48
  %53 = load i64, ptr @res, align 8, !tbaa !11
  %54 = icmp sgt i64 %53, 0
  br label %55

55:                                               ; preds = %52, %48
  %56 = phi i1 [ false, %48 ], [ %54, %52 ]
  br i1 %56, label %58, label %57

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

58:                                               ; preds = %55
  %59 = load i32, ptr %3, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [233333 x %struct.Node], ptr @e, i64 0, i64 %60
  %62 = getelementptr inbounds %struct.Node, ptr %61, i32 0, i32 0
  %63 = load i32, ptr %62, align 8, !tbaa !9
  %64 = load i32, ptr @r, align 4, !tbaa !5
  %65 = add nsw i32 %63, %64
  %66 = load i32, ptr @avg, align 4, !tbaa !5
  %67 = sub nsw i32 %65, %66
  %68 = icmp sgt i32 %67, 0
  br i1 %68, label %69, label %97

69:                                               ; preds = %58
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %70 = load i32, ptr %3, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [233333 x %struct.Node], ptr @e, i64 0, i64 %71
  %73 = getelementptr inbounds %struct.Node, ptr %72, i32 0, i32 0
  %74 = load i32, ptr %73, align 8, !tbaa !9
  %75 = sext i32 %74 to i64
  %76 = load i32, ptr @r, align 4, !tbaa !5
  %77 = sext i32 %76 to i64
  %78 = add nsw i64 %75, %77
  %79 = load i32, ptr @avg, align 4, !tbaa !5
  %80 = sext i32 %79 to i64
  %81 = sub nsw i64 %78, %80
  store i64 %81, ptr %5, align 8, !tbaa !11
  %82 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) @res, ptr noundef nonnull align 8 dereferenceable(8) %5)
  %83 = load i64, ptr %82, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  store i64 %83, ptr %4, align 8, !tbaa !11
  %84 = load i64, ptr %4, align 8, !tbaa !11
  %85 = load i64, ptr @res, align 8, !tbaa !11
  %86 = sub nsw i64 %85, %84
  store i64 %86, ptr @res, align 8, !tbaa !11
  %87 = load i64, ptr %4, align 8, !tbaa !11
  %88 = load i32, ptr %3, align 4, !tbaa !5
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [233333 x %struct.Node], ptr @e, i64 0, i64 %89
  %91 = getelementptr inbounds %struct.Node, ptr %90, i32 0, i32 1
  %92 = load i32, ptr %91, align 4, !tbaa !15
  %93 = sext i32 %92 to i64
  %94 = mul nsw i64 %87, %93
  %95 = load i64, ptr @ans, align 8, !tbaa !11
  %96 = add nsw i64 %95, %94
  store i64 %96, ptr @ans, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  br label %97

97:                                               ; preds = %69, %58
  br label %98

98:                                               ; preds = %97
  %99 = load i32, ptr %3, align 4, !tbaa !5
  %100 = add nsw i32 %99, 1
  store i32 %100, ptr %3, align 4, !tbaa !5
  br label %48, !llvm.loop !16

101:                                              ; preds = %57
  %102 = load i64, ptr @ans, align 8, !tbaa !11
  %103 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %102)
  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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #4

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


; 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
@r = external dso_local global i32, align 4
@avg = external dso_local global i32, align 4
@res = external dso_local global i64, align 8
@ans = external dso_local local_unnamed_addr global i64, align 8
@e = external dso_local global [233333 x %struct.Node], align 16
@.str = external hidden unnamed_addr constant [7 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i64, align 8
  %2 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n, ptr noundef nonnull @r, ptr noundef nonnull @avg)
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %.._crit_edge_crit_edge

.._crit_edge_crit_edge:                           ; preds = %0
  %.pre30 = sext i32 %3 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.._crit_edge_crit_edge
  %.pre-phi = phi i64 [ %.pre30, %.._crit_edge_crit_edge ], [ %21, %.lr.ph ]
  %5 = getelementptr inbounds %struct.Node, ptr @e, i64 %.pre-phi
  tail call void @_ZSt4sortIP4NodeEvT_S2_(ptr noundef nonnull @e, ptr noundef nonnull %5)
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = icmp sgt i32 %6, 0
  %8 = load i64, ptr @res, align 8
  %9 = icmp sgt i64 %8, 0
  %10 = select i1 %7, i1 %9, i1 false
  br i1 %10, label %.lr.ph19.preheader, label %._crit_edge20

.lr.ph19.preheader:                               ; preds = %._crit_edge
  %.pre26 = load i32, ptr @r, align 4, !tbaa !5
  %.pre28 = load i32, ptr @avg, align 4, !tbaa !5
  br label %.lr.ph19

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %11 = getelementptr inbounds [233333 x %struct.Node], ptr @e, i64 0, i64 %indvars.iv
  %12 = getelementptr inbounds i8, ptr %11, i64 4
  %13 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %11, ptr noundef nonnull %12)
  %14 = load i32, ptr @avg, align 4, !tbaa !5
  %15 = load i32, ptr %11, align 8, !tbaa !9
  %16 = sub nsw i32 %14, %15
  store i32 %16, ptr %11, align 8, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = load i64, ptr @res, align 8, !tbaa !11
  %19 = add nsw i64 %18, %17
  store i64 %19, ptr @res, align 8, !tbaa !11
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %20 = load i32, ptr @n, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = icmp slt i64 %indvars.iv.next, %21
  br i1 %22, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge20:                                    ; preds = %49, %._crit_edge
  %23 = load i64, ptr @ans, align 8, !tbaa !11
  %24 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %23)
  ret i32 0

.lr.ph19:                                         ; preds = %.lr.ph19.preheader, %49
  %25 = phi i64 [ %8, %.lr.ph19.preheader ], [ %50, %49 ]
  %26 = phi i32 [ %6, %.lr.ph19.preheader ], [ %51, %49 ]
  %27 = phi i32 [ %.pre28, %.lr.ph19.preheader ], [ %52, %49 ]
  %28 = phi i32 [ %.pre26, %.lr.ph19.preheader ], [ %53, %49 ]
  %indvars.iv23 = phi i64 [ 0, %.lr.ph19.preheader ], [ %indvars.iv.next24, %49 ]
  %29 = getelementptr inbounds [233333 x %struct.Node], ptr @e, i64 0, i64 %indvars.iv23
  %30 = load i32, ptr %29, align 8, !tbaa !9
  %31 = add nsw i32 %28, %30
  %32 = icmp sgt i32 %31, %27
  br i1 %32, label %33, label %49

33:                                               ; preds = %.lr.ph19
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #5
  %34 = sext i32 %30 to i64
  %35 = sext i32 %28 to i64
  %36 = add nsw i64 %35, %34
  %37 = sext i32 %27 to i64
  %38 = sub nsw i64 %36, %37
  store i64 %38, ptr %1, align 8, !tbaa !11
  %39 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) @res, ptr noundef nonnull align 8 dereferenceable(8) %1)
  %40 = load i64, ptr %39, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #5
  %41 = load i64, ptr @res, align 8, !tbaa !11
  %42 = sub nsw i64 %41, %40
  store i64 %42, ptr @res, align 8, !tbaa !11
  %43 = getelementptr inbounds i8, ptr %29, i64 4
  %44 = load i32, ptr %43, align 4, !tbaa !15
  %45 = sext i32 %44 to i64
  %46 = mul nsw i64 %40, %45
  %47 = load i64, ptr @ans, align 8, !tbaa !11
  %48 = add nsw i64 %46, %47
  store i64 %48, ptr @ans, align 8, !tbaa !11
  %.pre = load i32, ptr @r, align 4, !tbaa !5
  %.pre27 = load i32, ptr @avg, align 4, !tbaa !5
  %.pre29 = load i32, ptr @n, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %.lr.ph19, %33
  %50 = phi i64 [ %25, %.lr.ph19 ], [ %42, %33 ]
  %51 = phi i32 [ %26, %.lr.ph19 ], [ %.pre29, %33 ]
  %52 = phi i32 [ %27, %.lr.ph19 ], [ %.pre27, %33 ]
  %53 = phi i32 [ %28, %.lr.ph19 ], [ %.pre, %33 ]
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, 1
  %54 = sext i32 %51 to i64
  %55 = icmp slt i64 %indvars.iv.next24, %54
  %56 = icmp sgt i64 %50, 0
  %57 = select i1 %55, i1 %56, i1 false
  br i1 %57, label %.lr.ph19, label %._crit_edge20, !llvm.loop !16
}

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

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

; Function Attrs: mustprogress 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) local_unnamed_addr #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #4

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


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