double square_euclidean_distance(const double *a, const double *b, int k) {
  double d = 0, diff;
  while (--k >= 0) {
    diff = (a[k] - b[k]);
    d += diff * diff;
  }
  return d;
}


x86 assembly (clang, -O0):
<square_euclidean_distance>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jl     1169 <square_euclidean_distance+0x69>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
subsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1117 <square_euclidean_distance+0x17>
movsd  -0x20(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<square_euclidean_distance>:
test   %edx,%edx
jle    1115 <square_euclidean_distance+0x15>
mov    %edx,%eax
test   $0x1,%al
jne    1119 <square_euclidean_distance+0x19>
xorpd  %xmm0,%xmm0
cmp    $0x1,%edx
jne    113a <square_euclidean_distance+0x3a>
jmp    117a <square_euclidean_distance+0x7a>
xorps  %xmm0,%xmm0
ret
add    $0xffffffffffffffff,%rax
mov    %eax,%ecx
movsd  (%rdi,%rcx,8),%xmm1
subsd  (%rsi,%rcx,8),%xmm1
mulsd  %xmm1,%xmm1
xorpd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
cmp    $0x1,%edx
je     117a <square_euclidean_distance+0x7a>
add    $0xfffffffffffffffe,%rax
xchg   %ax,%ax
lea    0x1(%rax),%rcx
mov    %ecx,%edx
movsd  (%rdi,%rdx,8),%xmm1
subsd  (%rsi,%rdx,8),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
mov    %eax,%edx
shl    $0x3,%rdx
movsd  (%rdi,%rdx,1),%xmm0
subsd  (%rsi,%rdx,1),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
add    $0xfffffffffffffffe,%rax
cmp    $0x1,%rcx
ja     1140 <square_euclidean_distance+0x40>
ret

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

LLVM IR (-O2):
define double @square_euclidean_distance(double* nocapture %0, double* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %43
5: ; preds = %3
%6 = zext i32 %2 to i64
%7 = and i64 %6, 1
%8 = icmp eq i64 %7, 0
br i1 %8, label %18, label %9
9: ; preds = %5
%10 = add nsw i64 %6, -1
%11 = and i64 %10, 4294967295
%12 = getelementptr inbounds double, double* %0, i64 %11
%13 = load double, double* %12
%14 = getelementptr inbounds double, double* %1, i64 %11
%15 = load double, double* %14
%16 = fsub double %13, %15
%17 = tail call double @llvm.fmuladd.f64(double %16, double %16, double 0.000000e+00)
br label %18
18: ; preds = %9, %5
%19 = phi i64 [ %6, %5 ], [ %10, %9 ]
%20 = phi double [ 0.000000e+00, %5 ], [ %17, %9 ]
%21 = phi double [ undef, %5 ], [ %17, %9 ]
%22 = icmp eq i32 %2, 1
br i1 %22, label %43, label %23
23: ; preds = %18, %23
%24 = phi i64 [ %34, %23 ], [ %19, %18 ]
%25 = phi double [ %41, %23 ], [ %20, %18 ]
%26 = add nsw i64 %24, -1
%27 = and i64 %26, 4294967295
%28 = getelementptr inbounds double, double* %0, i64 %27
%29 = load double, double* %28
%30 = getelementptr inbounds double, double* %1, i64 %27
%31 = load double, double* %30
%32 = fsub double %29, %31
%33 = tail call double @llvm.fmuladd.f64(double %32, double %32, double %25)
%34 = add nsw i64 %24, -2
%35 = and i64 %34, 4294967295
%36 = getelementptr inbounds double, double* %0, i64 %35
%37 = load double, double* %36
%38 = getelementptr inbounds double, double* %1, i64 %35
%39 = load double, double* %38
%40 = fsub double %37, %39
%41 = tail call double @llvm.fmuladd.f64(double %40, double %40, double %33)
%42 = icmp ugt i64 %26, 1
br i1 %42, label %23, label %43
43: ; preds = %18, %23, %3
%44 = phi double [ 0.000000e+00, %3 ], [ %21, %18 ], [ %41, %23 ]
ret double %44
}