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

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

@K = external dso_local global i32, align 4
@seg = external dso_local global [262144 x %"struct.std::array"], 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
declare dso_local void @_Z4pushiii(i32 noundef, i32 noundef, i32 noundef) #1

; 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: mustprogress uwtable
define dso_local void @_Z3updiRKSt5arrayI2miLm11EEiii(i32 noundef %0, ptr noundef nonnull align 4 dereferenceable(44) %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca %struct.mi, align 4
  %13 = alloca %struct.mi, align 4
  %14 = alloca %struct.mi, align 4
  %15 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  store i32 %4, ptr %10, align 4, !tbaa !5
  %16 = load i32, ptr %8, align 4, !tbaa !5
  %17 = load i32, ptr %9, align 4, !tbaa !5
  %18 = load i32, ptr %10, align 4, !tbaa !5
  call void @_Z4pushiii(i32 noundef %16, i32 noundef %17, i32 noundef %18)
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = load i32, ptr %9, align 4, !tbaa !5
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %26, label %22

22:                                               ; preds = %5
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr %10, align 4, !tbaa !5
  %25 = icmp sgt i32 %23, %24
  br i1 %25, label %26, label %27

26:                                               ; preds = %22, %5
  br label %89

27:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %56, %27
  %29 = load i32, ptr %11, align 4, !tbaa !5
  %30 = load i32, ptr @K, align 4, !tbaa !5
  %31 = icmp sle i32 %29, %30
  br i1 %31, label %33, label %32

32:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %59

33:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [262144 x %"struct.std::array"], ptr @seg, i64 0, i64 %35
  %37 = load i32, ptr %11, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %36, i64 noundef %38) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %13, ptr align 4 %39, i64 4, i1 false), !tbaa.struct !11
  %40 = load ptr, ptr %7, align 8, !tbaa !9
  %41 = load i32, ptr %11, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNKSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %40, i64 noundef %42) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %14, ptr align 4 %43, i64 4, i1 false), !tbaa.struct !11
  %44 = getelementptr inbounds %struct.mi, ptr %13, i32 0, i32 0
  %45 = load i32, ptr %44, align 4
  %46 = getelementptr inbounds %struct.mi, ptr %14, i32 0, i32 0
  %47 = load i32, ptr %46, align 4
  %48 = call i32 @_Zpl2miS_(i32 %45, i32 %47)
  %49 = getelementptr inbounds %struct.mi, ptr %12, i32 0, i32 0
  store i32 %48, ptr %49, align 4
  %50 = load i32, ptr %8, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [262144 x %"struct.std::array"], ptr @seg, i64 0, i64 %51
  %53 = load i32, ptr %11, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %52, i64 noundef %54) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %55, ptr align 4 %12, i64 4, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %56

56:                                               ; preds = %33
  %57 = load i32, ptr %11, align 4, !tbaa !5
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %11, align 4, !tbaa !5
  br label %28, !llvm.loop !12

59:                                               ; preds = %32
  %60 = load i32, ptr %9, align 4, !tbaa !5
  %61 = load i32, ptr %10, align 4, !tbaa !5
  %62 = icmp eq i32 %60, %61
  br i1 %62, label %63, label %64

63:                                               ; preds = %59
  br label %89

64:                                               ; preds = %59
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  %65 = load i32, ptr %9, align 4, !tbaa !5
  %66 = load i32, ptr %10, align 4, !tbaa !5
  %67 = add nsw i32 %65, %66
  %68 = sdiv i32 %67, 2
  store i32 %68, ptr %15, align 4, !tbaa !5
  %69 = load i32, ptr %6, align 4, !tbaa !5
  %70 = load i32, ptr %15, align 4, !tbaa !5
  %71 = icmp sle i32 %69, %70
  br i1 %71, label %72, label %79

72:                                               ; preds = %64
  %73 = load i32, ptr %6, align 4, !tbaa !5
  %74 = load ptr, ptr %7, align 8, !tbaa !9
  %75 = load i32, ptr %8, align 4, !tbaa !5
  %76 = mul nsw i32 2, %75
  %77 = load i32, ptr %9, align 4, !tbaa !5
  %78 = load i32, ptr %15, align 4, !tbaa !5
  call void @_Z3updiRKSt5arrayI2miLm11EEiii(i32 noundef %73, ptr noundef nonnull align 4 dereferenceable(44) %74, i32 noundef %76, i32 noundef %77, i32 noundef %78)
  br label %88

