#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 asin(float);
int /*<<< orphan*/ axis_to_quat(float *, float, float *);
int /*<<< orphan*/ tb_project_to_sphere(float, float, float);
int /*<<< orphan*/ vcross(float *, float *, float *);
double vlength(float *);
int /*<<< orphan*/ vset(float *, float, float, int /*<<< orphan*/);
int /*<<< orphan*/ vsub(float *, float *, float *);
int /*<<< orphan*/ vzero(float *);
void trackball(float q[4], float p1x, float p1y, float p2x, float p2y) {
  float a[3];
  float phi;
  float p1[3], p2[3], d[3];
  float t;
  if (p1x == p2x && p1y == p2y) {
    vzero(q);
    q[3] = 1.0;
    return;
  }
  vset(p1, p1x, p1y, tb_project_to_sphere((0.8f), p1x, p1y));
  vset(p2, p2x, p2y, tb_project_to_sphere((0.8f), p2x, p2y));
  vcross(p2, p1, a);
  vsub(p1, p2, d);
  t = vlength(d) / (2.0 * (0.8f));
  if (t > 1.0)
    t = 1.0;
  if (t < -1.0)
    t = -1.0;
  phi = 2.0 * asin(t);
  axis_to_quat(a, phi, q);
}


x86 assembly (clang, -O0):
<trackball>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
movss  %xmm0,-0xc(%rbp)
movss  %xmm1,-0x10(%rbp)
movss  %xmm2,-0x14(%rbp)
movss  %xmm3,-0x18(%rbp)
movss  -0xc(%rbp),%xmm0
ucomiss -0x14(%rbp),%xmm0
jne    11e9 <trackball+0x69>
jp     11e9 <trackball+0x69>
movss  -0x10(%rbp),%xmm0
ucomiss -0x18(%rbp),%xmm0
jne    11e9 <trackball+0x69>
jp     11e9 <trackball+0x69>
mov    -0x8(%rbp),%rdi
call   10a0 <vzero@plt>
mov    -0x8(%rbp),%rax
movss  0xe45(%rip),%xmm0
movss  %xmm0,0xc(%rax)
jmp    133d <trackball+0x1bd>
lea    -0x34(%rbp),%rax
mov    %rax,-0x70(%rbp)
movss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x68(%rbp)
movss  -0x10(%rbp),%xmm0
movss  %xmm0,-0x64(%rbp)
movss  -0xc(%rbp),%xmm1
movss  -0x10(%rbp),%xmm2
movss  0xe09(%rip),%xmm0
call   1030 <tb_project_to_sphere@plt>
mov    -0x70(%rbp),%rdi
movss  -0x68(%rbp),%xmm0
movss  -0x64(%rbp),%xmm1
mov    %eax,%esi
call   1060 <vset@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
movss  -0x14(%rbp),%xmm0
movss  %xmm0,-0x58(%rbp)
movss  -0x18(%rbp),%xmm0
movss  %xmm0,-0x54(%rbp)
movss  -0x14(%rbp),%xmm1
movss  -0x18(%rbp),%xmm2
movss  0xdc1(%rip),%xmm0
call   1030 <tb_project_to_sphere@plt>
mov    -0x60(%rbp),%rdi
movss  -0x58(%rbp),%xmm0
movss  -0x54(%rbp),%xmm1
mov    %eax,%esi
call   1060 <vset@plt>
lea    -0x40(%rbp),%rdi
lea    -0x34(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1090 <vcross@plt>
lea    -0x34(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x4c(%rbp),%rdx
call   1040 <vsub@plt>
lea    -0x4c(%rbp),%rdi
call   1050 <vlength@plt>
movsd  0xd5c(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x50(%rbp)
movss  -0x50(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xd36(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    12e1 <trackball+0x161>
movss  0xd48(%rip),%xmm0
movss  %xmm0,-0x50(%rbp)
movss  -0x50(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xd1e(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    1309 <trackball+0x189>
movss  0xd24(%rip),%xmm0
movss  %xmm0,-0x50(%rbp)
movss  -0x50(%rbp),%xmm0
call   1070 <asin@plt>
movaps %xmm0,%xmm1
movsd  0xcfa(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x28(%rbp)
lea    -0x24(%rbp),%rdi
movss  -0x28(%rbp),%xmm0
mov    -0x8(%rbp),%rsi
call   1080 <axis_to_quat@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trackball>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40,%rsp
movaps %xmm1,%xmm4
movaps %xmm0,%xmm1
mov    %rdi,%r12
ucomiss %xmm2,%xmm0
jne    11ba <trackball+0x3a>
jp     11ba <trackball+0x3a>
ucomiss %xmm3,%xmm4
jne    11ba <trackball+0x3a>
jp     11ba <trackball+0x3a>
mov    %r12,%rdi
call   10a0 <vzero@plt>
movl   $0x3f800000,0xc(%r12)
80 3f
jmp    1298 <trackball+0x118>
movss  0xe3e(%rip),%xmm0
movss  %xmm1,0xc(%rsp)
movss  %xmm2,(%rsp)
movaps %xmm4,%xmm2
movss  %xmm3,0x4(%rsp)
movss  %xmm4,0x8(%rsp)
call   1030 <tb_project_to_sphere@plt>
lea    0x28(%rsp),%r15
mov    %r15,%rdi
movss  0xc(%rsp),%xmm0
movss  0x8(%rsp),%xmm1
mov    %eax,%esi
call   1060 <vset@plt>
movss  0xdfc(%rip),%xmm0
movss  (%rsp),%xmm1
movss  0x4(%rsp),%xmm2
call   1030 <tb_project_to_sphere@plt>
lea    0x1c(%rsp),%rbx
mov    %rbx,%rdi
movss  (%rsp),%xmm0
movss  0x4(%rsp),%xmm1
mov    %eax,%esi
call   1060 <vset@plt>
lea    0x34(%rsp),%r14
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1090 <vcross@plt>
lea    0x10(%rsp),%r13
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1040 <vsub@plt>
mov    %r13,%rdi
call   1050 <vlength@plt>
divsd  0xdac(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  0xd94(%rip),%xmm1
minss  %xmm0,%xmm1
movss  0xd8c(%rip),%xmm0
maxss  %xmm1,%xmm0
call   1070 <asin@plt>
addsd  %xmm0,%xmm0
cvtsd2ss %xmm0,%xmm0
mov    %r14,%rdi
mov    %r12,%rsi
call   1080 <axis_to_quat@plt>
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @trackball(float* %0, float %1, float %2, float %3, float %4) {
%6 = alloca float*
%7 = alloca float
%8 = alloca float
%9 = alloca float
%10 = alloca float
%11 = alloca [3 x float]
%12 = alloca float
%13 = alloca [3 x float]
%14 = alloca [3 x float]
%15 = alloca [3 x float]
%16 = alloca float
store float* %0, float** %6
store float %1, float* %7
store float %2, float* %8
store float %3, float* %9
store float %4, float* %10
%17 = load float, float* %7
%18 = load float, float* %9
%19 = fcmp oeq float %17, %18
br i1 %19, label %20, label %29
20: ; preds = %5
%21 = load float, float* %8
%22 = load float, float* %10
%23 = fcmp oeq float %21, %22
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load float*, float** %6
%26 = call i32 @vzero(float* %25)
%27 = load float*, float** %6
%28 = getelementptr inbounds float, float* %27, i64 3
store float 1.000000e+00, float* %28
br label %74
29: ; preds = %20, %5
%30 = getelementptr inbounds [3 x float], [3 x float]* %13, i64 0, i64 0
%31 = load float, float* %7
%32 = load float, float* %8
%33 = load float, float* %7
%34 = load float, float* %8
%35 = call i32 @tb_project_to_sphere(float 0x3FE99999A0000000, float %33, float %34)
%36 = call i32 @vset(float* %30, float %31, float %32, i32 %35)
%37 = getelementptr inbounds [3 x float], [3 x float]* %14, i64 0, i64 0
%38 = load float, float* %9
%39 = load float, float* %10
%40 = load float, float* %9
%41 = load float, float* %10
%42 = call i32 @tb_project_to_sphere(float 0x3FE99999A0000000, float %40, float %41)
%43 = call i32 @vset(float* %37, float %38, float %39, i32 %42)
%44 = getelementptr inbounds [3 x float], [3 x float]* %14, i64 0, i64 0
%45 = getelementptr inbounds [3 x float], [3 x float]* %13, i64 0, i64 0
%46 = getelementptr inbounds [3 x float], [3 x float]* %11, i64 0, i64 0
%47 = call i32 @vcross(float* %44, float* %45, float* %46)
%48 = getelementptr inbounds [3 x float], [3 x float]* %13, i64 0, i64 0
%49 = getelementptr inbounds [3 x float], [3 x float]* %14, i64 0, i64 0
%50 = getelementptr inbounds [3 x float], [3 x float]* %15, i64 0, i64 0
%51 = call i32 @vsub(float* %48, float* %49, float* %50)
%52 = getelementptr inbounds [3 x float], [3 x float]* %15, i64 0, i64 0
%53 = call double @vlength(float* %52)
%54 = fdiv double %53, 0x3FF99999A0000000
%55 = fptrunc double %54 to float
store float %55, float* %16
%56 = load float, float* %16
%57 = fpext float %56 to double
%58 = fcmp ogt double %57, 1.000000e+00
br i1 %58, label %59, label %60
59: ; preds = %29
store float 1.000000e+00, float* %16
br label %60
60: ; preds = %59, %29
%61 = load float, float* %16
%62 = fpext float %61 to double
%63 = fcmp olt double %62, -1.000000e+00
br i1 %63, label %64, label %65
64: ; preds = %60
store float -1.000000e+00, float* %16
br label %65
65: ; preds = %64, %60
%66 = load float, float* %16
%67 = call double @asin(float %66)
%68 = fmul double 2.000000e+00, %67
%69 = fptrunc double %68 to float
store float %69, float* %12
%70 = getelementptr inbounds [3 x float], [3 x float]* %11, i64 0, i64 0
%71 = load float, float* %12
%72 = load float*, float** %6
%73 = call i32 @axis_to_quat(float* %70, float %71, float* %72)
br label %74
74: ; preds = %65, %24
ret void
}

LLVM IR (-O2):
define void @trackball(float* %0, float %1, float %2, float %3, float %4) {
%6 = alloca [3 x float]
%7 = alloca [3 x float]
%8 = alloca [3 x float]
%9 = alloca [3 x float]
%10 = bitcast [3 x float]* %6 to i8*
%11 = bitcast [3 x float]* %7 to i8*
%12 = bitcast [3 x float]* %8 to i8*
%13 = bitcast [3 x float]* %9 to i8*
%14 = fcmp oeq float %1, %3
%15 = fcmp oeq float %2, %4
%16 = and i1 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %5
%18 = tail call i32 @vzero(float* %0)
%19 = getelementptr inbounds float, float* %0, i64 3
store float 1.000000e+00, float* %19
br label %42
20: ; preds = %5
%21 = getelementptr inbounds [3 x float], [3 x float]* %7, i64 0, i64 0
%22 = tail call i32 @tb_project_to_sphere(float 0x3FE99999A0000000, float %1, float %2)
%23 = call i32 @vset(float* %21, float %1, float %2, i32 %22)
%24 = getelementptr inbounds [3 x float], [3 x float]* %8, i64 0, i64 0
%25 = call i32 @tb_project_to_sphere(float 0x3FE99999A0000000, float %3, float %4)
%26 = call i32 @vset(float* %24, float %3, float %4, i32 %25)
%27 = getelementptr inbounds [3 x float], [3 x float]* %6, i64 0, i64 0
%28 = call i32 @vcross(float* %24, float* %21, float* %27)
%29 = getelementptr inbounds [3 x float], [3 x float]* %9, i64 0, i64 0
%30 = call i32 @vsub(float* %21, float* %24, float* %29)
%31 = call double @vlength(float* %29)
%32 = fdiv double %31, 0x3FF99999A0000000
%33 = fptrunc double %32 to float
%34 = fcmp ogt float %33, 1.000000e+00
%35 = select i1 %34, float 1.000000e+00, float %33
%36 = fcmp olt float %35, -1.000000e+00
%37 = select i1 %36, float -1.000000e+00, float %35
%38 = call double @asin(float %37)
%39 = fmul double %38, 2.000000e+00
%40 = fptrunc double %39 to float
%41 = call i32 @axis_to_quat(float* %27, float %40, float* %0)
br label %42
42: ; preds = %20, %17
ret void
}