double dInnerProduct(const double *a, const double *b, int num) {
  const double *end = a + num;
  double sum = 0;
  while (a != end) {
    sum += (*a) * (*b);
    ++a;
    ++b;
  }
  return sum;
}


x86 assembly (clang, -O0):
<dInnerProduct>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1177 <dInnerProduct+0x77>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
jmp    112a <dInnerProduct+0x2a>
movsd  -0x28(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<dInnerProduct>:
test   %edx,%edx
je     1138 <dInnerProduct+0x38>
movslq %edx,%rax
shl    $0x3,%rax
xorpd  %xmm0,%xmm0
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  (%rdi,%rcx,1),%xmm1
mulsd  (%rsi,%rcx,1),%xmm1
addsd  %xmm1,%xmm0
add    $0x8,%rcx
cmp    %rcx,%rax
jne    1120 <dInnerProduct+0x20>
ret
xorps  %xmm0,%xmm0
ret

LLVM IR (-O0):
define double @dInnerProduct(double* %0, double* %1, i32 %2) {
%4 = alloca double*
%5 = alloca double*
%6 = alloca i32
%7 = alloca double*
%8 = alloca double
store double* %0, double** %4
store double* %1, double** %5
store i32 %2, i32* %6
%9 = load double*, double** %4
%10 = load i32, i32* %6
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds double, double* %9, i64 %11
store double* %12, double** %7
store double 0.000000e+00, double* %8
br label %13
13: ; preds = %17, %3
%14 = load double*, double** %4
%15 = load double*, double** %7
%16 = icmp ne double* %14, %15
br i1 %16, label %17, label %28
17: ; preds = %13
%18 = load double*, double** %4
%19 = load double, double* %18
%20 = load double*, double** %5
%21 = load double, double* %20
%22 = load double, double* %8
%23 = call double @llvm.fmuladd.f64(double %19, double %21, double %22)
store double %23, double* %8
%24 = load double*, double** %4
%25 = getelementptr inbounds double, double* %24, i32 1
store double* %25, double** %4
%26 = load double*, double** %5
%27 = getelementptr inbounds double, double* %26, i32 1
store double* %27, double** %5
br label %13
28: ; preds = %13
%29 = load double, double* %8
ret double %29
}

LLVM IR (-O2):
define double @dInnerProduct(double* %0, double* nocapture %1, i32 %2) {
%4 = sext i32 %2 to i64
%5 = getelementptr inbounds double, double* %0, i64 %4
%6 = icmp eq i32 %2, 0
br i1 %6, label %17, label %7
7: ; preds = %3, %7
%8 = phi double [ %13, %7 ], [ 0.000000e+00, %3 ]
%9 = phi double* [ %14, %7 ], [ %0, %3 ]
%10 = phi double* [ %15, %7 ], [ %1, %3 ]
%11 = load double, double* %9
%12 = load double, double* %10
%13 = tail call double @llvm.fmuladd.f64(double %11, double %12, double %8)
%14 = getelementptr inbounds double, double* %9, i64 1
%15 = getelementptr inbounds double, double* %10, i64 1
%16 = icmp eq double* %14, %5
br i1 %16, label %17, label %7
17: ; preds = %7, %3
%18 = phi double [ 0.000000e+00, %3 ], [ %13, %7 ]
ret double %18
}