; LLVM IR from ravireddy07/cpp-book :: src/Coding-Blocks/MixturesDP.ll (Z8mixturesPxxx)

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

@dp = external dso_local global [100 x [100 x i64]], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z3sumPxxx(ptr noundef, i64 noundef, i64 noundef) #0

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

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

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

14:                                               ; preds = %3
  store i64 0, ptr %4, align 8
  br label %91

15:                                               ; preds = %3
  %16 = load i64, ptr %6, align 8, !tbaa !9
  %17 = load i64, ptr %7, align 8, !tbaa !9
  %18 = sub nsw i64 %16, %17
  %19 = trunc i64 %18 to i32
  %20 = call i32 @llvm.abs.i32(i32 %19, i1 true)
  %21 = icmp eq i32 %20, 1
  br i1 %21, label %22, label %32

22:                                               ; preds = %15
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load i64, ptr %6, align 8, !tbaa !9
  %25 = getelementptr inbounds i64, ptr %23, i64 %24
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = load i64, ptr %7, align 8, !tbaa !9
  %29 = getelementptr inbounds i64, ptr %27, i64 %28
  %30 = load i64, ptr %29, align 8, !tbaa !9
  %31 = mul nsw i64 %26, %30
  store i64 %31, ptr %4, align 8
  br label %91

32:                                               ; preds = %15
  %33 = load i64, ptr %6, align 8, !tbaa !9
  %34 = getelementptr inbounds [100 x [100 x i64]], ptr @dp, i64 0, i64 %33
  %35 = load i64, ptr %7, align 8, !tbaa !9
  %36 = getelementptr inbounds [100 x i64], ptr %34, i64 0, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !9
  %38 = icmp ne i64 %37, -1
  br i1 %38, label %39, label %45

39:                                               ; preds = %32
  %40 = load i64, ptr %6, align 8, !tbaa !9
  %41 = getelementptr inbounds [100 x [100 x i64]], ptr @dp, i64 0, i64 %40
  %42 = load i64, ptr %7, align 8, !tbaa !9
  %43 = getelementptr inbounds [100 x i64], ptr %41, i64 0, i64 %42
  %44 = load i64, ptr %43, align 8, !tbaa !9
  store i64 %44, ptr %4, align 8
  br label %91

45:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 2147483647, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %46 = load i64, ptr %6, align 8, !tbaa !9
  %47 = add nsw i64 %46, 1
  store i64 %47, ptr %9, align 8, !tbaa !9
  br label %48

48:                                               ; preds = %81, %45
  %49 = load i64, ptr %9, align 8, !tbaa !9
  %50 = load i64, ptr %7, align 8, !tbaa !9
  %51 = icmp sle i64 %49, %50
  br i1 %51, label %53, label %52

52:                                               ; preds = %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %84

53:                                               ; preds = %48
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = load i64, ptr %6, align 8, !tbaa !9
  %56 = load i64, ptr %9, align 8, !tbaa !9
  %57 = sub nsw i64 %56, 1
  %58 = call noundef i64 @_Z8mixturesPxxx(ptr noundef %54, i64 noundef %55, i64 noundef %57)
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = load i64, ptr %9, align 8, !tbaa !9
  %61 = load i64, ptr %7, align 8, !tbaa !9
  %62 = call noundef i64 @_Z8mixturesPxxx(ptr noundef %59, i64 noundef %60, i64 noundef %61)
  %63 = add nsw i64 %58, %62
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = load i64, ptr %6, align 8, !tbaa !9
  %66 = load i64, ptr %9, align 8, !tbaa !9
  %67 = sub nsw i64 %66, 1
  %68 = call noundef i64 @_Z3sumPxxx(ptr noundef %64, i64 noundef %65, i64 noundef %67)
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = load i64, ptr %9, align 8, !tbaa !9
  %71 = load i64, ptr %7, align 8, !tbaa !9
  %72 = call noundef i64 @_Z3sumPxxx(ptr noundef %69, i64 noundef %70, i64 noundef %71)
  %73 = mul nsw i64 %68, %72
  %74 = add nsw i64 %63, %73
  store i64 %74, ptr %10, align 8, !tbaa !9
  %75 = load i64, ptr %10, align 8, !tbaa !9
  %76 = load i64, ptr %8, align 8, !tbaa !9
  %77 = icmp slt i64 %75, %76
  br i1 %77, label %78, label %80

