; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2019-20/Feb/Plat/help_NKlogN+NK^2.ll (Z3advSt5arrayI2miLm11EE)

; 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.mi = type { i32 }
%"struct.std::array" = type { [11 x %struct.mi] }

@K = external dso_local global i32, align 4
@cum = external dso_local global [11 x [11 x %struct.mi]], align 16

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

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

; Function Attrs: mustprogress uwtable
declare dso_local i32 @_Zpl2miS_(i32, i32) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3advSt5arrayI2miLm11EE(ptr dead_on_unwind noalias writable sret(%"struct.std::array") align 4 %0, ptr noundef byval(%"struct.std::array") align 8 %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca %struct.mi, align 4
  %7 = alloca %struct.mi, align 4
  %8 = alloca %struct.mi, align 4
  %9 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %10 = load i32, ptr @K, align 4, !tbaa !5
  store i32 %10, ptr %3, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %62, %2
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = icmp sge i32 %12, 0
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  br label %65

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %16 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %16, ptr %5, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %58, %15
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = load i32, ptr @K, align 4, !tbaa !5
  %20 = icmp sle i32 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %61

22:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = icmp eq i32 %23, %24
  br i1 %25, label %26, label %30

26:                                               ; preds = %22
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %1, i64 noundef %28) #5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 %29, i64 4, i1 false), !tbaa.struct !9
  br label %51

30:                                               ; preds = %22
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [11 x [11 x %struct.mi]], ptr @cum, i64 0, i64 %32
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = sub nsw i32 %34, 1
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [11 x %struct.mi], ptr %33, i64 0, i64 %36
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %7, ptr align 4 %37, i64 4, i1 false), !tbaa.struct !9
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [11 x [11 x %struct.mi]], ptr @cum, i64 0, i64 %40
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [11 x %struct.mi], ptr %41, i64 0, i64 %43
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %8, ptr align 4 %44, i64 4, i1 false), !tbaa.struct !9
  %45 = getelementptr inbounds %struct.mi, ptr %7, i32 0, i32 0
  %46 = load i32, ptr %45, align 4
  %47 = getelementptr inbounds %struct.mi, ptr %8, i32 0, i32 0
  %48 = load i32, ptr %47, align 4
  %49 = call i32 @_Zpl2miS_(i32 %46, i32 %48)
  %50 = getelementptr inbounds %struct.mi, ptr %6, i32 0, i32 0
  store i32 %49, ptr %50, align 4
  br label %51

51:                                               ; preds = %30, %26
  %52 = load i32, ptr %3, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [11 x [11 x %struct.mi]], ptr @cum, i64 0, i64 %53
  %55 = load i32, ptr %5, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [11 x %struct.mi], ptr %54, i64 0, i64 %56
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %57, ptr align 4 %6, i64 4, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  br label %58

58:                                               ; preds = %51
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %5, align 4, !tbaa !5
  br label %17, !llvm.loop !10

61:                                               ; preds = %21
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %3, align 4, !tbaa !5
  %64 = add nsw i32 %63, -1
  store i32 %64, ptr %3, align 4, !tbaa !5
  br label %11, !llvm.loop !12

65:                                               ; preds = %14
  call void @_ZNSt5arrayI2miLm11EEC2Ev(ptr noundef nonnull align 4 dereferenceable(44) %0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %66

66:                                               ; preds = %78, %65
  %67 = load i32, ptr %9, align 4, !tbaa !5
  %68 = load i32, ptr @K, align 4, !tbaa !5
  %69 = icmp sle i32 %67, %68
  br i1 %69, label %71, label %70

70:                                               ; preds = %66
  store i32 8, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  br label %81

71:                                               ; preds = %66
  %72 = load i32, ptr %9, align 4, !tbaa !5
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds [11 x %struct.mi], ptr @cum, i64 0, i64 %73
  %75 = load i32, ptr %9, align 4, !tbaa !5
  %76 = sext i32 %75 to i64
  %77 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %0, i64 noundef %76) #5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %77, ptr align 4 %74, i64 4, i1 false), !tbaa.struct !9
  br label %78

78:                                               ; preds = %71
  %79 = load i32, ptr %9, align 4, !tbaa !5
  %80 = add nsw i32 %79, 1
  store i32 %80, ptr %9, align 4, !tbaa !5
  br label %66, !llvm.loop !13

81:                                               ; preds = %70
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44), i64 noundef) #2 align 2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZNSt5arrayI2miLm11EEC2Ev(ptr noundef nonnull align 4 dereferenceable(44)) unnamed_addr #4 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { nocallback nofree 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 = { 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 = !{i64 0, i64 4, !5}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
!13 = distinct !{!13, !11}


; 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.mi = type { i32 }
%"struct.std::array" = type { [11 x %struct.mi] }

@K = external dso_local local_unnamed_addr global i32, align 4
@cum = external dso_local local_unnamed_addr global [11 x [11 x %struct.mi]], align 16

