void quaternion_multiply(float out[4], const float a[4], const float b[4]) {
  const float w1 = a[3];
  const float x1 = a[0];
  const float y1 = a[1];
  const float z1 = a[2];
  const float w2 = b[3];
  const float x2 = b[0];
  const float y2 = b[1];
  const float z2 = b[2];
  out[0] = w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2;
  out[1] = w1 * y2 - x1 * z2 + y1 * w2 + z1 * x2;
  out[2] = w1 * z2 + x1 * y2 - y1 * x2 + z1 * w2;
  out[3] = w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2;
}


x86 assembly (clang, -O0):
<quaternion_multiply>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movss  0xc(%rax),%xmm0
movss  %xmm0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movss  (%rax),%xmm0
movss  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movss  0x4(%rax),%xmm0
movss  %xmm0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movss  0x8(%rax),%xmm0
movss  %xmm0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movss  0xc(%rax),%xmm0
movss  %xmm0,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
movss  (%rax),%xmm0
movss  %xmm0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
movss  0x4(%rax),%xmm0
movss  %xmm0,-0x34(%rbp)
mov    -0x18(%rbp),%rax
movss  0x8(%rax),%xmm0
movss  %xmm0,-0x38(%rbp)
movss  -0x1c(%rbp),%xmm0
movss  -0x30(%rbp),%xmm2
movss  -0x20(%rbp),%xmm1
mulss  -0x2c(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  -0x24(%rbp),%xmm1
movss  -0x38(%rbp),%xmm2
mulss  %xmm2,%xmm1
addss  %xmm0,%xmm1
movss  -0x28(%rbp),%xmm0
movss  -0x34(%rbp),%xmm2
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movss  %xmm0,(%rax)
movss  -0x1c(%rbp),%xmm0
movss  -0x34(%rbp),%xmm2
movss  -0x20(%rbp),%xmm1
mulss  -0x38(%rbp),%xmm1
movd   %xmm1,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  -0x24(%rbp),%xmm1
movss  -0x2c(%rbp),%xmm2
mulss  %xmm2,%xmm1
addss  %xmm0,%xmm1
movss  -0x28(%rbp),%xmm0
movss  -0x30(%rbp),%xmm2
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movss  %xmm0,0x4(%rax)
movss  -0x1c(%rbp),%xmm0
movss  -0x38(%rbp),%xmm2
movss  -0x20(%rbp),%xmm1
mulss  -0x34(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  -0x24(%rbp),%xmm1
movss  -0x30(%rbp),%xmm2
movd   %xmm1,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm1
mulss  %xmm2,%xmm1
addss  %xmm0,%xmm1
movss  -0x28(%rbp),%xmm0
movss  -0x2c(%rbp),%xmm2
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movss  %xmm0,0x8(%rax)
movss  -0x1c(%rbp),%xmm0
movss  -0x2c(%rbp),%xmm2
movss  -0x20(%rbp),%xmm1
mulss  -0x30(%rbp),%xmm1
movd   %xmm1,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  -0x24(%rbp),%xmm1
movss  -0x34(%rbp),%xmm2
movd   %xmm1,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm1
mulss  %xmm2,%xmm1
addss  %xmm0,%xmm1
movss  -0x28(%rbp),%xmm0
movss  -0x38(%rbp),%xmm2
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movss  %xmm0,0xc(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<quaternion_multiply>:
movups (%rdx),%xmm0
movaps %xmm0,%xmm1
shufps $0xb1,%xmm0,%xmm1
movaps 0xeef(%rip),%xmm3
movss  (%rsi),%xmm6
movss  0x4(%rsi),%xmm5
movss  0x8(%rsi),%xmm8
movss  0xc(%rsi),%xmm7
movaps %xmm8,%xmm2
xorps  %xmm3,%xmm2
movaps %xmm6,%xmm4
xorps  %xmm3,%xmm4
xorps  %xmm5,%xmm3
unpcklps %xmm6,%xmm4
shufps $0x11,%xmm4,%xmm4
movaps %xmm0,%xmm6
shufps $0x1b,%xmm0,%xmm6
mulps  %xmm4,%xmm6
shufps $0x0,%xmm7,%xmm7
mulps  %xmm0,%xmm7
addps  %xmm6,%xmm7
shufps $0x0,%xmm3,%xmm5
shufps $0x4e,%xmm0,%xmm0
mulps  %xmm5,%xmm0
addps  %xmm7,%xmm0
unpcklps %xmm8,%xmm2
shufps $0x14,%xmm2,%xmm2
mulps  %xmm1,%xmm2
addps  %xmm0,%xmm2
movups %xmm2,(%rdi)
ret

LLVM IR (-O0):
define void @quaternion_multiply(float* %0, float* %1, float* %2) {
%4 = alloca float*
%5 = alloca float*
%6 = alloca float*
%7 = alloca float
%8 = alloca float
%9 = alloca float
%10 = alloca float
%11 = alloca float
%12 = alloca float
%13 = alloca float
%14 = alloca float
store float* %0, float** %4
store float* %1, float** %5
store float* %2, float** %6
%15 = load float*, float** %5
%16 = getelementptr inbounds float, float* %15, i64 3
%17 = load float, float* %16
store float %17, float* %7
%18 = load float*, float** %5
%19 = getelementptr inbounds float, float* %18, i64 0
%20 = load float, float* %19
store float %20, float* %8
%21 = load float*, float** %5
%22 = getelementptr inbounds float, float* %21, i64 1
%23 = load float, float* %22
store float %23, float* %9
%24 = load float*, float** %5
%25 = getelementptr inbounds float, float* %24, i64 2
%26 = load float, float* %25
store float %26, float* %10
%27 = load float*, float** %6
%28 = getelementptr inbounds float, float* %27, i64 3
%29 = load float, float* %28
store float %29, float* %11
%30 = load float*, float** %6
%31 = getelementptr inbounds float, float* %30, i64 0
%32 = load float, float* %31
store float %32, float* %12
%33 = load float*, float** %6
%34 = getelementptr inbounds float, float* %33, i64 1
%35 = load float, float* %34
store float %35, float* %13
%36 = load float*, float** %6
%37 = getelementptr inbounds float, float* %36, i64 2
%38 = load float, float* %37
store float %38, float* %14
%39 = load float, float* %7
%40 = load float, float* %12
%41 = load float, float* %8
%42 = load float, float* %11
%43 = fmul float %41, %42
%44 = call float @llvm.fmuladd.f32(float %39, float %40, float %43)
%45 = load float, float* %9
%46 = load float, float* %14
%47 = call float @llvm.fmuladd.f32(float %45, float %46, float %44)
%48 = load float, float* %10
%49 = load float, float* %13
%50 = fneg float %48
%51 = call float @llvm.fmuladd.f32(float %50, float %49, float %47)
%52 = load float*, float** %4
%53 = getelementptr inbounds float, float* %52, i64 0
store float %51, float* %53
%54 = load float, float* %7
%55 = load float, float* %13
%56 = load float, float* %8
%57 = load float, float* %14
%58 = fmul float %56, %57
%59 = fneg float %58
%60 = call float @llvm.fmuladd.f32(float %54, float %55, float %59)
%61 = load float, float* %9
%62 = load float, float* %11
%63 = call float @llvm.fmuladd.f32(float %61, float %62, float %60)
%64 = load float, float* %10
%65 = load float, float* %12
%66 = call float @llvm.fmuladd.f32(float %64, float %65, float %63)
%67 = load float*, float** %4
%68 = getelementptr inbounds float, float* %67, i64 1
store float %66, float* %68
%69 = load float, float* %7
%70 = load float, float* %14
%71 = load float, float* %8
%72 = load float, float* %13
%73 = fmul float %71, %72
%74 = call float @llvm.fmuladd.f32(float %69, float %70, float %73)
%75 = load float, float* %9
%76 = load float, float* %12
%77 = fneg float %75
%78 = call float @llvm.fmuladd.f32(float %77, float %76, float %74)
%79 = load float, float* %10
%80 = load float, float* %11
%81 = call float @llvm.fmuladd.f32(float %79, float %80, float %78)
%82 = load float*, float** %4
%83 = getelementptr inbounds float, float* %82, i64 2
store float %81, float* %83
%84 = load float, float* %7
%85 = load float, float* %11
%86 = load float, float* %8
%87 = load float, float* %12
%88 = fmul float %86, %87
%89 = fneg float %88
%90 = call float @llvm.fmuladd.f32(float %84, float %85, float %89)
%91 = load float, float* %9
%92 = load float, float* %13
%93 = fneg float %91
%94 = call float @llvm.fmuladd.f32(float %93, float %92, float %90)
%95 = load float, float* %10
%96 = load float, float* %14
%97 = fneg float %95
%98 = call float @llvm.fmuladd.f32(float %97, float %96, float %94)
%99 = load float*, float** %4
%100 = getelementptr inbounds float, float* %99, i64 3
store float %98, float* %100
ret void
}

LLVM IR (-O2):
define void @quaternion_multiply(float* nocapture writeonly %0, float* nocapture %1, float* nocapture %2) {
%4 = getelementptr inbounds float, float* %1, i64 3
%5 = load float, float* %4
%6 = load float, float* %1
%7 = getelementptr inbounds float, float* %1, i64 1
%8 = load float, float* %7
%9 = getelementptr inbounds float, float* %1, i64 2
%10 = load float, float* %9
%11 = bitcast float* %2 to <4 x float>*
%12 = load <4 x float>, <4 x float>* %11
%13 = shufflevector <4 x float> %12, <4 x float> poison, <4 x i32> <i32 1, i32 0, i32 3, i32 2>
%14 = fneg float %10
%15 = fneg float %6
%16 = fneg float %8
%17 = insertelement <4 x float> poison, float %15, i64 0
%18 = insertelement <4 x float> %17, float %6, i64 1
%19 = shufflevector <4 x float> %18, <4 x float> poison, <4 x i32> <i32 0, i32 1, i32 0, i32 1>
%20 = fmul <4 x float> %12, %19
%21 = shufflevector <4 x float> %20, <4 x float> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
%22 = insertelement <4 x float> poison, float %5, i64 0
%23 = shufflevector <4 x float> %22, <4 x float> poison, <4 x i32> zeroinitializer
%24 = call <4 x float> @llvm.fmuladd.v4f32(<4 x float> %23, <4 x float> %12, <4 x float> %21)
%25 = insertelement <4 x float> poison, float %8, i64 0
%26 = insertelement <4 x float> %25, float %16, i64 1
%27 = shufflevector <4 x float> %26, <4 x float> poison, <4 x i32> <i32 0, i32 0, i32 1, i32 1>
%28 = shufflevector <4 x float> %12, <4 x float> undef, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
%29 = call <4 x float> @llvm.fmuladd.v4f32(<4 x float> %27, <4 x float> %28, <4 x float> %24)
%30 = insertelement <4 x float> poison, float %14, i64 0
%31 = insertelement <4 x float> %30, float %10, i64 1
%32 = shufflevector <4 x float> %31, <4 x float> poison, <4 x i32> <i32 0, i32 1, i32 1, i32 0>
%33 = call <4 x float> @llvm.fmuladd.v4f32(<4 x float> %32, <4 x float> %13, <4 x float> %29)
%34 = bitcast float* %0 to <4 x float>*
store <4 x float> %33, <4 x float>* %34
ret void
}