#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 */
float sqrt(float);
void normalize_quaternion(float orientation[4]) {
  float factor = orientation[0] * orientation[0];
  factor += orientation[1] * orientation[1];
  factor += orientation[2] * orientation[2];
  factor += orientation[3] * orientation[3];
  if (factor > 0.0f) {
    factor = sqrt(factor);
    orientation[0] /= factor;
    orientation[1] /= factor;
    orientation[2] /= factor;
    orientation[3] /= factor;
  }
}


x86 assembly (clang, -O0):
<normalize_quaternion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
mov    -0x8(%rbp),%rax
mulss  (%rax),%xmm0
movss  %xmm0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movss  0x4(%rax),%xmm0
mov    -0x8(%rbp),%rax
movss  0x4(%rax),%xmm2
movss  -0xc(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movss  0x8(%rax),%xmm0
mov    -0x8(%rbp),%rax
movss  0x8(%rax),%xmm2
movss  -0xc(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movss  0xc(%rax),%xmm0
mov    -0x8(%rbp),%rax
movss  0xc(%rax),%xmm2
movss  -0xc(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0xc(%rbp)
movss  -0xc(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jbe    1217 <normalize_quaternion+0x107>
movss  -0xc(%rbp),%xmm0
call   1030 <sqrt@plt>
movss  %xmm0,-0xc(%rbp)
movss  -0xc(%rbp),%xmm1
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
divss  %xmm1,%xmm0
movss  %xmm0,(%rax)
movss  -0xc(%rbp),%xmm1
mov    -0x8(%rbp),%rax
movss  0x4(%rax),%xmm0
divss  %xmm1,%xmm0
movss  %xmm0,0x4(%rax)
movss  -0xc(%rbp),%xmm1
mov    -0x8(%rbp),%rax
movss  0x8(%rax),%xmm0
divss  %xmm1,%xmm0
movss  %xmm0,0x8(%rax)
movss  -0xc(%rbp),%xmm1
mov    -0x8(%rbp),%rax
movss  0xc(%rax),%xmm0
divss  %xmm1,%xmm0
movss  %xmm0,0xc(%rax)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<normalize_quaternion>:
push   %rbx
movss  (%rdi),%xmm0
movss  0x4(%rdi),%xmm1
mulss  %xmm0,%xmm0
mulss  %xmm1,%xmm1
addss  %xmm0,%xmm1
movss  0x8(%rdi),%xmm2
mulss  %xmm2,%xmm2
addss  %xmm1,%xmm2
movss  0xc(%rdi),%xmm0
mulss  %xmm0,%xmm0
addss  %xmm2,%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jbe    1165 <normalize_quaternion+0x55>
mov    %rdi,%rbx
jb     1153 <normalize_quaternion+0x43>
sqrtss %xmm0,%xmm0
jmp    1158 <normalize_quaternion+0x48>
call   1030 <sqrt@plt>
movups (%rbx),%xmm1
shufps $0x0,%xmm0,%xmm0
divps  %xmm0,%xmm1
movups %xmm1,(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
define void @normalize_quaternion(float* %0) {
%2 = alloca float*
%3 = alloca float
store float* %0, float** %2
%4 = load float*, float** %2
%5 = getelementptr inbounds float, float* %4, i64 0
%6 = load float, float* %5
%7 = load float*, float** %2
%8 = getelementptr inbounds float, float* %7, i64 0
%9 = load float, float* %8
%10 = fmul float %6, %9
store float %10, float* %3
%11 = load float*, float** %2
%12 = getelementptr inbounds float, float* %11, i64 1
%13 = load float, float* %12
%14 = load float*, float** %2
%15 = getelementptr inbounds float, float* %14, i64 1
%16 = load float, float* %15
%17 = load float, float* %3
%18 = call float @llvm.fmuladd.f32(float %13, float %16, float %17)
store float %18, float* %3
%19 = load float*, float** %2
%20 = getelementptr inbounds float, float* %19, i64 2
%21 = load float, float* %20
%22 = load float*, float** %2
%23 = getelementptr inbounds float, float* %22, i64 2
%24 = load float, float* %23
%25 = load float, float* %3
%26 = call float @llvm.fmuladd.f32(float %21, float %24, float %25)
store float %26, float* %3
%27 = load float*, float** %2
%28 = getelementptr inbounds float, float* %27, i64 3
%29 = load float, float* %28
%30 = load float*, float** %2
%31 = getelementptr inbounds float, float* %30, i64 3
%32 = load float, float* %31
%33 = load float, float* %3
%34 = call float @llvm.fmuladd.f32(float %29, float %32, float %33)
store float %34, float* %3
%35 = load float, float* %3
%36 = fcmp ogt float %35, 0.000000e+00
br i1 %36, label %37, label %60
37: ; preds = %1
%38 = load float, float* %3
%39 = call float @sqrt(float %38)
store float %39, float* %3
%40 = load float, float* %3
%41 = load float*, float** %2
%42 = getelementptr inbounds float, float* %41, i64 0
%43 = load float, float* %42
%44 = fdiv float %43, %40
store float %44, float* %42
%45 = load float, float* %3
%46 = load float*, float** %2
%47 = getelementptr inbounds float, float* %46, i64 1
%48 = load float, float* %47
%49 = fdiv float %48, %45
store float %49, float* %47
%50 = load float, float* %3
%51 = load float*, float** %2
%52 = getelementptr inbounds float, float* %51, i64 2
%53 = load float, float* %52
%54 = fdiv float %53, %50
store float %54, float* %52
%55 = load float, float* %3
%56 = load float*, float** %2
%57 = getelementptr inbounds float, float* %56, i64 3
%58 = load float, float* %57
%59 = fdiv float %58, %55
store float %59, float* %57
br label %60
60: ; preds = %37, %1
ret void
}

LLVM IR (-O2):
define void @normalize_quaternion(float* nocapture %0) {
%2 = load float, float* %0
%3 = fmul float %2, %2
%4 = getelementptr inbounds float, float* %0, i64 1
%5 = load float, float* %4
%6 = tail call float @llvm.fmuladd.f32(float %5, float %5, float %3)
%7 = getelementptr inbounds float, float* %0, i64 2
%8 = load float, float* %7
%9 = tail call float @llvm.fmuladd.f32(float %8, float %8, float %6)
%10 = getelementptr inbounds float, float* %0, i64 3
%11 = load float, float* %10
%12 = tail call float @llvm.fmuladd.f32(float %11, float %11, float %9)
%13 = fcmp ogt float %12, 0.000000e+00
br i1 %13, label %14, label %22
14: ; preds = %1
%15 = tail call float @sqrt(float %12)
%16 = bitcast float* %0 to <4 x float>*
%17 = load <4 x float>, <4 x float>* %16
%18 = insertelement <4 x float> poison, float %15, i64 0
%19 = shufflevector <4 x float> %18, <4 x float> poison, <4 x i32> zeroinitializer
%20 = fdiv <4 x float> %17, %19
%21 = bitcast float* %0 to <4 x float>*
store <4 x float> %20, <4 x float>* %21
br label %22
22: ; preds = %14, %1
ret void
}