// File: /workspace/github_cpp_makefile_star2_O1/Luthaf[P]zeopp/ray.cc
// Function: Sphere::hitSphere(ray, hitdata&)
void Sphere::hitSphere(ray r,hitdata& hitsphere)
{
  Point newCenter = center-r.base; //map the rand_ray to the origin and move system
  double temp=(r.vector*newCenter)*(r.vector*newCenter)-(newCenter*newCenter)+radius*radius;
  if (temp>0) //check inside of sqrt is not negative
    { 
      if ((r.vector * newCenter) - sqrt(temp) > 0) //If it hits the sphere twice pick the closest
	{ 
	  hitsphere.hit=true;
	  hitsphere.dist=(r.vector*newCenter) - sqrt(temp);
	  hitsphere.hitpoint=r.base+r.vector.scale(hitsphere.dist);
	}
      else if ((r.vector * newCenter) + sqrt(temp) > 0) //Ray was inside sphere (this is possible in some cases)
	{ 
	  hitsphere.hit=true;
	  hitsphere.dist=(r.vector*newCenter) + sqrt(temp);
	  hitsphere.hitpoint=r.base+r.vector.scale(hitsphere.dist);
    	} 
    }
  return;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %rbx
movq %rdi, %r15
leaq 0xc8(%rsp), %r14
movq -0x8(%r14), %rax
movq %rax, 0x10(%rsp)
movaps -0x18(%r14), %xmm0
movups %xmm0, (%rsp)
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x8576e
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x857d2
movsd %xmm0, 0x18(%rsp)
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x857d2
movsd %xmm0, 0x20(%rsp)
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movupd (%r12), %xmm0
movupd %xmm0, (%rsp)
movq %r12, %rdi
callq 0x857d2
movsd 0x20(%rsp), %xmm1
mulsd 0x18(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movsd 0x18(%r15), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x4b4b4
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x28(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %r14, %rdi
movsd %xmm1, 0x18(%rsp)
callq 0x857d2
movsd 0x18(%rsp), %xmm1
movapd %xmm0, %xmm2
ucomisd 0x107f96(%rip), %xmm1   # 0x1532c0
jb 0x4b335
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x4b34a
movapd %xmm1, %xmm0
movsd %xmm2, 0x20(%rsp)
callq 0x4350
movsd 0x20(%rsp), %xmm2
leaq 0xb0(%rsp), %r15
ucomisd %xmm0, %xmm2
jbe 0x4b392
movb $0x1, (%rbx)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x28(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %r14, %rdi
callq 0x857d2
movapd %xmm0, %xmm2
xorpd %xmm1, %xmm1
movsd 0x18(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x4b3c9
sqrtsd %xmm0, %xmm0
jmp 0x4b3da
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x28(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %r14, %rdi
callq 0x857d2
movapd %xmm0, %xmm1
xorpd %xmm2, %xmm2
movsd 0x18(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jb 0x4b3ed
sqrtsd %xmm0, %xmm0
jmp 0x4b402
movsd %xmm2, 0x18(%rsp)
callq 0x4350
movsd 0x18(%rsp), %xmm2
subsd %xmm0, %xmm2
movsd %xmm2, 0x20(%rbx)
leaq 0x70(%rsp), %r12
jmp 0x4b470
movsd %xmm1, 0x20(%rsp)
callq 0x4350
xorpd %xmm2, %xmm2
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
ucomisd %xmm2, %xmm1
jbe 0x4b4b4
movb $0x1, (%rbx)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x28(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %r14, %rdi
callq 0x857d2
movsd 0x18(%rsp), %xmm1
movapd %xmm0, %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x4b44d
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x4b462
movapd %xmm1, %xmm0
movsd %xmm2, 0x18(%rsp)
callq 0x4350
movsd 0x18(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x20(%rbx)
leaq 0x58(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movapd %xmm2, %xmm0
callq 0x85590
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%rsp)
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x857a0
movq 0x10(%r14), %rax
movq %rax, 0x18(%rbx)
movupd (%r14), %xmm0
movupd %xmm0, 0x8(%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
nop