79:                                               ; preds = %64
  %80 = load i32, ptr %6, align 4, !tbaa !5
  %81 = load ptr, ptr %7, align 8, !tbaa !9
  %82 = load i32, ptr %8, align 4, !tbaa !5
  %83 = mul nsw i32 2, %82
  %84 = add nsw i32 %83, 1
  %85 = load i32, ptr %15, align 4, !tbaa !5
  %86 = add nsw i32 %85, 1
  %87 = load i32, ptr %10, align 4, !tbaa !5
  call void @_Z3updiRKSt5arrayI2miLm11EEiii(i32 noundef %80, ptr noundef nonnull align 4 dereferenceable(44) %81, i32 noundef %84, i32 noundef %86, i32 noundef %87)
  br label %88

88:                                               ; preds = %79, %72
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  br label %89

89:                                               ; preds = %88, %63, %26
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNKSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44), i64 noundef) #2 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 = { 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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{i64 0, i64 4, !5}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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

@K = external dso_local local_unnamed_addr global i32, align 4
@seg = external dso_local global [262144 x %"struct.std::array"], align 16

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

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

; 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: mustprogress uwtable
define dso_local void @_Z3updiRKSt5arrayI2miLm11EEiii(i32 noundef %0, ptr noundef nonnull align 4 dereferenceable(44) %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  tail call void @_Z4pushiii(i32 noundef %2, i32 noundef %3, i32 noundef %4)
  %6 = icmp slt i32 %0, %3
  %7 = icmp sgt i32 %0, %4
  %or.cond4557 = or i1 %6, %7
  br i1 %or.cond4557, label %tailrecurse.outer._crit_edge, label %.preheader.preheader

.preheader.preheader:                             ; preds = %5, %tailrecurse.outer
  %.tr40.ph60 = phi i32 [ %.tr4047, %tailrecurse.outer ], [ %4, %5 ]
  %.tr39.ph59 = phi i32 [ %24, %tailrecurse.outer ], [ %3, %5 ]
  %.tr38.ph58 = phi i32 [ %23, %tailrecurse.outer ], [ %2, %5 ]
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %tailrecurse
  %.tr4047 = phi i32 [ %21, %tailrecurse ], [ %.tr40.ph60, %.preheader.preheader ]
  %.tr3846 = phi i32 [ %22, %tailrecurse ], [ %.tr38.ph58, %.preheader.preheader ]
  %8 = load i32, ptr @K, align 4, !tbaa !5
  %.not43 = icmp slt i32 %8, 0
  br i1 %.not43, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %9 = sext i32 %.tr3846 to i64
  %10 = getelementptr inbounds [262144 x %"struct.std::array"], ptr @seg, i64 0, i64 %9
  br label %12

._crit_edge:                                      ; preds = %12, %.preheader
  %11 = icmp eq i32 %.tr39.ph59, %.tr4047
  br i1 %11, label %tailrecurse.outer._crit_edge, label %19

12:                                               ; preds = %.lr.ph, %12
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %12 ]
  %13 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %10, i64 noundef %indvars.iv) #2
  %.sroa.03.0.copyload = load i32, ptr %13, align 4, !tbaa !5
  %14 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNKSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %1, i64 noundef %indvars.iv) #2
  %.sroa.0.0.copyload = load i32, ptr %14, align 4, !tbaa !5
  %15 = tail call i32 @_Zpl2miS_(i32 %.sroa.03.0.copyload, i32 %.sroa.0.0.copyload)
  %16 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %10, i64 noundef %indvars.iv) #2
  store i32 %15, ptr %16, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %17 = load i32, ptr @K, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %.not.not = icmp slt i64 %indvars.iv, %18
  br i1 %.not.not, label %12, label %._crit_edge, !llvm.loop !9

19:                                               ; preds = %._crit_edge
  %20 = add nsw i32 %.tr4047, %.tr39.ph59
  %21 = sdiv i32 %20, 2
  %.not36 = icmp slt i32 %21, %0
  %22 = shl nsw i32 %.tr3846, 1
  br i1 %.not36, label %tailrecurse.outer, label %tailrecurse

tailrecurse:                                      ; preds = %19
  tail call void @_Z4pushiii(i32 noundef %22, i32 noundef %.tr39.ph59, i32 noundef %21)
  br label %.preheader

tailrecurse.outer:                                ; preds = %19
  %23 = or disjoint i32 %22, 1
  %24 = add nsw i32 %21, 1
  tail call void @_Z4pushiii(i32 noundef %23, i32 noundef %24, i32 noundef %.tr4047)
  %25 = icmp slt i32 %.tr4047, %0
  br i1 %25, label %tailrecurse.outer._crit_edge, label %.preheader.preheader

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %._crit_edge, %5
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNKSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44), i64 noundef) local_unnamed_addr #1 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 = { 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"}


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