; LLVM IR from tangjz/acm-icpc :: 51nod (1163)

; 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.std::pair" = type { i32, i32 }
%"struct.std::greater" = type { i8 }

@n = external dso_local global i32, align 4
@fa = external dso_local global [50001 x i32], align 16
@ans = external dso_local global i64, align 8
@a = external dso_local global [50001 x %"struct.std::pair"], 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
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z4findi(i32 noundef) #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 %"struct.std::greater", align 1
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #6
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %22, %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) #6
  br label %25

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

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

25:                                               ; preds = %11
  %26 = load i32, ptr @n, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %"struct.std::pair", ptr @a, i64 %27
  call void @_ZSt4sortIPSt4pairIiiESt7greaterIS1_EEvT_S5_T0_(ptr noundef @a, ptr noundef %28)
  %29 = load i32, ptr @n, align 4, !tbaa !5
  %30 = add nsw i32 %29, 1
  %31 = sext i32 %30 to i64
  %32 = mul i64 %31, 4
  call void @llvm.memset.p0.i64(ptr align 16 @fa, i8 -1, i64 %32, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #6
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %81, %25
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = load i32, ptr @n, align 4, !tbaa !5
  %36 = icmp slt i32 %34, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #6
  br label %84

38:                                               ; preds = %33
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [50001 x %"struct.std::pair"], ptr @a, i64 0, i64 %40
  %42 = getelementptr inbounds %"struct.std::pair", ptr %41, i32 0, i32 1
  %43 = load i32, ptr %42, align 4, !tbaa !11
  %44 = load i32, ptr @n, align 4, !tbaa !5
  %45 = icmp sgt i32 %43, %44
  br i1 %45, label %46, label %55

46:                                               ; preds = %38
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [50001 x %"struct.std::pair"], ptr @a, i64 0, i64 %48
  %50 = getelementptr inbounds %"struct.std::pair", ptr %49, i32 0, i32 0
  %51 = load i32, ptr %50, align 8, !tbaa !13
  %52 = sext i32 %51 to i64
  %53 = load i64, ptr @ans, align 8, !tbaa !14
  %54 = add nsw i64 %53, %52
  store i64 %54, ptr @ans, align 8, !tbaa !14
  br label %80

55:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #6
  %56 = load i32, ptr %4, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [50001 x %"struct.std::pair"], ptr @a, i64 0, i64 %57
  %59 = getelementptr inbounds %"struct.std::pair", ptr %58, i32 0, i32 1
  %60 = load i32, ptr %59, align 4, !tbaa !11
  %61 = call noundef i32 @_Z4findi(i32 noundef %60)
  store i32 %61, ptr %5, align 4, !tbaa !5
  %62 = load i32, ptr %5, align 4, !tbaa !5
  %63 = icmp sgt i32 %62, 0
  br i1 %63, label %64, label %79

64:                                               ; preds = %55
  %65 = load i32, ptr %4, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [50001 x %"struct.std::pair"], ptr @a, i64 0, i64 %66
  %68 = getelementptr inbounds %"struct.std::pair", ptr %67, i32 0, i32 0
  %69 = load i32, ptr %68, align 8, !tbaa !13
  %70 = sext i32 %69 to i64
  %71 = load i64, ptr @ans, align 8, !tbaa !14
  %72 = add nsw i64 %71, %70
  store i64 %72, ptr @ans, align 8, !tbaa !14
  %73 = load i32, ptr %5, align 4, !tbaa !5
  %74 = sub nsw i32 %73, 1
  %75 = call noundef i32 @_Z4findi(i32 noundef %74)
  %76 = load i32, ptr %5, align 4, !tbaa !5
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds [50001 x i32], ptr @fa, i64 0, i64 %77
  store i32 %75, ptr %78, align 4, !tbaa !5
  br label %79

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

80:                                               ; preds = %79, %46
  br label %81

81:                                               ; preds = %80
  %82 = load i32, ptr %4, align 4, !tbaa !5
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %4, align 4, !tbaa !5
  br label %33, !llvm.loop !16

84:                                               ; preds = %37
  %85 = load i64, ptr @ans, align 8, !tbaa !14
  %86 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %85)
  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 @_ZSt4sortIPSt4pairIiiESt7greaterIS1_EEvT_S5_T0_(ptr noundef, ptr noundef) #4

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #5

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

