; LLVM IR from tangjz/acm-icpc :: self_training/2016-07-24 (B)

; 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::greater" = type { i8 }

@n = external dso_local global i32, align 4
@m = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@a = external dso_local global [100001 x i32], align 16
@ans = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 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 %"struct.std::greater", align 1
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %8 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @m)
  store i32 100001, ptr @q, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %20, %0
  %10 = load i32, ptr @m, align 4, !tbaa !5
  %11 = add nsw i32 %10, -1
  store i32 %11, ptr @m, align 4, !tbaa !5
  %12 = icmp ne i32 %10, 0
  br i1 %12, label %13, label %21

13:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  %14 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2)
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = load i32, ptr @q, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %18, label %20

18:                                               ; preds = %13
  %19 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %19, ptr @q, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %18, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  br label %9, !llvm.loop !9

21:                                               ; preds = %9
  %22 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %33, %21
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = load i32, ptr @n, align 4, !tbaa !5
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %28, label %27

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

28:                                               ; preds = %23
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i32, ptr @a, i64 %30
  %32 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %31)
  br label %33

33:                                               ; preds = %28
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %3, align 4, !tbaa !5
  br label %23, !llvm.loop !11

36:                                               ; preds = %27
  %37 = load i32, ptr @n, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i32, ptr @a, i64 %38
  call void @_ZSt4sortIPiSt7greaterIiEEvT_S3_T0_(ptr noundef @a, ptr noundef %39)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %72, %36
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = load i32, ptr @n, align 4, !tbaa !5
  %43 = icmp slt i32 %41, %42
  br i1 %43, label %45, label %44

44:                                               ; preds = %40
  store i32 7, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %77

45:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %68, %45
  %47 = load i32, ptr %7, align 4, !tbaa !5
  %48 = load i32, ptr @q, align 4, !tbaa !5
  %49 = icmp slt i32 %47, %48
  br i1 %49, label %50, label %56

50:                                               ; preds = %46
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = load i32, ptr %7, align 4, !tbaa !5
  %53 = add nsw i32 %51, %52
  %54 = load i32, ptr @n, align 4, !tbaa !5
  %55 = icmp slt i32 %53, %54
  br label %56

56:                                               ; preds = %50, %46
  %57 = phi i1 [ false, %46 ], [ %55, %50 ]
  br i1 %57, label %59, label %58

58:                                               ; preds = %56
  store i32 10, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %71

59:                                               ; preds = %56
  %60 = load i32, ptr %5, align 4, !tbaa !5
  %61 = load i32, ptr %7, align 4, !tbaa !5
  %62 = add nsw i32 %60, %61
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [100001 x i32], ptr @a, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = load i32, ptr @ans, align 4, !tbaa !5
  %67 = add nsw i32 %66, %65
  store i32 %67, ptr @ans, align 4, !tbaa !5
  br label %68

68:                                               ; preds = %59
  %69 = load i32, ptr %7, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %7, align 4, !tbaa !5
  br label %46, !llvm.loop !12

71:                                               ; preds = %58
  br label %72

72:                                               ; preds = %71
  %73 = load i32, ptr @q, align 4, !tbaa !5
  %74 = add nsw i32 %73, 2
  %75 = load i32, ptr %5, align 4, !tbaa !5
  %76 = add nsw i32 %75, %74
  store i32 %76, ptr %5, align 4, !tbaa !5
  br label %40, !llvm.loop !13

77:                                               ; preds = %44
  %78 = load i32, ptr @ans, align 4, !tbaa !5
  %79 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %78)
  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 @_ZSt4sortIPiSt7greaterIiEEvT_S3_T0_(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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !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
@m = external dso_local global i32, align 4
@q = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local global [100001 x i32], align 16
@ans = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @m)
  store i32 100001, ptr @q, align 4, !tbaa !5
  %3 = load i32, ptr @m, align 4, !tbaa !5
  %4 = add nsw i32 %3, -1
  store i32 %4, ptr @m, align 4, !tbaa !5
  %.not17 = icmp eq i32 %3, 0
  br i1 %.not17, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %10
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #5
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = load i32, ptr @q, align 4, !tbaa !5
  %8 = icmp slt i32 %6, %7
  br i1 %8, label %9, label %10

9:                                                ; preds = %.lr.ph
  store i32 %6, ptr @q, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %9, %.lr.ph
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #5
  %11 = load i32, ptr @m, align 4, !tbaa !5
  %12 = add nsw i32 %11, -1
  store i32 %12, ptr @m, align 4, !tbaa !5
  %.not = icmp eq i32 %11, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %10, %0
  %13 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %14 = load i32, ptr @n, align 4, !tbaa !5
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %.lr.ph20, label %._crit_edge.._crit_edge21_crit_edge

._crit_edge.._crit_edge21_crit_edge:              ; preds = %._crit_edge
  %.pre46 = sext i32 %14 to i64
  br label %._crit_edge21

._crit_edge21:                                    ; preds = %.lr.ph20, %._crit_edge.._crit_edge21_crit_edge
  %.pre-phi = phi i64 [ %.pre46, %._crit_edge.._crit_edge21_crit_edge ], [ %48, %.lr.ph20 ]
  %16 = getelementptr inbounds i32, ptr @a, i64 %.pre-phi
  call void @_ZSt4sortIPiSt7greaterIiEEvT_S3_T0_(ptr noundef nonnull @a, ptr noundef nonnull %16)
  %17 = load i32, ptr @n, align 4, !tbaa !5
  %18 = icmp sgt i32 %17, 0
  %ans.promoted27 = load i32, ptr @ans, align 4, !tbaa !5
  br i1 %18, label %.preheader.lr.ph, label %._crit_edge34

