; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/160 Division 1/Problem A (A)

; 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
@a = external dso_local global [100000 x i32], align 16
@q = external dso_local global [100000 x i32], align 16
@cost = external dso_local global i32, align 4
@cnt = 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
  store i32 0, ptr %1, align 4
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @m)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %17, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = load i32, ptr @m, 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) #4
  br label %20

12:                                               ; preds = %7
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [100000 x i32], ptr @q, i64 0, i64 %14
  %16 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %15)
  br label %17

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

20:                                               ; preds = %11
  %21 = 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 %22

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

26:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %35

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

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

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

42:                                               ; preds = %69, %35
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = load i32, ptr @n, align 4, !tbaa !5
  %45 = icmp slt i32 %43, %44
  br i1 %45, label %47, label %46

46:                                               ; preds = %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %72

47:                                               ; preds = %42
  %48 = load i32, ptr @cnt, align 4, !tbaa !5
  %49 = load i32, ptr @q, align 16, !tbaa !5
  %50 = icmp eq i32 %48, %49
  br i1 %50, label %51, label %54

51:                                               ; preds = %47
  %52 = load i32, ptr @cnt, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr @cnt, align 4, !tbaa !5
  br label %69

54:                                               ; preds = %47
  %55 = load i32, ptr @cnt, align 4, !tbaa !5
  %56 = load i32, ptr @q, align 16, !tbaa !5
  %57 = add nsw i32 %56, 1
  %58 = icmp eq i32 %55, %57
  br i1 %58, label %59, label %60

59:                                               ; preds = %54
  store i32 0, ptr @cnt, align 4, !tbaa !5
  br label %69

60:                                               ; preds = %54
  %61 = load i32, ptr %5, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [100000 x i32], ptr @a, i64 0, i64 %62
  %64 = load i32, ptr %63, align 4, !tbaa !5
  %65 = load i32, ptr @cost, align 4, !tbaa !5
  %66 = add nsw i32 %65, %64
  store i32 %66, ptr @cost, align 4, !tbaa !5
  %67 = load i32, ptr @cnt, align 4, !tbaa !5
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr @cnt, align 4, !tbaa !5
  br label %69

69:                                               ; preds = %60, %59, %51
  %70 = load i32, ptr %5, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %5, align 4, !tbaa !5
  br label %42, !llvm.loop !12

72:                                               ; preds = %46
  %73 = load i32, ptr @cost, align 4, !tbaa !5
  %74 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %73)
  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 @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) #3

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


; 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
@a = external dso_local global [100000 x i32], align 16
@q = external dso_local global [100000 x i32], align 16
@cost = external dso_local local_unnamed_addr global i32, align 4
@cnt = 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 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @m)
  %2 = load i32, ptr @m, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %0
  %4 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph16, label %._crit_edge17

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

._crit_edge17:                                    ; preds = %.lr.ph16, %._crit_edge
  %12 = load i32, ptr @m, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds i32, ptr @q, i64 %13
  tail call void @_ZSt4sortIPiEvT_S1_(ptr noundef nonnull @q, ptr noundef nonnull %14)
  %15 = load i32, ptr @n, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds i32, ptr @a, i64 %16
  tail call void @_ZSt4sortIPiSt7greaterIiEEvT_S3_T0_(ptr noundef nonnull @a, ptr noundef nonnull %17)
  %18 = load i32, ptr @n, align 4, !tbaa !5
  %19 = icmp sgt i32 %18, 0
  %cost.promoted = load i32, ptr @cost, align 4, !tbaa !5
  br i1 %19, label %.lr.ph20, label %._crit_edge21

.lr.ph20:                                         ; preds = %._crit_edge17
  %cnt.promoted = load i32, ptr @cnt, align 4, !tbaa !5
  %20 = load i32, ptr @q, align 16, !tbaa !5
  %21 = add nsw i32 %20, 1
  %wide.trip.count = zext nneg i32 %18 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %22 = icmp eq i32 %18, 1
  br i1 %22, label %._crit_edge21.loopexit.unr-lcssa, label %.lr.ph20.new

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

.lr.ph16:                                         ; preds = %._crit_edge, %.lr.ph16
  %indvars.iv23 = phi i64 [ %indvars.iv.next24, %.lr.ph16 ], [ 0, %._crit_edge ]
  %23 = getelementptr inbounds [100000 x i32], ptr @a, i64 0, i64 %indvars.iv23
  %24 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %23)
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, 1
  %25 = load i32, ptr @n, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = icmp slt i64 %indvars.iv.next24, %26
  br i1 %27, label %.lr.ph16, label %._crit_edge17, !llvm.loop !11

._crit_edge21.loopexit.unr-lcssa.loopexit:        ; preds = %60
  store i32 %.sink.1, ptr @cnt, align 4, !tbaa !5
  br label %._crit_edge21.loopexit.unr-lcssa

