double kinetic_energy(double velocities[][3], double m, int amount) {
  int i, j;
  double vel, kinetic = 0;
  for (i = 0; i < amount; i++) {
    for (j = 0; j < 3; j++) {
      vel = velocities[i][j];
      kinetic += (m * vel * vel) / 2.;
    }
  }
  return kinetic;
}


x86 assembly (clang, -O0):
<kinetic_energy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119f <kinetic_energy+0x9f>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jge    118c <kinetic_energy+0x8c>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  0xe90(%rip),%xmm1
divsd  %xmm1,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1132 <kinetic_energy+0x32>
jmp    1191 <kinetic_energy+0x91>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    111f <kinetic_energy+0x1f>
movsd  -0x30(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<kinetic_energy>:
test   %esi,%esi
jle    1179 <kinetic_energy+0x79>
mov    %esi,%eax
movapd %xmm0,%xmm2
unpcklpd %xmm0,%xmm2
shl    $0x3,%rax
lea    (%rax,%rax,2),%rax
xorpd  %xmm1,%xmm1
xor    %ecx,%ecx
movsd  0xedc(%rip),%xmm3
movapd 0xee4(%rip),%xmm4
nopl   0x0(%rax)
movsd  (%rdi,%rcx,1),%xmm5
movapd %xmm5,%xmm6
mulsd  %xmm0,%xmm6
mulsd  %xmm5,%xmm6
mulsd  %xmm3,%xmm6
addsd  %xmm1,%xmm6
movupd 0x8(%rdi,%rcx,1),%xmm5
movapd %xmm5,%xmm1
mulpd  %xmm2,%xmm1
mulpd  %xmm5,%xmm1
mulpd  %xmm4,%xmm1
addsd  %xmm1,%xmm6
unpckhpd %xmm1,%xmm1
addsd  %xmm6,%xmm1
add    $0x18,%rcx
cmp    %rcx,%rax
jne    1130 <kinetic_energy+0x30>
movapd %xmm1,%xmm0
ret
xorps  %xmm0,%xmm0
ret

LLVM IR (-O0):
define double @kinetic_energy([3 x double]* %0, double %1, i32 %2) {
%4 = alloca [3 x double]*
%5 = alloca double
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca double
store [3 x double]* %0, [3 x double]** %4
store double %1, double* %5
store i32 %2, i32* %6
store double 0.000000e+00, double* %10
store i32 0, i32* %7
br label %11
11: ; preds = %40, %3
%12 = load i32, i32* %7
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %43
15: ; preds = %11
store i32 0, i32* %8
br label %16
16: ; preds = %36, %15
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 3
br i1 %18, label %19, label %39
19: ; preds = %16
%20 = load [3 x double]*, [3 x double]** %4
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [3 x double], [3 x double]* %20, i64 %22
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [3 x double], [3 x double]* %23, i64 0, i64 %25
%27 = load double, double* %26
store double %27, double* %9
%28 = load double, double* %5
%29 = load double, double* %9
%30 = fmul double %28, %29
%31 = load double, double* %9
%32 = fmul double %30, %31
%33 = fdiv double %32, 2.000000e+00
%34 = load double, double* %10
%35 = fadd double %34, %33
store double %35, double* %10
br label %36
36: ; preds = %19
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 1
store i32 %38, i32* %8
br label %16
39: ; preds = %16
br label %40
40: ; preds = %39
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %11
43: ; preds = %11
%44 = load double, double* %10
ret double %44
}

LLVM IR (-O2):
define double @kinetic_energy([3 x double]* nocapture %0, double %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %30
5: ; preds = %3
%6 = zext i32 %2 to i64
%7 = insertelement <2 x double> poison, double %1, i64 0
%8 = shufflevector <2 x double> %7, <2 x double> poison, <2 x i32> zeroinitializer
br label %9
9: ; preds = %5, %9
%10 = phi i64 [ 0, %5 ], [ %28, %9 ]
%11 = phi double [ 0.000000e+00, %5 ], [ %27, %9 ]
%12 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 %10, i64 0
%13 = load double, double* %12
%14 = fmul double %13, %1
%15 = fmul double %13, %14
%16 = fmul double %15, 5.000000e-01
%17 = fadd double %11, %16
%18 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 %10, i64 1
%19 = bitcast double* %18 to <2 x double>*
%20 = load <2 x double>, <2 x double>* %19
%21 = fmul <2 x double> %20, %8
%22 = fmul <2 x double> %20, %21
%23 = fmul <2 x double> %22, <double 5.000000e-01, double 5.000000e-01>
%24 = extractelement <2 x double> %23, i64 0
%25 = fadd double %17, %24
%26 = extractelement <2 x double> %23, i64 1
%27 = fadd double %25, %26
%28 = add nuw nsw i64 %10, 1
%29 = icmp eq i64 %28, %6
br i1 %29, label %30, label %9
30: ; preds = %9, %3
%31 = phi double [ 0.000000e+00, %3 ], [ %27, %9 ]
ret double %31
}