#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
double sqrt(double);
void Matrix2Quaternion(float m[3][3], float q[4]) {
  double w = sqrt(1.0 + m[0][0] + m[1][1] + m[2][2]) / 2.0;
  double w4 = 4.0 * w;
  double x = (m[2][1] - m[1][2]) / w4;
  double y = (m[0][2] - m[2][0]) / w4;
  double z = (m[1][0] - m[0][1]) / w4;
  q[0] = w;
  q[1] = x;
  q[2] = y;
  q[3] = z;
}


x86 assembly (clang, -O0):
<Matrix2Quaternion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xedc(%rip),%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movss  0x10(%rax),%xmm1
cvtss2sd %xmm1,%xmm1
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movss  0x20(%rax),%xmm1
cvtss2sd %xmm1,%xmm1
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
movsd  0xea1(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xe88(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movss  0x1c(%rax),%xmm0
mov    -0x8(%rbp),%rax
subss  0x14(%rax),%xmm0
cvtss2sd %xmm0,%xmm0
divsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movss  0x8(%rax),%xmm0
mov    -0x8(%rbp),%rax
subss  0x18(%rax),%xmm0
cvtss2sd %xmm0,%xmm0
divsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%rax
movss  0xc(%rax),%xmm0
mov    -0x8(%rbp),%rax
subss  0x4(%rax),%xmm0
cvtss2sd %xmm0,%xmm0
divsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
mov    -0x10(%rbp),%rax
movss  %xmm0,(%rax)
movsd  -0x28(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
mov    -0x10(%rbp),%rax
movss  %xmm0,0x4(%rax)
movsd  -0x30(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
mov    -0x10(%rbp),%rax
movss  %xmm0,0x8(%rax)
movsd  -0x38(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
mov    -0x10(%rbp),%rax
movss  %xmm0,0xc(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Matrix2Quaternion>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movss  (%rdi),%xmm0
movss  0x10(%rdi),%xmm1
cvtss2sd %xmm0,%xmm0
addsd  0xed1(%rip),%xmm0
cvtss2sd %xmm1,%xmm1
addsd  %xmm0,%xmm1
movss  0x20(%rdi),%xmm0
cvtss2sd %xmm0,%xmm0
addsd  %xmm1,%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1154 <Matrix2Quaternion+0x44>
sqrtsd %xmm0,%xmm0
jmp    1159 <Matrix2Quaternion+0x49>
call   1030 <sqrt@plt>
mulsd  0xea7(%rip),%xmm0
movsd  0xea7(%rip),%xmm1
movsd  0x14(%rbx),%xmm2
movss  0x8(%rbx),%xmm3
movss  0x1c(%rbx),%xmm4
unpcklps %xmm3,%xmm4
subps  %xmm2,%xmm4
cvtps2pd %xmm4,%xmm2
mulsd  %xmm0,%xmm1
movss  0xc(%rbx),%xmm3
subss  0x4(%rbx),%xmm3
cvtss2sd %xmm3,%xmm3
divsd  %xmm1,%xmm3
unpcklpd %xmm1,%xmm1
divpd  %xmm1,%xmm2
unpcklpd %xmm2,%xmm0
shufpd $0x1,%xmm3,%xmm2
cvtpd2ps %xmm2,%xmm1
cvtpd2ps %xmm0,%xmm0
unpcklpd %xmm1,%xmm0
movupd %xmm0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @Matrix2Quaternion([3 x float]* %0, float* %1) {
%3 = alloca [3 x float]*
%4 = alloca float*
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
store [3 x float]* %0, [3 x float]** %3
store float* %1, float** %4
%10 = load [3 x float]*, [3 x float]** %3
%11 = getelementptr inbounds [3 x float], [3 x float]* %10, i64 0
%12 = getelementptr inbounds [3 x float], [3 x float]* %11, i64 0, i64 0
%13 = load float, float* %12
%14 = fpext float %13 to double
%15 = fadd double 1.000000e+00, %14
%16 = load [3 x float]*, [3 x float]** %3
%17 = getelementptr inbounds [3 x float], [3 x float]* %16, i64 1
%18 = getelementptr inbounds [3 x float], [3 x float]* %17, i64 0, i64 1
%19 = load float, float* %18
%20 = fpext float %19 to double
%21 = fadd double %15, %20
%22 = load [3 x float]*, [3 x float]** %3
%23 = getelementptr inbounds [3 x float], [3 x float]* %22, i64 2
%24 = getelementptr inbounds [3 x float], [3 x float]* %23, i64 0, i64 2
%25 = load float, float* %24
%26 = fpext float %25 to double
%27 = fadd double %21, %26
%28 = call double @sqrt(double %27)
%29 = fdiv double %28, 2.000000e+00
store double %29, double* %5
%30 = load double, double* %5
%31 = fmul double 4.000000e+00, %30
store double %31, double* %6
%32 = load [3 x float]*, [3 x float]** %3
%33 = getelementptr inbounds [3 x float], [3 x float]* %32, i64 2
%34 = getelementptr inbounds [3 x float], [3 x float]* %33, i64 0, i64 1
%35 = load float, float* %34
%36 = load [3 x float]*, [3 x float]** %3
%37 = getelementptr inbounds [3 x float], [3 x float]* %36, i64 1
%38 = getelementptr inbounds [3 x float], [3 x float]* %37, i64 0, i64 2
%39 = load float, float* %38
%40 = fsub float %35, %39
%41 = fpext float %40 to double
%42 = load double, double* %6
%43 = fdiv double %41, %42
store double %43, double* %7
%44 = load [3 x float]*, [3 x float]** %3
%45 = getelementptr inbounds [3 x float], [3 x float]* %44, i64 0
%46 = getelementptr inbounds [3 x float], [3 x float]* %45, i64 0, i64 2
%47 = load float, float* %46
%48 = load [3 x float]*, [3 x float]** %3
%49 = getelementptr inbounds [3 x float], [3 x float]* %48, i64 2
%50 = getelementptr inbounds [3 x float], [3 x float]* %49, i64 0, i64 0
%51 = load float, float* %50
%52 = fsub float %47, %51
%53 = fpext float %52 to double
%54 = load double, double* %6
%55 = fdiv double %53, %54
store double %55, double* %8
%56 = load [3 x float]*, [3 x float]** %3
%57 = getelementptr inbounds [3 x float], [3 x float]* %56, i64 1
%58 = getelementptr inbounds [3 x float], [3 x float]* %57, i64 0, i64 0
%59 = load float, float* %58
%60 = load [3 x float]*, [3 x float]** %3
%61 = getelementptr inbounds [3 x float], [3 x float]* %60, i64 0
%62 = getelementptr inbounds [3 x float], [3 x float]* %61, i64 0, i64 1
%63 = load float, float* %62
%64 = fsub float %59, %63
%65 = fpext float %64 to double
%66 = load double, double* %6
%67 = fdiv double %65, %66
store double %67, double* %9
%68 = load double, double* %5
%69 = fptrunc double %68 to float
%70 = load float*, float** %4
%71 = getelementptr inbounds float, float* %70, i64 0
store float %69, float* %71
%72 = load double, double* %7
%73 = fptrunc double %72 to float
%74 = load float*, float** %4
%75 = getelementptr inbounds float, float* %74, i64 1
store float %73, float* %75
%76 = load double, double* %8
%77 = fptrunc double %76 to float
%78 = load float*, float** %4
%79 = getelementptr inbounds float, float* %78, i64 2
store float %77, float* %79
%80 = load double, double* %9
%81 = fptrunc double %80 to float
%82 = load float*, float** %4
%83 = getelementptr inbounds float, float* %82, i64 3
store float %81, float* %83
ret void
}

LLVM IR (-O2):
define void @Matrix2Quaternion([3 x float]* nocapture %0, float* nocapture writeonly %1) {
%3 = getelementptr inbounds [3 x float], [3 x float]* %0, i64 0, i64 0
%4 = load float, float* %3
%5 = fpext float %4 to double
%6 = fadd double %5, 1.000000e+00
%7 = getelementptr inbounds [3 x float], [3 x float]* %0, i64 1, i64 1
%8 = load float, float* %7
%9 = fpext float %8 to double
%10 = fadd double %6, %9
%11 = getelementptr inbounds [3 x float], [3 x float]* %0, i64 2, i64 2
%12 = load float, float* %11
%13 = fpext float %12 to double
%14 = fadd double %10, %13
%15 = tail call double @sqrt(double %14)
%16 = fmul double %15, 5.000000e-01
%17 = fmul double %16, 4.000000e+00
%18 = getelementptr inbounds [3 x float], [3 x float]* %0, i64 2, i64 1
%19 = load float, float* %18
%20 = getelementptr inbounds [3 x float], [3 x float]* %0, i64 1, i64 2
%21 = getelementptr inbounds [3 x float], [3 x float]* %0, i64 0, i64 2
%22 = load float, float* %21
%23 = bitcast float* %20 to <2 x float>*
%24 = load <2 x float>, <2 x float>* %23
%25 = insertelement <2 x float> poison, float %19, i64 0
%26 = insertelement <2 x float> %25, float %22, i64 1
%27 = fsub <2 x float> %26, %24
%28 = fpext <2 x float> %27 to <2 x double>
%29 = insertelement <2 x double> poison, double %17, i64 0
%30 = shufflevector <2 x double> %29, <2 x double> poison, <2 x i32> zeroinitializer
%31 = fdiv <2 x double> %28, %30
%32 = getelementptr inbounds [3 x float], [3 x float]* %0, i64 1, i64 0
%33 = load float, float* %32
%34 = getelementptr inbounds [3 x float], [3 x float]* %0, i64 0, i64 1
%35 = load float, float* %34
%36 = fsub float %33, %35
%37 = fpext float %36 to double
%38 = fdiv double %37, %17
%39 = insertelement <4 x double> poison, double %16, i64 0
%40 = shufflevector <2 x double> %31, <2 x double> poison, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
%41 = shufflevector <4 x double> %39, <4 x double> %40, <4 x i32> <i32 0, i32 4, i32 5, i32 undef>
%42 = insertelement <4 x double> %41, double %38, i64 3
%43 = fptrunc <4 x double> %42 to <4 x float>
%44 = bitcast float* %1 to <4 x float>*
store <4 x float> %43, <4 x float>* %44
ret void
}