#include <stdlib.h>
#include "MatMulC.h"

void MatMulC(int rows, int len, double *a, double *bin, double *c)
{
  int i,j,k;
  double *b = malloc(len * sizeof(double));

  // Transpose bin with result in b.
  for(int n = 0; n < len; n++ ) {
    b[n] = bin[rows * (n % rows) + (n / rows)];
  }

  for( i = 0; i < rows; i++ ) {
    for( j = 0; j < rows; j+=2 ) {
      double sum0 = 0.0;
      double sum1 = 0.0;
      for( k = 0; k < rows; k++ ) {
        sum0 += a[i*rows+k] * b[j*rows + k];
        sum1 += a[i*rows+k] * b[(j + 1)*rows + k];
      }
      c[i*rows + j] = sum0;
      c[i*rows + j+1] = sum1;
    }
  }

  free(b);
}

#include <stdlib.h>
#include "MatMulC.h"

void MatMulC(int rows, int len, double *a, double *b, double *c)
{
  int i,j,k;

  for( i = 0; i < rows; i++ ) {
    for( j = 0; j < rows; j+=2 ) {
      double sum0 = 0.0;
      double sum1 = 0.0;
      for( k = 0; k < rows; k++ ) {
        sum0 += a[i*rows+k] * b[k*rows + j];
        sum1 += a[i*rows+k] * b[k*rows + j + 1];
      }
      c[i*rows + j] = sum0;
      c[i*rows + j+1] = sum1;
    }
  }
}

--- 
+++ 
@@ -1,20 +1,28 @@
 #include <stdlib.h>
 #include "MatMulC.h"
 
-void MatMulC(int rows, int len, double *a, double *b, double *c)
+void MatMulC(int rows, int len, double *a, double *bin, double *c)
 {
   int i,j,k;
+  double *b = malloc(len * sizeof(double));
+
+  // Transpose bin with result in b.
+  for(int n = 0; n < len; n++ ) {
+    b[n] = bin[rows * (n % rows) + (n / rows)];
+  }
 
   for( i = 0; i < rows; i++ ) {
     for( j = 0; j < rows; j+=2 ) {
       double sum0 = 0.0;
       double sum1 = 0.0;
       for( k = 0; k < rows; k++ ) {
-        sum0 += a[i*rows+k] * b[k*rows + j];
-        sum1 += a[i*rows+k] * b[k*rows + j + 1];
+        sum0 += a[i*rows+k] * b[j*rows + k];
+        sum1 += a[i*rows+k] * b[(j + 1)*rows + k];
       }
       c[i*rows + j] = sum0;
       c[i*rows + j+1] = sum1;
     }
   }
+
+  free(b);
 }
Transpose the second matrix in the C reference for MatMul.
