// File: /workspace/github_cpp_makefile_star2_O2/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 0x63e4e
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x63e92
movsd %xmm0, 0x18(%rsp)
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x63e92
movsd %xmm0, 0x20(%rsp)
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movupd (%r12), %xmm0
movupd %xmm0, (%rsp)
movq %r12, %rdi
callq 0x63e92
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 0x38e86
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x28(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %r14, %rdi
movsd %xmm1, 0x18(%rsp)
callq 0x63e92
movsd 0x18(%rsp), %xmm1
movapd %xmm0, %xmm2
ucomisd 0xc75de(%rip), %xmm1    # 0x1002e0
jb 0x38d0a
sqrtsd %xmm1, %xmm0
jmp 0x38d1f
movapd %xmm1, %xmm0
movsd %xmm2, 0x20(%rsp)
callq 0x4390
movsd 0x20(%rsp), %xmm2
leaq 0xb0(%rsp), %r15
ucomisd %xmm0, %xmm2
jbe 0x38d67
movb $0x1, (%rbx)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x28(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %r14, %rdi
callq 0x63e92
movapd %xmm0, %xmm2
xorpd %xmm1, %xmm1
movsd 0x18(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x38d9e
sqrtsd %xmm0, %xmm0
jmp 0x38daf
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x28(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %r14, %rdi
callq 0x63e92
movapd %xmm0, %xmm1
xorpd %xmm2, %xmm2
movsd 0x18(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jb 0x38dc2
sqrtsd %xmm0, %xmm0
jmp 0x38dd7
movsd %xmm2, 0x18(%rsp)
callq 0x4390
movsd 0x18(%rsp), %xmm2
subsd %xmm0, %xmm2
movsd %xmm2, 0x20(%rbx)
leaq 0x70(%rsp), %r12
jmp 0x38e42
movsd %xmm1, 0x20(%rsp)
callq 0x4390
xorpd %xmm2, %xmm2
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
ucomisd %xmm2, %xmm1
jbe 0x38e86
movb $0x1, (%rbx)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x28(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %r14, %rdi
callq 0x63e92
movsd 0x18(%rsp), %xmm1
movapd %xmm0, %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x38e1f
sqrtsd %xmm1, %xmm0
jmp 0x38e34
movapd %xmm1, %xmm0
movsd %xmm2, 0x18(%rsp)
callq 0x4390
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 0x63d70
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 0x63e70
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