void matrix_multiply_3x3_3x3(const double a[9], const double b[9],
                             double r[9]) {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      double acc = 0;
      for (int k = 0; k < 3; k++)
        acc += a[3 * i + k] * b[j + 3 * k];
      r[i * 3 + j] = acc;
    }
  }
}


x86 assembly (clang, -O0):
<matrix_multiply_3x3_3x3>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jge    11cc <matrix_multiply_3x3_3x3+0xcc>
movl   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x20(%rbp)
jge    11b9 <matrix_multiply_3x3_3x3+0xb9>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3,-0x2c(%rbp)
jge    1193 <matrix_multiply_3x3_3x3+0x93>
mov    -0x8(%rbp),%rax
imul   $0x3,-0x1c(%rbp),%ecx
add    -0x2c(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
imul   $0x3,-0x2c(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1141 <matrix_multiply_3x3_3x3+0x41>
movsd  -0x28(%rbp),%xmm0
mov    -0x18(%rbp),%rax
imul   $0x3,-0x1c(%rbp),%ecx
add    -0x20(%rbp),%ecx
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1128 <matrix_multiply_3x3_3x3+0x28>
jmp    11be <matrix_multiply_3x3_3x3+0xbe>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1117 <matrix_multiply_3x3_3x3+0x17>
pop    %rbp
ret

x86 assembly (clang, -O2):
<matrix_multiply_3x3_3x3>:
movsd  (%rdi),%xmm1
movsd  0x8(%rdi),%xmm2
mulsd  (%rsi),%xmm1
xorpd  %xmm0,%xmm0
addsd  %xmm0,%xmm1
mulsd  0x18(%rsi),%xmm2
addsd  %xmm1,%xmm2
movsd  0x10(%rdi),%xmm1
mulsd  0x30(%rsi),%xmm1
addsd  %xmm2,%xmm1
movsd  %xmm1,(%rdx)
movsd  (%rdi),%xmm1
movsd  0x8(%rdi),%xmm2
mulsd  0x8(%rsi),%xmm1
addsd  %xmm0,%xmm1
mulsd  0x20(%rsi),%xmm2
addsd  %xmm1,%xmm2
movsd  0x10(%rdi),%xmm1
mulsd  0x38(%rsi),%xmm1
addsd  %xmm2,%xmm1
movsd  %xmm1,0x8(%rdx)
movsd  (%rdi),%xmm1
mulsd  0x10(%rsi),%xmm1
movsd  0x8(%rdi),%xmm2
addsd  %xmm0,%xmm1
mulsd  0x28(%rsi),%xmm2
addsd  %xmm1,%xmm2
movsd  0x10(%rdi),%xmm1
mulsd  0x40(%rsi),%xmm1
addsd  %xmm2,%xmm1
movsd  %xmm1,0x10(%rdx)
movsd  0x18(%rdi),%xmm1
movsd  0x20(%rdi),%xmm2
mulsd  (%rsi),%xmm1
mulsd  0x18(%rsi),%xmm2
addsd  %xmm0,%xmm1
movsd  0x28(%rdi),%xmm3
mulsd  0x30(%rsi),%xmm3
addsd  %xmm1,%xmm2
addsd  %xmm2,%xmm3
movsd  %xmm3,0x18(%rdx)
movsd  0x18(%rdi),%xmm1
movsd  0x20(%rdi),%xmm2
mulsd  0x8(%rsi),%xmm1
addsd  %xmm0,%xmm1
mulsd  0x20(%rsi),%xmm2
addsd  %xmm1,%xmm2
movsd  0x28(%rdi),%xmm1
mulsd  0x38(%rsi),%xmm1
addsd  %xmm2,%xmm1
movsd  %xmm1,0x20(%rdx)
movsd  0x18(%rdi),%xmm1
movsd  0x20(%rdi),%xmm2
mulsd  0x10(%rsi),%xmm1
addsd  %xmm0,%xmm1
mulsd  0x28(%rsi),%xmm2
addsd  %xmm1,%xmm2
movsd  0x28(%rdi),%xmm1
mulsd  0x40(%rsi),%xmm1
addsd  %xmm2,%xmm1
movsd  %xmm1,0x28(%rdx)
movsd  0x30(%rdi),%xmm1
mulsd  (%rsi),%xmm1
movsd  0x38(%rdi),%xmm2
addsd  %xmm0,%xmm1
mulsd  0x18(%rsi),%xmm2
addsd  %xmm1,%xmm2
movsd  0x40(%rdi),%xmm1
mulsd  0x30(%rsi),%xmm1
addsd  %xmm2,%xmm1
movsd  %xmm1,0x30(%rdx)
movsd  0x30(%rdi),%xmm1
movsd  0x38(%rdi),%xmm2
mulsd  0x8(%rsi),%xmm1
mulsd  0x20(%rsi),%xmm2
addsd  %xmm0,%xmm1
movsd  0x40(%rdi),%xmm3
mulsd  0x38(%rsi),%xmm3
addsd  %xmm1,%xmm2
addsd  %xmm2,%xmm3
movsd  %xmm3,0x38(%rdx)
movsd  0x30(%rdi),%xmm1
movsd  0x38(%rdi),%xmm2
mulsd  0x10(%rsi),%xmm1
addsd  %xmm0,%xmm1
mulsd  0x28(%rsi),%xmm2
addsd  %xmm1,%xmm2
movsd  0x40(%rdi),%xmm0
mulsd  0x40(%rsi),%xmm0
addsd  %xmm2,%xmm0
movsd  %xmm0,0x40(%rdx)
ret

LLVM IR (-O0):
define void @matrix_multiply_3x3_3x3(double* %0, double* %1, double* %2) {
%4 = alloca double*
%5 = alloca double*
%6 = alloca double*
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca i32
store double* %0, double** %4
store double* %1, double** %5
store double* %2, double** %6
store i32 0, i32* %7
br label %11
11: ; preds = %57, %3
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %60
14: ; preds = %11
store i32 0, i32* %8
br label %15
15: ; preds = %53, %14
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 3
br i1 %17, label %18, label %56
18: ; preds = %15
store double 0.000000e+00, double* %9
store i32 0, i32* %10
br label %19
19: ; preds = %41, %18
%20 = load i32, i32* %10
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %44
22: ; preds = %19
%23 = load double*, double** %4
%24 = load i32, i32* %7
%25 = mul nsw i32 3, %24
%26 = load i32, i32* %10
%27 = add nsw i32 %25, %26
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds double, double* %23, i64 %28
%30 = load double, double* %29
%31 = load double*, double** %5
%32 = load i32, i32* %8
%33 = load i32, i32* %10
%34 = mul nsw i32 3, %33
%35 = add nsw i32 %32, %34
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds double, double* %31, i64 %36
%38 = load double, double* %37
%39 = load double, double* %9
%40 = call double @llvm.fmuladd.f64(double %30, double %38, double %39)
store double %40, double* %9
br label %41
41: ; preds = %22
%42 = load i32, i32* %10
%43 = add nsw i32 %42, 1
store i32 %43, i32* %10
br label %19
44: ; preds = %19
%45 = load double, double* %9
%46 = load double*, double** %6
%47 = load i32, i32* %7
%48 = mul nsw i32 %47, 3
%49 = load i32, i32* %8
%50 = add nsw i32 %48, %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds double, double* %46, i64 %51
store double %45, double* %52
br label %53
53: ; preds = %44
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 1
store i32 %55, i32* %8
br label %15
56: ; preds = %15
br label %57
57: ; preds = %56
%58 = load i32, i32* %7
%59 = add nsw i32 %58, 1
store i32 %59, i32* %7
br label %11
60: ; preds = %11
ret void
}

LLVM IR (-O2):
define void @matrix_multiply_3x3_3x3(double* nocapture %0, double* nocapture %1, double* nocapture writeonly %2) {
%4 = load double, double* %0
%5 = load double, double* %1
%6 = tail call double @llvm.fmuladd.f64(double %4, double %5, double 0.000000e+00)
%7 = getelementptr inbounds double, double* %0, i64 1
%8 = load double, double* %7
%9 = getelementptr inbounds double, double* %1, i64 3
%10 = load double, double* %9
%11 = tail call double @llvm.fmuladd.f64(double %8, double %10, double %6)
%12 = getelementptr inbounds double, double* %0, i64 2
%13 = load double, double* %12
%14 = getelementptr inbounds double, double* %1, i64 6
%15 = load double, double* %14
%16 = tail call double @llvm.fmuladd.f64(double %13, double %15, double %11)
store double %16, double* %2
%17 = load double, double* %0
%18 = getelementptr inbounds double, double* %1, i64 1
%19 = load double, double* %18
%20 = tail call double @llvm.fmuladd.f64(double %17, double %19, double 0.000000e+00)
%21 = load double, double* %7
%22 = getelementptr inbounds double, double* %1, i64 4
%23 = load double, double* %22
%24 = tail call double @llvm.fmuladd.f64(double %21, double %23, double %20)
%25 = load double, double* %12
%26 = getelementptr inbounds double, double* %1, i64 7
%27 = load double, double* %26
%28 = tail call double @llvm.fmuladd.f64(double %25, double %27, double %24)
%29 = getelementptr inbounds double, double* %2, i64 1
store double %28, double* %29
%30 = load double, double* %0
%31 = getelementptr inbounds double, double* %1, i64 2
%32 = load double, double* %31
%33 = tail call double @llvm.fmuladd.f64(double %30, double %32, double 0.000000e+00)
%34 = load double, double* %7
%35 = getelementptr inbounds double, double* %1, i64 5
%36 = load double, double* %35
%37 = tail call double @llvm.fmuladd.f64(double %34, double %36, double %33)
%38 = load double, double* %12
%39 = getelementptr inbounds double, double* %1, i64 8
%40 = load double, double* %39
%41 = tail call double @llvm.fmuladd.f64(double %38, double %40, double %37)
%42 = getelementptr inbounds double, double* %2, i64 2
store double %41, double* %42
%43 = getelementptr inbounds double, double* %0, i64 3
%44 = load double, double* %43
%45 = load double, double* %1
%46 = tail call double @llvm.fmuladd.f64(double %44, double %45, double 0.000000e+00)
%47 = getelementptr inbounds double, double* %0, i64 4
%48 = load double, double* %47
%49 = load double, double* %9
%50 = tail call double @llvm.fmuladd.f64(double %48, double %49, double %46)
%51 = getelementptr inbounds double, double* %0, i64 5
%52 = load double, double* %51
%53 = load double, double* %14
%54 = tail call double @llvm.fmuladd.f64(double %52, double %53, double %50)
%55 = getelementptr inbounds double, double* %2, i64 3
store double %54, double* %55
%56 = load double, double* %43
%57 = load double, double* %18
%58 = tail call double @llvm.fmuladd.f64(double %56, double %57, double 0.000000e+00)
%59 = load double, double* %47
%60 = load double, double* %22
%61 = tail call double @llvm.fmuladd.f64(double %59, double %60, double %58)
%62 = load double, double* %51
%63 = load double, double* %26
%64 = tail call double @llvm.fmuladd.f64(double %62, double %63, double %61)
%65 = getelementptr inbounds double, double* %2, i64 4
store double %64, double* %65
%66 = load double, double* %43
%67 = load double, double* %31
%68 = tail call double @llvm.fmuladd.f64(double %66, double %67, double 0.000000e+00)
%69 = load double, double* %47
%70 = load double, double* %35
%71 = tail call double @llvm.fmuladd.f64(double %69, double %70, double %68)
%72 = load double, double* %51
%73 = load double, double* %39
%74 = tail call double @llvm.fmuladd.f64(double %72, double %73, double %71)
%75 = getelementptr inbounds double, double* %2, i64 5
store double %74, double* %75
%76 = getelementptr inbounds double, double* %0, i64 6
%77 = load double, double* %76
%78 = load double, double* %1
%79 = tail call double @llvm.fmuladd.f64(double %77, double %78, double 0.000000e+00)
%80 = getelementptr inbounds double, double* %0, i64 7
%81 = load double, double* %80
%82 = load double, double* %9
%83 = tail call double @llvm.fmuladd.f64(double %81, double %82, double %79)
%84 = getelementptr inbounds double, double* %0, i64 8
%85 = load double, double* %84
%86 = load double, double* %14
%87 = tail call double @llvm.fmuladd.f64(double %85, double %86, double %83)
%88 = getelementptr inbounds double, double* %2, i64 6
store double %87, double* %88
%89 = load double, double* %76
%90 = load double, double* %18
%91 = tail call double @llvm.fmuladd.f64(double %89, double %90, double 0.000000e+00)
%92 = load double, double* %80
%93 = load double, double* %22
%94 = tail call double @llvm.fmuladd.f64(double %92, double %93, double %91)
%95 = load double, double* %84
%96 = load double, double* %26
%97 = tail call double @llvm.fmuladd.f64(double %95, double %96, double %94)
%98 = getelementptr inbounds double, double* %2, i64 7
store double %97, double* %98
%99 = load double, double* %76
%100 = load double, double* %31
%101 = tail call double @llvm.fmuladd.f64(double %99, double %100, double 0.000000e+00)
%102 = load double, double* %80
%103 = load double, double* %35
%104 = tail call double @llvm.fmuladd.f64(double %102, double %103, double %101)
%105 = load double, double* %84
%106 = load double, double* %39
%107 = tail call double @llvm.fmuladd.f64(double %105, double %106, double %104)
%108 = getelementptr inbounds double, double* %2, i64 8
store double %107, double* %108
ret void
}