// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1c8, %rsp            # imm = 0x1C8
movl %r8d, 0x2c(%rsp)
movq %rcx, 0x90(%rsp)
movq %rdi, 0x60(%rsp)
movq (%rsi), %rax
cmpq %rax, 0x8(%rsi)
je 0x7c1f
movq %rdx, %rbx
movq %rsi, %r15
movsd (%rax), %xmm0
movapd %xmm0, 0x80(%rsp)
leaq 0x68(%rsp), %rdi
movq %rdx, %rsi
callq 0x9478
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
leaq 0x30(%rsp), %rdi
movl $0x3, %edx
callq 0x82e6
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8fe2
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
xorl %edi, %edi
xorl %edx, %edx
callq 0x9124
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x8fe2
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x1, %esi
movq %rbx, %rdi
callq 0x8fe2
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
movl $0x1, %edx
xorl %edi, %edi
callq 0x9124
movq %r14, %rdi
movq %rax, %rsi
callq 0x8fe2
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x2, %esi
movq %rbx, %rdi
callq 0x8fe2
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
movl $0x2, %edx
xorl %edi, %edi
callq 0x9124
movq %r14, %rdi
movq %rax, %rsi
callq 0x8fe2
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq %rax, %rcx
cmpq $0x8, %rcx
jne 0x76c9
movapd 0x30(%rsp), %xmm0
movq 0x60(%rsp), %rax
movupd %xmm0, (%rax)
leaq 0x10(%rax), %rdi
movq %r14, %rsi
callq 0x9478
jmp 0x7bd8
movl $0x1, %ebp
movl $0x1, %ebx
xorpd %xmm1, %xmm1
leaq 0x98(%rsp), %r13
leaq 0x190(%rsp), %r12
movq %rbp, 0xc8(%rsp)
movapd 0x80(%rsp), %xmm0
addsd (%rax,%rbx,8), %xmm0
movapd %xmm0, 0x80(%rsp)
ucomisd %xmm1, %xmm0
jne 0x7712
jnp 0x7c3e
cmpl $0x0, 0x2c(%rsp)
je 0x7bca
movapd 0x80(%rsp), %xmm0
movapd %xmm0, %xmm1
xorpd 0x2190e(%rip), %xmm1    # 0x29040
movapd %xmm1, 0x180(%rsp)
mulsd %xmm0, %xmm0
movapd %xmm0, 0x170(%rsp)
movl 0x2c(%rsp), %r14d
xorpd %xmm0, %xmm0
movq 0x90(%rsp), %rdi
movsd 0x218b7(%rip), %xmm1    # 0x29018
callq 0x9bf2
movsd %xmm0, 0x10(%rsp)
movapd 0x180(%rsp), %xmm0
movapd 0x80(%rsp), %xmm1
movq 0x90(%rsp), %rdi
callq 0x72c4
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
mulsd %xmm0, %xmm0
movapd 0x170(%rsp), %xmm1
subsd %xmm0, %xmm1
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
movapd %xmm1, 0xb0(%rsp)
ucomisd 0x21860(%rip), %xmm1    # 0x29020
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, 0x20(%rsp)
jae 0x77e2
movapd 0xb0(%rsp), %xmm0
callq 0x62b0
movsd %xmm0, 0x20(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x6610
movsd %xmm0, 0xd0(%rsp)
movapd 0xb0(%rsp), %xmm0
ucomisd 0x21819(%rip), %xmm0    # 0x29020
jae 0x781d
movapd 0xb0(%rsp), %xmm0
callq 0x62b0
movsd %xmm0, 0x18(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x60a0
movsd %xmm0, 0x10(%rsp)
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x8fe2
movsd 0x20(%rsp), %xmm0
mulsd 0xd0(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0x1, %esi
leaq 0x68(%rsp), %rdi
callq 0x8fe2
movsd 0x18(%rsp), %xmm0
mulsd 0x10(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movl $0x2, %esi
leaq 0x68(%rsp), %rdi
callq 0x8fe2
movsd 0x8(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq $0x3, 0xa0(%rsp)
movl $0x18, %edi
callq 0x6300
movq %rax, 0xa8(%rsp)
movq %r13, %rdi
xorl %esi, %esi
callq 0x8fe2
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x1, %esi
movq %r13, %rdi
callq 0x8fe2
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x2, %esi
movq %r13, %rdi
callq 0x8fe2
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq (%r15), %rsi
leaq (%rsi,%rbx,8), %rdx
addq $0x8, %rdx
leaq 0xf0(%rsp), %rdi
movq %r12, %rcx
callq 0x85c0
movq 0x30667(%rip), %rcx     # 0x37f80
movq (%rcx), %rax
movq -0x18(%rax), %rax
movq $0xf, 0x8(%rcx,%rax)
leaq 0x30(%rsp), %rax
movq %rax, 0x190(%rsp)
movq %rbx, 0x198(%rsp)
leaq 0x120(%rsp), %rdi
movq %r12, %rsi
callq 0x8560
leaq 0xd8(%rsp), %rdi
movq %r13, %rsi
callq 0x9478
leaq 0x1b8(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x8602
movq $0x0, 0x138(%rsp)
movq $0x3, 0x140(%rsp)
movq %r12, %rdi
leaq 0x30(%rsp), %rsi
movq %rbp, %rdx
leaq 0x138(%rsp), %rcx
callq 0xa014
leaq 0x148(%rsp), %rdi
movq %r12, %rsi
callq 0x8730
leaq 0x108(%rsp), %rbp
movq (%r15), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %rbp, %rdi
leaq 0xf0(%rsp), %rsi
callq 0x87a2
leaq 0xd8(%rsp), %rdi
leaq 0x148(%rsp), %rsi
movsd 0x10(%rsp), %xmm0
movq %rbp, %rdx
callq 0x7452
movl %eax, %ebp
movq 0x108(%rsp), %rdi
testq %rdi, %rdi
je 0x7a16
movq 0x118(%rsp), %rsi
subq %rdi, %rsi
callq 0x6310
movq 0x160(%rsp), %rsi
testq %rsi, %rsi
je 0x7a34
movq 0x168(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0xe0(%rsp), %rsi
testq %rsi, %rsi
je 0x7a52
movq 0xe8(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0x128(%rsp), %rsi
testq %rsi, %rsi
je 0x7a70
movq 0x130(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x7a8d
movq 0x100(%rsp), %rsi
subq %rdi, %rsi
callq 0x6310
movq 0xa0(%rsp), %rsi
testq %rsi, %rsi
je 0x7aab
movq 0xa8(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
testb %bpl, %bpl
je 0x7abe
decl %r14d
jne 0x774d
jmp 0x7bca
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x8fe2
leaq 0x40(%rsp), %r14
movq 0xc8(%rsp), %rbp
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x1, %esi
leaq 0x68(%rsp), %rdi
callq 0x8fe2
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x2, %esi
leaq 0x68(%rsp), %rdi
callq 0x8fe2
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq (%r15), %rax
movsd (%rax,%rbx,8), %xmm0
movapd %xmm0, 0x80(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x9124
movq %r14, %rdi
movq %rax, %rsi
callq 0x8fe2
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
movl $0x1, %edx
movq %rbx, %rdi
callq 0x9124
movq %r14, %rdi
movq %rax, %rsi
callq 0x8fe2
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
movl $0x2, %edx
movq %rbx, %rdi
callq 0x9124
movq %r14, %rdi
movq %rax, %rsi
callq 0x8fe2
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
incl %ebp
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movq %rbp, %rbx
cmpq %rbp, %rcx
xorpd %xmm1, %xmm1
jne 0x76e7
jmp 0x76a9
movq 0x60(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x82e6
movq 0x48(%rsp), %rsi
testq %rsi, %rsi
je 0x7bf0
movq 0x50(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0x70(%rsp), %rsi
testq %rsi, %rsi
je 0x7c08
movq 0x78(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0x60(%rsp), %rax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2145a(%rip), %rdi     # 0x29080
leaq 0x21464(%rip), %rsi     # 0x29091
leaq 0x2146a(%rip), %rcx     # 0x2909e
movl $0x5c, %edx
callq 0x6210
leaq 0x214b6(%rip), %rdi     # 0x290fb
leaq 0x21445(%rip), %rsi     # 0x29091
leaq 0x2144b(%rip), %rcx     # 0x2909e
movl $0x65, %edx
callq 0x6210
jmp 0x7d5a
jmp 0x7d5a
movq %rax, %rbx
jmp 0x7d75
jmp 0x7d5a
jmp 0x7d5a
jmp 0x7d5a
jmp 0x7c87
jmp 0x7c87
jmp 0x7d37
movq %rax, %rbx
jmp 0x7cdc
movq %rax, %rbx
jmp 0x7d18
movq %rax, %rbx
jmp 0x7cbe
movq %rax, %rbx
jmp 0x7cfa
movq %rax, %rbx
movq 0x108(%rsp), %rdi
testq %rdi, %rdi
je 0x7cbe
movq 0x118(%rsp), %rsi
subq %rdi, %rsi
callq 0x6310
movq 0x160(%rsp), %rsi
testq %rsi, %rsi
je 0x7cdc
movq 0x168(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0xe0(%rsp), %rsi
testq %rsi, %rsi
je 0x7cfa
movq 0xe8(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0x128(%rsp), %rsi
testq %rsi, %rsi
je 0x7d18
movq 0x130(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x7d3a
movq 0x100(%rsp), %rsi
subq %rdi, %rsi
callq 0x6310
jmp 0x7d3a
movq %rax, %rbx
movq 0xa0(%rsp), %rsi
testq %rsi, %rsi
je 0x7d5d
movq 0xa8(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
jmp 0x7d5d
movq %rax, %rbx
movq 0x48(%rsp), %rsi
testq %rsi, %rsi
je 0x7d75
movq 0x50(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0x70(%rsp), %rsi
testq %rsi, %rsi
je 0x7d8d
movq 0x78(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq %rbx, %rdi
callq 0x65b0