; Function Attrs: mustprogress uwtable
declare dso_local i32 @_Zpl2miS_(i32, i32) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3advSt5arrayI2miLm11EE(ptr dead_on_unwind noalias writable sret(%"struct.std::array") align 4 %0, ptr noundef byval(%"struct.std::array") align 8 %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @K, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, -1
  br i1 %4, label %.preheader.preheader, label %._crit_edge27

.preheader.preheader:                             ; preds = %2
  %5 = zext nneg i32 %3 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %6 = phi i32 [ %3, %.preheader.preheader ], [ %14, %._crit_edge ]
  %indvars.iv = phi i64 [ %5, %.preheader.preheader ], [ %indvars.iv.next, %._crit_edge ]
  %7 = sext i32 %6 to i64
  %.not2324 = icmp sgt i64 %indvars.iv, %7
  br i1 %.not2324, label %._crit_edge, label %8

8:                                                ; preds = %.preheader
  %9 = add nuw nsw i64 %indvars.iv, 1
  %10 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %1, i64 noundef %indvars.iv) #3
  %.sroa.05.0.copyload.peel = load i32, ptr %10, align 4, !tbaa !5
  %.pre = load i32, ptr @K, align 4, !tbaa !5
  %11 = getelementptr inbounds [11 x [11 x %struct.mi]], ptr @cum, i64 0, i64 %indvars.iv, i64 %indvars.iv
  store i32 %.sroa.05.0.copyload.peel, ptr %11, align 4, !tbaa !5
  %12 = sext i32 %.pre to i64
  %.not23.not.peel = icmp slt i64 %indvars.iv, %12
  br i1 %.not23.not.peel, label %.peel.next, label %._crit_edge

._crit_edge27:                                    ; preds = %._crit_edge, %2
  call void @_ZNSt5arrayI2miLm11EEC2Ev(ptr noundef nonnull align 4 dereferenceable(44) %0)
  %13 = load i32, ptr @K, align 4, !tbaa !5
  %.not28 = icmp slt i32 %13, 0
  br i1 %.not28, label %._crit_edge32, label %.lr.ph31

._crit_edge:                                      ; preds = %.peel.next, %8, %.preheader
  %14 = phi i32 [ %6, %.preheader ], [ %.pre, %8 ], [ %.pre42, %.peel.next ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %15 = icmp sgt i64 %indvars.iv, 0
  br i1 %15, label %.preheader, label %._crit_edge27, !llvm.loop !9

.peel.next:                                       ; preds = %8, %.peel.next
  %.sroa.04.0.copyload = phi i32 [ %17, %.peel.next ], [ %.sroa.05.0.copyload.peel, %8 ]
  %indvars.iv33 = phi i64 [ %indvars.iv.next34, %.peel.next ], [ %9, %8 ]
  %16 = getelementptr inbounds [11 x [11 x %struct.mi]], ptr @cum, i64 0, i64 %9, i64 %indvars.iv33
  %.sroa.0.0.copyload = load i32, ptr %16, align 4, !tbaa !5
  %17 = call i32 @_Zpl2miS_(i32 %.sroa.04.0.copyload, i32 %.sroa.0.0.copyload)
  %.pre42 = load i32, ptr @K, align 4, !tbaa !5
  %18 = getelementptr inbounds [11 x [11 x %struct.mi]], ptr @cum, i64 0, i64 %indvars.iv, i64 %indvars.iv33
  store i32 %17, ptr %18, align 4, !tbaa !5
  %indvars.iv.next34 = add nuw nsw i64 %indvars.iv33, 1
  %19 = sext i32 %.pre42 to i64
  %.not23.not = icmp slt i64 %indvars.iv33, %19
  br i1 %.not23.not, label %.peel.next, label %._crit_edge, !llvm.loop !11

._crit_edge32:                                    ; preds = %.lr.ph31, %._crit_edge27
  ret void

.lr.ph31:                                         ; preds = %._crit_edge27, %.lr.ph31
  %indvars.iv38 = phi i64 [ %indvars.iv.next39, %.lr.ph31 ], [ 0, %._crit_edge27 ]
  %20 = getelementptr inbounds [11 x %struct.mi], ptr @cum, i64 0, i64 %indvars.iv38
  %21 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %0, i64 noundef %indvars.iv38) #3
  %22 = load i32, ptr %20, align 4, !tbaa !5
  store i32 %22, ptr %21, align 4, !tbaa !5
  %indvars.iv.next39 = add nuw nsw i64 %indvars.iv38, 1
  %23 = load i32, ptr @K, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %.not.not = icmp slt i64 %indvars.iv38, %24
  br i1 %.not.not, label %.lr.ph31, label %._crit_edge32, !llvm.loop !13
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44), i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZNSt5arrayI2miLm11EEC2Ev(ptr noundef nonnull align 4 dereferenceable(44)) unnamed_addr #2 align 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 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 #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" }
attributes #3 = { 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}
!12 = !{!"llvm.loop.peeled.count", i32 1}
!13 = distinct !{!13, !10}


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