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

24:                                               ; preds = %6
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = load i32, ptr %12, align 4, !tbaa !5
  %27 = icmp sgt i32 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %24, %6
  br label %89

29:                                               ; preds = %24
  %30 = load i32, ptr %7, align 4, !tbaa !5
  %31 = load i32, ptr %11, align 4, !tbaa !5
  %32 = icmp sle i32 %30, %31
  br i1 %32, label %33, label %68

33:                                               ; preds = %29
  %34 = load i32, ptr %12, align 4, !tbaa !5
  %35 = load i32, ptr %8, align 4, !tbaa !5
  %36 = icmp sle i32 %34, %35
  br i1 %36, label %37, label %68

37:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  store i32 0, ptr %13, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %64, %37
  %39 = load i32, ptr %13, align 4, !tbaa !5
  %40 = load i32, ptr @K, align 4, !tbaa !5
  %41 = icmp sle i32 %39, %40
  br i1 %41, label %43, label %42

42:                                               ; preds = %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  br label %67

43:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  %44 = load ptr, ptr %9, align 8, !tbaa !9
  %45 = load i32, ptr %13, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %44, i64 noundef %46) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %15, ptr align 4 %47, i64 4, i1 false), !tbaa.struct !11
  %48 = load i32, ptr %10, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [262144 x %"struct.std::array"], ptr @seg, i64 0, i64 %49
  %51 = load i32, ptr %13, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %50, i64 noundef %52) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %16, ptr align 4 %53, i64 4, i1 false), !tbaa.struct !11
  %54 = getelementptr inbounds %struct.mi, ptr %15, i32 0, i32 0
  %55 = load i32, ptr %54, align 4
  %56 = getelementptr inbounds %struct.mi, ptr %16, i32 0, i32 0
  %57 = load i32, ptr %56, align 4
  %58 = call i32 @_Zpl2miS_(i32 %55, i32 %57)
  %59 = getelementptr inbounds %struct.mi, ptr %14, i32 0, i32 0
  store i32 %58, ptr %59, align 4
  %60 = load ptr, ptr %9, align 8, !tbaa !9
  %61 = load i32, ptr %13, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %60, i64 noundef %62) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %63, ptr align 4 %14, i64 4, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  br label %64

64:                                               ; preds = %43
  %65 = load i32, ptr %13, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %13, align 4, !tbaa !5
  br label %38, !llvm.loop !12

67:                                               ; preds = %42
  br label %89

68:                                               ; preds = %33, %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  %69 = load i32, ptr %11, align 4, !tbaa !5
  %70 = load i32, ptr %12, align 4, !tbaa !5
  %71 = add nsw i32 %69, %70
  %72 = sdiv i32 %71, 2
  store i32 %72, ptr %17, align 4, !tbaa !5
  %73 = load i32, ptr %7, align 4, !tbaa !5
  %74 = load i32, ptr %8, align 4, !tbaa !5
  %75 = load ptr, ptr %9, align 8, !tbaa !9
  %76 = load i32, ptr %10, align 4, !tbaa !5
  %77 = mul nsw i32 2, %76
  %78 = load i32, ptr %11, align 4, !tbaa !5
  %79 = load i32, ptr %17, align 4, !tbaa !5
  call void @_Z5queryiiRSt5arrayI2miLm11EEiii(i32 noundef %73, i32 noundef %74, ptr noundef nonnull align 4 dereferenceable(44) %75, i32 noundef %77, i32 noundef %78, i32 noundef %79)
  %80 = load i32, ptr %7, align 4, !tbaa !5
  %81 = load i32, ptr %8, align 4, !tbaa !5
  %82 = load ptr, ptr %9, align 8, !tbaa !9
  %83 = load i32, ptr %10, align 4, !tbaa !5
  %84 = mul nsw i32 2, %83
  %85 = add nsw i32 %84, 1
  %86 = load i32, ptr %17, align 4, !tbaa !5
  %87 = add nsw i32 %86, 1
  %88 = load i32, ptr %12, align 4, !tbaa !5
  call void @_Z5queryiiRSt5arrayI2miLm11EEiii(i32 noundef %80, i32 noundef %81, ptr noundef nonnull align 4 dereferenceable(44) %82, i32 noundef %85, i32 noundef %87, i32 noundef %88)
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  br label %89

