void energy(double *mj, double (*vj)[3], double *p, int nj, double *ke,
            double *pe) {
  int i, k;
  *pe = 0;
  *ke = 0;
  for (i = 0; i < nj; i++) {
    *pe += mj[i] * p[i];
    for (k = 0; k < 3; k++) {
      *ke += 0.5 * mj[i] * vj[i][k] * vj[i][k];
    }
  }
  *pe /= 2.0;
}


x86 assembly (clang, -O0):
<energy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x30(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
mov    -0x28(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11fd <energy+0xfd>
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mov    -0x30(%rbp),%rax
movsd  (%rax),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
movl   $0x0,-0x38(%rbp)
cmpl   $0x3,-0x38(%rbp)
jge    11ea <energy+0xea>
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  0xe75(%rip),%xmm0
mulsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
movslq -0x38(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
movslq -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mov    -0x28(%rbp),%rax
movsd  (%rax),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1179 <energy+0x79>
jmp    11ef <energy+0xef>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1138 <energy+0x38>
mov    -0x30(%rbp),%rax
movsd  (%rax),%xmm0
movsd  0xdf3(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<energy>:
movq   $0x0,(%r9)
movq   $0x0,(%r8)
test   %ecx,%ecx
jle    11ae <energy+0xae>
mov    %ecx,%eax
add    $0x10,%rsi
xor    %ecx,%ecx
movsd  0xeda(%rip),%xmm0
cs nopw 0x0(%rax,%rax,1)
movsd  (%rdi,%rcx,8),%xmm1
mulsd  (%rdx,%rcx,8),%xmm1
addsd  (%r9),%xmm1
movsd  %xmm1,(%r9)
movsd  (%rdi,%rcx,8),%xmm1
mulsd  %xmm0,%xmm1
movsd  -0x10(%rsi),%xmm2
mulsd  %xmm2,%xmm1
mulsd  %xmm2,%xmm1
addsd  (%r8),%xmm1
movsd  %xmm1,(%r8)
movsd  (%rdi,%rcx,8),%xmm2
mulsd  %xmm0,%xmm2
movsd  -0x8(%rsi),%xmm3
mulsd  %xmm3,%xmm2
mulsd  %xmm3,%xmm2
addsd  %xmm1,%xmm2
movsd  %xmm2,(%r8)
movsd  (%rdi,%rcx,8),%xmm1
mulsd  %xmm0,%xmm1
movsd  (%rsi),%xmm3
mulsd  %xmm3,%xmm1
mulsd  %xmm3,%xmm1
addsd  %xmm2,%xmm1
movsd  %xmm1,(%r8)
add    $0x1,%rcx
add    $0x18,%rsi
cmp    %rcx,%rax
jne    1130 <energy+0x30>
movsd  (%r9),%xmm0
mulsd  0xe45(%rip),%xmm0
movsd  %xmm0,(%r9)
ret

LLVM IR (-O0):
define void @energy(double* %0, [3 x double]* %1, double* %2, i32 %3, double* %4, double* %5) {
%7 = alloca double*
%8 = alloca [3 x double]*
%9 = alloca double*
%10 = alloca i32
%11 = alloca double*
%12 = alloca double*
%13 = alloca i32
%14 = alloca i32
store double* %0, double** %7
store [3 x double]* %1, [3 x double]** %8
store double* %2, double** %9
store i32 %3, i32* %10
store double* %4, double** %11
store double* %5, double** %12
%15 = load double*, double** %12
store double 0.000000e+00, double* %15
%16 = load double*, double** %11
store double 0.000000e+00, double* %16
store i32 0, i32* %13
br label %17
17: ; preds = %69, %6
%18 = load i32, i32* %13
%19 = load i32, i32* %10
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %72
21: ; preds = %17
%22 = load double*, double** %7
%23 = load i32, i32* %13
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds double, double* %22, i64 %24
%26 = load double, double* %25
%27 = load double*, double** %9
%28 = load i32, i32* %13
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds double, double* %27, i64 %29
%31 = load double, double* %30
%32 = load double*, double** %12
%33 = load double, double* %32
%34 = call double @llvm.fmuladd.f64(double %26, double %31, double %33)
store double %34, double* %32
store i32 0, i32* %14
br label %35
35: ; preds = %65, %21
%36 = load i32, i32* %14
%37 = icmp slt i32 %36, 3
br i1 %37, label %38, label %68
38: ; preds = %35
%39 = load double*, double** %7
%40 = load i32, i32* %13
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds double, double* %39, i64 %41
%43 = load double, double* %42
%44 = fmul double 5.000000e-01, %43
%45 = load [3 x double]*, [3 x double]** %8
%46 = load i32, i32* %13
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [3 x double], [3 x double]* %45, i64 %47
%49 = load i32, i32* %14
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [3 x double], [3 x double]* %48, i64 0, i64 %50
%52 = load double, double* %51
%53 = fmul double %44, %52
%54 = load [3 x double]*, [3 x double]** %8
%55 = load i32, i32* %13
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [3 x double], [3 x double]* %54, i64 %56
%58 = load i32, i32* %14
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [3 x double], [3 x double]* %57, i64 0, i64 %59
%61 = load double, double* %60
%62 = load double*, double** %11
%63 = load double, double* %62
%64 = call double @llvm.fmuladd.f64(double %53, double %61, double %63)
store double %64, double* %62
br label %65
65: ; preds = %38
%66 = load i32, i32* %14
%67 = add nsw i32 %66, 1
store i32 %67, i32* %14
br label %35
68: ; preds = %35
br label %69
69: ; preds = %68
%70 = load i32, i32* %13
%71 = add nsw i32 %70, 1
store i32 %71, i32* %13
br label %17
72: ; preds = %17
%73 = load double*, double** %12
%74 = load double, double* %73
%75 = fdiv double %74, 2.000000e+00
store double %75, double* %73
ret void
}

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