; LLVM IR from microsoft/LightAIMD :: src (matrix)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

16:                                               ; preds = %47, %5
  %17 = load i64, ptr %11, align 8, !tbaa !9
  %18 = load i64, ptr %8, align 8, !tbaa !9
  %19 = icmp ult i64 %17, %18
  br i1 %19, label %21, label %20

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

21:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %22 = load i64, ptr %11, align 8, !tbaa !9
  %23 = load i64, ptr %9, align 8, !tbaa !9
  %24 = mul i64 %22, %23
  store i64 %24, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #2
  store i64 0, ptr %14, align 8, !tbaa !9
  br label %25

25:                                               ; preds = %43, %21
  %26 = load i64, ptr %14, align 8, !tbaa !9
  %27 = load i64, ptr %9, align 8, !tbaa !9
  %28 = icmp ult i64 %26, %27
  br i1 %28, label %30, label %29

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

30:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #2
  %31 = load i64, ptr %13, align 8, !tbaa !9
  %32 = load i64, ptr %14, align 8, !tbaa !9
  %33 = add i64 %31, %32
  store i64 %33, ptr %15, align 8, !tbaa !9
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = load i64, ptr %15, align 8, !tbaa !9
  %36 = getelementptr inbounds double, ptr %34, i64 %35
  %37 = load double, ptr %36, align 8, !tbaa !11
  %38 = load double, ptr %10, align 8, !tbaa !11
  %39 = fmul double %37, %38
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = load i64, ptr %15, align 8, !tbaa !9
  %42 = getelementptr inbounds double, ptr %40, i64 %41
  store double %39, ptr %42, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  br label %43

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

46:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  br label %47

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

50:                                               ; preds = %20
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = !{!"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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @mat_scalar_multiply(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, i64 noundef %2, i64 noundef %3, double noundef %4) local_unnamed_addr #0 {
  %.not = icmp eq i64 %2, 0
  %.not20 = icmp eq i64 %3, 0
  %or.cond = or i1 %.not, %.not20
  br i1 %or.cond, label %._crit_edge19, label %.lr.ph.us.preheader

.lr.ph.us.preheader:                              ; preds = %5
  %6 = ptrtoint ptr %1 to i64
  %7 = ptrtoint ptr %0 to i64
  %8 = sub i64 %6, %7
  %min.iters.check = icmp ult i64 %3, 4
  %diff.check = icmp ult i64 %8, 32
  %or.cond24 = or i1 %min.iters.check, %diff.check
  %n.vec = and i64 %3, -4
  %broadcast.splatinsert = insertelement <2 x double> poison, double %4, i64 0
  %broadcast.splat = shufflevector <2 x double> %broadcast.splatinsert, <2 x double> poison, <2 x i32> zeroinitializer
  %cmp.n = icmp eq i64 %n.vec, %3
  %xtraiter = and i64 %3, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %._crit_edge.us
  %.016.us = phi i64 [ %50, %._crit_edge.us ], [ 0, %.lr.ph.us.preheader ]
  %9 = mul i64 %.016.us, %3
  br i1 %or.cond24, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %.lr.ph.us, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %.lr.ph.us ]
  %10 = add i64 %index, %9
  %11 = getelementptr inbounds double, ptr %0, i64 %10
  %12 = getelementptr inbounds i8, ptr %11, i64 16
  %wide.load = load <2 x double>, ptr %11, align 8, !tbaa !5
  %wide.load23 = load <2 x double>, ptr %12, align 8, !tbaa !5
  %13 = fmul <2 x double> %wide.load, %broadcast.splat
  %14 = fmul <2 x double> %wide.load23, %broadcast.splat
  %15 = getelementptr inbounds double, ptr %1, i64 %10
  %16 = getelementptr inbounds i8, ptr %15, i64 16
  store <2 x double> %13, ptr %15, align 8, !tbaa !5
  store <2 x double> %14, ptr %16, align 8, !tbaa !5
  %index.next = add nuw i64 %index, 4
  %17 = icmp eq i64 %index.next, %n.vec
  br i1 %17, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph.us
  %.01415.us.ph = phi i64 [ 0, %.lr.ph.us ], [ %n.vec, %middle.block ]
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader, %scalar.ph.prol
  %.01415.us.prol = phi i64 [ %23, %scalar.ph.prol ], [ %.01415.us.ph, %scalar.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %scalar.ph.prol ], [ 0, %scalar.ph.preheader ]
  %18 = add i64 %.01415.us.prol, %9
  %19 = getelementptr inbounds double, ptr %0, i64 %18
  %20 = load double, ptr %19, align 8, !tbaa !5
  %21 = fmul double %20, %4
  %22 = getelementptr inbounds double, ptr %1, i64 %18
  store double %21, ptr %22, align 8, !tbaa !5
  %23 = add nuw i64 %.01415.us.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol, !llvm.loop !13

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %.01415.us.unr = phi i64 [ %.01415.us.ph, %scalar.ph.preheader ], [ %23, %scalar.ph.prol ]
  %24 = sub i64 %.01415.us.ph, %3
  %25 = icmp ugt i64 %24, -4
  br i1 %25, label %._crit_edge.us, label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %.01415.us = phi i64 [ %49, %scalar.ph ], [ %.01415.us.unr, %scalar.ph.prol.loopexit ]
  %26 = add i64 %.01415.us, %9
  %27 = getelementptr inbounds double, ptr %0, i64 %26
  %28 = load double, ptr %27, align 8, !tbaa !5
  %29 = fmul double %28, %4
  %30 = getelementptr inbounds double, ptr %1, i64 %26
  store double %29, ptr %30, align 8, !tbaa !5
  %31 = add nuw i64 %.01415.us, 1
  %32 = add i64 %31, %9
  %33 = getelementptr inbounds double, ptr %0, i64 %32
  %34 = load double, ptr %33, align 8, !tbaa !5
  %35 = fmul double %34, %4
  %36 = getelementptr inbounds double, ptr %1, i64 %32
  store double %35, ptr %36, align 8, !tbaa !5
  %37 = add nuw i64 %.01415.us, 2
  %38 = add i64 %37, %9
  %39 = getelementptr inbounds double, ptr %0, i64 %38
  %40 = load double, ptr %39, align 8, !tbaa !5
  %41 = fmul double %40, %4
  %42 = getelementptr inbounds double, ptr %1, i64 %38
  store double %41, ptr %42, align 8, !tbaa !5
  %43 = add nuw i64 %.01415.us, 3
  %44 = add i64 %43, %9
  %45 = getelementptr inbounds double, ptr %0, i64 %44
  %46 = load double, ptr %45, align 8, !tbaa !5
  %47 = fmul double %46, %4
  %48 = getelementptr inbounds double, ptr %1, i64 %44
  store double %47, ptr %48, align 8, !tbaa !5
  %49 = add nuw i64 %.01415.us, 4
  %exitcond.not.3 = icmp eq i64 %49, %3
  br i1 %exitcond.not.3, label %._crit_edge.us, label %scalar.ph, !llvm.loop !15

._crit_edge.us:                                   ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %50 = add nuw i64 %.016.us, 1
  %exitcond22.not = icmp eq i64 %50, %2
  br i1 %exitcond22.not, label %._crit_edge19, label %.lr.ph.us, !llvm.loop !16

._crit_edge19:                                    ; preds = %._crit_edge.us, %5
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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" }

!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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.unroll.disable"}
!15 = distinct !{!15, !10, !11}
!16 = distinct !{!16, !10}


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