void smgen(double *a, double *eval, double *evec, int n) {
  double *p, *q, *ps, *r, *s, *t, *v = evec + n * n;
  for (ps = a, p = evec; p < v; p += n) {
    for (q = evec; q < v; q += n, ++ps) {
      *ps = 0.;
      for (r = eval, s = p, t = q; r < eval + n;)
        *ps += *r++ * *s++ * *t++;
    }
  }
}


x86 assembly (clang, -O0):
<smgen>:
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
mov    -0x1c(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x58(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x58(%rbp),%rax
jae    123b <smgen+0x13b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x58(%rbp),%rax
jae    121c <smgen+0x11c>
mov    -0x38(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
shl    $0x3,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    11f1 <smgen+0xf1>
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x40(%rbp)
movsd  (%rax),%xmm0
mov    -0x48(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x48(%rbp)
mulsd  (%rax),%xmm0
mov    -0x50(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x50(%rbp)
movsd  (%rax),%xmm2
mov    -0x38(%rbp),%rax
movsd  (%rax),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
jmp    1183 <smgen+0x83>
jmp    11f6 <smgen+0xf6>
mov    -0x1c(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
jmp    1152 <smgen+0x52>
jmp    1221 <smgen+0x121>
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    113c <smgen+0x3c>
pop    %rbp
ret

x86 assembly (clang, -O2):
<smgen>:
push   %r14
push   %rbx
mov    %ecx,%eax
imul   %eax,%eax
test   %eax,%eax
je     117c <smgen+0x7c>
lea    (%rdx,%rax,8),%r8
movslq %ecx,%r9
lea    (%rsi,%r9,8),%r10
shl    $0x3,%r9
mov    %rdx,%r11
jmp    1128 <smgen+0x28>
add    %r9,%r11
cmp    %r8,%r11
jae    117c <smgen+0x7c>
mov    %rdx,%r14
jmp    113c <smgen+0x3c>
nopl   (%rax)
add    %r9,%r14
add    $0x8,%rdi
cmp    %r8,%r14
jae    1120 <smgen+0x20>
movq   $0x0,(%rdi)
test   %ecx,%ecx
jle    1130 <smgen+0x30>
xorpd  %xmm0,%xmm0
xor    %ebx,%ebx
nopl   (%rax)
movsd  (%rsi,%rbx,1),%xmm1
mulsd  (%r11,%rbx,1),%xmm1
mulsd  (%r14,%rbx,1),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rdi)
lea    (%rsi,%rbx,1),%rax
add    $0x8,%rax
add    $0x8,%rbx
cmp    %r10,%rax
jb     1150 <smgen+0x50>
jmp    1130 <smgen+0x30>
pop    %rbx
pop    %r14
ret

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

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