; LLVM IR from AllAlgorithms/c :: algorithms/math/matrix-multiply-with-2d (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: nounwind uwtable
define dso_local void @MatrixMutiply(i32 noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5) #0 {
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  store i32 %0, ptr %7, align 4, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !5
  store ptr %3, ptr %10, align 8, !tbaa !9
  store ptr %4, ptr %11, align 8, !tbaa !9
  store ptr %5, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  store i32 0, ptr %14, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %71, %6
  %19 = load i32, ptr %14, align 4, !tbaa !5
  %20 = load i32, ptr %7, align 4, !tbaa !5
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  store i32 2, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  br label %74

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #2
  store i32 0, ptr %16, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %67, %23
  %25 = load i32, ptr %16, align 4, !tbaa !5
  %26 = load i32, ptr %9, align 4, !tbaa !5
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 5, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #2
  br label %70

29:                                               ; preds = %24
  store i32 0, ptr %13, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #2
  store i32 0, ptr %17, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %55, %29
  %31 = load i32, ptr %17, align 4, !tbaa !5
  %32 = load i32, ptr %8, align 4, !tbaa !5
  %33 = icmp slt i32 %31, %32
  br i1 %33, label %35, label %34

34:                                               ; preds = %30
  store i32 8, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #2
  br label %58

35:                                               ; preds = %30
  %36 = load ptr, ptr %10, align 8, !tbaa !9
  %37 = load i32, ptr %14, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [2 x i32], ptr %36, i64 %38
  %40 = load i32, ptr %17, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [2 x i32], ptr %39, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = load ptr, ptr %11, align 8, !tbaa !9
  %45 = load i32, ptr %17, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [2 x i32], ptr %44, i64 %46
  %48 = load i32, ptr %16, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [2 x i32], ptr %47, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = mul nsw i32 %43, %51
  %53 = load i32, ptr %13, align 4, !tbaa !5
  %54 = add nsw i32 %53, %52
  store i32 %54, ptr %13, align 4, !tbaa !5
  br label %55

55:                                               ; preds = %35
  %56 = load i32, ptr %17, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %17, align 4, !tbaa !5
  br label %30, !llvm.loop !11

58:                                               ; preds = %34
  %59 = load i32, ptr %13, align 4, !tbaa !5
  %60 = load ptr, ptr %12, align 8, !tbaa !9
  %61 = load i32, ptr %14, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [2 x i32], ptr %60, i64 %62
  %64 = load i32, ptr %16, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [2 x i32], ptr %63, i64 0, i64 %65
  store i32 %59, ptr %66, align 4, !tbaa !5
  br label %67

67:                                               ; preds = %58
  %68 = load i32, ptr %16, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %16, align 4, !tbaa !5
  br label %24, !llvm.loop !13

70:                                               ; preds = %28
  br label %71

71:                                               ; preds = %70
  %72 = load i32, ptr %14, align 4, !tbaa !5
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %14, align 4, !tbaa !5
  br label %18, !llvm.loop !14

74:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  ret void
}

; 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

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


; 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 @MatrixMutiply(i32 noundef %0, i32 noundef %1, i32 noundef %2, ptr nocapture noundef readonly %3, ptr nocapture noundef readonly %4, ptr nocapture noundef writeonly %5) local_unnamed_addr #0 {
  %7 = icmp sgt i32 %0, 0
  %8 = icmp sgt i32 %2, 0
  %or.cond = and i1 %7, %8
  br i1 %or.cond, label %.preheader22.lr.ph.split.us, label %._crit_edge

.preheader22.lr.ph.split.us:                      ; preds = %6
  %9 = icmp sgt i32 %1, 0
  br i1 %9, label %.preheader22.us.us.preheader, label %.preheader22.us.preheader

.preheader22.us.preheader:                        ; preds = %.preheader22.lr.ph.split.us
  %10 = zext nneg i32 %2 to i64
  %11 = shl nuw nsw i64 %10, 2
  %wide.trip.count = zext nneg i32 %0 to i64
  %xtraiter = and i64 %wide.trip.count, 7
  %12 = icmp ult i32 %0, 8
  br i1 %12, label %._crit_edge.loopexit57.unr-lcssa, label %.preheader22.us.preheader.new

.preheader22.us.preheader.new:                    ; preds = %.preheader22.us.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483640
  br label %.preheader22.us

