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


x86 assembly (clang, -O0):
<trackball>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  %xmm3,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jne    11e7 <trackball+0x77>
jp     11e7 <trackball+0x77>
movsd  -0x18(%rbp),%xmm0
ucomisd -0x28(%rbp),%xmm0
jne    11e7 <trackball+0x77>
jp     11e7 <trackball+0x77>
mov    -0x8(%rbp),%rdi
xorps  %xmm2,%xmm2
movaps %xmm2,%xmm0
movaps %xmm2,%xmm1
call   1060 <vset@plt>
mov    -0x8(%rbp),%rax
movsd  0xe23(%rip),%xmm0
movsd  %xmm0,0x18(%rax)
jmp    130f <trackball+0x19f>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm2
movsd  0xdf3(%rip),%xmm0
call   1030 <tb_project_to_sphere@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x80(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
movsd  -0x20(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
movsd  0xdc3(%rip),%xmm0
call   1030 <tb_project_to_sphere@plt>
movsd  %xmm0,-0x70(%rbp)
lea    -0x40(%rbp),%rdi
lea    -0x80(%rbp),%rsi
lea    -0x60(%rbp),%rdx
call   1090 <vcross@plt>
lea    -0xa0(%rbp),%rdi
lea    -0x60(%rbp),%rsi
lea    -0x80(%rbp),%rdx
call   1040 <vsub@plt>
lea    -0xa0(%rbp),%rdi
call   1050 <vlength@plt>
movsd  0xd88(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0xa8(%rbp)
ff
movsd  -0xa8(%rbp),%xmm0
ff
movsd  0xd64(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    12b6 <trackball+0x146>
movsd  0xd52(%rip),%xmm0
movsd  %xmm0,-0xa8(%rbp)
ff
movsd  0xd52(%rip),%xmm0
ucomisd -0xa8(%rbp),%xmm0
ff
jbe    12dc <trackball+0x16c>
movsd  0xd3c(%rip),%xmm0
movsd  %xmm0,-0xa8(%rbp)
ff
movsd  -0xa8(%rbp),%xmm0
ff
call   1070 <asin@plt>
movaps %xmm0,%xmm1
movsd  0xd14(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
movsd  -0x48(%rbp),%xmm0
mov    -0x8(%rbp),%rsi
call   1080 <axis_to_quat@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trackball>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x90,%rsp
movapd %xmm2,%xmm4
movapd %xmm1,%xmm2
movapd %xmm0,%xmm1
mov    %rdi,%r13
ucomisd %xmm4,%xmm0
jne    11c6 <trackball+0x56>
jp     11c6 <trackball+0x56>
ucomisd %xmm3,%xmm2
jne    11c6 <trackball+0x56>
jp     11c6 <trackball+0x56>
xorpd  %xmm0,%xmm0
xorpd  %xmm1,%xmm1
xorpd  %xmm2,%xmm2
mov    %r13,%rdi
call   1060 <vset@plt>
movabs $0x3ff0000000000000,%rax
mov    %rax,0x18(%r13)
jmp    1286 <trackball+0x116>
movsd  %xmm1,0x30(%rsp)
movsd  %xmm2,0x38(%rsp)
movsd  0xe26(%rip),%xmm0
movsd  %xmm3,0x8(%rsp)
movsd  %xmm4,(%rsp)
call   1030 <tb_project_to_sphere@plt>
movsd  %xmm0,0x40(%rsp)
movsd  (%rsp),%xmm1
movsd  %xmm1,0x10(%rsp)
movsd  0x8(%rsp),%xmm2
movsd  %xmm2,0x18(%rsp)
movsd  0xdf1(%rip),%xmm0
call   1030 <tb_project_to_sphere@plt>
movsd  %xmm0,0x20(%rsp)
lea    0x70(%rsp),%r14
lea    0x10(%rsp),%r15
lea    0x30(%rsp),%r12
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1090 <vcross@plt>
lea    0x50(%rsp),%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1040 <vsub@plt>
mov    %rbx,%rdi
call   1050 <vlength@plt>
mulsd  0xdae(%rip),%xmm0
movsd  0xd9e(%rip),%xmm1
minsd  %xmm0,%xmm1
movsd  0xda2(%rip),%xmm0
maxsd  %xmm1,%xmm0
call   1070 <asin@plt>
addsd  %xmm0,%xmm0
mov    %r14,%rdi
mov    %r13,%rsi
call   1080 <axis_to_quat@plt>
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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