// File: /workspace/github_cpp_makefile_star2_O2/Luthaf[P]zeopp/ray.cc
// Function: rayTraceToSphere(ATOM_NETWORK*, std::vector<Sphere, std::allocator<Sphere>>&, ray, std::vector<Plane, std::allocator<Plane>>&, hitdata&)
void rayTraceToSphere(ATOM_NETWORK *cell,vector<Sphere>& spheres,ray r, vector<Plane>& faces,hitdata& hitobject)
{
  
#if DEBUG
  cout << "Current Ray Length: " << hitobject.dist << endl;
#endif
  
  if (hitobject.dist > MAXRAYDIST){
    return;
  }
  
  

  //Find the closest atom that it will hit
  hitdata hitsphere = findClosestSphere(spheres,r);
  
  //If I hit an Sphere great!
  if (hitsphere.hit == true)
    {
      if (hitsphere.id == hitobject.id)
	{
	  //cerr << "Just hit same atom this is not allowable" << endl;
	}
      hitobject.hit = true;
      hitobject.hitpoint = hitsphere.hitpoint;
      hitobject.dist += hitsphere.dist;
      hitobject.id = hitsphere.id;
      hitobject.object = hitsphere.object;
      return;
    }
  //If I didn't hit a Sphere then I must hit a unitcell face
  hitsphere = findClosestPlane(faces,r);
  
  assert(hitsphere.hit==true); //Thus it hit a plane

  hitobject.hit = true;
  hitobject.hitpoint = hitsphere.hitpoint;
  hitobject.dist += hitsphere.dist;
  hitobject.id = -1;
  hitobject.object = NULL;
  
  //I am moving the point just slightly outside of the unit cell to be shifted back in
  r.base = cell->shiftXYZInUC(hitobject.hitpoint+r.vector.scale(threshold));
  
  //this is a recursive call to get the total length of the ray
  rayTraceToSphere(cell,spheres,r,faces,hitobject);
  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movsd 0x20(%rcx), %xmm0
ucomisd 0xc1eee(%rip), %xmm0    # 0xfc040
ja 0x3a2dc
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0x110(%rsp), %r13
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rdx
callq 0x3909f
cmpb $0x1, (%rbp)
jne 0x3a1bc
movb $0x1, (%rbx)
movups 0x38(%rsp), %xmm0
movups %xmm0, 0x8(%rbx)
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rbx)
movsd 0x50(%rsp), %xmm0
addsd 0x20(%rbx), %xmm0
movsd %xmm0, 0x20(%rbx)
movl 0x58(%rsp), %eax
movl %eax, 0x28(%rbx)
movq 0x60(%rsp), %rax
movq %rax, 0x30(%rbx)
jmp 0x3a2dc
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x391b4
movq 0x30(%rbp), %rax
movq %rax, 0x60(%rsp)
movups (%rbp), %xmm0
movups 0x10(%rbp), %xmm1
movups 0x20(%rbp), %xmm2
movaps %xmm2, 0x50(%rsp)
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
cmpb $0x0, 0x30(%rsp)
je 0x3a2ee
movb $0x1, (%rbx)
leaq 0x8(%rbx), %rax
movq %rax, 0x68(%rsp)
movups 0x38(%rsp), %xmm0
movups %xmm0, 0x8(%rbx)
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rbx)
movsd 0x50(%rsp), %xmm0
addsd 0x20(%rbx), %xmm0
movsd %xmm0, 0x20(%rbx)
orl $-0x1, 0x28(%rbx)
andq $0x0, 0x30(%rbx)
leaq 0x18(%r13), %rsi
leaq 0x70(%rsp), %rbp
movsd 0xc6f9f(%rip), %xmm0    # 0x1011e8
movq %rbp, %rdi
callq 0x63d70
movq 0x10(%rbp), %rax
movq %rax, 0x10(%rsp)
movups (%rbp), %xmm0
movups %xmm0, (%rsp)
leaq 0x88(%rsp), %rbp
movq %rbp, %rdi
movq 0x68(%rsp), %rsi
callq 0x63e70
movq 0x10(%rbp), %rax
movq %rax, 0x10(%rsp)
movups (%rbp), %xmm0
movups %xmm0, (%rsp)
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3f9c2
movq 0x10(%rbp), %rax
movq %rax, 0x10(%r13)
movups (%rbp), %xmm0
movups %xmm0, (%r13)
movupd (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x3a134
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc70aa(%rip), %rdi     # 0x10139f
leaq 0xc6f92(%rip), %rsi     # 0x10128e
leaq 0xc70b0(%rip), %rcx     # 0x1013b3
movl $0x195, %edx            # imm = 0x195
callq 0x42c0