void cmatrix_add(double *m, const double *a, const double *b, int rows,
                 int cols) {
  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      int index = (((i)*cols) + (j));
      m[index] = a[index] + b[index];
    }
  }
}


x86 assembly (clang, -O0):
<cmatrix_add>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1192 <cmatrix_add+0x92>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    117f <cmatrix_add+0x7f>
mov    -0x24(%rbp),%eax
imul   -0x20(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
addsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1131 <cmatrix_add+0x31>
jmp    1184 <cmatrix_add+0x84>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    111e <cmatrix_add+0x1e>
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmatrix_add>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,-0x58(%rsp)
mov    %rsi,-0x60(%rsp)
mov    %rdi,-0x68(%rsp)
test   %ecx,%ecx
jle    13f8 <cmatrix_add+0x2f8>
movslq %r8d,%rdx
mov    %ecx,%r9d
mov    %edx,%eax
mov    %eax,%ecx
and    $0xfffffffc,%ecx
mov    %rcx,-0x30(%rsp)
add    $0xfffffffffffffffc,%rcx
mov    %rcx,-0x38(%rsp)
mov    %rcx,%rsi
shr    $0x2,%rsi
add    $0x1,%rsi
mov    %rsi,%rcx
mov    %rsi,-0x40(%rsp)
and    $0xfffffffffffffffe,%rsi
mov    %rsi,-0x48(%rsp)
mov    %rax,%rcx
neg    %rcx
mov    %rcx,-0x28(%rsp)
mov    -0x68(%rsp),%rdi
lea    0x30(%rdi),%rbx
mov    %rdx,-0x8(%rsp)
lea    0x0(,%rdx,8),%rsi
mov    -0x58(%rsp),%rcx
lea    0x30(%rcx),%rbp
mov    -0x60(%rsp),%rdx
lea    0x30(%rdx),%r14
lea    0x8(%rdx),%r11
lea    0x8(%rcx),%r15
lea    0x8(%rdi),%r10
mov    %r9,%rdi
xor    %r12d,%r12d
mov    %r8d,-0x6c(%rsp)
mov    %r9,-0x50(%rsp)
jmp    11cf <cmatrix_add+0xcf>
nopl   0x0(%rax)
add    $0x1,%r12
add    %rsi,%rbx
add    %rsi,%rbp
add    %rsi,%r14
add    %rsi,%r11
add    %rsi,%r15
add    %rsi,%r10
cmp    %rdi,%r12
je     13f8 <cmatrix_add+0x2f8>
test   %r8d,%r8d
jle    11b0 <cmatrix_add+0xb0>
mov    %r12,%r9
imul   -0x8(%rsp),%r9
cmp    $0x4,%r8d
jae    11f0 <cmatrix_add+0xf0>
xor    %edx,%edx
jmp    1370 <cmatrix_add+0x270>
nopw   0x0(%rax,%rax,1)
lea    (%r9,%rax,1),%rcx
mov    -0x68(%rsp),%rdx
lea    (%rdx,%r9,8),%r13
lea    (%rdx,%rcx,8),%rdx
mov    %rdx,-0x20(%rsp)
mov    -0x60(%rsp),%rdx
lea    (%rdx,%r9,8),%rdi
mov    %rdi,-0x18(%rsp)
lea    (%rdx,%rcx,8),%rdi
mov    -0x58(%rsp),%rdx
lea    (%rdx,%r9,8),%r8
mov    %r8,-0x10(%rsp)
lea    (%rdx,%rcx,8),%rcx
cmp    %rdi,%r13
setb   %dl
mov    -0x20(%rsp),%rdi
cmp    %rdi,-0x18(%rsp)
setb   %r8b
cmp    %rcx,%r13
setb   %cl
cmp    %rdi,-0x10(%rsp)
setb   %r13b
test   %r8b,%dl
jne    1357 <cmatrix_add+0x257>
mov    $0x0,%edx
and    %r13b,%cl
mov    -0x6c(%rsp),%r8d
mov    -0x50(%rsp),%rdi
jne    1370 <cmatrix_add+0x270>
cmpq   $0x0,-0x38(%rsp)
je     13e5 <cmatrix_add+0x2e5>
mov    -0x48(%rsp),%rcx
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movupd -0x30(%r14,%r13,8),%xmm0
movupd -0x20(%r14,%r13,8),%xmm1
movupd -0x30(%rbp,%r13,8),%xmm2
addpd  %xmm0,%xmm2
movupd -0x20(%rbp,%r13,8),%xmm0
addpd  %xmm1,%xmm0
movupd %xmm2,-0x30(%rbx,%r13,8)
movupd %xmm0,-0x20(%rbx,%r13,8)
movupd -0x10(%r14,%r13,8),%xmm0
movupd (%r14,%r13,8),%xmm1
movupd -0x10(%rbp,%r13,8),%xmm2
addpd  %xmm0,%xmm2
movupd 0x0(%rbp,%r13,8),%xmm0
addpd  %xmm1,%xmm0
movupd %xmm2,-0x10(%rbx,%r13,8)
movupd %xmm0,(%rbx,%r13,8)
add    $0x8,%r13
add    $0xfffffffffffffffe,%rcx
jne    1290 <cmatrix_add+0x190>
testb  $0x1,-0x40(%rsp)
je     1344 <cmatrix_add+0x244>
add    %r9,%r13
mov    -0x60(%rsp),%rcx
movupd (%rcx,%r13,8),%xmm0
movupd 0x10(%rcx,%r13,8),%xmm1
mov    -0x58(%rsp),%rcx
movupd (%rcx,%r13,8),%xmm2
addpd  %xmm0,%xmm2
movupd 0x10(%rcx,%r13,8),%xmm0
addpd  %xmm1,%xmm0
mov    -0x68(%rsp),%rcx
movupd %xmm2,(%rcx,%r13,8)
movupd %xmm0,0x10(%rcx,%r13,8)
mov    -0x30(%rsp),%rcx
mov    %rcx,%rdx
cmp    %rax,%rcx
je     11b0 <cmatrix_add+0xb0>
jmp    1370 <cmatrix_add+0x270>
xor    %edx,%edx
mov    -0x6c(%rsp),%r8d
mov    -0x50(%rsp),%rdi
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rdx,%rcx
test   $0x1,%al
je     13a2 <cmatrix_add+0x2a2>
add    %rdx,%r9
mov    -0x60(%rsp),%rcx
movsd  (%rcx,%r9,8),%xmm0
mov    -0x58(%rsp),%rcx
addsd  (%rcx,%r9,8),%xmm0
mov    -0x68(%rsp),%rcx
movsd  %xmm0,(%rcx,%r9,8)
mov    %rdx,%rcx
or     $0x1,%rcx
not    %rdx
cmp    -0x28(%rsp),%rdx
je     11b0 <cmatrix_add+0xb0>
movsd  -0x8(%r11,%rcx,8),%xmm0
addsd  -0x8(%r15,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%r10,%rcx,8)
movsd  (%r11,%rcx,8),%xmm0
addsd  (%r15,%rcx,8),%xmm0
movsd  %xmm0,(%r10,%rcx,8)
add    $0x2,%rcx
cmp    %rcx,%rax
jne    13b0 <cmatrix_add+0x2b0>
jmp    11b0 <cmatrix_add+0xb0>
xor    %r13d,%r13d
testb  $0x1,-0x40(%rsp)
jne    1303 <cmatrix_add+0x203>
jmp    1344 <cmatrix_add+0x244>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @cmatrix_add(double* nocapture writeonly %0, double* nocapture %1, double* nocapture %2, i32 %3, i32 %4) {
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %130
7: ; preds = %5
%8 = icmp sgt i32 %4, 0
%9 = sext i32 %4 to i64
%10 = zext i32 %3 to i64
%11 = zext i32 %4 to i64
%12 = and i64 %11, 4294967292
%13 = add nsw i64 %12, -4
%14 = lshr exact i64 %13, 2
%15 = add nuw nsw i64 %14, 1
%16 = icmp ult i32 %4, 4
%17 = and i64 %11, 4294967292
%18 = and i64 %15, 1
%19 = icmp eq i64 %13, 0
%20 = and i64 %15, 9223372036854775806
%21 = icmp eq i64 %18, 0
%22 = icmp eq i64 %17, %11
%23 = and i64 %11, 1
%24 = icmp eq i64 %23, 0
%25 = sub nsw i64 0, %11
br label %26
26: ; preds = %7, %131
%27 = phi i64 [ 0, %7 ], [ %132, %131 ]
%28 = mul i64 %27, %9
%29 = getelementptr double, double* %0, i64 %28
%30 = add i64 %28, %11
%31 = getelementptr double, double* %0, i64 %30
%32 = getelementptr double, double* %1, i64 %28
%33 = getelementptr double, double* %1, i64 %30
%34 = getelementptr double, double* %2, i64 %28
%35 = getelementptr double, double* %2, i64 %30
br i1 %8, label %36, label %131
36: ; preds = %26
%37 = mul nsw i64 %27, %9
br i1 %16, label %115, label %38
38: ; preds = %36
%39 = icmp ult double* %29, %33
%40 = icmp ult double* %32, %31
%41 = and i1 %39, %40
%42 = icmp ult double* %29, %35
%43 = icmp ult double* %34, %31
%44 = and i1 %42, %43
%45 = or i1 %41, %44
br i1 %45, label %115, label %46
46: ; preds = %38
br i1 %19, label %92, label %47
47: ; preds = %46, %47
%48 = phi i64 [ %89, %47 ], [ 0, %46 ]
%49 = phi i64 [ %90, %47 ], [ 0, %46 ]
%50 = add nsw i64 %48, %37
%51 = getelementptr inbounds double, double* %1, i64 %50
%52 = bitcast double* %51 to <2 x double>*
%53 = load <2 x double>, <2 x double>* %52
%54 = getelementptr inbounds double, double* %51, i64 2
%55 = bitcast double* %54 to <2 x double>*
%56 = load <2 x double>, <2 x double>* %55
%57 = getelementptr inbounds double, double* %2, i64 %50
%58 = bitcast double* %57 to <2 x double>*
%59 = load <2 x double>, <2 x double>* %58
%60 = getelementptr inbounds double, double* %57, i64 2
%61 = bitcast double* %60 to <2 x double>*
%62 = load <2 x double>, <2 x double>* %61
%63 = fadd <2 x double> %53, %59
%64 = fadd <2 x double> %56, %62
%65 = getelementptr inbounds double, double* %0, i64 %50
%66 = bitcast double* %65 to <2 x double>*
store <2 x double> %63, <2 x double>* %66
%67 = getelementptr inbounds double, double* %65, i64 2
%68 = bitcast double* %67 to <2 x double>*
store <2 x double> %64, <2 x double>* %68
%69 = or i64 %48, 4
%70 = add nsw i64 %69, %37
%71 = getelementptr inbounds double, double* %1, i64 %70
%72 = bitcast double* %71 to <2 x double>*
%73 = load <2 x double>, <2 x double>* %72
%74 = getelementptr inbounds double, double* %71, i64 2
%75 = bitcast double* %74 to <2 x double>*
%76 = load <2 x double>, <2 x double>* %75
%77 = getelementptr inbounds double, double* %2, i64 %70
%78 = bitcast double* %77 to <2 x double>*
%79 = load <2 x double>, <2 x double>* %78
%80 = getelementptr inbounds double, double* %77, i64 2
%81 = bitcast double* %80 to <2 x double>*
%82 = load <2 x double>, <2 x double>* %81
%83 = fadd <2 x double> %73, %79
%84 = fadd <2 x double> %76, %82
%85 = getelementptr inbounds double, double* %0, i64 %70
%86 = bitcast double* %85 to <2 x double>*
store <2 x double> %83, <2 x double>* %86
%87 = getelementptr inbounds double, double* %85, i64 2
%88 = bitcast double* %87 to <2 x double>*
store <2 x double> %84, <2 x double>* %88
%89 = add nuw i64 %48, 8
%90 = add i64 %49, 2
%91 = icmp eq i64 %90, %20
br i1 %91, label %92, label %47
92: ; preds = %47, %46
%93 = phi i64 [ 0, %46 ], [ %89, %47 ]
br i1 %21, label %114, label %94
94: ; preds = %92
%95 = add nsw i64 %93, %37
%96 = getelementptr inbounds double, double* %1, i64 %95
%97 = bitcast double* %96 to <2 x double>*
%98 = load <2 x double>, <2 x double>* %97
%99 = getelementptr inbounds double, double* %96, i64 2
%100 = bitcast double* %99 to <2 x double>*
%101 = load <2 x double>, <2 x double>* %100
%102 = getelementptr inbounds double, double* %2, i64 %95
%103 = bitcast double* %102 to <2 x double>*
%104 = load <2 x double>, <2 x double>* %103
%105 = getelementptr inbounds double, double* %102, i64 2
%106 = bitcast double* %105 to <2 x double>*
%107 = load <2 x double>, <2 x double>* %106
%108 = fadd <2 x double> %98, %104
%109 = fadd <2 x double> %101, %107
%110 = getelementptr inbounds double, double* %0, i64 %95
%111 = bitcast double* %110 to <2 x double>*
store <2 x double> %108, <2 x double>* %111
%112 = getelementptr inbounds double, double* %110, i64 2
%113 = bitcast double* %112 to <2 x double>*
store <2 x double> %109, <2 x double>* %113
br label %114
114: ; preds = %92, %94
br i1 %22, label %131, label %115
115: ; preds = %38, %36, %114
%116 = phi i64 [ 0, %38 ], [ 0, %36 ], [ %17, %114 ]
%117 = xor i64 %116, -1
br i1 %24, label %127, label %118
118: ; preds = %115
%119 = add nsw i64 %116, %37
%120 = getelementptr inbounds double, double* %1, i64 %119
%121 = load double, double* %120
%122 = getelementptr inbounds double, double* %2, i64 %119
%123 = load double, double* %122
%124 = fadd double %121, %123
%125 = getelementptr inbounds double, double* %0, i64 %119
store double %124, double* %125
%126 = or i64 %116, 1
br label %127
127: ; preds = %118, %115
%128 = phi i64 [ %116, %115 ], [ %126, %118 ]
%129 = icmp eq i64 %117, %25
br i1 %129, label %131, label %134
130: ; preds = %131, %5
ret void
131: ; preds = %127, %134, %114, %26
%132 = add nuw nsw i64 %27, 1
%133 = icmp eq i64 %132, %10
br i1 %133, label %130, label %26
134: ; preds = %127, %134
%135 = phi i64 [ %151, %134 ], [ %128, %127 ]
%136 = add nsw i64 %135, %37
%137 = getelementptr inbounds double, double* %1, i64 %136
%138 = load double, double* %137
%139 = getelementptr inbounds double, double* %2, i64 %136
%140 = load double, double* %139
%141 = fadd double %138, %140
%142 = getelementptr inbounds double, double* %0, i64 %136
store double %141, double* %142
%143 = add nuw nsw i64 %135, 1
%144 = add nsw i64 %143, %37
%145 = getelementptr inbounds double, double* %1, i64 %144
%146 = load double, double* %145
%147 = getelementptr inbounds double, double* %2, i64 %144
%148 = load double, double* %147
%149 = fadd double %146, %148
%150 = getelementptr inbounds double, double* %0, i64 %144
store double %149, double* %150
%151 = add nuw nsw i64 %135, 2
%152 = icmp eq i64 %151, %11
br i1 %152, label %131, label %134
}