.preheader.lr.ph:                                 ; preds = %._crit_edge21
  %19 = load i32, ptr @q, align 4, !tbaa !5
  %20 = icmp sgt i32 %19, 0
  br i1 %20, label %.preheader.us.preheader, label %._crit_edge34

.preheader.us.preheader:                          ; preds = %.preheader.lr.ph
  %21 = zext nneg i32 %17 to i64
  %22 = add nsw i32 %19, -1
  %wide.trip.count = zext i32 %22 to i64
  br label %.lr.ph36.preheader

.lr.ph36.preheader:                               ; preds = %.critedge.us, %.preheader.us.preheader
  %.01133.us = phi i32 [ %37, %.critedge.us ], [ 0, %.preheader.us.preheader ]
  %ans.promoted3032.us = phi i32 [ %.lcssa, %.critedge.us ], [ %ans.promoted27, %.preheader.us.preheader ]
  %23 = sext i32 %.01133.us to i64
  %24 = sub nsw i64 %21, %23
  %smax = call i64 @llvm.smax.i64(i64 %24, i64 1)
  %25 = add nsw i64 %smax, -1
  %umin = call i64 @llvm.umin.i64(i64 %25, i64 %wide.trip.count)
  %26 = add nuw nsw i64 %umin, 1
  %27 = sext i32 %.01133.us to i64
  %invariant.op = sub nsw i64 %21, %27
  %min.iters.check = icmp ult i64 %umin, 7
  br i1 %min.iters.check, label %.lr.ph36.preheader52, label %vector.ph

.lr.ph36.preheader52:                             ; preds = %middle.block, %.lr.ph36.preheader
  %indvars.iv43.ph = phi i64 [ 0, %.lr.ph36.preheader ], [ %n.vec, %middle.block ]
  %.ph = phi i32 [ %ans.promoted3032.us, %.lr.ph36.preheader ], [ %35, %middle.block ]
  br label %.lr.ph36

vector.ph:                                        ; preds = %.lr.ph36.preheader
  %n.vec = and i64 %26, 8589934584
  %28 = insertelement <4 x i32> <i32 poison, i32 0, i32 0, i32 0>, i32 %ans.promoted3032.us, i64 0
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ %28, %vector.ph ], [ %32, %vector.body ]
  %vec.phi50 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %33, %vector.body ]
  %29 = add nsw i64 %index, %27
  %30 = getelementptr inbounds [100001 x i32], ptr @a, i64 0, i64 %29
  %31 = getelementptr inbounds i8, ptr %30, i64 16
  %wide.load = load <4 x i32>, ptr %30, align 4, !tbaa !5
  %wide.load51 = load <4 x i32>, ptr %31, align 4, !tbaa !5
  %32 = add <4 x i32> %vec.phi, %wide.load
  %33 = add <4 x i32> %vec.phi50, %wide.load51
  %index.next = add nuw i64 %index, 8
  %34 = icmp eq i64 %index.next, %n.vec
  br i1 %34, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %33, %32
  %35 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %26, %n.vec
  br i1 %cmp.n, label %.critedge.us, label %.lr.ph36.preheader52

.critedge.us:                                     ; preds = %.lr.ph36, %middle.block
  %.lcssa = phi i32 [ %35, %middle.block ], [ %43, %.lr.ph36 ]
  %36 = add i32 %.01133.us, 2
  %37 = add i32 %36, %19
  %38 = icmp slt i32 %37, %17
  br i1 %38, label %.lr.ph36.preheader, label %._crit_edge34.loopexit, !llvm.loop !14

.lr.ph36:                                         ; preds = %.lr.ph36.preheader52, %.lr.ph36
  %indvars.iv43 = phi i64 [ %indvars.iv.next44, %.lr.ph36 ], [ %indvars.iv43.ph, %.lr.ph36.preheader52 ]
  %39 = phi i32 [ %43, %.lr.ph36 ], [ %.ph, %.lr.ph36.preheader52 ]
  %40 = add nsw i64 %indvars.iv43, %27
  %41 = getelementptr inbounds [100001 x i32], ptr @a, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = add nsw i32 %39, %42
  %exitcond.not = icmp ne i64 %indvars.iv43, %wide.trip.count
  %indvars.iv.next44 = add nuw nsw i64 %indvars.iv43, 1
  %44 = icmp slt i64 %indvars.iv.next44, %invariant.op
  %or.cond = select i1 %exitcond.not, i1 %44, i1 false
  br i1 %or.cond, label %.lr.ph36, label %.critedge.us, !llvm.loop !15

.lr.ph20:                                         ; preds = %._crit_edge, %.lr.ph20
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph20 ], [ 0, %._crit_edge ]
  %45 = getelementptr inbounds i32, ptr @a, i64 %indvars.iv
  %46 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %45)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %47 = load i32, ptr @n, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = icmp slt i64 %indvars.iv.next, %48
  br i1 %49, label %.lr.ph20, label %._crit_edge21, !llvm.loop !16

._crit_edge34.loopexit:                           ; preds = %.critedge.us
  store i32 %.lcssa, ptr @ans, align 4, !tbaa !5
  br label %._crit_edge34

._crit_edge34:                                    ; preds = %._crit_edge21, %._crit_edge34.loopexit, %.preheader.lr.ph
  %50 = phi i32 [ %ans.promoted27, %.preheader.lr.ph ], [ %.lcssa, %._crit_edge34.loopexit ], [ %ans.promoted27, %._crit_edge21 ]
  %51 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %50)
  ret i32 0
}

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

; 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.smax.i64(i64, i64) #4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #4

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = distinct !{!11, !10, !12, !13}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10, !13, !12}
!16 = distinct !{!16, !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