double deviation_cosine(double matrix1[3][3], double matrix2[3][3]) {
  double trace;
  int i, j;
  trace = 0;
  for (i = 0; i < 3; ++i)
    for (j = 0; j < 3; ++j)
      trace += matrix1[i][j] * matrix2[i][j];
  return (trace - 1.0) / 2.0;
}


x86 assembly (clang, -O0):
<deviation_cosine>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jge    1199 <deviation_cosine+0x99>
movl   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x20(%rbp)
jge    1186 <deviation_cosine+0x86>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
movslq -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
movslq -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x18(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    112c <deviation_cosine+0x2c>
jmp    118b <deviation_cosine+0x8b>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    111b <deviation_cosine+0x1b>
movsd  -0x18(%rbp),%xmm0
movsd  0xe62(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  0xe4e(%rip),%xmm1
divsd  %xmm1,%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<deviation_cosine>:
movsd  (%rdi),%xmm0
movsd  0x8(%rdi),%xmm1
mulsd  (%rsi),%xmm0
xorpd  %xmm2,%xmm2
addsd  %xmm0,%xmm2
mulsd  0x8(%rsi),%xmm1
addsd  %xmm2,%xmm1
movsd  0x10(%rdi),%xmm0
mulsd  0x10(%rsi),%xmm0
addsd  %xmm1,%xmm0
movsd  0x18(%rdi),%xmm1
mulsd  0x18(%rsi),%xmm1
addsd  %xmm0,%xmm1
movsd  0x20(%rdi),%xmm0
mulsd  0x20(%rsi),%xmm0
addsd  %xmm1,%xmm0
movsd  0x28(%rdi),%xmm1
mulsd  0x28(%rsi),%xmm1
addsd  %xmm0,%xmm1
movsd  0x30(%rdi),%xmm0
mulsd  0x30(%rsi),%xmm0
addsd  %xmm1,%xmm0
movsd  0x38(%rdi),%xmm1
mulsd  0x38(%rsi),%xmm1
addsd  %xmm0,%xmm1
movsd  0x40(%rdi),%xmm0
mulsd  0x40(%rsi),%xmm0
addsd  %xmm1,%xmm0
addsd  0xe78(%rip),%xmm0
mulsd  0xe78(%rip),%xmm0
ret

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

LLVM IR (-O2):
define double @deviation_cosine([3 x double]* nocapture %0, [3 x double]* nocapture %1) {
%3 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 0, i64 0
%4 = load double, double* %3
%5 = getelementptr inbounds [3 x double], [3 x double]* %1, i64 0, i64 0
%6 = load double, double* %5
%7 = tail call double @llvm.fmuladd.f64(double %4, double %6, double 0.000000e+00)
%8 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 0, i64 1
%9 = load double, double* %8
%10 = getelementptr inbounds [3 x double], [3 x double]* %1, i64 0, i64 1
%11 = load double, double* %10
%12 = tail call double @llvm.fmuladd.f64(double %9, double %11, double %7)
%13 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 0, i64 2
%14 = load double, double* %13
%15 = getelementptr inbounds [3 x double], [3 x double]* %1, i64 0, i64 2
%16 = load double, double* %15
%17 = tail call double @llvm.fmuladd.f64(double %14, double %16, double %12)
%18 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 1, i64 0
%19 = load double, double* %18
%20 = getelementptr inbounds [3 x double], [3 x double]* %1, i64 1, i64 0
%21 = load double, double* %20
%22 = tail call double @llvm.fmuladd.f64(double %19, double %21, double %17)
%23 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 1, i64 1
%24 = load double, double* %23
%25 = getelementptr inbounds [3 x double], [3 x double]* %1, i64 1, i64 1
%26 = load double, double* %25
%27 = tail call double @llvm.fmuladd.f64(double %24, double %26, double %22)
%28 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 1, i64 2
%29 = load double, double* %28
%30 = getelementptr inbounds [3 x double], [3 x double]* %1, i64 1, i64 2
%31 = load double, double* %30
%32 = tail call double @llvm.fmuladd.f64(double %29, double %31, double %27)
%33 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 2, i64 0
%34 = load double, double* %33
%35 = getelementptr inbounds [3 x double], [3 x double]* %1, i64 2, i64 0
%36 = load double, double* %35
%37 = tail call double @llvm.fmuladd.f64(double %34, double %36, double %32)
%38 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 2, i64 1
%39 = load double, double* %38
%40 = getelementptr inbounds [3 x double], [3 x double]* %1, i64 2, i64 1
%41 = load double, double* %40
%42 = tail call double @llvm.fmuladd.f64(double %39, double %41, double %37)
%43 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 2, i64 2
%44 = load double, double* %43
%45 = getelementptr inbounds [3 x double], [3 x double]* %1, i64 2, i64 2
%46 = load double, double* %45
%47 = tail call double @llvm.fmuladd.f64(double %44, double %46, double %42)
%48 = fadd double %47, -1.000000e+00
%49 = fmul double %48, 5.000000e-01
ret double %49
}