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

; 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
@lazy = external dso_local global [262144 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 nounwind uwtable
declare dso_local void @_ZN2miC2Ex(ptr noundef nonnull align 4 dereferenceable(4), i64 noundef) unnamed_addr #2 align 2

; 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 @_Z3muliiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca %struct.mi, 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
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !5
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = load i32, ptr %7, align 4, !tbaa !5
  %17 = load i32, ptr %8, align 4, !tbaa !5
  call void @_Z4pushiii(i32 noundef %15, i32 noundef %16, i32 noundef %17)
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = load i32, ptr %8, align 4, !tbaa !5
  %20 = icmp sgt i32 %18, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %4
  br label %87

22:                                               ; preds = %4
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = icmp sle i32 %23, %24
  br i1 %25, label %26, label %33

26:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  call void @_ZN2miC2Ex(ptr noundef nonnull align 4 dereferenceable(4) %9, i64 noundef 2)
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [262144 x %struct.mi], ptr @lazy, i64 0, i64 %28
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %29, ptr align 4 %9, i64 4, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  %30 = load i32, ptr %6, align 4, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = load i32, ptr %8, align 4, !tbaa !5
  call void @_Z4pushiii(i32 noundef %30, i32 noundef %31, i32 noundef %32)
  br label %87

33:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %34 = load i32, ptr %7, align 4, !tbaa !5
  %35 = load i32, ptr %8, align 4, !tbaa !5
  %36 = add nsw i32 %34, %35
  %37 = sdiv i32 %36, 2
  store i32 %37, ptr %10, align 4, !tbaa !5
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = mul nsw i32 2, %39
  %41 = load i32, ptr %7, align 4, !tbaa !5
  %42 = load i32, ptr %10, align 4, !tbaa !5
  call void @_Z3muliiii(i32 noundef %38, i32 noundef %40, i32 noundef %41, i32 noundef %42)
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = mul nsw i32 2, %44
  %46 = add nsw i32 %45, 1
  %47 = load i32, ptr %10, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  %49 = load i32, ptr %8, align 4, !tbaa !5
  call void @_Z3muliiii(i32 noundef %43, i32 noundef %46, i32 noundef %48, i32 noundef %49)
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %83, %33
  %51 = load i32, ptr %11, align 4, !tbaa !5
  %52 = load i32, ptr @K, align 4, !tbaa !5
  %53 = icmp sle i32 %51, %52
  br i1 %53, label %55, label %54

54:                                               ; preds = %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %86

55:                                               ; preds = %50
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = mul nsw i32 2, %56
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [262144 x %"struct.std::array"], ptr @seg, i64 0, i64 %58
  %60 = load i32, ptr %11, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %59, i64 noundef %61) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %13, ptr align 4 %62, i64 4, i1 false), !tbaa.struct !9
  %63 = load i32, ptr %6, align 4, !tbaa !5
  %64 = mul nsw i32 2, %63
  %65 = add nsw i32 %64, 1
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [262144 x %"struct.std::array"], ptr @seg, i64 0, i64 %66
  %68 = load i32, ptr %11, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %67, i64 noundef %69) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %14, ptr align 4 %70, i64 4, i1 false), !tbaa.struct !9
  %71 = getelementptr inbounds %struct.mi, ptr %13, i32 0, i32 0
  %72 = load i32, ptr %71, align 4
  %73 = getelementptr inbounds %struct.mi, ptr %14, i32 0, i32 0
  %74 = load i32, ptr %73, align 4
  %75 = call i32 @_Zpl2miS_(i32 %72, i32 %74)
  %76 = getelementptr inbounds %struct.mi, ptr %12, i32 0, i32 0
  store i32 %75, ptr %76, align 4
  %77 = load i32, ptr %6, align 4, !tbaa !5
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [262144 x %"struct.std::array"], ptr @seg, i64 0, i64 %78
  %80 = load i32, ptr %11, align 4, !tbaa !5
  %81 = sext i32 %80 to i64
  %82 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %79, i64 noundef %81) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %82, ptr align 4 %12, i64 4, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %83

83:                                               ; preds = %55
  %84 = load i32, ptr %11, align 4, !tbaa !5
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %11, align 4, !tbaa !5
  br label %50, !llvm.loop !10

86:                                               ; preds = %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %87

87:                                               ; preds = %86, %26, %21
  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 = !{i64 0, i64 4, !5}
!10 = distinct !{!10, !11}
!11 = !{!"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
@lazy = external dso_local local_unnamed_addr global [262144 x %struct.mi], align 16

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

; Function Attrs: mustprogress 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) local_unnamed_addr #1

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z4pushiii(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #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) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3muliiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #1 {
  %5 = alloca %struct.mi, align 4
  tail call void @_Z4pushiii(i32 noundef %1, i32 noundef %2, i32 noundef %3)
  %6 = icmp sgt i32 %0, %3
  br i1 %6, label %.loopexit, label %7

7:                                                ; preds = %4
  %.not = icmp sgt i32 %0, %2
  br i1 %.not, label %12, label %8

8:                                                ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #3
  call void @_ZN2miC2Ex(ptr noundef nonnull align 4 dereferenceable(4) %5, i64 noundef 2)
  %9 = sext i32 %1 to i64
  %10 = getelementptr inbounds [262144 x %struct.mi], ptr @lazy, i64 0, i64 %9
  %11 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %11, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #3
  call void @_Z4pushiii(i32 noundef %1, i32 noundef %2, i32 noundef %3)
  br label %.loopexit

12:                                               ; preds = %7
  %13 = add nsw i32 %3, %2
  %14 = sdiv i32 %13, 2
  %15 = shl nsw i32 %1, 1
  tail call void @_Z3muliiii(i32 noundef %0, i32 noundef %15, i32 noundef %2, i32 noundef %14)
  %16 = or disjoint i32 %15, 1
  %17 = add nsw i32 %14, 1
  tail call void @_Z3muliiii(i32 noundef %0, i32 noundef %16, i32 noundef %17, i32 noundef %3)
  %18 = load i32, ptr @K, align 4, !tbaa !5
  %.not3435 = icmp slt i32 %18, 0
  br i1 %.not3435, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %12
  %19 = sext i32 %15 to i64
  %20 = getelementptr inbounds [262144 x %"struct.std::array"], ptr @seg, i64 0, i64 %19
  %21 = sext i32 %16 to i64
  %22 = getelementptr inbounds [262144 x %"struct.std::array"], ptr @seg, i64 0, i64 %21
  %23 = sext i32 %1 to i64
  %24 = getelementptr inbounds [262144 x %"struct.std::array"], ptr @seg, i64 0, i64 %23
  br label %25

25:                                               ; preds = %.lr.ph, %25
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %25 ]
  %26 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %20, i64 noundef %indvars.iv) #3
  %.sroa.01.0.copyload = load i32, ptr %26, align 4, !tbaa !5
  %27 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %22, i64 noundef %indvars.iv) #3
  %.sroa.0.0.copyload = load i32, ptr %27, align 4, !tbaa !5
  %28 = tail call i32 @_Zpl2miS_(i32 %.sroa.01.0.copyload, i32 %.sroa.0.0.copyload)
  %29 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayI2miLm11EEixEm(ptr noundef nonnull align 4 dereferenceable(44) %24, i64 noundef %indvars.iv) #3
  store i32 %28, ptr %29, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %30 = load i32, ptr @K, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %.not34.not = icmp slt i64 %indvars.iv, %31
  br i1 %.not34.not, label %25, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %25, %12, %4, %8
  ret void
}

attributes #0 = { mustprogress 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 = { 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,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass