; LLVM IR from Dev-XYS/Algorithms :: Matrix-Multiplication(Naive).ll (Z21matrix_multiplicationiii)

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

@A = external dso_local global [100 x [100 x i32]], align 16
@B = external dso_local global [100 x [100 x i32]], align 16
@R = external dso_local global [100 x [100 x i32]], align 16

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

11:                                               ; preds = %60, %3
  %12 = load i32, ptr %7, align 4, !tbaa !5
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %16, label %15

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

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

17:                                               ; preds = %56, %16
  %18 = load i32, ptr %9, align 4, !tbaa !5
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  store i32 5, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %59

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

23:                                               ; preds = %52, %22
  %24 = load i32, ptr %10, align 4, !tbaa !5
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %28, label %27

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

28:                                               ; preds = %23
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [100 x [100 x i32]], ptr @A, i64 0, i64 %30
  %32 = load i32, ptr %10, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [100 x i32], ptr %31, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = load i32, ptr %10, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [100 x [100 x i32]], ptr @B, i64 0, i64 %37
  %39 = load i32, ptr %9, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [100 x i32], ptr %38, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = mul nsw i32 %35, %42
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [100 x [100 x i32]], ptr @R, i64 0, i64 %45
  %47 = load i32, ptr %9, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [100 x i32], ptr %46, i64 0, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = add nsw i32 %50, %43
  store i32 %51, ptr %49, align 4, !tbaa !5
  br label %52

52:                                               ; preds = %28
  %53 = load i32, ptr %10, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %10, align 4, !tbaa !5
  br label %23, !llvm.loop !9

55:                                               ; preds = %27
  br label %56

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

59:                                               ; preds = %21
  br label %60

60:                                               ; preds = %59
  %61 = load i32, ptr %7, align 4, !tbaa !5
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %7, align 4, !tbaa !5
  br label %11, !llvm.loop !12

