; 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_subtract(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, i64 noundef %4) #1 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, 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 ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  store i64 %4, ptr %10, align 8, !tbaa !9
  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 = %50, %5
  %17 = load i64, ptr %11, align 8, !tbaa !9
  %18 = load i64, ptr %9, 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 %53

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 %10, 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 = %46, %21
  %26 = load i64, ptr %14, align 8, !tbaa !9
  %27 = load i64, ptr %10, 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 %49

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 ptr, ptr %7, align 8, !tbaa !5
  %39 = load i64, ptr %15, align 8, !tbaa !9
  %40 = getelementptr inbounds double, ptr %38, i64 %39
  %41 = load double, ptr %40, align 8, !tbaa !11
  %42 = fsub double %37, %41
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = load i64, ptr %15, align 8, !tbaa !9
  %45 = getelementptr inbounds double, ptr %43, i64 %44
  store double %42, ptr %45, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  br label %46

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

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

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

53:                                               ; 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_subtract(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, ptr nocapture noundef writeonly %2, i64 noundef %3, i64 noundef %4) local_unnamed_addr #0 {
  %.not = icmp eq i64 %3, 0
  %.not21 = icmp eq i64 %4, 0
  %or.cond = or i1 %.not, %.not21
  br i1 %or.cond, label %._crit_edge20, label %.lr.ph.us.preheader

.lr.ph.us.preheader:                              ; preds = %5
  %6 = ptrtoint ptr %2 to i64
  %7 = ptrtoint ptr %0 to i64
  %8 = ptrtoint ptr %1 to i64
  %9 = sub i64 %6, %7
  %10 = sub i64 %6, %8
  %min.iters.check = icmp ult i64 %4, 4
  %diff.check = icmp ult i64 %9, 32
  %diff.check24 = icmp ult i64 %10, 32
  %conflict.rdx = or i1 %diff.check, %diff.check24
  %n.vec = and i64 %4, -4
  %cmp.n = icmp eq i64 %n.vec, %4
  %xtraiter = and i64 %4, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  %11 = add i64 %4, -1
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %._crit_edge.us
  %.017.us = phi i64 [ %48, %._crit_edge.us ], [ 0, %.lr.ph.us.preheader ]
  %12 = mul i64 %.017.us, %4
  %brmerge = or i1 %min.iters.check, %conflict.rdx
  br i1 %brmerge, 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 ]
  %13 = add i64 %index, %12
  %14 = getelementptr inbounds double, ptr %0, i64 %13
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  %wide.load = load <2 x double>, ptr %14, align 8, !tbaa !5
  %wide.load25 = load <2 x double>, ptr %15, align 8, !tbaa !5
  %16 = getelementptr inbounds double, ptr %1, i64 %13
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  %wide.load26 = load <2 x double>, ptr %16, align 8, !tbaa !5
  %wide.load27 = load <2 x double>, ptr %17, align 8, !tbaa !5
  %18 = fsub <2 x double> %wide.load, %wide.load26
  %19 = fsub <2 x double> %wide.load25, %wide.load27
  %20 = getelementptr inbounds double, ptr %2, i64 %13
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  store <2 x double> %18, ptr %20, align 8, !tbaa !5
  store <2 x double> %19, ptr %21, align 8, !tbaa !5
  %index.next = add nuw i64 %index, 4
  %22 = icmp eq i64 %index.next, %n.vec
  br i1 %22, 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 = %.lr.ph.us, %middle.block
  %.01516.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
  %23 = add i64 %.01516.us.ph, %12
  %24 = getelementptr inbounds double, ptr %0, i64 %23
  %25 = load double, ptr %24, align 8, !tbaa !5
  %26 = getelementptr inbounds double, ptr %1, i64 %23
  %27 = load double, ptr %26, align 8, !tbaa !5
  %28 = fsub double %25, %27
  %29 = getelementptr inbounds double, ptr %2, i64 %23
  store double %28, ptr %29, align 8, !tbaa !5
  %30 = or disjoint i64 %.01516.us.ph, 1
  br label %scalar.ph.prol.loopexit

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %.01516.us.unr = phi i64 [ %.01516.us.ph, %scalar.ph.preheader ], [ %30, %scalar.ph.prol ]
  %31 = icmp eq i64 %.01516.us.ph, %11
  br i1 %31, label %._crit_edge.us, label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %.01516.us = phi i64 [ %47, %scalar.ph ], [ %.01516.us.unr, %scalar.ph.prol.loopexit ]
  %32 = add i64 %.01516.us, %12
  %33 = getelementptr inbounds double, ptr %0, i64 %32
  %34 = load double, ptr %33, align 8, !tbaa !5
  %35 = getelementptr inbounds double, ptr %1, i64 %32
  %36 = load double, ptr %35, align 8, !tbaa !5
  %37 = fsub double %34, %36
  %38 = getelementptr inbounds double, ptr %2, i64 %32
  store double %37, ptr %38, align 8, !tbaa !5
  %39 = add nuw i64 %.01516.us, 1
  %40 = add i64 %39, %12
  %41 = getelementptr inbounds double, ptr %0, i64 %40
  %42 = load double, ptr %41, align 8, !tbaa !5
  %43 = getelementptr inbounds double, ptr %1, i64 %40
  %44 = load double, ptr %43, align 8, !tbaa !5
  %45 = fsub double %42, %44
  %46 = getelementptr inbounds double, ptr %2, i64 %40
  store double %45, ptr %46, align 8, !tbaa !5
  %47 = add nuw i64 %.01516.us, 2
  %exitcond.not.1 = icmp eq i64 %47, %4
  br i1 %exitcond.not.1, label %._crit_edge.us, label %scalar.ph, !llvm.loop !13

._crit_edge.us:                                   ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %48 = add nuw i64 %.017.us, 1
  %exitcond23.not = icmp eq i64 %48, %3
  br i1 %exitcond23.not, label %._crit_edge20, label %.lr.ph.us, !llvm.loop !14

._crit_edge20:                                    ; 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, !10, !11}
!14 = distinct !{!14, !10}


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