; LLVM IR from ahr9n/psolving-paradigms :: 4.dynamic-programming/20.building-up/UVa 348 - Optimal Array Multiplication Sequenc_c758d635 (Z2dpii)

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

@a = external dso_local global [11 x [2 x i64]], align 16
@mem = external dso_local global [11 x [11 x i64]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z2dpii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %8 = load i32, ptr %4, align 4, !tbaa !5
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = icmp eq i32 %8, %9
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  store i64 0, ptr %3, align 8
  br label %95

12:                                               ; preds = %2
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [11 x [11 x i64]], ptr @mem, i64 0, i64 %14
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [11 x i64], ptr %15, i64 0, i64 %17
  %19 = load i64, ptr %18, align 8, !tbaa !9
  %20 = xor i64 %19, -1
  %21 = icmp ne i64 %20, 0
  br i1 %21, label %22, label %30

22:                                               ; preds = %12
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [11 x [11 x i64]], ptr @mem, i64 0, i64 %24
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [11 x i64], ptr %25, i64 0, i64 %27
  %29 = load i64, ptr %28, align 8, !tbaa !9
  store i64 %29, ptr %3, align 8
  br label %95

30:                                               ; preds = %12
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [11 x [11 x i64]], ptr @mem, i64 0, i64 %32
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [11 x i64], ptr %33, i64 0, i64 %35
  store i64 1000000000000000, ptr %36, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %37 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %37, ptr %6, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %84, %30
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = load i32, ptr %5, align 4, !tbaa !5
  %41 = icmp slt i32 %39, %40
  br i1 %41, label %43, label %42

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

43:                                               ; preds = %38
  %44 = load i32, ptr %4, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [11 x [11 x i64]], ptr @mem, i64 0, i64 %45
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [11 x i64], ptr %46, i64 0, i64 %48
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %50 = load i32, ptr %4, align 4, !tbaa !5
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = call noundef i64 @_Z2dpii(i32 noundef %50, i32 noundef %51)
  %53 = load i32, ptr %6, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  %55 = load i32, ptr %5, align 4, !tbaa !5
  %56 = call noundef i64 @_Z2dpii(i32 noundef %54, i32 noundef %55)
  %57 = add nsw i64 %52, %56
  %58 = load i32, ptr %4, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [11 x [2 x i64]], ptr @a, i64 0, i64 %59
  %61 = getelementptr inbounds [2 x i64], ptr %60, i64 0, i64 0
  %62 = load i64, ptr %61, align 16, !tbaa !9
  %63 = load i32, ptr %6, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [11 x [2 x i64]], ptr @a, i64 0, i64 %64
  %66 = getelementptr inbounds [2 x i64], ptr %65, i64 0, i64 1
  %67 = load i64, ptr %66, align 8, !tbaa !9
  %68 = mul nsw i64 %62, %67
  %69 = load i32, ptr %5, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [11 x [2 x i64]], ptr @a, i64 0, i64 %70
  %72 = getelementptr inbounds [2 x i64], ptr %71, i64 0, i64 1
  %73 = load i64, ptr %72, align 8, !tbaa !9
  %74 = mul nsw i64 %68, %73
  %75 = add nsw i64 %57, %74
  store i64 %75, ptr %7, align 8, !tbaa !9
  %76 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %49, ptr noundef nonnull align 8 dereferenceable(8) %7)
  %77 = load i64, ptr %76, align 8, !tbaa !9
  %78 = load i32, ptr %4, align 4, !tbaa !5
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [11 x [11 x i64]], ptr @mem, i64 0, i64 %79
  %81 = load i32, ptr %5, align 4, !tbaa !5
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds [11 x i64], ptr %80, i64 0, i64 %82
  store i64 %77, ptr %83, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %84

84:                                               ; preds = %43
  %85 = load i32, ptr %6, align 4, !tbaa !5
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %6, align 4, !tbaa !5
  br label %38, !llvm.loop !11

87:                                               ; preds = %42
  %88 = load i32, ptr %4, align 4, !tbaa !5
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [11 x [11 x i64]], ptr @mem, i64 0, i64 %89
  %91 = load i32, ptr %5, align 4, !tbaa !5
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds [11 x i64], ptr %90, i64 0, i64 %92
  %94 = load i64, ptr %93, align 8, !tbaa !9
  store i64 %94, ptr %3, align 8
  br label %95

95:                                               ; preds = %87, %22, %11
  %96 = load i64, ptr %3, align 8
  ret i64 %96
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@a = external dso_local local_unnamed_addr global [11 x [2 x i64]], align 16
@mem = external dso_local global [11 x [11 x i64]], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z2dpii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca i64, align 8
  %4 = icmp eq i32 %0, %1
  br i1 %4, label %._crit_edge, label %5

5:                                                ; preds = %2
  %6 = sext i32 %0 to i64
  %7 = sext i32 %1 to i64
  %8 = getelementptr inbounds [11 x [11 x i64]], ptr @mem, i64 0, i64 %6, i64 %7
  %9 = load i64, ptr %8, align 8, !tbaa !5
  %.not = icmp eq i64 %9, -1
  br i1 %.not, label %10, label %._crit_edge

10:                                               ; preds = %5
  store i64 1000000000000000, ptr %8, align 8, !tbaa !5
  %11 = icmp slt i32 %0, %1
  br i1 %11, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %10
  %12 = getelementptr inbounds [11 x [2 x i64]], ptr @a, i64 0, i64 %6
  %13 = getelementptr inbounds [11 x [2 x i64]], ptr @a, i64 0, i64 %7, i64 1
  br label %14

14:                                               ; preds = %.lr.ph, %14
  %indvars.iv = phi i64 [ %6, %.lr.ph ], [ %indvars.iv.next, %14 ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %15 = trunc nsw i64 %indvars.iv to i32
  %16 = call noundef i64 @_Z2dpii(i32 noundef %0, i32 noundef %15)
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %17 = trunc nsw i64 %indvars.iv.next to i32
  %18 = call noundef i64 @_Z2dpii(i32 noundef %17, i32 noundef %1)
  %19 = add nsw i64 %18, %16
  %20 = load i64, ptr %12, align 16, !tbaa !5
  %21 = getelementptr inbounds [11 x [2 x i64]], ptr @a, i64 0, i64 %indvars.iv, i64 1
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = mul nsw i64 %22, %20
  %24 = load i64, ptr %13, align 8, !tbaa !5
  %25 = mul nsw i64 %23, %24
  %26 = add nsw i64 %19, %25
  store i64 %26, ptr %3, align 8, !tbaa !5
  %27 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %8, ptr noundef nonnull align 8 dereferenceable(8) %3)
  %28 = load i64, ptr %27, align 8, !tbaa !5
  store i64 %28, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  %exitcond.not = icmp eq i64 %indvars.iv.next, %7
  br i1 %exitcond.not, label %._crit_edge, label %14, !llvm.loop !9

._crit_edge:                                      ; preds = %14, %10, %5, %2
  %.026 = phi i64 [ 0, %2 ], [ %9, %5 ], [ 1000000000000000, %10 ], [ %28, %14 ]
  ret i64 %.026
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2

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

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 = !{!"long long", !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,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass