void vector_dot_double(const double *a, const double *b, double *c,
                       const unsigned int dim) {
  *c = 0.0;
  for (unsigned int i = 0; i < dim; i++) {
    *c += a[i] * b[i];
  }
}


x86 assembly (clang, -O0):
<vector_dot_double>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    116b <vector_dot_double+0x6b>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%ecx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
movsd  (%rax,%rcx,8),%xmm2
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1125 <vector_dot_double+0x25>
pop    %rbp
ret

x86 assembly (clang, -O2):
<vector_dot_double>:
movq   $0x0,(%rdx)
test   %ecx,%ecx
je     11cb <vector_dot_double+0xcb>
mov    %ecx,%ecx
lea    -0x1(%rcx),%rax
mov    %ecx,%r8d
and    $0x3,%r8d
cmp    $0x3,%rax
jae    112a <vector_dot_double+0x2a>
xorpd  %xmm0,%xmm0
xor    %eax,%eax
jmp    1197 <vector_dot_double+0x97>
and    $0xfffffffc,%ecx
xorpd  %xmm0,%xmm0
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  (%rdi,%rax,8),%xmm1
mulsd  (%rsi,%rax,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rdx)
movsd  0x8(%rdi,%rax,8),%xmm0
mulsd  0x8(%rsi,%rax,8),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rdx)
movsd  0x10(%rdi,%rax,8),%xmm1
mulsd  0x10(%rsi,%rax,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rdx)
movsd  0x18(%rdi,%rax,8),%xmm0
mulsd  0x18(%rsi,%rax,8),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rdx)
add    $0x4,%rax
cmp    %rax,%rcx
jne    1140 <vector_dot_double+0x40>
test   %r8,%r8
je     11cb <vector_dot_double+0xcb>
lea    (%rsi,%rax,8),%rcx
lea    (%rdi,%rax,8),%rax
xor    %esi,%esi
cs nopw 0x0(%rax,%rax,1)
movsd  (%rax,%rsi,8),%xmm1
mulsd  (%rcx,%rsi,8),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rdx)
add    $0x1,%rsi
cmp    %rsi,%r8
jne    11b0 <vector_dot_double+0xb0>
ret

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

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