; LLVM IR from AllAlgorithms/c :: algorithms/math/matrix-multiplication (matmul)

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

@first_mat = external dso_local global [50 x [50 x double]], align 16
@second_mat = external dso_local global [50 x [50 x double]], align 16
@multi_result = external dso_local global [50 x [50 x double]], align 16

; 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 @MatrixMultiplication() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %55, %0
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 50
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  store i32 2, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %58

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

11:                                               ; preds = %51, %10
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = icmp slt i32 %12, 50
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  store i32 5, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %54

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

16:                                               ; preds = %39, %15
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = icmp slt i32 %17, 50
  br i1 %18, label %20, label %19

19:                                               ; preds = %16
  store i32 8, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %42

20:                                               ; preds = %16
  %21 = load i32, ptr %1, align 4, !tbaa !5
  %22 = sitofp i32 %21 to double
  %23 = load i32, ptr %2, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [50 x [50 x double]], ptr @first_mat, i64 0, i64 %24
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [50 x double], ptr %25, i64 0, i64 %27
  %29 = load double, ptr %28, align 8, !tbaa !9
  %30 = load i32, ptr %5, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [50 x [50 x double]], ptr @second_mat, i64 0, i64 %31
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [50 x double], ptr %32, i64 0, i64 %34
  %36 = load double, ptr %35, align 8, !tbaa !9
  %37 = call double @llvm.fmuladd.f64(double %29, double %36, double %22)
  %38 = fptosi double %37 to i32
  store i32 %38, ptr %1, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %20
  %40 = load i32, ptr %5, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %5, align 4, !tbaa !5
  br label %16, !llvm.loop !11

42:                                               ; preds = %19
  %43 = load i32, ptr %1, align 4, !tbaa !5
  %44 = sitofp i32 %43 to double
  %45 = load i32, ptr %2, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [50 x [50 x double]], ptr @multi_result, i64 0, i64 %46
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [50 x double], ptr %47, i64 0, i64 %49
  store double %44, ptr %50, align 8, !tbaa !9
  br label %51

51:                                               ; preds = %42
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %4, align 4, !tbaa !5
  br label %11, !llvm.loop !13

54:                                               ; preds = %14
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %2, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %2, align 4, !tbaa !5
  br label %6, !llvm.loop !14

58:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

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

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 = !{!"double", !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"

@first_mat = external dso_local local_unnamed_addr global [50 x [50 x double]], align 16
@second_mat = external dso_local local_unnamed_addr global [50 x [50 x double]], align 16
@multi_result = external dso_local local_unnamed_addr global [50 x [50 x double]], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @MatrixMultiplication() local_unnamed_addr #0 {
  br label %.preheader17

.preheader17:                                     ; preds = %0, %2
  %indvars.iv27 = phi i64 [ 0, %0 ], [ %indvars.iv.next28, %2 ]
  br label %.preheader

1:                                                ; preds = %2
  ret void

.preheader:                                       ; preds = %.preheader17, %3
  %indvars.iv23 = phi i64 [ 0, %.preheader17 ], [ %indvars.iv.next24, %3 ]
  br label %6

2:                                                ; preds = %3
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 1
  %exitcond30.not = icmp eq i64 %indvars.iv.next28, 50
  br i1 %exitcond30.not, label %1, label %.preheader17, !llvm.loop !5

3:                                                ; preds = %6
  %4 = sitofp i32 %20 to double
  %5 = getelementptr inbounds [50 x [50 x double]], ptr @multi_result, i64 0, i64 %indvars.iv27, i64 %indvars.iv23
  store double %4, ptr %5, align 8, !tbaa !7
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, 1
  %exitcond26.not = icmp eq i64 %indvars.iv.next24, 50
  br i1 %exitcond26.not, label %2, label %.preheader, !llvm.loop !11

6:                                                ; preds = %6, %.preheader
  %indvars.iv = phi i64 [ 0, %.preheader ], [ %indvars.iv.next.1, %6 ]
  %.01618 = phi i32 [ 0, %.preheader ], [ %20, %6 ]
  %7 = sitofp i32 %.01618 to double
  %8 = getelementptr inbounds [50 x [50 x double]], ptr @first_mat, i64 0, i64 %indvars.iv27, i64 %indvars.iv
  %9 = load double, ptr %8, align 16, !tbaa !7
  %10 = getelementptr inbounds [50 x [50 x double]], ptr @second_mat, i64 0, i64 %indvars.iv, i64 %indvars.iv23
  %11 = load double, ptr %10, align 8, !tbaa !7
  %12 = tail call double @llvm.fmuladd.f64(double %9, double %11, double %7)
  %13 = fptosi double %12 to i32
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %14 = sitofp i32 %13 to double
  %15 = getelementptr inbounds [50 x [50 x double]], ptr @first_mat, i64 0, i64 %indvars.iv27, i64 %indvars.iv.next
  %16 = load double, ptr %15, align 8, !tbaa !7
  %17 = getelementptr inbounds [50 x [50 x double]], ptr @second_mat, i64 0, i64 %indvars.iv.next, i64 %indvars.iv23
  %18 = load double, ptr %17, align 8, !tbaa !7
  %19 = tail call double @llvm.fmuladd.f64(double %16, double %18, double %14)
  %20 = fptosi double %19 to i32
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next.1, 50
  br i1 %exitcond.not.1, label %3, label %6, !llvm.loop !12
}

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

attributes #0 = { 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 = { mustprogress 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"double", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = distinct !{!11, !6}
!12 = distinct !{!12, !6}


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