78:                                               ; preds = %53
  %79 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %79, ptr %8, align 8, !tbaa !9
  br label %80

80:                                               ; preds = %78, %53
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %81

81:                                               ; preds = %80
  %82 = load i64, ptr %9, align 8, !tbaa !9
  %83 = add nsw i64 %82, 1
  store i64 %83, ptr %9, align 8, !tbaa !9
  br label %48, !llvm.loop !11

84:                                               ; preds = %52
  %85 = load i64, ptr %8, align 8, !tbaa !9
  %86 = load i64, ptr %6, align 8, !tbaa !9
  %87 = getelementptr inbounds [100 x [100 x i64]], ptr @dp, i64 0, i64 %86
  %88 = load i64, ptr %7, align 8, !tbaa !9
  %89 = getelementptr inbounds [100 x i64], ptr %87, i64 0, i64 %88
  store i64 %85, ptr %89, align 8, !tbaa !9
  %90 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %90, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %91

91:                                               ; preds = %84, %39, %22, %14
  %92 = load i64, ptr %4, align 8
  ret i64 %92
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #3

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"any pointer", !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"

@dp = external dso_local local_unnamed_addr global [100 x [100 x i64]], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z3sumPxxx(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z8mixturesPxxx(ptr noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq i64 %1, %2
  br i1 %4, label %26, label %5

5:                                                ; preds = %3
  %6 = sub nsw i64 %1, %2
  %7 = trunc i64 %6 to i32
  %8 = tail call i32 @llvm.abs.i32(i32 %7, i1 true)
  %9 = icmp eq i32 %8, 1
  br i1 %9, label %10, label %16

10:                                               ; preds = %5
  %11 = getelementptr inbounds i64, ptr %0, i64 %1
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = getelementptr inbounds i64, ptr %0, i64 %2
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = mul nsw i64 %14, %12
  br label %26

16:                                               ; preds = %5
  %17 = getelementptr inbounds [100 x [100 x i64]], ptr @dp, i64 0, i64 %1, i64 %2
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %.not = icmp eq i64 %18, -1
  br i1 %.not, label %.preheader, label %26

.preheader:                                       ; preds = %16
  %.not41.not42 = icmp slt i64 %1, %2
  br i1 %.not41.not42, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.036.lcssa = phi i64 [ 2147483647, %.preheader ], [ %spec.select, %.lr.ph ]
  store i64 %.036.lcssa, ptr %17, align 8, !tbaa !5
  br label %26

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.035.in44 = phi i64 [ %.035, %.lr.ph ], [ %1, %.preheader ]
  %.03643 = phi i64 [ %spec.select, %.lr.ph ], [ 2147483647, %.preheader ]
  %.035 = add i64 %.035.in44, 1
  %19 = tail call noundef i64 @_Z8mixturesPxxx(ptr noundef %0, i64 noundef %1, i64 noundef %.035.in44)
  %20 = tail call noundef i64 @_Z8mixturesPxxx(ptr noundef %0, i64 noundef %.035, i64 noundef %2)
  %21 = add nsw i64 %20, %19
  %22 = tail call noundef i64 @_Z3sumPxxx(ptr noundef %0, i64 noundef %1, i64 noundef %.035.in44)
  %23 = tail call noundef i64 @_Z3sumPxxx(ptr noundef %0, i64 noundef %.035, i64 noundef %2)
  %24 = mul nsw i64 %23, %22
  %25 = add nsw i64 %21, %24
  %spec.select = tail call i64 @llvm.smin.i64(i64 %25, i64 %.03643)
  %exitcond.not = icmp eq i64 %.035, %2
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

26:                                               ; preds = %16, %3, %._crit_edge, %10
  %.0 = phi i64 [ %15, %10 ], [ %.036.lcssa, %._crit_edge ], [ 0, %3 ], [ %18, %16 ]
  ret i64 %.0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) #2

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 speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass