void quatd_from_mat3d(double quat[4], const double matrix[9]) {
  int X = 0, Y = 1, Z = 2, W = 3;
  double trace = matrix[0] + matrix[4] + matrix[8];
  if (trace > 0.0) {
    double s = sqrtf(trace + 1.0);
    quat[W] = s * 0.5;
    s = 0.5 / s;
    quat[X] = (matrix[mat3_getIndex(Z, Y)] - matrix[mat3_getIndex(Y, Z)]) * s;
    quat[Y] = (matrix[mat3_getIndex(X, Z)] - matrix[mat3_getIndex(Z, X)]) * s;
    quat[Z] = (matrix[mat3_getIndex(Y, Z)] - matrix[mat3_getIndex(X, Y)]) * s;
  } else {
    int next[3] = {Y, Z, X};
    int i = X;
    if (matrix[mat3_getIndex(Y, Y)] > matrix[mat3_getIndex(X, X)])
      i = Y;
    if (matrix[mat3_getIndex(Z, Z)] > matrix[mat3_getIndex(i, i)])
      i = Z;
    int j = next[i];
    int k = next[j];
    float s =
        sqrtf((matrix[mat3_getIndex(i, i)] -
               (matrix[mat3_getIndex(j, j)] + matrix[mat3_getIndex(k, k)])) +
              1.0);
    quat[i] = s * 0.5;
    s = 0.5 / s;
    quat[W] = (matrix[mat3_getIndex(k, j)] - matrix[mat3_getIndex(j, k)]) * s;
    quat[j] = (matrix[mat3_getIndex(j, i)] + matrix[mat3_getIndex(i, j)]) * s;
    quat[k] = (matrix[mat3_getIndex(k, i)] + matrix[mat3_getIndex(i, k)]) * s;
  }
}


