#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 */
int /*<<< orphan*/ normalize_quat(double *);
int /*<<< orphan*/ vadd(double *, double *, double *);
int /*<<< orphan*/ vcopy(double *, double *);
int /*<<< orphan*/ vcross(double *, double *, double *);
double vdot(double *, double *);
int /*<<< orphan*/ vscale(double *, double);
void add_quats(double q1[4], double q2[4], double dest[4]) {
  static int count = 0;
  double t1[4], t2[4], t3[4];
  double tf[4];
  vcopy(q1, t1);
  vscale(t1, q2[3]);
  vcopy(q2, t2);
  vscale(t2, q1[3]);
  vcross(q2, q1, t3);
  vadd(t1, t2, tf);
  vadd(t3, tf, tf);
  tf[3] = q1[3] * q2[3] - vdot(q1, q2);
  dest[0] = tf[0];
  dest[1] = tf[1];
  dest[2] = tf[2];
  dest[3] = tf[3];
  if (++count > 0) {
    count = 0;
    normalize_quat(dest);
  }
}


x86 assembly (clang, -O0):
<add_quats>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <vcopy@plt>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
movsd  0x18(%rax),%xmm0
call   1070 <vscale@plt>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1080 <vcopy@plt>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rax
movsd  0x18(%rax),%xmm0
call   1070 <vscale@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
lea    -0x80(%rbp),%rdx
call   1050 <vcross@plt>
lea    -0x40(%rbp),%rdi
lea    -0x60(%rbp),%rsi
lea    -0xa0(%rbp),%rdx
call   1030 <vadd@plt>
lea    -0x80(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
lea    -0xa0(%rbp),%rdx
call   1030 <vadd@plt>
mov    -0x8(%rbp),%rax
movsd  0x18(%rax),%xmm0
movsd  %xmm0,-0xa8(%rbp)
ff
mov    -0x10(%rbp),%rax
movsd  0x18(%rax),%xmm0
movsd  %xmm0,-0xb0(%rbp)
ff
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <vdot@plt>
movsd  -0xb0(%rbp),%xmm2
ff
movaps %xmm0,%xmm1
movsd  -0xa8(%rbp),%xmm0
ff
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x88(%rbp)
ff
movsd  -0xa0(%rbp),%xmm0
ff
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0x98(%rbp),%xmm0
ff
mov    -0x18(%rbp),%rax
movsd  %xmm0,0x8(%rax)
movsd  -0x90(%rbp),%xmm0
ff
mov    -0x18(%rbp),%rax
movsd  %xmm0,0x10(%rax)
movsd  -0x88(%rbp),%xmm0
ff
mov    -0x18(%rbp),%rax
movsd  %xmm0,0x18(%rax)
mov    0x2db1(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2da8(%rip)
cmp    $0x0,%eax
jle    12c8 <add_quats+0x168>
movl   $0x0,0x2d95(%rip)
mov    -0x18(%rbp),%rdi
call   1040 <normalize_quat@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_quats>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x30(%rsp),%rbp
mov    %rbp,%rsi
call   1080 <vcopy@plt>
movsd  0x18(%r15),%xmm0
mov    %rbp,%rdi
call   1070 <vscale@plt>
lea    0x70(%rsp),%r13
mov    %r15,%rdi
mov    %r13,%rsi
call   1080 <vcopy@plt>
movsd  0x18(%rbx),%xmm0
mov    %r13,%rdi
call   1070 <vscale@plt>
lea    0x50(%rsp),%rbp
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1050 <vcross@plt>
lea    0x10(%rsp),%r12
lea    0x30(%rsp),%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1030 <vadd@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r12,%rdx
call   1030 <vadd@plt>
movsd  0x18(%rbx),%xmm0
mulsd  0x18(%r15),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <vdot@plt>
movsd  0x8(%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,0x28(%rsp)
movaps 0x10(%rsp),%xmm0
movups %xmm0,(%r14)
movsd  0x20(%rsp),%xmm0
movsd  %xmm0,0x10(%r14)
movsd  %xmm1,0x18(%r14)
mov    0x2e1f(%rip),%eax
lea    0x1(%rax),%ecx
mov    %ecx,0x2e16(%rip)
test   %eax,%eax
js     1254 <add_quats+0xf4>
movl   $0x0,0x2e08(%rip)
mov    %r14,%rdi
call   1040 <normalize_quat@plt>
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@add_quats.count = internal global i32 0
define void @add_quats(double* %0, double* %1, double* %2) {
%4 = alloca double*
%5 = alloca double*
%6 = alloca double*
%7 = alloca [4 x double]
%8 = alloca [4 x double]
%9 = alloca [4 x double]
%10 = alloca [4 x double]
store double* %0, double** %4
store double* %1, double** %5
store double* %2, double** %6
%11 = load double*, double** %4
%12 = getelementptr inbounds [4 x double], [4 x double]* %7, i64 0, i64 0
%13 = call i32 @vcopy(double* %11, double* %12)
%14 = getelementptr inbounds [4 x double], [4 x double]* %7, i64 0, i64 0
%15 = load double*, double** %5
%16 = getelementptr inbounds double, double* %15, i64 3
%17 = load double, double* %16
%18 = call i32 @vscale(double* %14, double %17)
%19 = load double*, double** %5
%20 = getelementptr inbounds [4 x double], [4 x double]* %8, i64 0, i64 0
%21 = call i32 @vcopy(double* %19, double* %20)
%22 = getelementptr inbounds [4 x double], [4 x double]* %8, i64 0, i64 0
%23 = load double*, double** %4
%24 = getelementptr inbounds double, double* %23, i64 3
%25 = load double, double* %24
%26 = call i32 @vscale(double* %22, double %25)
%27 = load double*, double** %5
%28 = load double*, double** %4
%29 = getelementptr inbounds [4 x double], [4 x double]* %9, i64 0, i64 0
%30 = call i32 @vcross(double* %27, double* %28, double* %29)
%31 = getelementptr inbounds [4 x double], [4 x double]* %7, i64 0, i64 0
%32 = getelementptr inbounds [4 x double], [4 x double]* %8, i64 0, i64 0
%33 = getelementptr inbounds [4 x double], [4 x double]* %10, i64 0, i64 0
%34 = call i32 @vadd(double* %31, double* %32, double* %33)
%35 = getelementptr inbounds [4 x double], [4 x double]* %9, i64 0, i64 0
%36 = getelementptr inbounds [4 x double], [4 x double]* %10, i64 0, i64 0
%37 = getelementptr inbounds [4 x double], [4 x double]* %10, i64 0, i64 0
%38 = call i32 @vadd(double* %35, double* %36, double* %37)
%39 = load double*, double** %4
%40 = getelementptr inbounds double, double* %39, i64 3
%41 = load double, double* %40
%42 = load double*, double** %5
%43 = getelementptr inbounds double, double* %42, i64 3
%44 = load double, double* %43
%45 = load double*, double** %4
%46 = load double*, double** %5
%47 = call double @vdot(double* %45, double* %46)
%48 = fneg double %47
%49 = call double @llvm.fmuladd.f64(double %41, double %44, double %48)
%50 = getelementptr inbounds [4 x double], [4 x double]* %10, i64 0, i64 3
store double %49, double* %50
%51 = getelementptr inbounds [4 x double], [4 x double]* %10, i64 0, i64 0
%52 = load double, double* %51
%53 = load double*, double** %6
%54 = getelementptr inbounds double, double* %53, i64 0
store double %52, double* %54
%55 = getelementptr inbounds [4 x double], [4 x double]* %10, i64 0, i64 1
%56 = load double, double* %55
%57 = load double*, double** %6
%58 = getelementptr inbounds double, double* %57, i64 1
store double %56, double* %58
%59 = getelementptr inbounds [4 x double], [4 x double]* %10, i64 0, i64 2
%60 = load double, double* %59
%61 = load double*, double** %6
%62 = getelementptr inbounds double, double* %61, i64 2
store double %60, double* %62
%63 = getelementptr inbounds [4 x double], [4 x double]* %10, i64 0, i64 3
%64 = load double, double* %63
%65 = load double*, double** %6
%66 = getelementptr inbounds double, double* %65, i64 3
store double %64, double* %66
%67 = load i32, i32* @add_quats.count
%68 = add nsw i32 %67, 1
store i32 %68, i32* @add_quats.count
%69 = icmp sgt i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %3
store i32 0, i32* @add_quats.count
%71 = load double*, double** %6
%72 = call i32 @normalize_quat(double* %71)
br label %73
73: ; preds = %70, %3
ret void
}

LLVM IR (-O2):
@add_quats.count = internal unnamed_addr global i32 0
define void @add_quats(double* %0, double* %1, double* %2) {
%4 = alloca [4 x double]
%5 = alloca [4 x double]
%6 = alloca [4 x double]
%7 = alloca [4 x double]
%8 = bitcast [4 x double]* %4 to i8*
%9 = bitcast [4 x double]* %5 to i8*
%10 = bitcast [4 x double]* %6 to i8*
%11 = bitcast [4 x double]* %7 to i8*
%12 = getelementptr inbounds [4 x double], [4 x double]* %4, i64 0, i64 0
%13 = call i32 @vcopy(double* %0, double* %12)
%14 = getelementptr inbounds double, double* %1, i64 3
%15 = load double, double* %14
%16 = call i32 @vscale(double* %12, double %15)
%17 = getelementptr inbounds [4 x double], [4 x double]* %5, i64 0, i64 0
%18 = call i32 @vcopy(double* %1, double* %17)
%19 = getelementptr inbounds double, double* %0, i64 3
%20 = load double, double* %19
%21 = call i32 @vscale(double* %17, double %20)
%22 = getelementptr inbounds [4 x double], [4 x double]* %6, i64 0, i64 0
%23 = call i32 @vcross(double* %1, double* %0, double* %22)
%24 = getelementptr inbounds [4 x double], [4 x double]* %7, i64 0, i64 0
%25 = call i32 @vadd(double* %12, double* %17, double* %24)
%26 = call i32 @vadd(double* %22, double* %24, double* %24)
%27 = load double, double* %19
%28 = load double, double* %14
%29 = call double @vdot(double* %0, double* %1)
%30 = fneg double %29
%31 = call double @llvm.fmuladd.f64(double %27, double %28, double %30)
%32 = getelementptr inbounds [4 x double], [4 x double]* %7, i64 0, i64 3
store double %31, double* %32
%33 = bitcast [4 x double]* %7 to <2 x double>*
%34 = load <2 x double>, <2 x double>* %33
%35 = bitcast double* %2 to <2 x double>*
store <2 x double> %34, <2 x double>* %35
%36 = getelementptr inbounds [4 x double], [4 x double]* %7, i64 0, i64 2
%37 = load double, double* %36
%38 = getelementptr inbounds double, double* %2, i64 2
store double %37, double* %38
%39 = getelementptr inbounds double, double* %2, i64 3
store double %31, double* %39
%40 = load i32, i32* @add_quats.count
%41 = add nsw i32 %40, 1
store i32 %41, i32* @add_quats.count
%42 = icmp sgt i32 %40, -1
br i1 %42, label %43, label %45
43: ; preds = %3
store i32 0, i32* @add_quats.count
%44 = call i32 @normalize_quat(double* %2)
br label %45
45: ; preds = %43, %3
ret void
}