#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 dotProduct(double *, double *);
scalar_t__ sqr(double);
double sqrt(scalar_t__);
double *subVector(double *, double *);
double sphereIntersection(double *Ro, double *Rd, double *position,
                          double radius) {
  double t;
  double *RoSubPosition = subVector(Ro, position);
  double b = 2 * dotProduct(Rd, RoSubPosition);
  double c = dotProduct(RoSubPosition, RoSubPosition) - radius;
  double sqrtDelta = sqrt(sqr(b) - 4 * c);
  t = (-b - sqrtDelta) / 2;
  if (t < 0) {
    t = (-b + sqrtDelta) / 2;
  }
  return t;
}


x86 assembly (clang, -O0):
<sphereIntersection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <subVector@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <dotProduct@plt>
movaps %xmm0,%xmm1
movsd  0xe7e(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <dotProduct@plt>
subsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1050 <sqr@plt>
cvtsi2sd %rax,%xmm1
movsd  -0x40(%rbp),%xmm0
movsd  0xe4a(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rdi
call   1060 <sqrt@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x38(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
subsd  -0x48(%rbp),%xmm0
movsd  0xe02(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x28(%rbp),%xmm0
jbe    1247 <sphereIntersection+0x107>
movsd  -0x38(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
addsd  -0x48(%rbp),%xmm0
movsd  0xdc2(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sphereIntersection>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movsd  %xmm0,0x8(%rsp)
mov    %rsi,%r14
mov    %rdx,%rsi
call   1030 <subVector@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <dotProduct@plt>
addsd  %xmm0,%xmm0
movapd %xmm0,0x10(%rsp)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1040 <dotProduct@plt>
subsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
movaps 0x10(%rsp),%xmm0
call   1050 <sqr@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  0x8(%rsp),%xmm1
mulsd  0xe59(%rip),%xmm1
addsd  %xmm0,%xmm1
cvttsd2si %xmm1,%rdi
call   1060 <sqrt@plt>
movapd 0xe53(%rip),%xmm1
movapd 0x10(%rsp),%xmm3
xorpd  %xmm3,%xmm1
subsd  %xmm0,%xmm1
movsd  0xe35(%rip),%xmm2
mulsd  %xmm2,%xmm1
subsd  %xmm3,%xmm0
mulsd  %xmm2,%xmm0
xorpd  %xmm2,%xmm2
movapd %xmm1,%xmm3
cmpltsd %xmm2,%xmm3
andpd  %xmm3,%xmm0
andnpd %xmm1,%xmm3
orpd   %xmm3,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define double @sphereIntersection(double* %0, double* %1, double* %2, double %3) {
%5 = alloca double*
%6 = alloca double*
%7 = alloca double*
%8 = alloca double
%9 = alloca double
%10 = alloca double*
%11 = alloca double
%12 = alloca double
%13 = alloca double
store double* %0, double** %5
store double* %1, double** %6
store double* %2, double** %7
store double %3, double* %8
%14 = load double*, double** %5
%15 = load double*, double** %7
%16 = call double* @subVector(double* %14, double* %15)
store double* %16, double** %10
%17 = load double*, double** %6
%18 = load double*, double** %10
%19 = call double @dotProduct(double* %17, double* %18)
%20 = fmul double 2.000000e+00, %19
store double %20, double* %11
%21 = load double*, double** %10
%22 = load double*, double** %10
%23 = call double @dotProduct(double* %21, double* %22)
%24 = load double, double* %8
%25 = fsub double %23, %24
store double %25, double* %12
%26 = load double, double* %11
%27 = call i64 @sqr(double %26)
%28 = sitofp i64 %27 to double
%29 = load double, double* %12
%30 = call double @llvm.fmuladd.f64(double -4.000000e+00, double %29, double %28)
%31 = fptosi double %30 to i64
%32 = call double @sqrt(i64 %31)
store double %32, double* %13
%33 = load double, double* %11
%34 = fneg double %33
%35 = load double, double* %13
%36 = fsub double %34, %35
%37 = fdiv double %36, 2.000000e+00
store double %37, double* %9
%38 = load double, double* %9
%39 = fcmp olt double %38, 0.000000e+00
br i1 %39, label %40, label %46
40: ; preds = %4
%41 = load double, double* %11
%42 = fneg double %41
%43 = load double, double* %13
%44 = fadd double %42, %43
%45 = fdiv double %44, 2.000000e+00
store double %45, double* %9
br label %46
46: ; preds = %40, %4
%47 = load double, double* %9
ret double %47
}

LLVM IR (-O2):
define double @sphereIntersection(double* %0, double* %1, double* %2, double %3) {
%5 = tail call double* @subVector(double* %0, double* %2)
%6 = tail call double @dotProduct(double* %1, double* %5)
%7 = fmul double %6, 2.000000e+00
%8 = tail call double @dotProduct(double* %5, double* %5)
%9 = fsub double %8, %3
%10 = tail call i64 @sqr(double %7)
%11 = sitofp i64 %10 to double
%12 = tail call double @llvm.fmuladd.f64(double %9, double -4.000000e+00, double %11)
%13 = fptosi double %12 to i64
%14 = tail call double @sqrt(i64 %13)
%15 = fneg double %7
%16 = fsub double %15, %14
%17 = fmul double %16, 5.000000e-01
%18 = fcmp olt double %17, 0.000000e+00
%19 = fsub double %14, %7
%20 = fmul double %19, 5.000000e-01
%21 = select i1 %18, double %20, double %17
ret double %21
}