void normalize_quat(double q[4]) {
  int i;
  double mag;
  mag = (q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
  for (i = 0; i < 4; i++) {
    q[i] /= mag;
  }
}


x86 assembly (clang, -O0):
<normalize_quat>:
push   %rbp
mov    %rsp,%rbp
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
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x4,-0xc(%rbp)
jge    11a5 <normalize_quat+0xa5>
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    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1172 <normalize_quat+0x72>
pop    %rbp
ret

x86 assembly (clang, -O2):
<normalize_quat>:
movupd (%rdi),%xmm0
movupd 0x10(%rdi),%xmm1
movapd %xmm0,%xmm2
mulpd  %xmm0,%xmm2
unpckhpd %xmm2,%xmm2
movapd %xmm0,%xmm3
mulsd  %xmm0,%xmm3
addsd  %xmm2,%xmm3
movapd %xmm1,%xmm2
mulsd  %xmm1,%xmm2
addsd  %xmm3,%xmm2
movapd %xmm1,%xmm3
unpckhpd %xmm1,%xmm3
mulsd  %xmm3,%xmm3
addsd  %xmm2,%xmm3
unpcklpd %xmm3,%xmm3
divpd  %xmm3,%xmm0
movupd %xmm0,(%rdi)
divpd  %xmm3,%xmm1
movupd %xmm1,0x10(%rdi)
ret

LLVM IR (-O0):
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)
store double %32, double* %4
store i32 0, i32* %3
br label %33
33: ; preds = %44, %1
%34 = load i32, i32* %3
%35 = icmp slt i32 %34, 4
br i1 %35, label %36, label %47
36: ; preds = %33
%37 = load double, double* %4
%38 = load double*, double** %2
%39 = load i32, i32* %3
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds double, double* %38, i64 %40
%42 = load double, double* %41
%43 = fdiv double %42, %37
store double %43, double* %41
br label %44
44: ; preds = %36
%45 = load i32, i32* %3
%46 = add nsw i32 %45, 1
store i32 %46, i32* %3
br label %33
47: ; preds = %33
ret void
}

LLVM IR (-O2):
define void @normalize_quat(double* nocapture %0) {
%2 = bitcast double* %0 to <2 x double>*
%3 = load <2 x double>, <2 x double>* %2
%4 = fmul <2 x double> %3, %3
%5 = extractelement <2 x double> %4, i64 1
%6 = extractelement <2 x double> %3, i64 0
%7 = tail call double @llvm.fmuladd.f64(double %6, double %6, double %5)
%8 = getelementptr inbounds double, double* %0, i64 2
%9 = bitcast double* %8 to <2 x double>*
%10 = load <2 x double>, <2 x double>* %9
%11 = extractelement <2 x double> %10, i64 0
%12 = tail call double @llvm.fmuladd.f64(double %11, double %11, double %7)
%13 = extractelement <2 x double> %10, i64 1
%14 = tail call double @llvm.fmuladd.f64(double %13, double %13, double %12)
%15 = insertelement <2 x double> poison, double %14, i64 0
%16 = shufflevector <2 x double> %15, <2 x double> poison, <2 x i32> zeroinitializer
%17 = fdiv <2 x double> %3, %16
%18 = bitcast double* %0 to <2 x double>*
store <2 x double> %17, <2 x double>* %18
%19 = fdiv <2 x double> %10, %16
%20 = bitcast double* %8 to <2 x double>*
store <2 x double> %19, <2 x double>* %20
ret void
}