void cf_diagonal_multiply(const int rows, const int cols,
                          double matrix[rows * cols], double diagonal[cols]) {
  int i, j;
  for (i = 0; i < rows; i++) {
    for (j = 0; j < cols; j++) {
      matrix[CF_INDEX(i, j, rows, cols)] *= diagonal[j];
    }
  }
}


x86 assembly (clang, -O0):
<cf_diagonal_multiply>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11b2 <cf_diagonal_multiply+0xa2>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    119f <cf_diagonal_multiply+0x8f>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
mov    $0x0,%al
call   1030 <CF_INDEX@plt>
movsd  -0x30(%rbp),%xmm0
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
mulsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1140 <cf_diagonal_multiply+0x30>
jmp    11a4 <cf_diagonal_multiply+0x94>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112d <cf_diagonal_multiply+0x1d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cf_diagonal_multiply>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,0xc(%rsp)
test   %edi,%edi
jle    119b <cf_diagonal_multiply+0x8b>
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %edi,%r12d
mov    0xc(%rsp),%ebp
xor    %r13d,%r13d
jmp    1149 <cf_diagonal_multiply+0x39>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r13d
cmp    %r12d,%r13d
je     119b <cf_diagonal_multiply+0x8b>
cmpl   $0x0,0xc(%rsp)
jle    1140 <cf_diagonal_multiply+0x30>
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  (%r14,%r15,8),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %r13d,%edi
mov    %r15d,%esi
mov    %r12d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1030 <CF_INDEX@plt>
cltq
movsd  0x10(%rsp),%xmm0
mulsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,(%rbx,%rax,8)
add    $0x1,%r15
cmp    %r15,%rbp
jne    1160 <cf_diagonal_multiply+0x50>
jmp    1140 <cf_diagonal_multiply+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @cf_diagonal_multiply(i32 %0, i32 %1, double* nocapture %2, double* nocapture %3) {
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %26
6: ; preds = %4
%7 = icmp sgt i32 %1, 0
%8 = zext i32 %1 to i64
br label %9
9: ; preds = %6, %23
%10 = phi i32 [ 0, %6 ], [ %24, %23 ]
br i1 %7, label %11, label %23
11: ; preds = %9, %11
%12 = phi i64 [ %21, %11 ], [ 0, %9 ]
%13 = getelementptr inbounds double, double* %3, i64 %12
%14 = load double, double* %13
%15 = trunc i64 %12 to i32
%16 = tail call i32 (i32, i32, i32, i32, ...) bitcast (i32 (...)* @CF_INDEX to i32 (i32, i32, i32, i32, ...)*)(i32 %10, i32 %15, i32 %0, i32 %1)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds double, double* %2, i64 %17
%19 = load double, double* %18
%20 = fmul double %14, %19
store double %20, double* %18
%21 = add nuw nsw i64 %12, 1
%22 = icmp eq i64 %21, %8
br i1 %22, label %23, label %11
23: ; preds = %11, %9
%24 = add nuw nsw i32 %10, 1
%25 = icmp eq i32 %24, %0
br i1 %25, label %26, label %9
26: ; preds = %23, %4
ret void
}