void quaternion_to_rotation_matrix(double **R, double *q) {
  double a = q[0];
  double b = q[1];
  double c = q[2];
  double d = q[3];
  R[0][0] = a * a + b * b - c * c - d * d;
  R[0][1] = 2 * b * c - 2 * a * d;
  R[0][2] = 2 * b * d + 2 * a * c;
  R[1][0] = 2 * b * c + 2 * a * d;
  R[1][1] = a * a - b * b + c * c - d * d;
  R[1][2] = 2 * c * d - 2 * a * b;
  R[2][0] = 2 * b * d - 2 * a * c;
  R[2][1] = 2 * c * d + 2 * a * b;
  R[2][2] = a * a - b * b - c * c + d * d;
}


x86 assembly (clang, -O0):
<quaternion_to_rotation_matrix>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsd  0x8(%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsd  0x10(%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsd  0x18(%rax),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  -0x28(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movsd  %xmm0,(%rax)
movsd  0xe3c(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm2
movsd  0xe2a(%rip),%xmm1
mulsd  -0x18(%rbp),%xmm1
mulsd  -0x30(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movsd  %xmm0,0x8(%rax)
movsd  0xded(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
movsd  0xddb(%rip),%xmm1
mulsd  -0x18(%rbp),%xmm1
mulsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movsd  %xmm0,0x10(%rax)
movsd  0xdb5(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm2
movsd  0xda3(%rip),%xmm1
mulsd  -0x18(%rbp),%xmm1
mulsd  -0x30(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
movsd  %xmm0,(%rax)
movsd  -0x18(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  -0x20(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  -0x28(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
movsd  %xmm0,0x8(%rax)
movsd  0xd02(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
movsd  0xcf0(%rip),%xmm1
mulsd  -0x18(%rbp),%xmm1
mulsd  -0x20(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
movsd  %xmm0,0x10(%rax)
movsd  0xcb2(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
movsd  0xca0(%rip),%xmm1
mulsd  -0x18(%rbp),%xmm1
mulsd  -0x28(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rax
movsd  %xmm0,(%rax)
movsd  0xc63(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
movsd  0xc51(%rip),%xmm1
mulsd  -0x18(%rbp),%xmm1
mulsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rax
movsd  %xmm0,0x8(%rax)
movsd  -0x18(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  -0x20(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  -0x28(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rax
movsd  %xmm0,0x10(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<quaternion_to_rotation_matrix>:
movsd  (%rsi),%xmm10
movsd  0x8(%rsi),%xmm3
movupd 0x10(%rsi),%xmm12
movapd %xmm10,%xmm9
addsd  %xmm10,%xmm10
movapd %xmm10,%xmm11
movapd %xmm3,%xmm4
movapd %xmm10,%xmm6
mulsd  %xmm3,%xmm10
mulsd  %xmm3,%xmm3
mulsd  %xmm9,%xmm9
movapd %xmm9,%xmm7
addsd  %xmm3,%xmm7
movapd 0xeb8(%rip),%xmm8
movapd %xmm12,%xmm2
xorpd  %xmm8,%xmm2
mulsd  %xmm12,%xmm2
addsd  %xmm2,%xmm7
movapd %xmm12,%xmm0
unpckhpd %xmm12,%xmm0
movapd %xmm0,%xmm5
xorpd  %xmm8,%xmm5
mov    (%rdi),%rax
mulsd  %xmm0,%xmm11
movapd %xmm11,%xmm1
xorpd  %xmm8,%xmm1
unpcklpd %xmm1,%xmm7
unpcklpd %xmm12,%xmm4
addpd  %xmm4,%xmm4
movapd %xmm4,%xmm1
unpcklpd %xmm5,%xmm1
mulpd  %xmm12,%xmm1
mulsd  %xmm12,%xmm6
subsd  %xmm3,%xmm9
mulsd  %xmm12,%xmm12
addsd  %xmm9,%xmm12
unpcklpd %xmm12,%xmm11
addpd  %xmm1,%xmm11
shufpd $0x1,%xmm1,%xmm1
addpd  %xmm7,%xmm1
movupd %xmm1,(%rax)
movapd %xmm4,%xmm1
mulsd  %xmm0,%xmm1
addsd  %xmm6,%xmm1
movsd  %xmm1,0x10(%rax)
mov    0x8(%rdi),%rax
movupd %xmm11,(%rax)
movapd %xmm4,%xmm1
unpckhpd %xmm4,%xmm1
mulsd  %xmm0,%xmm1
subsd  %xmm10,%xmm1
movsd  %xmm1,0x10(%rax)
xorpd  %xmm8,%xmm6
mov    0x10(%rdi),%rax
unpcklpd %xmm10,%xmm6
mulpd  %xmm0,%xmm4
addpd  %xmm6,%xmm4
movupd %xmm4,(%rax)
addsd  %xmm2,%xmm9
mulsd  %xmm0,%xmm0
addsd  %xmm9,%xmm0
movsd  %xmm0,0x10(%rax)
ret

LLVM IR (-O0):
define void @quaternion_to_rotation_matrix(double** %0, double* %1) {
%3 = alloca double**
%4 = alloca double*
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
store double** %0, double*** %3
store double* %1, double** %4
%9 = load double*, double** %4
%10 = getelementptr inbounds double, double* %9, i64 0
%11 = load double, double* %10
store double %11, double* %5
%12 = load double*, double** %4
%13 = getelementptr inbounds double, double* %12, i64 1
%14 = load double, double* %13
store double %14, double* %6
%15 = load double*, double** %4
%16 = getelementptr inbounds double, double* %15, i64 2
%17 = load double, double* %16
store double %17, double* %7
%18 = load double*, double** %4
%19 = getelementptr inbounds double, double* %18, i64 3
%20 = load double, double* %19
store double %20, double* %8
%21 = load double, double* %5
%22 = load double, double* %5
%23 = load double, double* %6
%24 = load double, double* %6
%25 = fmul double %23, %24
%26 = call double @llvm.fmuladd.f64(double %21, double %22, double %25)
%27 = load double, double* %7
%28 = load double, double* %7
%29 = fneg double %27
%30 = call double @llvm.fmuladd.f64(double %29, double %28, double %26)
%31 = load double, double* %8
%32 = load double, double* %8
%33 = fneg double %31
%34 = call double @llvm.fmuladd.f64(double %33, double %32, double %30)
%35 = load double**, double*** %3
%36 = getelementptr inbounds double*, double** %35, i64 0
%37 = load double*, double** %36
%38 = getelementptr inbounds double, double* %37, i64 0
store double %34, double* %38
%39 = load double, double* %6
%40 = fmul double 2.000000e+00, %39
%41 = load double, double* %7
%42 = load double, double* %5
%43 = fmul double 2.000000e+00, %42
%44 = load double, double* %8
%45 = fmul double %43, %44
%46 = fneg double %45
%47 = call double @llvm.fmuladd.f64(double %40, double %41, double %46)
%48 = load double**, double*** %3
%49 = getelementptr inbounds double*, double** %48, i64 0
%50 = load double*, double** %49
%51 = getelementptr inbounds double, double* %50, i64 1
store double %47, double* %51
%52 = load double, double* %6
%53 = fmul double 2.000000e+00, %52
%54 = load double, double* %8
%55 = load double, double* %5
%56 = fmul double 2.000000e+00, %55
%57 = load double, double* %7
%58 = fmul double %56, %57
%59 = call double @llvm.fmuladd.f64(double %53, double %54, double %58)
%60 = load double**, double*** %3
%61 = getelementptr inbounds double*, double** %60, i64 0
%62 = load double*, double** %61
%63 = getelementptr inbounds double, double* %62, i64 2
store double %59, double* %63
%64 = load double, double* %6
%65 = fmul double 2.000000e+00, %64
%66 = load double, double* %7
%67 = load double, double* %5
%68 = fmul double 2.000000e+00, %67
%69 = load double, double* %8
%70 = fmul double %68, %69
%71 = call double @llvm.fmuladd.f64(double %65, double %66, double %70)
%72 = load double**, double*** %3
%73 = getelementptr inbounds double*, double** %72, i64 1
%74 = load double*, double** %73
%75 = getelementptr inbounds double, double* %74, i64 0
store double %71, double* %75
%76 = load double, double* %5
%77 = load double, double* %5
%78 = load double, double* %6
%79 = load double, double* %6
%80 = fmul double %78, %79
%81 = fneg double %80
%82 = call double @llvm.fmuladd.f64(double %76, double %77, double %81)
%83 = load double, double* %7
%84 = load double, double* %7
%85 = call double @llvm.fmuladd.f64(double %83, double %84, double %82)
%86 = load double, double* %8
%87 = load double, double* %8
%88 = fneg double %86
%89 = call double @llvm.fmuladd.f64(double %88, double %87, double %85)
%90 = load double**, double*** %3
%91 = getelementptr inbounds double*, double** %90, i64 1
%92 = load double*, double** %91
%93 = getelementptr inbounds double, double* %92, i64 1
store double %89, double* %93
%94 = load double, double* %7
%95 = fmul double 2.000000e+00, %94
%96 = load double, double* %8
%97 = load double, double* %5
%98 = fmul double 2.000000e+00, %97
%99 = load double, double* %6
%100 = fmul double %98, %99
%101 = fneg double %100
%102 = call double @llvm.fmuladd.f64(double %95, double %96, double %101)
%103 = load double**, double*** %3
%104 = getelementptr inbounds double*, double** %103, i64 1
%105 = load double*, double** %104
%106 = getelementptr inbounds double, double* %105, i64 2
store double %102, double* %106
%107 = load double, double* %6
%108 = fmul double 2.000000e+00, %107
%109 = load double, double* %8
%110 = load double, double* %5
%111 = fmul double 2.000000e+00, %110
%112 = load double, double* %7
%113 = fmul double %111, %112
%114 = fneg double %113
%115 = call double @llvm.fmuladd.f64(double %108, double %109, double %114)
%116 = load double**, double*** %3
%117 = getelementptr inbounds double*, double** %116, i64 2
%118 = load double*, double** %117
%119 = getelementptr inbounds double, double* %118, i64 0
store double %115, double* %119
%120 = load double, double* %7
%121 = fmul double 2.000000e+00, %120
%122 = load double, double* %8
%123 = load double, double* %5
%124 = fmul double 2.000000e+00, %123
%125 = load double, double* %6
%126 = fmul double %124, %125
%127 = call double @llvm.fmuladd.f64(double %121, double %122, double %126)
%128 = load double**, double*** %3
%129 = getelementptr inbounds double*, double** %128, i64 2
%130 = load double*, double** %129
%131 = getelementptr inbounds double, double* %130, i64 1
store double %127, double* %131
%132 = load double, double* %5
%133 = load double, double* %5
%134 = load double, double* %6
%135 = load double, double* %6
%136 = fmul double %134, %135
%137 = fneg double %136
%138 = call double @llvm.fmuladd.f64(double %132, double %133, double %137)
%139 = load double, double* %7
%140 = load double, double* %7
%141 = fneg double %139
%142 = call double @llvm.fmuladd.f64(double %141, double %140, double %138)
%143 = load double, double* %8
%144 = load double, double* %8
%145 = call double @llvm.fmuladd.f64(double %143, double %144, double %142)
%146 = load double**, double*** %3
%147 = getelementptr inbounds double*, double** %146, i64 2
%148 = load double*, double** %147
%149 = getelementptr inbounds double, double* %148, i64 2
store double %145, double* %149
ret void
}

LLVM IR (-O2):
define void @quaternion_to_rotation_matrix(double** nocapture %0, double* nocapture %1) {
%3 = load double, double* %1
%4 = getelementptr inbounds double, double* %1, i64 1
%5 = load double, double* %4
%6 = getelementptr inbounds double, double* %1, i64 2
%7 = bitcast double* %6 to <2 x double>*
%8 = load <2 x double>, <2 x double>* %7
%9 = fmul double %5, %5
%10 = tail call double @llvm.fmuladd.f64(double %3, double %3, double %9)
%11 = extractelement <2 x double> %8, i64 0
%12 = fneg double %11
%13 = tail call double @llvm.fmuladd.f64(double %12, double %11, double %10)
%14 = extractelement <2 x double> %8, i64 1
%15 = fneg double %14
%16 = load double*, double** %0
%17 = fmul double %3, 2.000000e+00
%18 = fmul double %17, %14
%19 = fneg double %18
%20 = insertelement <2 x double> poison, double %5, i64 0
%21 = shufflevector <2 x double> %20, <2 x double> %8, <2 x i32> <i32 0, i32 2>
%22 = fmul <2 x double> %21, <double 2.000000e+00, double 2.000000e+00>
%23 = extractelement <2 x double> %22, i64 0
%24 = insertelement <2 x double> %22, double %15, i64 1
%25 = insertelement <2 x double> poison, double %19, i64 0
%26 = insertelement <2 x double> %25, double %13, i64 1
%27 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %24, <2 x double> %8, <2 x double> %26)
%28 = shufflevector <2 x double> %27, <2 x double> poison, <2 x i32> <i32 1, i32 0>
%29 = bitcast double* %16 to <2 x double>*
store <2 x double> %28, <2 x double>* %29
%30 = fmul double %17, %11
%31 = tail call double @llvm.fmuladd.f64(double %23, double %14, double %30)
%32 = getelementptr inbounds double, double* %16, i64 2
store double %31, double* %32
%33 = getelementptr inbounds double*, double** %0, i64 1
%34 = load double*, double** %33
%35 = fneg double %9
%36 = tail call double @llvm.fmuladd.f64(double %3, double %3, double %35)
%37 = tail call double @llvm.fmuladd.f64(double %11, double %11, double %36)
%38 = insertelement <2 x double> poison, double %18, i64 0
%39 = insertelement <2 x double> %38, double %37, i64 1
%40 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %24, <2 x double> %8, <2 x double> %39)
%41 = bitcast double* %34 to <2 x double>*
store <2 x double> %40, <2 x double>* %41
%42 = fmul double %17, %5
%43 = fneg double %42
%44 = extractelement <2 x double> %22, i64 1
%45 = tail call double @llvm.fmuladd.f64(double %44, double %14, double %43)
%46 = getelementptr inbounds double, double* %34, i64 2
store double %45, double* %46
%47 = fneg double %30
%48 = getelementptr inbounds double*, double** %0, i64 2
%49 = load double*, double** %48
%50 = shufflevector <2 x double> %8, <2 x double> undef, <2 x i32> <i32 1, i32 1>
%51 = insertelement <2 x double> poison, double %47, i64 0
%52 = insertelement <2 x double> %51, double %42, i64 1
%53 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %22, <2 x double> %50, <2 x double> %52)
%54 = bitcast double* %49 to <2 x double>*
store <2 x double> %53, <2 x double>* %54
%55 = tail call double @llvm.fmuladd.f64(double %12, double %11, double %36)
%56 = tail call double @llvm.fmuladd.f64(double %14, double %14, double %55)
%57 = getelementptr inbounds double, double* %49, i64 2
store double %56, double* %57
ret void
}