; LLVM IR from KhawajaAbaid/machine-learning-in-c :: linalg (matrix_vector_dot_product)

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

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

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @matrix_vector_dot_product(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3) #3 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  store i64 %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = call noalias ptr @calloc(i64 noundef %13, i64 noundef 8) #5
  store ptr %14, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %47, %4
  %16 = load i64, ptr %10, align 8, !tbaa !9
  %17 = load i64, ptr %7, align 8, !tbaa !9
  %18 = icmp ult i64 %16, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %15
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %50

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %21

21:                                               ; preds = %43, %20
  %22 = load i64, ptr %12, align 8, !tbaa !9
  %23 = load i64, ptr %8, align 8, !tbaa !9
  %24 = icmp ult i64 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  store i32 5, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %46

26:                                               ; preds = %21
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = load i64, ptr %10, align 8, !tbaa !9
  %29 = getelementptr inbounds ptr, ptr %27, i64 %28
  %30 = load ptr, ptr %29, align 8, !tbaa !5
  %31 = load i64, ptr %12, align 8, !tbaa !9
  %32 = getelementptr inbounds double, ptr %30, i64 %31
  %33 = load double, ptr %32, align 8, !tbaa !11
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = load i64, ptr %12, align 8, !tbaa !9
  %36 = getelementptr inbounds double, ptr %34, i64 %35
  %37 = load double, ptr %36, align 8, !tbaa !11
  %38 = load ptr, ptr %9, align 8, !tbaa !5
  %39 = load i64, ptr %10, align 8, !tbaa !9
  %40 = getelementptr inbounds double, ptr %38, i64 %39
  %41 = load double, ptr %40, align 8, !tbaa !11
  %42 = call double @llvm.fmuladd.f64(double %33, double %37, double %41)
  store double %42, ptr %40, align 8, !tbaa !11
  br label %43

43:                                               ; preds = %26
  %44 = load i64, ptr %12, align 8, !tbaa !9
  %45 = add i64 %44, 1
  store i64 %45, ptr %12, align 8, !tbaa !9
  br label %21, !llvm.loop !13

46:                                               ; preds = %25
  br label %47

47:                                               ; preds = %46
  %48 = load i64, ptr %10, align 8, !tbaa !9
  %49 = add i64 %48, 1
  store i64 %49, ptr %10, align 8, !tbaa !9
  br label %15, !llvm.loop !15

50:                                               ; preds = %19
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  ret ptr %51
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(0,1) "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0,1) }

!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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

