// File: /workspace/github_cpp_makefile_star2_O2/Chrom3D[P]Chrom3D/src/Util.cpp
// Function: util::createSelfAvoidingWalk(std::vector<double, std::allocator<double>>, boost::numeric::ublas::vector<double, boost::numeric::ublas::unbounded_array<double, std::allocator<double>>>, boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>&, unsigned int)
CoordinateMatrix createSelfAvoidingWalk(vector<double> radii, CoordinateVector origin, ENG& eng, uint maxIter) {
  assert(radii.size() > 0);
  CoordinateVector tmpRadii;
  double prevRadius=radii[0];
  CoordinateVector prevCoords = origin;
  CoordinateMatrix newCoords(radii.size(), 3); newCoords(0,0) = origin(0);   newCoords(0,1) = origin(1); newCoords(0,2) = origin(2);
  uint j;
  
  for(uint i=1; i!=radii.size(); i++) {
    double R = radii[i] + prevRadius;
    assert(R != 0);
    double x;
    double y;
    double z;
    for(j=0; j != maxIter; j++) {
      double theta = unif_real(0, 2*PI, eng);
      z = unif_real(-R,R, eng);
      x = sqrt(pow(R,2) - pow(z,2)) * cos(theta); // OBS: Speed up by doing sqrt only once??
      y = sqrt(pow(R,2) - pow(z,2)) * sin(theta); // Archimedes method (see http://tinyurl.com/nquq2d9)

      x += prevCoords(0);
      y += prevCoords(1);
      z += prevCoords(2);

      CoordinateVector proposedCoords(3); proposedCoords(0) = x; proposedCoords(1) = y; proposedCoords(2) = z;
      vector<double> tmpRadii(radii.begin(), radii.begin()+i+1);
     
      cout.precision(15);
      CoordinateVector newc = ublas::row(newCoords,i);

      if(not coordClash(proposedCoords, project(newCoords, boost::numeric::ublas::range(0, i), boost::numeric::ublas::range(0,3)), radii[i], tmpRadii)) {
	       break;
      }
    }
    
    if (j==maxIter) {
      // reached the end of maxIter without finding a structure that does not clash
      CoordinateMatrix newCoordsEmpty(0,0);
      return newCoordsEmpty;
    }
    
    prevCoords(0) = x; prevCoords(1) = y;  prevCoords(2) = z;
    prevRadius = radii[i];
    newCoords(i,0) = x;
    newCoords(i,1) = y;
    newCoords(i,2) = z;    
  }
  return newCoords;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rcx, 0x40(%rsp)
movq %rdi, 0x38(%rsp)
movq (%rsi), %rax
movq %rsi, 0x28(%rsp)
cmpq %rax, 0x8(%rsi)
je 0x765f
movl %r8d, %ebp
movq %rdx, %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x120(%rsp)
movsd (%rax), %xmm0
movapd %xmm0, 0x60(%rsp)
leaq 0x48(%rsp), %rdi
movq %rdx, %rsi
callq 0x8932
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq $0x3, %rsi
leaq 0x78(%rsp), %rdi
pushq $0x3
popq %rdx
callq 0x7af6
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8638
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x78(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8614
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x8638
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x78(%rsp), %rdi
pushq $0x1
popq %rdx
xorl %esi, %esi
callq 0x8614
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x8638
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x78(%rsp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x8614
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
incl %ebp
pushq $0x1
popq %rbx
leaq 0x188(%rsp), %r12
leaq 0x1a0(%rsp), %r13
movl %ebp, 0x34(%rsp)
movl %ebx, %ebx
movq 0x28(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbx, %rcx
je 0x7632
movapd 0x60(%rsp), %xmm0
addsd (%rax,%rbx,8), %xmm0
ucomisd 0x18df2(%rip), %xmm0    # 0x20020
jne 0x7236
jnp 0x7646
movapd %xmm0, %xmm1
xorpd 0x18dfe(%rip), %xmm1    # 0x20040
movapd %xmm1, 0xf0(%rsp)
movapd %xmm0, 0x60(%rsp)
mulsd %xmm0, %xmm0
movapd %xmm0, 0xe0(%rsp)
decl %ebp
je 0x75e8
xorpd %xmm0, %xmm0
movsd 0x18da6(%rip), %xmm1    # 0x20018
movq 0x40(%rsp), %rdi
callq 0x6e93
movsd %xmm0, 0x10(%rsp)
leaq 0x170(%rsp), %r14
movapd 0xf0(%rsp), %xmm0
movapd 0x60(%rsp), %xmm1
movq 0x40(%rsp), %rdi
callq 0x6e93
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
mulsd %xmm0, %xmm0
movapd 0xe0(%rsp), %xmm1
subsd %xmm0, %xmm1
sqrtsd %xmm1, %xmm0
movapd %xmm1, 0xa0(%rsp)
ucomisd 0x18d4b(%rip), %xmm1    # 0x20020
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, 0x20(%rsp)
jae 0x72f7
movapd 0xa0(%rsp), %xmm0
callq 0x62f0
movsd %xmm0, 0x20(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x6630
movsd %xmm0, 0xb0(%rsp)
movapd 0xa0(%rsp), %xmm0
ucomisd 0x18d04(%rip), %xmm0    # 0x20020
jae 0x7332
movapd 0xa0(%rsp), %xmm0
callq 0x62f0
movsd %xmm0, 0x18(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x60a0
movsd %xmm0, 0x10(%rsp)
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x8638
movsd 0x20(%rsp), %xmm0
mulsd 0xb0(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x48(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x8638
movsd 0x18(%rsp), %xmm0
mulsd 0x10(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0x48(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x8638
movsd 0x8(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r12, %rdi
pushq $0x3
popq %rsi
callq 0x7d3c
movq %r12, %rdi
xorl %esi, %esi
callq 0x8638
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x8638
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r12, %rdi
pushq $0x2
popq %rsi
callq 0x8638
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x28(%rsp), %rax
movq (%rax), %rsi
leaq (%rsi,%rbx,8), %rdx
addq $0x8, %rdx
leaq 0xc8(%rsp), %rdi
movq %r13, %rcx
callq 0x7d4a
movq 0x2ab68(%rip), %rcx     # 0x31f80
movq (%rcx), %rax
movq -0x18(%rax), %rax
movq $0xf, 0x8(%rcx,%rax)
leaq 0x78(%rsp), %rax
movq %rax, 0x1a0(%rsp)
movq %rbx, 0x1a8(%rsp)
leaq 0x100(%rsp), %rdi
movq %r13, %rsi
callq 0x7cfa
movq %r14, %rdi
movq %r12, %rsi
callq 0x8932
leaq 0x1c8(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x7d7c
andq $0x0, 0xb8(%rsp)
movq $0x3, 0xc0(%rsp)
movq %r13, %rdi
leaq 0x78(%rsp), %rsi
movq %r15, %rdx
leaq 0xb8(%rsp), %rcx
callq 0x9360
leaq 0x130(%rsp), %rdi
movq %r13, %rsi
callq 0x7e3c
leaq 0x158(%rsp), %r15
movq 0x28(%rsp), %rax
movq (%rax), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r15, %rdi
leaq 0xc8(%rsp), %rsi
callq 0x7e9c
movq %r14, %rdi
leaq 0x130(%rsp), %rsi
movsd 0x10(%rsp), %xmm0
movq %r15, %rdx
callq 0x7020
movq %r15, %rdi
movl %eax, %r15d
callq 0x92fe
leaq 0x140(%rsp), %rdi
callq 0x8012
movq %r14, %rdi
callq 0x8012
leaq 0x100(%rsp), %rdi
callq 0x8012
leaq 0xc8(%rsp), %rdi
callq 0x92fe
movq %r12, %rdi
callq 0x8012
testb %r15b, %r15b
jne 0x725e
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x8638
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x48(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x8638
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x48(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x8638
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x28(%rsp), %rax
movq (%rax), %rax
movsd (%rax,%rbx,8), %xmm0
movapd %xmm0, 0x60(%rsp)
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x8614
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
pushq $0x1
popq %rdx
callq 0x8614
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
pushq $0x2
popq %rdx
callq 0x8614
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
incl %ebx
movl 0x34(%rsp), %ebp
jmp 0x71fd
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x7af6
leaq 0x88(%rsp), %rdi
callq 0x8012
leaq 0x48(%rsp), %rdi
callq 0x8012
leaq 0x118(%rsp), %rdi
callq 0x8012
movq %rbx, %rax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x78(%rsp), %rsi
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x7eea
jmp 0x75f9
leaq 0x18a7e(%rip), %rdi     # 0x200cb
leaq 0x18a0d(%rip), %rsi     # 0x20061
leaq 0x18a13(%rip), %rcx     # 0x2006e
pushq $0x65
jmp 0x7676
leaq 0x189ea(%rip), %rdi     # 0x20050
leaq 0x189f4(%rip), %rsi     # 0x20061
leaq 0x189fa(%rip), %rcx     # 0x2006e
pushq $0x5c
popq %rdx
callq 0x6250
jmp 0x770e
jmp 0x770e
movq %rax, %rbx
jmp 0x771e
movq %rax, %rbx
jmp 0x7728
jmp 0x770e
jmp 0x770e
jmp 0x76d0
jmp 0x770e
movq %rax, %rbx
jmp 0x76e0
jmp 0x76fc
movq %rax, %rbx
jmp 0x76c1
jmp 0x76d0
movq %rax, %rbx
jmp 0x76ed
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x92fe
leaq 0x140(%rsp), %rdi
callq 0x8012
jmp 0x76d3
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
callq 0x8012
leaq 0x100(%rsp), %rdi
callq 0x8012
leaq 0xc8(%rsp), %rdi
callq 0x92fe
jmp 0x76ff
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
callq 0x8012
jmp 0x7711
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x8012
leaq 0x48(%rsp), %rdi
callq 0x8012
leaq 0x118(%rsp), %rdi
callq 0x8012
movq %rbx, %rdi
callq 0x65d0