x86 assembly (clang, -O0):
<quatd_from_mat3d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
movl   $0x3,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
addsd  0x20(%rax),%xmm0
mov    -0x10(%rbp),%rax
addsd  0x40(%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1308 <quatd_from_mat3d+0x1e8>
movsd  0xe80(%rip),%xmm0
addsd  -0x28(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
call   1030 <sqrtf@plt>
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xe59(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  0xe3f(%rip),%xmm0
divsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
mov    %eax,%ecx
mov    -0x98(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x90(%rbp)
ff
mov    -0x10(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
movsd  -0x90(%rbp),%xmm0
ff
mov    %eax,%ecx
mov    -0x88(%rbp),%rax
movslq %ecx,%rcx
subsd  (%rax,%rcx,8),%xmm0
mulsd  -0x30(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
mov    %eax,%ecx
mov    -0x80(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x78(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
movsd  -0x78(%rbp),%xmm0
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
movslq %ecx,%rcx
subsd  (%rax,%rcx,8),%xmm0
mulsd  -0x30(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
mov    %eax,%ecx
mov    -0x68(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
movsd  -0x60(%rbp),%xmm0
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
movslq %ecx,%rcx
subsd  (%rax,%rcx,8),%xmm0
mulsd  -0x30(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
jmp    167f <quatd_from_mat3d+0x55f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
mov    %eax,%ecx
mov    -0xb0(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0xa8(%rbp)
ff
mov    -0x10(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
movsd  -0xa8(%rbp),%xmm0
ff
mov    %eax,%ecx
mov    -0xa0(%rbp),%rax
movslq %ecx,%rcx
ucomisd (%rax,%rcx,8),%xmm0
jbe    138e <quatd_from_mat3d+0x26e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
mov    %eax,%ecx
mov    -0xc8(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0xc0(%rbp)
ff
mov    -0x10(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0x40(%rbp),%edi
mov    -0x40(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
movsd  -0xc0(%rbp),%xmm0
ff
mov    %eax,%ecx
mov    -0xb8(%rbp),%rax
movslq %ecx,%rcx
ucomisd (%rax,%rcx,8),%xmm0
jbe    13fc <quatd_from_mat3d+0x2dc>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
movslq -0x40(%rbp),%rax
mov    -0x3c(%rbp,%rax,4),%eax
mov    %eax,-0x44(%rbp)
movslq -0x44(%rbp),%rax
mov    -0x3c(%rbp,%rax,4),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    -0x40(%rbp),%edi
mov    -0x40(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
mov    %eax,%ecx
mov    -0x138(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x120(%rbp)
ff
mov    -0x10(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    -0x44(%rbp),%edi
mov    -0x44(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
mov    %eax,%ecx
mov    -0x130(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x128(%rbp)
ff
mov    -0x10(%rbp),%rax
mov    %rax,-0x118(%rbp)
mov    -0x48(%rbp),%edi
mov    -0x48(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
movsd  -0x128(%rbp),%xmm1
ff
movsd  -0x120(%rbp),%xmm0
ff
mov    %eax,%ecx
mov    -0x118(%rbp),%rax
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm1
subsd  %xmm1,%xmm0
movsd  0xb4f(%rip),%xmm1
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1030 <sqrtf@plt>
movss  %xmm0,-0x4c(%rbp)
movss  -0x4c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xb24(%rip),%xmm1
mulsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movslq -0x40(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movss  -0x4c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xb02(%rip),%xmm0
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x110(%rbp)
mov    -0x48(%rbp),%edi
mov    -0x44(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
mov    %eax,%ecx
mov    -0x110(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x108(%rbp)
ff
mov    -0x10(%rbp),%rax
mov    %rax,-0x100(%rbp)
mov    -0x44(%rbp),%edi
mov    -0x48(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
movsd  -0x108(%rbp),%xmm0
ff
mov    %eax,%ecx
mov    -0x100(%rbp),%rax
movslq %ecx,%rcx
subsd  (%rax,%rcx,8),%xmm0
movss  -0x4c(%rbp),%xmm1
cvtss2sd %xmm1,%xmm1
mulsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    %rax,-0xf8(%rbp)
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
mov    %eax,%ecx
mov    -0xf8(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0xf0(%rbp)
ff
mov    -0x10(%rbp),%rax
mov    %rax,-0xe8(%rbp)
mov    -0x40(%rbp),%edi
mov    -0x44(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
movsd  -0xf0(%rbp),%xmm0
ff
mov    %eax,%ecx
mov    -0xe8(%rbp),%rax
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm0
movss  -0x4c(%rbp),%xmm1
cvtss2sd %xmm1,%xmm1
mulsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    -0x48(%rbp),%edi
mov    -0x40(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
mov    %eax,%ecx
mov    -0xe0(%rbp),%rax
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0xd8(%rbp)
ff
mov    -0x10(%rbp),%rax
mov    %rax,-0xd0(%rbp)
mov    -0x40(%rbp),%edi
mov    -0x48(%rbp),%esi
mov    $0x0,%al
call   1040 <mat3_getIndex@plt>
movsd  -0xd8(%rbp),%xmm0
ff
mov    %eax,%ecx
mov    -0xd0(%rbp),%rax
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm0
movss  -0x4c(%rbp),%xmm1
cvtss2sd %xmm1,%xmm1
mulsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movslq -0x48(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<quatd_from_mat3d>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
movsd  (%rsi),%xmm0
addsd  0x20(%rsi),%xmm0
addsd  0x40(%rsi),%xmm0
mov    %rdi,%r14
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    116e <quatd_from_mat3d+0x4e>
addsd  0xeac(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
xorpd  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jb     128d <quatd_from_mat3d+0x16d>
sqrtss %xmm0,%xmm0
jmp    1292 <quatd_from_mat3d+0x172>
movabs $0x200000001,%rax
mov    %rax,0x1c(%rsp)
movl   $0x0,0x24(%rsp)
mov    $0x1,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %edi,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
xor    %r15d,%r15d
movsd  0x8(%rsp),%xmm0
ucomisd (%rbx,%rax,8),%xmm0
seta   %r15b
mov    $0x2,%ebp
mov    $0x2,%edi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %r15d,%edi
mov    %r15d,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  0x8(%rsp),%xmm0
ucomisd (%rbx,%rax,8),%xmm0
cmova  %ebp,%r15d
mov    0x1c(%rsp,%r15,4),%eax
movslq %eax,%r13
mov    0x1c(%rsp,%r13,4),%eax
movslq %eax,%r12
mov    %r15d,%edi
mov    %r15d,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %r13d,%edi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %r12d,%edi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  0x10(%rsp),%xmm1
addsd  (%rbx,%rax,8),%xmm1
movsd  0x8(%rsp),%xmm0
subsd  %xmm1,%xmm0
addsd  0xd8d(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
xorpd  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jb     1388 <quatd_from_mat3d+0x268>
sqrtss %xmm0,%xmm0
jmp    138d <quatd_from_mat3d+0x26d>
call   1030 <sqrtf@plt>
cvtss2sd %xmm0,%xmm0
movsd  0xd6a(%rip),%xmm2
movaps %xmm0,%xmm1
mulsd  %xmm2,%xmm1
movsd  %xmm1,0x18(%r14)
divsd  %xmm0,%xmm2
movsd  %xmm2,0x8(%rsp)
mov    $0x2,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    $0x1,%edi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  0x10(%rsp),%xmm0
subsd  (%rbx,%rax,8),%xmm0
mulsd  0x8(%rsp),%xmm0
movsd  %xmm0,(%r14)
xor    %edi,%edi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    $0x2,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  0x10(%rsp),%xmm0
subsd  (%rbx,%rax,8),%xmm0
mulsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%r14)
mov    $0x1,%edi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x10(%rsp)
xor    %edi,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  0x10(%rsp),%xmm0
subsd  (%rbx,%rax,8),%xmm0
mulsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x10(%r14)
jmp    1482 <quatd_from_mat3d+0x362>
call   1030 <sqrtf@plt>
xorps  %xmm1,%xmm1
cvtss2sd %xmm0,%xmm1
mulsd  0xc6c(%rip),%xmm1
movsd  %xmm1,(%r14,%r15,8)
movss  0xc66(%rip),%xmm1
divss  %xmm0,%xmm1
movss  %xmm1,0x8(%rsp)
mov    %r12d,%edi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %r13d,%edi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  0x10(%rsp),%xmm1
subsd  (%rbx,%rax,8),%xmm1
movss  0x8(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
mulsd  %xmm0,%xmm1
movsd  %xmm1,0x18(%r14)
mov    %r13d,%edi
mov    %r15d,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %r15d,%edi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  0x10(%rsp),%xmm0
addsd  (%rbx,%rax,8),%xmm0
mulsd  0x8(%rsp),%xmm0
movsd  %xmm0,(%r14,%r13,8)
mov    %r12d,%edi
mov    %r15d,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  (%rbx,%rax,8),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %r15d,%edi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <mat3_getIndex@plt>
cltq
movsd  0x10(%rsp),%xmm0
addsd  (%rbx,%rax,8),%xmm0
mulsd  0x8(%rsp),%xmm0
movsd  %xmm0,(%r14,%r12,8)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @quatd_from_mat3d(double* %0, double* %1) {
%3 = alloca double*
%4 = alloca double*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca [3 x i32]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca float
store double* %0, double** %3
store double* %1, double** %4
store i32 0, i32* %5
store i32 1, i32* %6
store i32 2, i32* %7
store i32 3, i32* %8
%16 = load double*, double** %4
%17 = getelementptr inbounds double, double* %16, i64 0
%18 = load double, double* %17
%19 = load double*, double** %4
%20 = getelementptr inbounds double, double* %19, i64 4
%21 = load double, double* %20
%22 = fadd double %18, %21
%23 = load double*, double** %4
%24 = getelementptr inbounds double, double* %23, i64 8
%25 = load double, double* %24
%26 = fadd double %22, %25
store double %26, double* %9
%27 = load double, double* %9
%28 = fcmp ogt double %27, 0.000000e+00
br i1 %28, label %29, label %106
29: ; preds = %2
%30 = load double, double* %9
%31 = fadd double %30, 1.000000e+00
%32 = fptrunc double %31 to float
%33 = call float @sqrtf(float %32)
%34 = fpext float %33 to double
store double %34, double* %10
%35 = load double, double* %10
%36 = fmul double %35, 5.000000e-01
%37 = load double*, double** %3
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds double, double* %37, i64 %39
store double %36, double* %40
%41 = load double, double* %10
%42 = fdiv double 5.000000e-01, %41
store double %42, double* %10
%43 = load double*, double** %4
%44 = load i32, i32* %7
%45 = load i32, i32* %6
%46 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %44, i32 %45)
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds double, double* %43, i64 %47
%49 = load double, double* %48
%50 = load double*, double** %4
%51 = load i32, i32* %6
%52 = load i32, i32* %7
%53 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %51, i32 %52)
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds double, double* %50, i64 %54
%56 = load double, double* %55
%57 = fsub double %49, %56
%58 = load double, double* %10
%59 = fmul double %57, %58
%60 = load double*, double** %3
%61 = load i32, i32* %5
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds double, double* %60, i64 %62
store double %59, double* %63
%64 = load double*, double** %4
%65 = load i32, i32* %5
%66 = load i32, i32* %7
%67 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %65, i32 %66)
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds double, double* %64, i64 %68
%70 = load double, double* %69
%71 = load double*, double** %4
%72 = load i32, i32* %7
%73 = load i32, i32* %5
%74 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %72, i32 %73)
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds double, double* %71, i64 %75
%77 = load double, double* %76
%78 = fsub double %70, %77
%79 = load double, double* %10
%80 = fmul double %78, %79
%81 = load double*, double** %3
%82 = load i32, i32* %6
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds double, double* %81, i64 %83
store double %80, double* %84
%85 = load double*, double** %4
%86 = load i32, i32* %6
%87 = load i32, i32* %7
%88 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %86, i32 %87)
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds double, double* %85, i64 %89
%91 = load double, double* %90
%92 = load double*, double** %4
%93 = load i32, i32* %5
%94 = load i32, i32* %6
%95 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %93, i32 %94)
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds double, double* %92, i64 %96
%98 = load double, double* %97
%99 = fsub double %91, %98
%100 = load double, double* %10
%101 = fmul double %99, %100
%102 = load double*, double** %3
%103 = load i32, i32* %7
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds double, double* %102, i64 %104
store double %101, double* %105
br label %261
106: ; preds = %2
%107 = getelementptr inbounds [3 x i32], [3 x i32]* %11, i64 0, i64 0
%108 = load i32, i32* %6
store i32 %108, i32* %107
%109 = getelementptr inbounds i32, i32* %107, i64 1
%110 = load i32, i32* %7
store i32 %110, i32* %109
%111 = getelementptr inbounds i32, i32* %109, i64 1
%112 = load i32, i32* %5
store i32 %112, i32* %111
%113 = load i32, i32* %5
store i32 %113, i32* %12
%114 = load double*, double** %4
%115 = load i32, i32* %6
%116 = load i32, i32* %6
%117 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %115, i32 %116)
%118 = sext i32 %117 to i64
%119 = getelementptr inbounds double, double* %114, i64 %118
%120 = load double, double* %119
%121 = load double*, double** %4
%122 = load i32, i32* %5
%123 = load i32, i32* %5
%124 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %122, i32 %123)
%125 = sext i32 %124 to i64
%126 = getelementptr inbounds double, double* %121, i64 %125
%127 = load double, double* %126
%128 = fcmp ogt double %120, %127
br i1 %128, label %129, label %131
129: ; preds = %106
%130 = load i32, i32* %6
store i32 %130, i32* %12
br label %131
131: ; preds = %129, %106
%132 = load double*, double** %4
%133 = load i32, i32* %7
%134 = load i32, i32* %7
%135 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %133, i32 %134)
%136 = sext i32 %135 to i64
%137 = getelementptr inbounds double, double* %132, i64 %136
%138 = load double, double* %137
%139 = load double*, double** %4
%140 = load i32, i32* %12
%141 = load i32, i32* %12
%142 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %140, i32 %141)
%143 = sext i32 %142 to i64
%144 = getelementptr inbounds double, double* %139, i64 %143
%145 = load double, double* %144
%146 = fcmp ogt double %138, %145
br i1 %146, label %147, label %149
147: ; preds = %131
%148 = load i32, i32* %7
store i32 %148, i32* %12
br label %149
149: ; preds = %147, %131
%150 = load i32, i32* %12
%151 = sext i32 %150 to i64
%152 = getelementptr inbounds [3 x i32], [3 x i32]* %11, i64 0, i64 %151
%153 = load i32, i32* %152
store i32 %153, i32* %13
%154 = load i32, i32* %13
%155 = sext i32 %154 to i64
%156 = getelementptr inbounds [3 x i32], [3 x i32]* %11, i64 0, i64 %155
%157 = load i32, i32* %156
store i32 %157, i32* %14
%158 = load double*, double** %4
%159 = load i32, i32* %12
%160 = load i32, i32* %12
%161 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %159, i32 %160)
%162 = sext i32 %161 to i64
%163 = getelementptr inbounds double, double* %158, i64 %162
%164 = load double, double* %163
%165 = load double*, double** %4
%166 = load i32, i32* %13
%167 = load i32, i32* %13
%168 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %166, i32 %167)
%169 = sext i32 %168 to i64
%170 = getelementptr inbounds double, double* %165, i64 %169
%171 = load double, double* %170
%172 = load double*, double** %4
%173 = load i32, i32* %14
%174 = load i32, i32* %14
%175 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %173, i32 %174)
%176 = sext i32 %175 to i64
%177 = getelementptr inbounds double, double* %172, i64 %176
%178 = load double, double* %177
%179 = fadd double %171, %178
%180 = fsub double %164, %179
%181 = fadd double %180, 1.000000e+00
%182 = fptrunc double %181 to float
%183 = call float @sqrtf(float %182)
store float %183, float* %15
%184 = load float, float* %15
%185 = fpext float %184 to double
%186 = fmul double %185, 5.000000e-01
%187 = load double*, double** %3
%188 = load i32, i32* %12
%189 = sext i32 %188 to i64
%190 = getelementptr inbounds double, double* %187, i64 %189
store double %186, double* %190
%191 = load float, float* %15
%192 = fpext float %191 to double
%193 = fdiv double 5.000000e-01, %192
%194 = fptrunc double %193 to float
store float %194, float* %15
%195 = load double*, double** %4
%196 = load i32, i32* %14
%197 = load i32, i32* %13
%198 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %196, i32 %197)
%199 = sext i32 %198 to i64
%200 = getelementptr inbounds double, double* %195, i64 %199
%201 = load double, double* %200
%202 = load double*, double** %4
%203 = load i32, i32* %13
%204 = load i32, i32* %14
%205 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %203, i32 %204)
%206 = sext i32 %205 to i64
%207 = getelementptr inbounds double, double* %202, i64 %206
%208 = load double, double* %207
%209 = fsub double %201, %208
%210 = load float, float* %15
%211 = fpext float %210 to double
%212 = fmul double %209, %211
%213 = load double*, double** %3
%214 = load i32, i32* %8
%215 = sext i32 %214 to i64
%216 = getelementptr inbounds double, double* %213, i64 %215
store double %212, double* %216
%217 = load double*, double** %4
%218 = load i32, i32* %13
%219 = load i32, i32* %12
%220 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %218, i32 %219)
%221 = sext i32 %220 to i64
%222 = getelementptr inbounds double, double* %217, i64 %221
%223 = load double, double* %222
%224 = load double*, double** %4
%225 = load i32, i32* %12
%226 = load i32, i32* %13
%227 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %225, i32 %226)
%228 = sext i32 %227 to i64
%229 = getelementptr inbounds double, double* %224, i64 %228
%230 = load double, double* %229
%231 = fadd double %223, %230
%232 = load float, float* %15
%233 = fpext float %232 to double
%234 = fmul double %231, %233
%235 = load double*, double** %3
%236 = load i32, i32* %13
%237 = sext i32 %236 to i64
%238 = getelementptr inbounds double, double* %235, i64 %237
store double %234, double* %238
%239 = load double*, double** %4
%240 = load i32, i32* %14
%241 = load i32, i32* %12
%242 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %240, i32 %241)
%243 = sext i32 %242 to i64
%244 = getelementptr inbounds double, double* %239, i64 %243
%245 = load double, double* %244
%246 = load double*, double** %4
%247 = load i32, i32* %12
%248 = load i32, i32* %14
%249 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %247, i32 %248)
%250 = sext i32 %249 to i64
%251 = getelementptr inbounds double, double* %246, i64 %250
%252 = load double, double* %251
%253 = fadd double %245, %252
%254 = load float, float* %15
%255 = fpext float %254 to double
%256 = fmul double %253, %255
%257 = load double*, double** %3
%258 = load i32, i32* %14
%259 = sext i32 %258 to i64
%260 = getelementptr inbounds double, double* %257, i64 %259
store double %256, double* %260
br label %261
261: ; preds = %149, %29
ret void
}

LLVM IR (-O2):
define void @quatd_from_mat3d(double* nocapture writeonly %0, double* %1) {
%3 = alloca [3 x i32]
%4 = load double, double* %1
%5 = getelementptr inbounds double, double* %1, i64 4
%6 = load double, double* %5
%7 = fadd double %4, %6
%8 = getelementptr inbounds double, double* %1, i64 8
%9 = load double, double* %8
%10 = fadd double %7, %9
%11 = fcmp ogt double %10, 0.000000e+00
br i1 %11, label %12, label %52
12: ; preds = %2
%13 = fadd double %10, 1.000000e+00
%14 = fptrunc double %13 to float
%15 = tail call float @sqrtf(float %14)
%16 = fpext float %15 to double
%17 = fmul double %16, 5.000000e-01
%18 = getelementptr inbounds double, double* %0, i64 3
store double %17, double* %18
%19 = fdiv double 5.000000e-01, %16
%20 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 2, i32 1)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds double, double* %1, i64 %21
%23 = load double, double* %22
%24 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 1, i32 2)
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds double, double* %1, i64 %25
%27 = load double, double* %26
%28 = fsub double %23, %27
%29 = fmul double %19, %28
store double %29, double* %0
%30 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 0, i32 2)
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds double, double* %1, i64 %31
%33 = load double, double* %32
%34 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 2, i32 0)
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds double, double* %1, i64 %35
%37 = load double, double* %36
%38 = fsub double %33, %37
%39 = fmul double %19, %38
%40 = getelementptr inbounds double, double* %0, i64 1
store double %39, double* %40
%41 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 1, i32 2)
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds double, double* %1, i64 %42
%44 = load double, double* %43
%45 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 0, i32 1)
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds double, double* %1, i64 %46
%48 = load double, double* %47
%49 = fsub double %44, %48
%50 = fmul double %19, %49
%51 = getelementptr inbounds double, double* %0, i64 2
store double %50, double* %51
br label %139
52: ; preds = %2
%53 = bitcast [3 x i32]* %3 to i8*
%54 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
store i32 1, i32* %54
%55 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 1
store i32 2, i32* %55
%56 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 2
store i32 0, i32* %56
%57 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 1, i32 1)
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds double, double* %1, i64 %58
%60 = load double, double* %59
%61 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 0, i32 0)
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds double, double* %1, i64 %62
%64 = load double, double* %63
%65 = fcmp ogt double %60, %64
%66 = zext i1 %65 to i32
%67 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 2, i32 2)
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds double, double* %1, i64 %68
%70 = load double, double* %69
%71 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %66, i32 %66)
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds double, double* %1, i64 %72
%74 = load double, double* %73
%75 = fcmp ogt double %70, %74
%76 = select i1 %75, i32 2, i32 %66
%77 = zext i32 %76 to i64
%78 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 %77
%79 = load i32, i32* %78
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 %80
%82 = load i32, i32* %81
%83 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %76, i32 %76)
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds double, double* %1, i64 %84
%86 = load double, double* %85
%87 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %79, i32 %79)
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds double, double* %1, i64 %88
%90 = load double, double* %89
%91 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %82, i32 %82)
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds double, double* %1, i64 %92
%94 = load double, double* %93
%95 = fadd double %90, %94
%96 = fsub double %86, %95
%97 = fadd double %96, 1.000000e+00
%98 = fptrunc double %97 to float
%99 = tail call float @sqrtf(float %98)
%100 = fpext float %99 to double
%101 = fmul double %100, 5.000000e-01
%102 = getelementptr inbounds double, double* %0, i64 %77
store double %101, double* %102
%103 = fdiv float 5.000000e-01, %99
%104 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %82, i32 %79)
%105 = sext i32 %104 to i64
%106 = getelementptr inbounds double, double* %1, i64 %105
%107 = load double, double* %106
%108 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %79, i32 %82)
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds double, double* %1, i64 %109
%111 = load double, double* %110
%112 = fsub double %107, %111
%113 = fpext float %103 to double
%114 = fmul double %112, %113
%115 = getelementptr inbounds double, double* %0, i64 3
store double %114, double* %115
%116 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %79, i32 %76)
%117 = sext i32 %116 to i64
%118 = getelementptr inbounds double, double* %1, i64 %117
%119 = load double, double* %118
%120 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %76, i32 %79)
%121 = sext i32 %120 to i64
%122 = getelementptr inbounds double, double* %1, i64 %121
%123 = load double, double* %122
%124 = fadd double %119, %123
%125 = fmul double %124, %113
%126 = getelementptr inbounds double, double* %0, i64 %80
store double %125, double* %126
%127 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %82, i32 %76)
%128 = sext i32 %127 to i64
%129 = getelementptr inbounds double, double* %1, i64 %128
%130 = load double, double* %129
%131 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat3_getIndex to i32 (i32, i32, ...)*)(i32 %76, i32 %82)
%132 = sext i32 %131 to i64
%133 = getelementptr inbounds double, double* %1, i64 %132
%134 = load double, double* %133
%135 = fadd double %130, %134
%136 = fmul double %135, %113
%137 = sext i32 %82 to i64
%138 = getelementptr inbounds double, double* %0, i64 %137
store double %136, double* %138
br label %139
139: ; preds = %52, %12
ret void
}