#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 *);
int /*<<< orphan*/ tb_projectHyp_to_sphere(double, double, double);
int /*<<< orphan*/ tb_project_to_sphere(double, double, double);
int /*<<< orphan*/ vcross(double *, double *, double *);
double vlength(double *);
int /*<<< orphan*/ vset(double *, double, double, int /*<<< orphan*/);
int /*<<< orphan*/ vsub(double *, double *, double *);
int /*<<< orphan*/ vzero(double *);
void trackball(char projTrueFlag, double ballsize, double q[4], double p1x,
               double p1y, double p2x, double p2y) {
  double a[3];
  double phi;
  double p1[3], p2[3], d[3];
  double t;
  if (p1x == p2x && p1y == p2y) {
    vzero(q);
    q[3] = 1.0;
    return;
  }
  if (projTrueFlag) {
    vset(p1, p1x, p1y, tb_project_to_sphere(ballsize, p1x, p1y));
    vset(p2, p2x, p2y, tb_project_to_sphere(ballsize, p2x, p2y));
  } else {
    vset(p1, p1x, p1y, tb_projectHyp_to_sphere(ballsize, p1x, p1y));
    vset(p2, p2x, p2y, tb_projectHyp_to_sphere(ballsize, p2x, p2y));
  }
  vcross(p2, p1, a);
  vsub(p1, p2, d);
  t = vlength(d) / (2.0 * ballsize);
  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    $0x120,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movsd  %xmm1,-0x20(%rbp)
movsd  %xmm2,-0x28(%rbp)
movsd  %xmm3,-0x30(%rbp)
movsd  %xmm4,-0x38(%rbp)
movsd  -0x20(%rbp),%xmm0
ucomisd -0x30(%rbp),%xmm0
jne    1209 <trackball+0x79>
jp     1209 <trackball+0x79>
movsd  -0x28(%rbp),%xmm0
ucomisd -0x38(%rbp),%xmm0
jne    1209 <trackball+0x79>
jp     1209 <trackball+0x79>
mov    -0x18(%rbp),%rdi
call   10b0 <vzero@plt>
mov    -0x18(%rbp),%rax
movsd  0xe01(%rip),%xmm0
movsd  %xmm0,0x18(%rax)
jmp    144d <trackball+0x2bd>
cmpb   $0x0,-0x1(%rbp)
je     12c9 <trackball+0x139>
lea    -0x70(%rbp),%rax
mov    %rax,-0xe8(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0xe0(%rbp)
ff
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0xd8(%rbp)
ff
movsd  -0x10(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
call   1040 <tb_project_to_sphere@plt>
mov    -0xe8(%rbp),%rdi
movsd  -0xe0(%rbp),%xmm0
ff
movsd  -0xd8(%rbp),%xmm1
ff
mov    %eax,%esi
call   1070 <vset@plt>
lea    -0x90(%rbp),%rax
mov    %rax,-0xd0(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0xc8(%rbp)
ff
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0xc0(%rbp)
ff
movsd  -0x10(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm2
call   1040 <tb_project_to_sphere@plt>
mov    -0xd0(%rbp),%rdi
movsd  -0xc8(%rbp),%xmm0
ff
movsd  -0xc0(%rbp),%xmm1
ff
mov    %eax,%esi
call   1070 <vset@plt>
jmp    137a <trackball+0x1ea>
lea    -0x70(%rbp),%rax
mov    %rax,-0x118(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x110(%rbp)
ff
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x108(%rbp)
ff
movsd  -0x10(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
call   1030 <tb_projectHyp_to_sphere@plt>
mov    -0x118(%rbp),%rdi
movsd  -0x110(%rbp),%xmm0
ff
movsd  -0x108(%rbp),%xmm1
ff
mov    %eax,%esi
call   1070 <vset@plt>
lea    -0x90(%rbp),%rax
mov    %rax,-0x100(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0xf8(%rbp)
ff
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0xf0(%rbp)
ff
movsd  -0x10(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm2
call   1030 <tb_projectHyp_to_sphere@plt>
mov    -0x100(%rbp),%rdi
movsd  -0xf8(%rbp),%xmm0
ff
movsd  -0xf0(%rbp),%xmm1
ff
mov    %eax,%esi
call   1070 <vset@plt>
lea    -0x90(%rbp),%rdi
lea    -0x70(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   10a0 <vcross@plt>
lea    -0x70(%rbp),%rdi
lea    -0x90(%rbp),%rsi
lea    -0xb0(%rbp),%rdx
call   1050 <vsub@plt>
lea    -0xb0(%rbp),%rdi
call   1060 <vlength@plt>
movsd  0xc4f(%rip),%xmm1
mulsd  -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0xb8(%rbp)
ff
movsd  -0xb8(%rbp),%xmm0
ff
movsd  0xc26(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    13f4 <trackball+0x264>
movsd  0xc14(%rip),%xmm0
movsd  %xmm0,-0xb8(%rbp)
ff
movsd  0xc14(%rip),%xmm0
ucomisd -0xb8(%rbp),%xmm0
ff
jbe    141a <trackball+0x28a>
movsd  0xbfe(%rip),%xmm0
movsd  %xmm0,-0xb8(%rbp)
ff
movsd  -0xb8(%rbp),%xmm0
ff
call   1080 <asin@plt>
movaps %xmm0,%xmm1
movsd  0xbd6(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x58(%rbp)
lea    -0x50(%rbp),%rdi
movsd  -0x58(%rbp),%xmm0
mov    -0x18(%rbp),%rsi
call   1090 <axis_to_quat@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trackball>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb0,%rsp
mov    %rsi,%r15
ucomisd %xmm3,%xmm1
jne    11ce <trackball+0x3e>
jp     11ce <trackball+0x3e>
ucomisd %xmm4,%xmm2
jne    11ce <trackball+0x3e>
jp     11ce <trackball+0x3e>
mov    %r15,%rdi
call   10b0 <vzero@plt>
movabs $0x3ff0000000000000,%rax
mov    %rax,0x18(%r15)
jmp    12e8 <trackball+0x158>
movsd  %xmm3,0x8(%rsp)
movsd  %xmm4,0x10(%rsp)
test   %dil,%dil
movsd  %xmm0,0x18(%rsp)
movsd  %xmm1,0x28(%rsp)
movsd  %xmm2,0x20(%rsp)
je     1227 <trackball+0x97>
call   1040 <tb_project_to_sphere@plt>
lea    0x50(%rsp),%rdi
movsd  0x28(%rsp),%xmm0
movsd  0x20(%rsp),%xmm1
mov    %eax,%esi
call   1070 <vset@plt>
movsd  0x18(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
movsd  0x10(%rsp),%xmm2
call   1040 <tb_project_to_sphere@plt>
jmp    125b <trackball+0xcb>
call   1030 <tb_projectHyp_to_sphere@plt>
lea    0x50(%rsp),%rdi
movsd  0x28(%rsp),%xmm0
movsd  0x20(%rsp),%xmm1
mov    %eax,%esi
call   1070 <vset@plt>
movsd  0x18(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
movsd  0x10(%rsp),%xmm2
call   1030 <tb_projectHyp_to_sphere@plt>
lea    0x30(%rsp),%rdi
movsd  0x8(%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
mov    %eax,%esi
call   1070 <vset@plt>
lea    0x30(%rsp),%r12
lea    0x50(%rsp),%r13
lea    0x90(%rsp),%r14
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   10a0 <vcross@plt>
lea    0x70(%rsp),%rbx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <vsub@plt>
mov    %rbx,%rdi
call   1060 <vlength@plt>
movsd  0x18(%rsp),%xmm1
addsd  %xmm1,%xmm1
divsd  %xmm1,%xmm0
movsd  0xd3c(%rip),%xmm1
minsd  %xmm0,%xmm1
movsd  0xd38(%rip),%xmm0
maxsd  %xmm1,%xmm0
call   1080 <asin@plt>
addsd  %xmm0,%xmm0
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <axis_to_quat@plt>
add    $0xb0,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @trackball(i8 signext %0, double %1, double* %2, double %3, double %4, double %5, double %6) {
%8 = alloca i8
%9 = alloca double
%10 = alloca double*
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca [3 x double]
%16 = alloca double
%17 = alloca [3 x double]
%18 = alloca [3 x double]
%19 = alloca [3 x double]
%20 = alloca double
store i8 %0, i8* %8
store double %1, double* %9
store double* %2, double** %10
store double %3, double* %11
store double %4, double* %12
store double %5, double* %13
store double %6, double* %14
%21 = load double, double* %11
%22 = load double, double* %13
%23 = fcmp oeq double %21, %22
br i1 %23, label %24, label %33
24: ; preds = %7
%25 = load double, double* %12
%26 = load double, double* %14
%27 = fcmp oeq double %25, %26
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load double*, double** %10
%30 = call i32 @vzero(double* %29)
%31 = load double*, double** %10
%32 = getelementptr inbounds double, double* %31, i64 3
store double 1.000000e+00, double* %32
br label %99
33: ; preds = %24, %7
%34 = load i8, i8* %8
%35 = icmp ne i8 %34, 0
br i1 %35, label %36, label %53
36: ; preds = %33
%37 = getelementptr inbounds [3 x double], [3 x double]* %17, i64 0, i64 0
%38 = load double, double* %11
%39 = load double, double* %12
%40 = load double, double* %9
%41 = load double, double* %11
%42 = load double, double* %12
%43 = call i32 @tb_project_to_sphere(double %40, double %41, double %42)
%44 = call i32 @vset(double* %37, double %38, double %39, i32 %43)
%45 = getelementptr inbounds [3 x double], [3 x double]* %18, i64 0, i64 0
%46 = load double, double* %13
%47 = load double, double* %14
%48 = load double, double* %9
%49 = load double, double* %13
%50 = load double, double* %14
%51 = call i32 @tb_project_to_sphere(double %48, double %49, double %50)
%52 = call i32 @vset(double* %45, double %46, double %47, i32 %51)
br label %70
53: ; preds = %33
%54 = getelementptr inbounds [3 x double], [3 x double]* %17, i64 0, i64 0
%55 = load double, double* %11
%56 = load double, double* %12
%57 = load double, double* %9
%58 = load double, double* %11
%59 = load double, double* %12
%60 = call i32 @tb_projectHyp_to_sphere(double %57, double %58, double %59)
%61 = call i32 @vset(double* %54, double %55, double %56, i32 %60)
%62 = getelementptr inbounds [3 x double], [3 x double]* %18, i64 0, i64 0
%63 = load double, double* %13
%64 = load double, double* %14
%65 = load double, double* %9
%66 = load double, double* %13
%67 = load double, double* %14
%68 = call i32 @tb_projectHyp_to_sphere(double %65, double %66, double %67)
%69 = call i32 @vset(double* %62, double %63, double %64, i32 %68)
br label %70
70: ; preds = %53, %36
%71 = getelementptr inbounds [3 x double], [3 x double]* %18, i64 0, i64 0
%72 = getelementptr inbounds [3 x double], [3 x double]* %17, i64 0, i64 0
%73 = getelementptr inbounds [3 x double], [3 x double]* %15, i64 0, i64 0
%74 = call i32 @vcross(double* %71, double* %72, double* %73)
%75 = getelementptr inbounds [3 x double], [3 x double]* %17, i64 0, i64 0
%76 = getelementptr inbounds [3 x double], [3 x double]* %18, i64 0, i64 0
%77 = getelementptr inbounds [3 x double], [3 x double]* %19, i64 0, i64 0
%78 = call i32 @vsub(double* %75, double* %76, double* %77)
%79 = getelementptr inbounds [3 x double], [3 x double]* %19, i64 0, i64 0
%80 = call double @vlength(double* %79)
%81 = load double, double* %9
%82 = fmul double 2.000000e+00, %81
%83 = fdiv double %80, %82
store double %83, double* %20
%84 = load double, double* %20
%85 = fcmp ogt double %84, 1.000000e+00
br i1 %85, label %86, label %87
86: ; preds = %70
store double 1.000000e+00, double* %20
br label %87
87: ; preds = %86, %70
%88 = load double, double* %20
%89 = fcmp olt double %88, -1.000000e+00
br i1 %89, label %90, label %91
90: ; preds = %87
store double -1.000000e+00, double* %20
br label %91
91: ; preds = %90, %87
%92 = load double, double* %20
%93 = call double @asin(double %92)
%94 = fmul double 2.000000e+00, %93
store double %94, double* %16
%95 = getelementptr inbounds [3 x double], [3 x double]* %15, i64 0, i64 0
%96 = load double, double* %16
%97 = load double*, double** %10
%98 = call i32 @axis_to_quat(double* %95, double %96, double* %97)
br label %99
99: ; preds = %91, %28
ret void
}

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