63:                                               ; preds = %15
  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 = { 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 = { 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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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

@A = external dso_local local_unnamed_addr global [100 x [100 x i32]], align 16
@B = external dso_local local_unnamed_addr global [100 x [100 x i32]], align 16
@R = external dso_local local_unnamed_addr global [100 x [100 x i32]], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z21matrix_multiplicationiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %1, 0
  %5 = icmp sgt i32 %2, 0
  %or.cond = and i1 %4, %5
  %6 = icmp sgt i32 %0, 0
  %or.cond39 = and i1 %or.cond, %6
  br i1 %or.cond39, label %.preheader17.us.us.preheader, label %._crit_edge

.preheader17.us.us.preheader:                     ; preds = %3
  %wide.trip.count37 = zext nneg i32 %1 to i64
  %wide.trip.count32 = zext nneg i32 %2 to i64
  %wide.trip.count = zext nneg i32 %0 to i64
  %min.iters.check = icmp ult i32 %0, 8
  %n.vec = and i64 %wide.trip.count, 2147483640
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br label %.preheader17.us.us

.preheader17.us.us:                               ; preds = %.preheader17.us.us.preheader, %._crit_edge20.split.us.us.us
  %indvars.iv34 = phi i64 [ 0, %.preheader17.us.us.preheader ], [ %indvars.iv.next35, %._crit_edge20.split.us.us.us ]
  br label %.preheader.us.us.us

.preheader.us.us.us:                              ; preds = %._crit_edge.us.us.us, %.preheader17.us.us
  %indvars.iv29 = phi i64 [ %indvars.iv.next30, %._crit_edge.us.us.us ], [ 0, %.preheader17.us.us ]
  %7 = getelementptr inbounds [100 x [100 x i32]], ptr @R, i64 0, i64 %indvars.iv34, i64 %indvars.iv29
  %.promoted.us.us.us = load i32, ptr %7, align 4, !tbaa !5
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.preheader.us.us.us
  %8 = insertelement <4 x i32> <i32 poison, i32 0, i32 0, i32 0>, i32 %.promoted.us.us.us, i64 0
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ %8, %vector.ph ], [ %44, %vector.body ]
  %vec.phi40 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %45, %vector.body ]
  %9 = or disjoint i64 %index, 1
  %10 = or disjoint i64 %index, 2
  %11 = or disjoint i64 %index, 3
  %12 = or disjoint i64 %index, 4
  %13 = or disjoint i64 %index, 5
  %14 = or disjoint i64 %index, 6
  %15 = or disjoint i64 %index, 7
  %16 = getelementptr inbounds [100 x [100 x i32]], ptr @A, i64 0, i64 %indvars.iv34, i64 %index
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  %wide.load = load <4 x i32>, ptr %16, align 16, !tbaa !5
  %wide.load41 = load <4 x i32>, ptr %17, align 16, !tbaa !5
  %18 = getelementptr inbounds [100 x [100 x i32]], ptr @B, i64 0, i64 %index, i64 %indvars.iv29
  %19 = getelementptr inbounds [100 x [100 x i32]], ptr @B, i64 0, i64 %9, i64 %indvars.iv29
  %20 = getelementptr inbounds [100 x [100 x i32]], ptr @B, i64 0, i64 %10, i64 %indvars.iv29
  %21 = getelementptr inbounds [100 x [100 x i32]], ptr @B, i64 0, i64 %11, i64 %indvars.iv29
  %22 = getelementptr inbounds [100 x [100 x i32]], ptr @B, i64 0, i64 %12, i64 %indvars.iv29
  %23 = getelementptr inbounds [100 x [100 x i32]], ptr @B, i64 0, i64 %13, i64 %indvars.iv29
  %24 = getelementptr inbounds [100 x [100 x i32]], ptr @B, i64 0, i64 %14, i64 %indvars.iv29
  %25 = getelementptr inbounds [100 x [100 x i32]], ptr @B, i64 0, i64 %15, i64 %indvars.iv29
  %26 = load i32, ptr %18, align 4, !tbaa !5
  %27 = load i32, ptr %19, align 4, !tbaa !5
  %28 = load i32, ptr %20, align 4, !tbaa !5
  %29 = load i32, ptr %21, align 4, !tbaa !5
  %30 = insertelement <4 x i32> poison, i32 %26, i64 0
  %31 = insertelement <4 x i32> %30, i32 %27, i64 1
  %32 = insertelement <4 x i32> %31, i32 %28, i64 2
  %33 = insertelement <4 x i32> %32, i32 %29, i64 3
  %34 = load i32, ptr %22, align 4, !tbaa !5
  %35 = load i32, ptr %23, align 4, !tbaa !5
  %36 = load i32, ptr %24, align 4, !tbaa !5
  %37 = load i32, ptr %25, align 4, !tbaa !5
  %38 = insertelement <4 x i32> poison, i32 %34, i64 0
  %39 = insertelement <4 x i32> %38, i32 %35, i64 1
  %40 = insertelement <4 x i32> %39, i32 %36, i64 2
  %41 = insertelement <4 x i32> %40, i32 %37, i64 3
  %42 = mul nsw <4 x i32> %33, %wide.load
  %43 = mul nsw <4 x i32> %41, %wide.load41
  %44 = add <4 x i32> %vec.phi, %42
  %45 = add <4 x i32> %vec.phi40, %43
  %index.next = add nuw i64 %index, 8
  %46 = icmp eq i64 %index.next, %n.vec
  br i1 %46, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %45, %44
  %47 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  br i1 %cmp.n, label %._crit_edge.us.us.us, 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 ]
  %.ph = phi i32 [ %.promoted.us.us.us, %.preheader.us.us.us ], [ %47, %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 ]
  %48 = phi i32 [ %54, %scalar.ph ], [ %.ph, %scalar.ph.preheader ]
  %49 = getelementptr inbounds [100 x [100 x i32]], ptr @A, i64 0, i64 %indvars.iv34, i64 %indvars.iv
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = getelementptr inbounds [100 x [100 x i32]], ptr @B, i64 0, i64 %indvars.iv, i64 %indvars.iv29
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = mul nsw i32 %52, %50
  %54 = add nsw i32 %48, %53
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge.us.us.us, label %scalar.ph, !llvm.loop !13

._crit_edge.us.us.us:                             ; preds = %scalar.ph, %middle.block
  %.lcssa = phi i32 [ %47, %middle.block ], [ %54, %scalar.ph ]
  store i32 %.lcssa, ptr %7, align 4, !tbaa !5
  %indvars.iv.next30 = add nuw nsw i64 %indvars.iv29, 1
  %exitcond33.not = icmp eq i64 %indvars.iv.next30, %wide.trip.count32
  br i1 %exitcond33.not, label %._crit_edge20.split.us.us.us, label %.preheader.us.us.us, !llvm.loop !14

._crit_edge20.split.us.us.us:                     ; preds = %._crit_edge.us.us.us
  %indvars.iv.next35 = add nuw nsw i64 %indvars.iv34, 1
  %exitcond38.not = icmp eq i64 %indvars.iv.next35, %wide.trip.count37
  br i1 %exitcond38.not, label %._crit_edge, label %.preheader17.us.us, !llvm.loop !15

._crit_edge:                                      ; preds = %._crit_edge20.split.us.us.us, %3
  ret void
}

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

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


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