// File: Luthaf[P]zeopp/ray.cc
// Function: duplicateSpheresOnFace(ATOM_NETWORK*, std::vector<Sphere, std::allocator<Sphere>>&, std::vector<Plane, std::allocator<Plane>>&)
void duplicateSpheresOnFace(ATOM_NETWORK *cell,vector<Sphere>& spheres,vector<Plane>& faces)
{
  Sphere temp_sphere;
  bool touch_face[6];
  
  vector<double> permutation_a;
  vector<double> permutation_b;
  vector<double> permutation_c;

  unsigned int size = spheres.size(); //because I will be adding spheres to list (there is a smarter way to do this...)
  for (unsigned int i=0; i<size; i++)
    {
      //Assemble touch_face[] for which faces it touches
      for (unsigned int j=0; j<faces.size(); j++) 
	{
	  if (faces[j].distToPlane(spheres[i].center) < (spheres[i].radius))
	    {
	      touch_face[j] = true;
	    }
	  else
	    {
	      touch_face[j] = false;
	    }
	}    
      
      //Add allowable permutations on the unitcell
      permutation_a.clear(); permutation_a.push_back(0.0);
      permutation_b.clear(); permutation_b.push_back(0.0);
      permutation_c.clear(); permutation_c.push_back(0.0);
      
      if (touch_face[0] == true)
	{
	  permutation_c.push_back(1.0);
	}
      if (touch_face[1] == true)
	{
	  permutation_b.push_back(1.0);
	}
      if (touch_face[2] == true)
	{
	  permutation_a.push_back(1.0);
	}
      if (touch_face[3] == true)
	{
	  permutation_c.push_back(-1.0);
	}
      if (touch_face[4] == true)
	{
	  permutation_b.push_back(-1.0);
	}
      if (touch_face[5] == true)
	{
	  permutation_a.push_back(-1.0);
	}
      
      //Iterate through all the permutations on the sphere
      temp_sphere = spheres[i];
      for (unsigned int a=0; a<permutation_a.size(); a++)
	{
	  for (unsigned int b=0; b<permutation_b.size(); b++)
	    {
	      for (unsigned int c=0; c<permutation_c.size(); c++)
		{
		  if (permutation_a[a] != 0 || permutation_b[b] != 0 || permutation_c[c] != 0)
		    {
		      temp_sphere.center = spheres[i].center;
		      temp_sphere.center = temp_sphere.center + cell->abc_to_xyz(Point(permutation_a[a],permutation_b[b],permutation_c[c]));
		      spheres.push_back(temp_sphere);
		    }
		}
	    }
	}
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x91d30
leaq -0x60(%rbp), %rdi
callq 0xf650
leaq -0x78(%rbp), %rdi
callq 0xf650
leaq -0x90(%rbp), %rdi
callq 0xf650
movq -0x10(%rbp), %rdi
callq 0x91bf0
movl %eax, -0x94(%rbp)
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x94(%rbp), %eax
jae 0x8d377
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movq %rax, -0x178(%rbp)
movq -0x18(%rbp), %rdi
callq 0x91c30
movq %rax, %rcx
movq -0x178(%rbp), %rax
cmpq %rcx, %rax
jae 0x8ceff
movq -0x18(%rbp), %rdi
movl -0x9c(%rbp), %eax
movl %eax, %esi
callq 0x91c60
movq %rax, -0x188(%rbp)
movq -0x10(%rbp), %rdi
movl -0x98(%rbp), %eax
movl %eax, %esi
callq 0x91c10
movq -0x188(%rbp), %rdi
movq 0x10(%rax), %rcx
movq %rcx, -0xb0(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0xc0(%rbp)
movq -0xb0(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x10(%rax)
movaps -0xc0(%rbp), %xmm0
movups %xmm0, (%rax)
callq 0x8c560
movsd %xmm0, -0x180(%rbp)
jmp 0x8ce78
movq -0x10(%rbp), %rdi
movl -0x98(%rbp), %eax
movl %eax, %esi
callq 0x91c10
movsd -0x180(%rbp), %xmm1
movsd 0x18(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x8cede
movl -0x9c(%rbp), %eax
movb $0x1, -0x46(%rbp,%rax)
jmp 0x8cee9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xf9d0
leaq -0x78(%rbp), %rdi
callq 0xf9d0
leaq -0x60(%rbp), %rdi
callq 0xf9d0
jmp 0x8d39e
movl -0x9c(%rbp), %eax
movb $0x0, -0x46(%rbp,%rax)
jmp 0x8ceeb
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x8cde3
leaq -0x60(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x851f0
movq -0x190(%rbp), %rdi
movq $0x0, -0xd8(%rbp)
leaq -0xd8(%rbp), %rsi
callq 0xf6d0
jmp 0x8cf2f
leaq -0x78(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x851f0
movq -0x198(%rbp), %rdi
movq $0x0, -0xe0(%rbp)
leaq -0xe0(%rbp), %rsi
callq 0xf6d0
jmp 0x8cf5f
leaq -0x90(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x851f0
movq -0x1a0(%rbp), %rdi
movq $0x0, -0xe8(%rbp)
leaq -0xe8(%rbp), %rsi
callq 0xf6d0
jmp 0x8cf92
movb -0x46(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x8cfc7
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0xf0(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0xf6d0
jmp 0x8cfc5
jmp 0x8cfc7
movb -0x45(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x8cff9
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0xf8(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0xf6d0
jmp 0x8cff7
jmp 0x8cff9
movb -0x44(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x8d02b
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0x100(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xf6d0
jmp 0x8d029
jmp 0x8d02b
movb -0x43(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x8d060
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, -0x108(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0xf6d0
jmp 0x8d05e
jmp 0x8d060
movb -0x42(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x8d092
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, -0x110(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0xf6d0
jmp 0x8d090
jmp 0x8d092
movb -0x41(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x8d0c4
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, -0x118(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0xf6d0
jmp 0x8d0c2
jmp 0x8d0c4
movq -0x10(%rbp), %rdi
movl -0x98(%rbp), %eax
movl %eax, %esi
callq 0x91c10
movq (%rax), %rcx
movq %rcx, -0x40(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x38(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x30(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x11c(%rbp)
movl -0x11c(%rbp), %eax
movq %rax, -0x1a8(%rbp)
leaq -0x60(%rbp), %rdi
callq 0xf990
movq %rax, %rcx
movq -0x1a8(%rbp), %rax
cmpq %rcx, %rax
jae 0x8d361
movl $0x0, -0x120(%rbp)
movl -0x120(%rbp), %eax
movq %rax, -0x1b0(%rbp)
leaq -0x78(%rbp), %rdi
callq 0xf990
movq %rax, %rcx
movq -0x1b0(%rbp), %rax
cmpq %rcx, %rax
jae 0x8d34b
movl $0x0, -0x124(%rbp)
movl -0x124(%rbp), %eax
movq %rax, -0x1b8(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xf990
movq %rax, %rcx
movq -0x1b8(%rbp), %rax
cmpq %rcx, %rax
jae 0x8d335
movl -0x11c(%rbp), %eax
movl %eax, %esi
leaq -0x60(%rbp), %rdi
callq 0xedb0
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8d1f8
jp 0x8d1f8
movl -0x120(%rbp), %eax
movl %eax, %esi
leaq -0x78(%rbp), %rdi
callq 0xedb0
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8d1f8
jp 0x8d1f8
movl -0x124(%rbp), %eax
movl %eax, %esi
leaq -0x90(%rbp), %rdi
callq 0xedb0
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8d1f8
jp 0x8d1f8
jmp 0x8d31f
movq -0x10(%rbp), %rdi
movl -0x98(%rbp), %eax
movl %eax, %esi
callq 0x91c10
movq 0x10(%rax), %rcx
movq %rcx, -0x30(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0x40(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movl -0x11c(%rbp), %eax
movl %eax, %esi
leaq -0x60(%rbp), %rdi
callq 0xedb0
movsd (%rax), %xmm0
movsd %xmm0, -0x1c8(%rbp)
movl -0x120(%rbp), %eax
movl %eax, %esi
leaq -0x78(%rbp), %rdi
callq 0xedb0
movsd (%rax), %xmm0
movsd %xmm0, -0x1c0(%rbp)
movl -0x124(%rbp), %eax
movl %eax, %esi
leaq -0x90(%rbp), %rdi
callq 0xedb0
movsd -0x1c8(%rbp), %xmm0
movsd -0x1c0(%rbp), %xmm1
movsd (%rax), %xmm2
leaq -0x170(%rbp), %rdi
callq 0xe9c70
jmp 0x8d29e
movq -0x1d0(%rbp), %rsi
movq -0x160(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x10(%rax)
movups -0x170(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x158(%rbp), %rdi
callq 0x961d0
jmp 0x8d2cb
movq -0x1d8(%rbp), %rsi
movq -0x148(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x10(%rax)
movups -0x158(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x140(%rbp), %rdi
callq 0xea420
jmp 0x8d2f8
movq -0x130(%rbp), %rax
movq %rax, -0x30(%rbp)
movups -0x140(%rbp), %xmm0
movaps %xmm0, -0x40(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x91d60
jmp 0x8d31d
jmp 0x8d31f
jmp 0x8d321
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x8d164
jmp 0x8d337
movl -0x120(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x120(%rbp)
jmp 0x8d131
jmp 0x8d34d
movl -0x11c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x11c(%rbp)
jmp 0x8d0fe
jmp 0x8d363
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x8cdc7
leaq -0x90(%rbp), %rdi
callq 0xf9d0
leaq -0x78(%rbp), %rdi
callq 0xf9d0
leaq -0x60(%rbp), %rdi
callq 0xf9d0
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
movq -0xc8(%rbp), %rdi
callq 0x58f0
nopw (%rax,%rax)