.preheader22.us.us.preheader:                     ; preds = %.preheader22.lr.ph.split.us
  %wide.trip.count47 = zext nneg i32 %0 to i64
  %wide.trip.count42 = zext nneg i32 %2 to i64
  %wide.trip.count37 = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 9
  %n.mod.vf = and i64 %wide.trip.count37, 7
  %13 = icmp eq i64 %n.mod.vf, 0
  %14 = select i1 %13, i64 8, i64 %n.mod.vf
  %n.vec = sub nsw i64 %wide.trip.count37, %14
  br label %.preheader22.us.us

.preheader22.us.us:                               ; preds = %.preheader22.us.us.preheader, %._crit_edge26.split.us.us.us
  %indvars.iv44 = phi i64 [ 0, %.preheader22.us.us.preheader ], [ %indvars.iv.next45, %._crit_edge26.split.us.us.us ]
  br label %.preheader.us.us.us

.preheader.us.us.us:                              ; preds = %._crit_edge.us.us.us, %.preheader22.us.us
  %indvars.iv39 = phi i64 [ %indvars.iv.next40, %._crit_edge.us.us.us ], [ 0, %.preheader22.us.us ]
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %.preheader.us.us.us, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %.preheader.us.us.us ]
  %vec.phi = phi <4 x i32> [ %22, %vector.body ], [ zeroinitializer, %.preheader.us.us.us ]
  %vec.phi51 = phi <4 x i32> [ %23, %vector.body ], [ zeroinitializer, %.preheader.us.us.us ]
  %15 = or disjoint i64 %index, 4
  %16 = getelementptr inbounds [2 x i32], ptr %3, i64 %indvars.iv44, i64 %index
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  %wide.load = load <4 x i32>, ptr %16, align 4, !tbaa !5
  %wide.load52 = load <4 x i32>, ptr %17, align 4, !tbaa !5
  %18 = getelementptr inbounds [2 x i32], ptr %4, i64 %index, i64 %indvars.iv39
  %19 = getelementptr inbounds [2 x i32], ptr %4, i64 %15, i64 %indvars.iv39
  %wide.vec = load <8 x i32>, ptr %18, align 4, !tbaa !5
  %wide.vec53 = load <8 x i32>, ptr %19, align 4, !tbaa !5
  %strided.vec = shufflevector <8 x i32> %wide.vec, <8 x i32> poison, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
  %strided.vec54 = shufflevector <8 x i32> %wide.vec53, <8 x i32> poison, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
  %20 = mul nsw <4 x i32> %strided.vec, %wide.load
  %21 = mul nsw <4 x i32> %strided.vec54, %wide.load52
  %22 = add <4 x i32> %20, %vec.phi
  %23 = add <4 x i32> %21, %vec.phi51
  %index.next = add nuw i64 %index, 8
  %24 = icmp eq i64 %index.next, %n.vec
  br i1 %24, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %23, %22
  %25 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  br label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.preheader.us.us.us
  %indvars.iv.ph = phi i64 [ 0, %.preheader.us.us.us ], [ %n.vec, %middle.block ]
  %.02123.us.us.us.ph = phi i32 [ 0, %.preheader.us.us.us ], [ %25, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %.02123.us.us.us = phi i32 [ %31, %scalar.ph ], [ %.02123.us.us.us.ph, %scalar.ph.preheader ]
  %26 = getelementptr inbounds [2 x i32], ptr %3, i64 %indvars.iv44, i64 %indvars.iv
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = getelementptr inbounds [2 x i32], ptr %4, i64 %indvars.iv, i64 %indvars.iv39
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = mul nsw i32 %29, %27
  %31 = add nsw i32 %30, %.02123.us.us.us
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond38.not = icmp eq i64 %indvars.iv.next, %wide.trip.count37
  br i1 %exitcond38.not, label %._crit_edge.us.us.us, label %scalar.ph, !llvm.loop !13

._crit_edge.us.us.us:                             ; preds = %scalar.ph
  %32 = getelementptr inbounds [2 x i32], ptr %5, i64 %indvars.iv44, i64 %indvars.iv39
  store i32 %31, ptr %32, align 4, !tbaa !5
  %indvars.iv.next40 = add nuw nsw i64 %indvars.iv39, 1
  %exitcond43.not = icmp eq i64 %indvars.iv.next40, %wide.trip.count42
  br i1 %exitcond43.not, label %._crit_edge26.split.us.us.us, label %.preheader.us.us.us, !llvm.loop !14

._crit_edge26.split.us.us.us:                     ; preds = %._crit_edge.us.us.us
  %indvars.iv.next45 = add nuw nsw i64 %indvars.iv44, 1
  %exitcond48.not = icmp eq i64 %indvars.iv.next45, %wide.trip.count47
  br i1 %exitcond48.not, label %._crit_edge, label %.preheader22.us.us, !llvm.loop !15

.preheader22.us:                                  ; preds = %.preheader22.us, %.preheader22.us.preheader.new
  %indvar = phi i64 [ 0, %.preheader22.us.preheader.new ], [ %indvar.next.7, %.preheader22.us ]
  %niter = phi i64 [ 0, %.preheader22.us.preheader.new ], [ %niter.next.7, %.preheader22.us ]
  %33 = shl nuw nsw i64 %indvar, 3
  %scevgep = getelementptr i8, ptr %5, i64 %33
  tail call void @llvm.memset.p0.i64(ptr align 4 %scevgep, i8 0, i64 %11, i1 false), !tbaa !5
  %indvar.next = shl i64 %indvar, 3
  %34 = or disjoint i64 %indvar.next, 8
  %scevgep.1 = getelementptr i8, ptr %5, i64 %34
  tail call void @llvm.memset.p0.i64(ptr align 4 %scevgep.1, i8 0, i64 %11, i1 false), !tbaa !5
  %indvar.next.1 = shl i64 %indvar, 3
  %35 = or disjoint i64 %indvar.next.1, 16
  %scevgep.2 = getelementptr i8, ptr %5, i64 %35
  tail call void @llvm.memset.p0.i64(ptr align 4 %scevgep.2, i8 0, i64 %11, i1 false), !tbaa !5
  %indvar.next.2 = shl i64 %indvar, 3
  %36 = or disjoint i64 %indvar.next.2, 24
  %scevgep.3 = getelementptr i8, ptr %5, i64 %36
  tail call void @llvm.memset.p0.i64(ptr align 4 %scevgep.3, i8 0, i64 %11, i1 false), !tbaa !5
  %indvar.next.3 = shl i64 %indvar, 3
  %37 = or disjoint i64 %indvar.next.3, 32
  %scevgep.4 = getelementptr i8, ptr %5, i64 %37
  tail call void @llvm.memset.p0.i64(ptr align 4 %scevgep.4, i8 0, i64 %11, i1 false), !tbaa !5
  %indvar.next.4 = shl i64 %indvar, 3
  %38 = or disjoint i64 %indvar.next.4, 40
  %scevgep.5 = getelementptr i8, ptr %5, i64 %38
  tail call void @llvm.memset.p0.i64(ptr align 4 %scevgep.5, i8 0, i64 %11, i1 false), !tbaa !5
  %indvar.next.5 = shl i64 %indvar, 3
  %39 = or disjoint i64 %indvar.next.5, 48
  %scevgep.6 = getelementptr i8, ptr %5, i64 %39
  tail call void @llvm.memset.p0.i64(ptr align 4 %scevgep.6, i8 0, i64 %11, i1 false), !tbaa !5
  %indvar.next.6 = shl i64 %indvar, 3
  %40 = or disjoint i64 %indvar.next.6, 56
  %scevgep.7 = getelementptr i8, ptr %5, i64 %40
  tail call void @llvm.memset.p0.i64(ptr align 4 %scevgep.7, i8 0, i64 %11, i1 false), !tbaa !5
  %indvar.next.7 = add nuw nsw i64 %indvar, 8
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge.loopexit57.unr-lcssa, label %.preheader22.us, !llvm.loop !15

._crit_edge.loopexit57.unr-lcssa:                 ; preds = %.preheader22.us, %.preheader22.us.preheader
  %indvar.unr = phi i64 [ 0, %.preheader22.us.preheader ], [ %indvar.next.7, %.preheader22.us ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.preheader22.us.epil

.preheader22.us.epil:                             ; preds = %._crit_edge.loopexit57.unr-lcssa, %.preheader22.us.epil
  %indvar.epil = phi i64 [ %indvar.next.epil, %.preheader22.us.epil ], [ %indvar.unr, %._crit_edge.loopexit57.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.preheader22.us.epil ], [ 0, %._crit_edge.loopexit57.unr-lcssa ]
  %41 = shl nuw nsw i64 %indvar.epil, 3
  %scevgep.epil = getelementptr i8, ptr %5, i64 %41
  tail call void @llvm.memset.p0.i64(ptr align 4 %scevgep.epil, i8 0, i64 %11, i1 false), !tbaa !5
  %indvar.next.epil = add nuw nsw i64 %indvar.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, label %.preheader22.us.epil, !llvm.loop !16

._crit_edge:                                      ; preds = %._crit_edge.loopexit57.unr-lcssa, %.preheader22.us.epil, %._crit_edge26.split.us.us.us, %6
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #2

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" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !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, !12, !11}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.unroll.disable"}


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