89:                                               ; preds = %68, %67, %28
  ret void
}

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 @_Z5queryiiRSt5arrayI2miLm11EEiii(i32 noundef %0, i32 noundef %1, ptr noundef nonnull align 4 dereferenceable(44) %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #0 {
  tail call void @_Z4pushiii(i32 noundef %3, i32 noundef %4, i32 noundef %5)
  %7 = icmp slt i32 %1, %4
  %8 = icmp sgt i32 %0, %5
  %or.cond48 = or i1 %8, %7
  br i1 %or.cond48, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %6
  %.not38 = icmp sgt i32 %5, %1
  br i1 %.not38, label %tailrecurse.us, label %.lr.ph.split.split

tailrecurse.us:                                   ; preds = %.lr.ph, %tailrecurse.us
  %.tr4450.us = phi i32 [ %13, %tailrecurse.us ], [ %4, %.lr.ph ]
  %.tr4349.us = phi i32 [ %12, %tailrecurse.us ], [ %3, %.lr.ph ]
  %9 = add nsw i32 %.tr4450.us, %5
  %10 = sdiv i32 %9, 2
  %11 = shl nsw i32 %.tr4349.us, 1
  tail call void @_Z5queryiiRSt5arrayI2miLm11EEiii(i32 noundef %0, i32 noundef %1, ptr noundef nonnull align 4 dereferenceable(44) %2, i32 noundef %11, i32 noundef %.tr4450.us, i32 noundef %10)
  %12 = or disjoint i32 %11, 1
  %13 = add nsw i32 %10, 1
  tail call void @_Z4pushiii(i32 noundef %12, i32 noundef %13, i32 noundef %5)
  %.not59 = icmp slt i32 %10, %1
  br i1 %.not59, label %tailrecurse.us, label %.loopexit

.lr.ph.split.split:                               ; preds = %.lr.ph, %tailrecurse
  %.tr4450 = phi i32 [ %28, %tailrecurse ], [ %4, %.lr.ph ]
  %.tr4349 = phi i32 [ %27, %tailrecurse ], [ %3, %.lr.ph ]
  %.not = icmp slt i32 %.tr4450, %0
  br i1 %.not, label %tailrecurse, label %.preheader

.preheader:                                       ; preds = %.lr.ph.split.split
  %14 = load i32, ptr @K, align 4, !tbaa !5
  %.not3955 = icmp slt i32 %14, 0
  br i1 %.not3955, label %.loopexit, label %.lr.ph57

.lr.ph57:                                         ; preds = %.preheader
  %15 = sext i32 %.tr4349 to i64
  %16 = getelementptr inbounds [262144 x %"struct.std::array"], ptr @seg, i64 0, i64 %15
  br label %17

17:                                               ; preds = %.lr.ph57, %17
  %indvars.iv = phi i64 [ 0, %.lr.ph57 ], [ %indvars.iv.next, %17 ]
  %18 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %2, i64 noundef %indvars.iv) #2
  %.sroa.02.0.copyload = load i32, ptr %18, align 4, !tbaa !5
  %19 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %16, i64 noundef %indvars.iv) #2
  %.sroa.0.0.copyload = load i32, ptr %19, align 4, !tbaa !5
  %20 = tail call i32 @_Zpl2miS_(i32 %.sroa.02.0.copyload, i32 %.sroa.0.0.copyload)
  %21 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %2, i64 noundef %indvars.iv) #2
  store i32 %20, ptr %21, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %22 = load i32, ptr @K, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %.not39.not = icmp slt i64 %indvars.iv, %23
  br i1 %.not39.not, label %17, label %.loopexit, !llvm.loop !9

tailrecurse:                                      ; preds = %.lr.ph.split.split
  %24 = add nsw i32 %.tr4450, %5
  %25 = sdiv i32 %24, 2
  %26 = shl nsw i32 %.tr4349, 1
  tail call void @_Z5queryiiRSt5arrayI2miLm11EEiii(i32 noundef %0, i32 noundef %1, ptr noundef nonnull align 4 dereferenceable(44) %2, i32 noundef %26, i32 noundef %.tr4450, i32 noundef %25)
  %27 = or disjoint i32 %26, 1
  %28 = add nsw i32 %25, 1
  tail call void @_Z4pushiii(i32 noundef %27, i32 noundef %28, i32 noundef %5)
  %.not58 = icmp slt i32 %25, %1
  br i1 %.not58, label %.lr.ph.split.split, label %.loopexit

.loopexit:                                        ; preds = %tailrecurse, %17, %tailrecurse.us, %6, %.preheader
  ret void
}

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