#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 normalize_quat(double q[4]) {
  int i;
  double mag;
  static double oldq[4];
  mag = sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
  if ((mag > 1.01) || (mag < 0.99)) {
    for (i = 0; i < 4; i++) {
      q[i] = oldq[i];
    }
    if (q[0] * q[0] + q[1] * q[1] + q[2] * q[2] == 0.)
      q[0] = 1.;
  } else
    for (i = 0; i < 4; i++) {
      q[i] /= mag;
      oldq[i] = q[i];
    }
}


x86 assembly (clang, -O0):
<normalize_quat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm2
mov    -0x8(%rbp),%rax
movsd  0x8(%rax),%xmm1
mov    -0x8(%rbp),%rax
mulsd  0x8(%rax),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movsd  0x10(%rax),%xmm1
mov    -0x8(%rbp),%rax
movsd  0x10(%rax),%xmm2
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
mov    -0x8(%rbp),%rax
movsd  0x18(%rax),%xmm0
mov    -0x8(%rbp),%rax
movsd  0x18(%rax),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xe6f(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     11ae <normalize_quat+0x9e>
movsd  0xe65(%rip),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    1256 <normalize_quat+0x146>
movl   $0x0,-0xc(%rbp)
cmpl   $0x4,-0xc(%rbp)
jge    11ea <normalize_quat+0xda>
movslq -0xc(%rbp),%rcx
lea    0x2e76(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11b5 <normalize_quat+0xa5>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm1
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm2
mov    -0x8(%rbp),%rax
movsd  0x8(%rax),%xmm0
mov    -0x8(%rbp),%rax
mulsd  0x8(%rax),%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
mov    -0x8(%rbp),%rax
movsd  0x10(%rax),%xmm0
mov    -0x8(%rbp),%rax
movsd  0x10(%rax),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1251 <normalize_quat+0x141>
jp     1251 <normalize_quat+0x141>
mov    -0x8(%rbp),%rax
movsd  0xdc3(%rip),%xmm0
movsd  %xmm0,(%rax)
jmp    12b2 <normalize_quat+0x1a2>
movl   $0x0,-0xc(%rbp)
cmpl   $0x4,-0xc(%rbp)
jge    12ad <normalize_quat+0x19d>
movsd  -0x18(%rbp),%xmm1
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movslq -0xc(%rbp),%rcx
lea    0x2da6(%rip),%rax
movsd  %xmm0,(%rax,%rcx,8)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    125d <normalize_quat+0x14d>
jmp    12b2 <normalize_quat+0x1a2>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<normalize_quat>:
push   %rbx
mov    %rdi,%rbx
movsd  (%rdi),%xmm0
movsd  0x8(%rdi),%xmm1
mulsd  %xmm1,%xmm1
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0x10(%rdi),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0x18(%rdi),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     115d <normalize_quat+0x4d>
sqrtsd %xmm0,%xmm0
ucomisd 0xea7(%rip),%xmm0
jbe    116c <normalize_quat+0x5c>
jmp    11aa <normalize_quat+0x9a>
call   1030 <sqrt@plt>
ucomisd 0xe96(%rip),%xmm0
ja     11aa <normalize_quat+0x9a>
movsd  0xe94(%rip),%xmm1
ucomisd %xmm0,%xmm1
ja     11aa <normalize_quat+0x9a>
movupd (%rbx),%xmm1
unpcklpd %xmm0,%xmm0
divpd  %xmm0,%xmm1
movupd %xmm1,(%rbx)
movapd %xmm1,0x2eae(%rip)
movupd 0x10(%rbx),%xmm1
divpd  %xmm0,%xmm1
movupd %xmm1,0x10(%rbx)
movapd %xmm1,0x2ea8(%rip)
pop    %rbx
ret
movaps 0x2e8f(%rip),%xmm0
movups %xmm0,(%rbx)
movaps 0x2e95(%rip),%xmm0
movups %xmm0,0x10(%rbx)
movsd  (%rbx),%xmm0
movsd  0x8(%rbx),%xmm1
mulsd  %xmm1,%xmm1
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0x10(%rbx),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jne    11fa <normalize_quat+0xea>
jp     11fa <normalize_quat+0xea>
movabs $0x3ff0000000000000,%rax
mov    %rax,(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
@normalize_quat.oldq = internal global [4 x double] zeroinitializer
define void @normalize_quat(double* %0) {
%2 = alloca double*
%3 = alloca i32
%4 = alloca double
store double* %0, double** %2
%5 = load double*, double** %2
%6 = getelementptr inbounds double, double* %5, i64 0
%7 = load double, double* %6
%8 = load double*, double** %2
%9 = getelementptr inbounds double, double* %8, i64 0
%10 = load double, double* %9
%11 = load double*, double** %2
%12 = getelementptr inbounds double, double* %11, i64 1
%13 = load double, double* %12
%14 = load double*, double** %2
%15 = getelementptr inbounds double, double* %14, i64 1
%16 = load double, double* %15
%17 = fmul double %13, %16
%18 = call double @llvm.fmuladd.f64(double %7, double %10, double %17)
%19 = load double*, double** %2
%20 = getelementptr inbounds double, double* %19, i64 2
%21 = load double, double* %20
%22 = load double*, double** %2
%23 = getelementptr inbounds double, double* %22, i64 2
%24 = load double, double* %23
%25 = call double @llvm.fmuladd.f64(double %21, double %24, double %18)
%26 = load double*, double** %2
%27 = getelementptr inbounds double, double* %26, i64 3
%28 = load double, double* %27
%29 = load double*, double** %2
%30 = getelementptr inbounds double, double* %29, i64 3
%31 = load double, double* %30
%32 = call double @llvm.fmuladd.f64(double %28, double %31, double %25)
%33 = call double @sqrt(double %32)
store double %33, double* %4
%34 = load double, double* %4
%35 = fcmp ogt double %34, 1.010000e+00
br i1 %35, label %39, label %36
36: ; preds = %1
%37 = load double, double* %4
%38 = fcmp olt double %37, 0x3FEFAE147AE147AE
br i1 %38, label %39, label %82
39: ; preds = %36, %1
store i32 0, i32* %3
br label %40
40: ; preds = %52, %39
%41 = load i32, i32* %3
%42 = icmp slt i32 %41, 4
br i1 %42, label %43, label %55
43: ; preds = %40
%44 = load i32, i32* %3
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [4 x double], [4 x double]* @normalize_quat.oldq, i64 0, i64 %45
%47 = load double, double* %46
%48 = load double*, double** %2
%49 = load i32, i32* %3
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds double, double* %48, i64 %50
store double %47, double* %51
br label %52
52: ; preds = %43
%53 = load i32, i32* %3
%54 = add nsw i32 %53, 1
store i32 %54, i32* %3
br label %40
55: ; preds = %40
%56 = load double*, double** %2
%57 = getelementptr inbounds double, double* %56, i64 0
%58 = load double, double* %57
%59 = load double*, double** %2
%60 = getelementptr inbounds double, double* %59, i64 0
%61 = load double, double* %60
%62 = load double*, double** %2
%63 = getelementptr inbounds double, double* %62, i64 1
%64 = load double, double* %63
%65 = load double*, double** %2
%66 = getelementptr inbounds double, double* %65, i64 1
%67 = load double, double* %66
%68 = fmul double %64, %67
%69 = call double @llvm.fmuladd.f64(double %58, double %61, double %68)
%70 = load double*, double** %2
%71 = getelementptr inbounds double, double* %70, i64 2
%72 = load double, double* %71
%73 = load double*, double** %2
%74 = getelementptr inbounds double, double* %73, i64 2
%75 = load double, double* %74
%76 = call double @llvm.fmuladd.f64(double %72, double %75, double %69)
%77 = fcmp oeq double %76, 0.000000e+00
br i1 %77, label %78, label %81
78: ; preds = %55
%79 = load double*, double** %2
%80 = getelementptr inbounds double, double* %79, i64 0
store double 1.000000e+00, double* %80
br label %81
81: ; preds = %78, %55
br label %106
82: ; preds = %36
store i32 0, i32* %3
br label %83
83: ; preds = %102, %82
%84 = load i32, i32* %3
%85 = icmp slt i32 %84, 4
br i1 %85, label %86, label %105
86: ; preds = %83
%87 = load double, double* %4
%88 = load double*, double** %2
%89 = load i32, i32* %3
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds double, double* %88, i64 %90
%92 = load double, double* %91
%93 = fdiv double %92, %87
store double %93, double* %91
%94 = load double*, double** %2
%95 = load i32, i32* %3
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds double, double* %94, i64 %96
%98 = load double, double* %97
%99 = load i32, i32* %3
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds [4 x double], [4 x double]* @normalize_quat.oldq, i64 0, i64 %100
store double %98, double* %101
br label %102
102: ; preds = %86
%103 = load i32, i32* %3
%104 = add nsw i32 %103, 1
store i32 %104, i32* %3
br label %83
105: ; preds = %83
br label %106
106: ; preds = %105, %81
ret void
}

LLVM IR (-O2):
@normalize_quat.oldq = internal unnamed_addr global [4 x double] zeroinitializer
define void @normalize_quat(double* nocapture %0) {
%2 = load double, double* %0
%3 = getelementptr inbounds double, double* %0, i64 1
%4 = load double, double* %3
%5 = fmul double %4, %4
%6 = tail call double @llvm.fmuladd.f64(double %2, double %2, double %5)
%7 = getelementptr inbounds double, double* %0, i64 2
%8 = load double, double* %7
%9 = tail call double @llvm.fmuladd.f64(double %8, double %8, double %6)
%10 = getelementptr inbounds double, double* %0, i64 3
%11 = load double, double* %10
%12 = tail call double @llvm.fmuladd.f64(double %11, double %11, double %9)
%13 = tail call double @sqrt(double %12)
%14 = fcmp ogt double %13, 1.010000e+00
%15 = fcmp olt double %13, 0x3FEFAE147AE147AE
%16 = or i1 %14, %15
br i1 %16, label %28, label %17
17: ; preds = %1
%18 = bitcast double* %0 to <2 x double>*
%19 = load <2 x double>, <2 x double>* %18
%20 = insertelement <2 x double> poison, double %13, i64 0
%21 = shufflevector <2 x double> %20, <2 x double> poison, <2 x i32> zeroinitializer
%22 = fdiv <2 x double> %19, %21
%23 = bitcast double* %0 to <2 x double>*
store <2 x double> %22, <2 x double>* %23
store <2 x double> %22, <2 x double>* bitcast ([4 x double]* @normalize_quat.oldq to <2 x double>*)
%24 = bitcast double* %7 to <2 x double>*
%25 = load <2 x double>, <2 x double>* %24
%26 = fdiv <2 x double> %25, %21
%27 = bitcast double* %7 to <2 x double>*
store <2 x double> %26, <2 x double>* %27
store <2 x double> %26, <2 x double>* bitcast (double* getelementptr inbounds ([4 x double], [4 x double]* @normalize_quat.oldq, i64 0, i64 2) to <2 x double>*)
br label %38
28: ; preds = %1
%29 = bitcast double* %0 to i8*
%30 = load double, double* %0
%31 = load double, double* %3
%32 = fmul double %31, %31
%33 = tail call double @llvm.fmuladd.f64(double %30, double %30, double %32)
%34 = load double, double* %7
%35 = tail call double @llvm.fmuladd.f64(double %34, double %34, double %33)
%36 = fcmp oeq double %35, 0.000000e+00
br i1 %36, label %37, label %38
37: ; preds = %28
store double 1.000000e+00, double* %0
br label %38
38: ; preds = %17, %28, %37
ret void
}