._crit_edge21.loopexit.unr-lcssa:                 ; preds = %._crit_edge21.loopexit.unr-lcssa.loopexit, %.lr.ph20
  %.lcssa.ph = phi i32 [ poison, %.lr.ph20 ], [ %61, %._crit_edge21.loopexit.unr-lcssa.loopexit ]
  %indvars.iv26.unr = phi i64 [ 0, %.lr.ph20 ], [ %indvars.iv.next27.1, %._crit_edge21.loopexit.unr-lcssa.loopexit ]
  %.unr = phi i32 [ %cnt.promoted, %.lr.ph20 ], [ %.sink.1, %._crit_edge21.loopexit.unr-lcssa.loopexit ]
  %.unr29 = phi i32 [ %cost.promoted, %.lr.ph20 ], [ %61, %._crit_edge21.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge21, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge21.loopexit.unr-lcssa
  %28 = icmp eq i32 %.unr, %20
  br i1 %28, label %._crit_edge21.loopexit.epilog-lcssa, label %29

29:                                               ; preds = %.epil.preheader
  %30 = icmp eq i32 %.unr, %21
  br i1 %30, label %._crit_edge21.loopexit.epilog-lcssa, label %31

31:                                               ; preds = %29
  %32 = getelementptr inbounds [100000 x i32], ptr @a, i64 0, i64 %indvars.iv26.unr
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = add nsw i32 %.unr29, %33
  store i32 %34, ptr @cost, align 4, !tbaa !5
  %35 = add nsw i32 %.unr, 1
  br label %._crit_edge21.loopexit.epilog-lcssa

._crit_edge21.loopexit.epilog-lcssa:              ; preds = %31, %29, %.epil.preheader
  %.sink.epil = phi i32 [ %35, %31 ], [ %21, %.epil.preheader ], [ 0, %29 ]
  %36 = phi i32 [ %34, %31 ], [ %.unr29, %.epil.preheader ], [ %.unr29, %29 ]
  store i32 %.sink.epil, ptr @cnt, align 4, !tbaa !5
  br label %._crit_edge21

._crit_edge21:                                    ; preds = %._crit_edge21.loopexit.epilog-lcssa, %._crit_edge21.loopexit.unr-lcssa, %._crit_edge17
  %37 = phi i32 [ %cost.promoted, %._crit_edge17 ], [ %.lcssa.ph, %._crit_edge21.loopexit.unr-lcssa ], [ %36, %._crit_edge21.loopexit.epilog-lcssa ]
  %38 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %37)
  ret i32 0

39:                                               ; preds = %60, %.lr.ph20.new
  %indvars.iv26 = phi i64 [ 0, %.lr.ph20.new ], [ %indvars.iv.next27.1, %60 ]
  %40 = phi i32 [ %cnt.promoted, %.lr.ph20.new ], [ %.sink.1, %60 ]
  %41 = phi i32 [ %cost.promoted, %.lr.ph20.new ], [ %61, %60 ]
  %niter = phi i64 [ 0, %.lr.ph20.new ], [ %niter.next.1, %60 ]
  %42 = icmp eq i32 %40, %20
  br i1 %42, label %50, label %43

43:                                               ; preds = %39
  %44 = icmp eq i32 %40, %21
  br i1 %44, label %50, label %45

45:                                               ; preds = %43
  %46 = getelementptr inbounds [100000 x i32], ptr @a, i64 0, i64 %indvars.iv26
  %47 = load i32, ptr %46, align 8, !tbaa !5
  %48 = add nsw i32 %41, %47
  store i32 %48, ptr @cost, align 4, !tbaa !5
  %49 = add nsw i32 %40, 1
  br label %50

50:                                               ; preds = %43, %39, %45
  %.sink = phi i32 [ %49, %45 ], [ %21, %39 ], [ 0, %43 ]
  %51 = phi i32 [ %48, %45 ], [ %41, %39 ], [ %41, %43 ]
  %indvars.iv.next27 = or disjoint i64 %indvars.iv26, 1
  %52 = icmp eq i32 %.sink, %20
  br i1 %52, label %60, label %53

53:                                               ; preds = %50
  %54 = icmp eq i32 %.sink, %21
  br i1 %54, label %60, label %55

55:                                               ; preds = %53
  %56 = getelementptr inbounds [100000 x i32], ptr @a, i64 0, i64 %indvars.iv.next27
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = add nsw i32 %51, %57
  store i32 %58, ptr @cost, align 4, !tbaa !5
  %59 = add nsw i32 %.sink, 1
  br label %60

60:                                               ; preds = %55, %53, %50
  %.sink.1 = phi i32 [ %59, %55 ], [ %21, %50 ], [ 0, %53 ]
  %61 = phi i32 [ %58, %55 ], [ %51, %50 ], [ %51, %53 ]
  %indvars.iv.next27.1 = add nuw nsw i64 %indvars.iv26, 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_edge21.loopexit.unr-lcssa.loopexit, label %39, !llvm.loop !12
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiSt7greaterIiEEvT_S3_T0_(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 = !{!"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}


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