attributes #0 = { 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 #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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #6 = { 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 = !{!"_ZTSSt4pairIiiE", !6, i64 0, !6, i64 4}
!13 = !{!12, !6, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"long long", !7, i64 0}
!16 = distinct !{!16, !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.std::pair" = type { i32, i32 }

@n = external dso_local global i32, align 4
@fa = external dso_local local_unnamed_addr global [50001 x i32], align 16
@ans = external dso_local local_unnamed_addr global i64, align 8
@a = external dso_local global [50001 x %"struct.std::pair"], 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
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z4findi(i32 noundef) local_unnamed_addr #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 ], [ %14, %.lr.ph ]
  %4 = getelementptr inbounds %"struct.std::pair", ptr @a, i64 %.pre-phi
  tail call void @_ZSt4sortIPSt4pairIiiESt7greaterIS1_EEvT_S5_T0_(ptr noundef nonnull @a, ptr noundef nonnull %4)
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %6 = add nsw i32 %5, 1
  %7 = sext i32 %6 to i64
  %8 = shl nsw i64 %7, 2
  tail call void @llvm.memset.p0.i64(ptr nonnull align 16 @fa, i8 -1, i64 %8, i1 false)
  %9 = icmp sgt i32 %5, 0
  br i1 %9, label %.lr.ph21, label %._crit_edge22

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

._crit_edge22:                                    ; preds = %40, %._crit_edge
  %16 = load i64, ptr @ans, align 8, !tbaa !11
  %17 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %16)
  ret i32 0

.lr.ph21:                                         ; preds = %._crit_edge, %40
  %indvars.iv25 = phi i64 [ %indvars.iv.next26, %40 ], [ 0, %._crit_edge ]
  %18 = phi i32 [ %41, %40 ], [ %5, %._crit_edge ]
  %19 = getelementptr inbounds [50001 x %"struct.std::pair"], ptr @a, i64 0, i64 %indvars.iv25
  %20 = getelementptr inbounds i8, ptr %19, i64 4
  %21 = load i32, ptr %20, align 4, !tbaa !13
  %22 = icmp sgt i32 %21, %18
  br i1 %22, label %23, label %28

23:                                               ; preds = %.lr.ph21
  %24 = load i32, ptr %19, align 8, !tbaa !15
  %25 = sext i32 %24 to i64
  %26 = load i64, ptr @ans, align 8, !tbaa !11
  %27 = add nsw i64 %26, %25
  store i64 %27, ptr @ans, align 8, !tbaa !11
  br label %40

28:                                               ; preds = %.lr.ph21
  %29 = tail call noundef i32 @_Z4findi(i32 noundef %21)
  %30 = icmp sgt i32 %29, 0
  br i1 %30, label %31, label %40

31:                                               ; preds = %28
  %32 = load i32, ptr %19, align 8, !tbaa !15
  %33 = sext i32 %32 to i64
  %34 = load i64, ptr @ans, align 8, !tbaa !11
  %35 = add nsw i64 %34, %33
  store i64 %35, ptr @ans, align 8, !tbaa !11
  %36 = add nsw i32 %29, -1
  %37 = tail call noundef i32 @_Z4findi(i32 noundef %36)
  %38 = zext nneg i32 %29 to i64
  %39 = getelementptr inbounds [50001 x i32], ptr @fa, i64 0, i64 %38
  store i32 %37, ptr %39, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %28, %31, %23
  %indvars.iv.next26 = add nuw nsw i64 %indvars.iv25, 1
  %41 = load i32, ptr @n, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = icmp slt i64 %indvars.iv.next26, %42
  br i1 %43, label %.lr.ph21, label %._crit_edge22, !llvm.loop !16
}

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

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

attributes #0 = { 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 #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" }
attributes #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!14, !6, i64 4}
!14 = !{!"_ZTSSt4pairIiiE", !6, i64 0, !6, i64 4}
!15 = !{!14, !6, i64 0}
!16 = distinct !{!16, !10}


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