; Function Attrs: nofree nounwind memory(readwrite, argmem: read) uwtable
define dso_local noalias noundef ptr @matrix_vector_dot_product(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #2 {
  %5 = tail call noalias ptr @calloc(i64 noundef %2, i64 noundef 8) #3
  %.not = icmp eq i64 %2, 0
  %.not18 = icmp eq i64 %3, 0
  %or.cond = or i1 %.not, %.not18
  br i1 %or.cond, label %._crit_edge17, label %.preheader.us.preheader

.preheader.us.preheader:                          ; preds = %4
  %6 = add i64 %3, -1
  %xtraiter = and i64 %3, 3
  %7 = icmp ult i64 %6, 3
  %unroll_iter = and i64 %3, -4
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %.01416.us = phi i64 [ %43, %._crit_edge.us ], [ 0, %.preheader.us.preheader ]
  %8 = getelementptr inbounds ptr, ptr %0, i64 %.01416.us
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = getelementptr inbounds double, ptr %5, i64 %.01416.us
  %.promoted.us = load double, ptr %10, align 8, !tbaa !9
  br i1 %7, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new

.preheader.us.new:                                ; preds = %.preheader.us, %.preheader.us.new
  %11 = phi double [ %34, %.preheader.us.new ], [ %.promoted.us, %.preheader.us ]
  %.015.us = phi i64 [ %35, %.preheader.us.new ], [ 0, %.preheader.us ]
  %niter = phi i64 [ %niter.next.3, %.preheader.us.new ], [ 0, %.preheader.us ]
  %12 = getelementptr inbounds double, ptr %9, i64 %.015.us
  %13 = load double, ptr %12, align 8, !tbaa !9
  %14 = getelementptr inbounds double, ptr %1, i64 %.015.us
  %15 = load double, ptr %14, align 8, !tbaa !9
  %16 = tail call double @llvm.fmuladd.f64(double %13, double %15, double %11)
  %17 = or disjoint i64 %.015.us, 1
  %18 = getelementptr inbounds double, ptr %9, i64 %17
  %19 = load double, ptr %18, align 8, !tbaa !9
  %20 = getelementptr inbounds double, ptr %1, i64 %17
  %21 = load double, ptr %20, align 8, !tbaa !9
  %22 = tail call double @llvm.fmuladd.f64(double %19, double %21, double %16)
  %23 = or disjoint i64 %.015.us, 2
  %24 = getelementptr inbounds double, ptr %9, i64 %23
  %25 = load double, ptr %24, align 8, !tbaa !9
  %26 = getelementptr inbounds double, ptr %1, i64 %23
  %27 = load double, ptr %26, align 8, !tbaa !9
  %28 = tail call double @llvm.fmuladd.f64(double %25, double %27, double %22)
  %29 = or disjoint i64 %.015.us, 3
  %30 = getelementptr inbounds double, ptr %9, i64 %29
  %31 = load double, ptr %30, align 8, !tbaa !9
  %32 = getelementptr inbounds double, ptr %1, i64 %29
  %33 = load double, ptr %32, align 8, !tbaa !9
  %34 = tail call double @llvm.fmuladd.f64(double %31, double %33, double %28)
  %35 = add nuw i64 %.015.us, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new, !llvm.loop !11

._crit_edge.us.unr-lcssa:                         ; preds = %.preheader.us.new, %.preheader.us
  %.lcssa.ph = phi double [ poison, %.preheader.us ], [ %34, %.preheader.us.new ]
  %.unr = phi double [ %.promoted.us, %.preheader.us ], [ %34, %.preheader.us.new ]
  %.015.us.unr = phi i64 [ 0, %.preheader.us ], [ %35, %.preheader.us.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa, %.epil.preheader
  %36 = phi double [ %41, %.epil.preheader ], [ %.unr, %._crit_edge.us.unr-lcssa ]
  %.015.us.epil = phi i64 [ %42, %.epil.preheader ], [ %.015.us.unr, %._crit_edge.us.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.us.unr-lcssa ]
  %37 = getelementptr inbounds double, ptr %9, i64 %.015.us.epil
  %38 = load double, ptr %37, align 8, !tbaa !9
  %39 = getelementptr inbounds double, ptr %1, i64 %.015.us.epil
  %40 = load double, ptr %39, align 8, !tbaa !9
  %41 = tail call double @llvm.fmuladd.f64(double %38, double %40, double %36)
  %42 = add nuw i64 %.015.us.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge.us, label %.epil.preheader, !llvm.loop !13

._crit_edge.us:                                   ; preds = %.epil.preheader, %._crit_edge.us.unr-lcssa
  %.lcssa = phi double [ %.lcssa.ph, %._crit_edge.us.unr-lcssa ], [ %41, %.epil.preheader ]
  store double %.lcssa, ptr %10, align 8, !tbaa !9
  %43 = add nuw i64 %.01416.us, 1
  %exitcond20.not = icmp eq i64 %43, %2
  br i1 %exitcond20.not, label %._crit_edge17, label %.preheader.us, !llvm.loop !15

._crit_edge17:                                    ; preds = %._crit_edge.us, %4
  ret ptr %5
}

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nofree nounwind memory(readwrite, argmem: read) 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 allocsize(0,1) }

!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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.unroll.disable"}
!15 = distinct !{!15, !12}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass