#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);
int quatNormalize(double *q, double *qout) {
  double mag = 0.0, sign, eps = 1.e-12;
  int i;
  sign = ((q[0] >= 0.0) ? 1.0 : -1.0);
  for (i = 0; i < 4; ++i) {
    mag += q[i] * q[i];
    qout[i] = q[i];
  }
  mag = sign / sqrt(mag + eps);
  for (i = 0; i < 4; ++i) {
    qout[i] *= mag;
  }
}


x86 assembly (clang, -O0):
<quatNormalize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xee0(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm1
movsd  0xebb(%rip),%xmm3
movsd  0xebb(%rip),%xmm2
xorps  %xmm0,%xmm0
cmplesd %xmm1,%xmm0
movaps %xmm0,%xmm1
andpd  %xmm3,%xmm1
andnpd %xmm2,%xmm0
orpd   %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x4,-0x34(%rbp)
jge    11ce <quatNormalize+0xbe>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1170 <quatNormalize+0x60>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x20(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
call   1030 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x4,-0x34(%rbp)
jge    122e <quatNormalize+0x11e>
movsd  -0x20(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11ff <quatNormalize+0xef>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<quatNormalize>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movsd  (%rdi),%xmm0
xorpd  %xmm1,%xmm1
xor    %eax,%eax
ucomisd %xmm1,%xmm0
setae  %al
movsd  %xmm0,(%rsi)
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0x8(%rdi),%xmm2
movsd  %xmm2,0x8(%rsi)
mulsd  %xmm2,%xmm2
addsd  %xmm0,%xmm2
movsd  0x10(%rdi),%xmm0
movsd  %xmm0,0x10(%rsi)
movapd %xmm0,%xmm3
mulsd  %xmm0,%xmm3
addsd  %xmm2,%xmm3
movsd  0x18(%rdi),%xmm0
movsd  %xmm0,0x18(%rsi)
mulsd  %xmm0,%xmm0
addsd  %xmm3,%xmm0
lea    0xe8a(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm2
addsd  0xe8d(%rip),%xmm0
ucomisd %xmm1,%xmm0
jb     118f <quatNormalize+0x7f>
sqrtsd %xmm0,%xmm0
jmp    119e <quatNormalize+0x8e>
movapd %xmm2,(%rsp)
call   1030 <sqrt@plt>
movapd (%rsp),%xmm2
divsd  %xmm0,%xmm2
unpcklpd %xmm2,%xmm2
movupd (%rbx),%xmm0
movupd 0x10(%rbx),%xmm1
mulpd  %xmm2,%xmm0
movupd %xmm0,(%rbx)
mulpd  %xmm2,%xmm1
movupd %xmm1,0x10(%rbx)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @quatNormalize(double* %0, double* %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca double*
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca i32
store double* %0, double** %4
store double* %1, double** %5
store double 0.000000e+00, double* %6
store double 0x3D719799812DEA11, double* %8
%10 = load double*, double** %4
%11 = getelementptr inbounds double, double* %10, i64 0
%12 = load double, double* %11
%13 = fcmp oge double %12, 0.000000e+00
%14 = zext i1 %13 to i64
%15 = select i1 %13, double 1.000000e+00, double -1.000000e+00
store double %15, double* %7
store i32 0, i32* %9
br label %16
16: ; preds = %41, %2
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 4
br i1 %18, label %19, label %44
19: ; preds = %16
%20 = load double*, double** %4
%21 = load i32, i32* %9
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds double, double* %20, i64 %22
%24 = load double, double* %23
%25 = load double*, double** %4
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds double, double* %25, i64 %27
%29 = load double, double* %28
%30 = load double, double* %6
%31 = call double @llvm.fmuladd.f64(double %24, double %29, double %30)
store double %31, double* %6
%32 = load double*, double** %4
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %32, i64 %34
%36 = load double, double* %35
%37 = load double*, double** %5
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds double, double* %37, i64 %39
store double %36, double* %40
br label %41
41: ; preds = %19
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %16
44: ; preds = %16
%45 = load double, double* %7
%46 = load double, double* %6
%47 = load double, double* %8
%48 = fadd double %46, %47
%49 = call double @sqrt(double %48)
%50 = fdiv double %45, %49
store double %50, double* %6
store i32 0, i32* %9
br label %51
51: ; preds = %62, %44
%52 = load i32, i32* %9
%53 = icmp slt i32 %52, 4
br i1 %53, label %54, label %65
54: ; preds = %51
%55 = load double, double* %6
%56 = load double*, double** %5
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds double, double* %56, i64 %58
%60 = load double, double* %59
%61 = fmul double %60, %55
store double %61, double* %59
br label %62
62: ; preds = %54
%63 = load i32, i32* %9
%64 = add nsw i32 %63, 1
store i32 %64, i32* %9
br label %51
65: ; preds = %51
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
define i32 @quatNormalize(double* nocapture %0, double* nocapture %1) {
%3 = load double, double* %0
%4 = fcmp oge double %3, 0.000000e+00
%5 = tail call double @llvm.fmuladd.f64(double %3, double %3, double 0.000000e+00)
store double %3, double* %1
%6 = getelementptr inbounds double, double* %0, i64 1
%7 = load double, double* %6
%8 = tail call double @llvm.fmuladd.f64(double %7, double %7, double %5)
%9 = getelementptr inbounds double, double* %1, i64 1
store double %7, double* %9
%10 = getelementptr inbounds double, double* %0, i64 2
%11 = load double, double* %10
%12 = tail call double @llvm.fmuladd.f64(double %11, double %11, double %8)
%13 = getelementptr inbounds double, double* %1, i64 2
store double %11, double* %13
%14 = getelementptr inbounds double, double* %0, i64 3
%15 = load double, double* %14
%16 = tail call double @llvm.fmuladd.f64(double %15, double %15, double %12)
%17 = getelementptr inbounds double, double* %1, i64 3
store double %15, double* %17
%18 = select i1 %4, double 1.000000e+00, double -1.000000e+00
%19 = fadd double %16, 0x3D719799812DEA11
%20 = tail call double @sqrt(double %19)
%21 = fdiv double %18, %20
%22 = bitcast double* %1 to <2 x double>*
%23 = load <2 x double>, <2 x double>* %22
%24 = insertelement <2 x double> poison, double %21, i64 0
%25 = shufflevector <2 x double> %24, <2 x double> poison, <2 x i32> zeroinitializer
%26 = fmul <2 x double> %25, %23
%27 = bitcast double* %1 to <2 x double>*
store <2 x double> %26, <2 x double>* %27
%28 = bitcast double* %13 to <2 x double>*
%29 = load <2 x double>, <2 x double>* %28
%30 = fmul <2 x double> %25, %29
%31 = bitcast double* %13 to <2 x double>*
store <2 x double> %30, <2 x double>* %31
ret i32 undef
}