// File: /workspace/github_cpp_makefile_star2_O1/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
movl %r8d, 0x14(%rsp)
movq %rcx, 0x90(%rsp)
movq %rdi, 0x68(%rsp)
movq (%rsi), %rax
cmpq %rax, 0x8(%rsi)
je 0x7c85
movq %rdx, %rbx
movq %rsi, %r14
movsd (%rax), %xmm0
movsd %xmm0, 0x88(%rsp)
leaq 0x70(%rsp), %rdi
movq %rdx, %rsi
callq 0x94b0
movq 0x8(%r14), %rsi
subq (%r14), %rsi
sarq $0x3, %rsi
leaq 0x30(%rsp), %rdi
movl $0x3, %edx
callq 0x8326
movq %rbx, %rdi
xorl %esi, %esi
callq 0x901a
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
xorl %edi, %edi
xorl %edx, %edx
callq 0x915c
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x901a
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x1, %esi
movq %rbx, %rdi
callq 0x901a
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
movl $0x1, %edx
xorl %edi, %edi
callq 0x915c
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x901a
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x2, %esi
movq %rbx, %rdi
callq 0x901a
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
movl $0x2, %edx
xorl %edi, %edi
callq 0x915c
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x901a
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq (%r14), %rax
movq %r14, 0x60(%rsp)
movq 0x8(%r14), %rcx
subq %rax, %rcx
cmpq $0x8, %rcx
je 0x7bff
cmpl $0x0, 0x14(%rsp)
sete 0x7(%rsp)
movl $0x1, %ecx
movl $0x1, %ebp
leaq 0x1a0(%rsp), %r12
leaq 0x100(%rsp), %r14
movq %rcx, 0xc0(%rsp)
movsd (%rax,%rbp,8), %xmm0
addsd 0x88(%rsp), %xmm0
movapd %xmm0, 0x130(%rsp)
ucomisd 0x2193b(%rip), %xmm0    # 0x29020
jne 0x76ed
jnp 0x7c66
movb 0x7(%rsp), %al
movb %al, 0x6(%rsp)
cmpl $0x0, 0x14(%rsp)
je 0x7acd
movapd 0x130(%rsp), %xmm0
movapd %xmm0, %xmm1
xorpd 0x2192b(%rip), %xmm1    # 0x29040
movapd %xmm1, 0x190(%rsp)
mulsd %xmm0, %xmm0
movapd %xmm0, 0x180(%rsp)
movl 0x14(%rsp), %r15d
movb 0x7(%rsp), %al
movb %al, 0x6(%rsp)
xorpd %xmm0, %xmm0
movq 0x90(%rsp), %rdi
movsd 0x218cc(%rip), %xmm1    # 0x29018
callq 0x9c2a
movsd %xmm0, 0x18(%rsp)
movapd 0x190(%rsp), %xmm0
movapd 0x130(%rsp), %xmm1
movq 0x90(%rsp), %rdi
callq 0x72a4
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
mulsd %xmm0, %xmm0
movapd 0x180(%rsp), %xmm1
subsd %xmm0, %xmm1
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
movapd %xmm1, 0xb0(%rsp)
ucomisd 0x21875(%rip), %xmm1    # 0x29020
movsd %xmm0, 0x20(%rsp)
movsd %xmm0, 0x28(%rsp)
jae 0x77cd
movapd 0xb0(%rsp), %xmm0
callq 0x62b0
movsd %xmm0, 0x28(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x6610
movsd %xmm0, 0xc8(%rsp)
movapd 0xb0(%rsp), %xmm0
ucomisd 0x2182e(%rip), %xmm0    # 0x29020
jae 0x7808
movapd 0xb0(%rsp), %xmm0
callq 0x62b0
movsd %xmm0, 0x20(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x60a0
movsd %xmm0, 0x18(%rsp)
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
callq 0x901a
movsd 0x28(%rsp), %xmm0
mulsd 0xc8(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movl $0x1, %esi
leaq 0x70(%rsp), %rdi
callq 0x901a
movsd 0x20(%rsp), %xmm0
mulsd 0x18(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0x2, %esi
leaq 0x70(%rsp), %rdi
callq 0x901a
movq %r14, %rbx
movsd 0x8(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq $0x3, 0xa0(%rsp)
movl $0x18, %edi
callq 0x6300
movq %rax, 0xa8(%rsp)
leaq 0x98(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x901a
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x1, %esi
movq %r14, %rdi
callq 0x901a
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x2, %esi
movq %r14, %rdi
callq 0x901a
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movq (%rax), %rsi
leaq (%rsi,%rbp,8), %rdx
addq $0x8, %rdx
leaq 0xe8(%rsp), %rdi
movq %r12, %r14
movq %r12, %rcx
callq 0x85fe
movq 0x30669(%rip), %rcx     # 0x37f80
movq (%rcx), %rax
movq -0x18(%rax), %rax
movq $0xf, 0x8(%rcx,%rax)
leaq 0x30(%rsp), %rax
movq %rax, 0x1a0(%rsp)
movq %rbp, 0x1a8(%rsp)
leaq 0x118(%rsp), %rdi
movq %r14, %rsi
callq 0x859e
leaq 0xd0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x94b0
leaq 0x1c8(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x8640
movq $0x0, 0x148(%rsp)
movq $0x3, 0x150(%rsp)
movq %r12, %r13
movq %r12, %rdi
leaq 0x30(%rsp), %rsi
movq %r14, %rdx
leaq 0x148(%rsp), %rcx
callq 0xa074
leaq 0x158(%rsp), %rdi
movq %r13, %rsi
callq 0x876e
movq %rbx, %r14
movq 0x60(%rsp), %rax
movq (%rax), %rax
movsd (%rax,%rbp,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
leaq 0xe8(%rsp), %rsi
callq 0x87e0
leaq 0xd0(%rsp), %rdi
leaq 0x158(%rsp), %rsi
movsd 0x18(%rsp), %xmm0
movq %r14, %rdx
callq 0x7432
movl %eax, %r14d
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x7a1d
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x6310
movq 0x170(%rsp), %rsi
testq %rsi, %rsi
je 0x7a3b
movq 0x178(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0xd8(%rsp), %rsi
testq %rsi, %rsi
je 0x7a59
movq 0xe0(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0x120(%rsp), %rsi
testq %rsi, %rsi
je 0x7a77
movq 0x128(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x7a94
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x6310
movq 0xa0(%rsp), %rsi
testq %rsi, %rsi
je 0x7ab2
movq 0xa8(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
testb %r14b, %r14b
je 0x7aca
decl %r15d
sete 0x6(%rsp)
movq %rbx, %r14
jne 0x7738
jmp 0x7acd
movq %rbx, %r14
movb 0x6(%rsp), %bl
testb $0x1, %bl
je 0x7ae9
movq 0x68(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8326
jmp 0x7bce
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
callq 0x901a
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x1, %esi
leaq 0x70(%rsp), %rdi
callq 0x901a
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x2, %esi
leaq 0x70(%rsp), %rdi
callq 0x901a
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rax
movq (%rax), %rax
movsd (%rax,%rbp,8), %xmm0
movsd %xmm0, 0x88(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
movq %rbp, %rdi
xorl %edx, %edx
callq 0x915c
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x901a
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
movl $0x1, %edx
movq %rbp, %rdi
callq 0x915c
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x901a
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
movl $0x2, %edx
movq %rbp, %rdi
callq 0x915c
leaq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x901a
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
testb $0x1, %bl
movq 0xc0(%rsp), %rdx
jne 0x7c1c
incl %edx
movq 0x60(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movq %rdx, %rbp
cmpq %rdx, %rcx
movq %rdx, %rcx
jne 0x76be
movapd 0x30(%rsp), %xmm0
movq 0x68(%rsp), %rax
movupd %xmm0, (%rax)
leaq 0x10(%rax), %rdi
leaq 0x40(%rsp), %rsi
callq 0x94b0
movq 0x48(%rsp), %rsi
testq %rsi, %rsi
je 0x7c34
movq 0x50(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0x78(%rsp), %rsi
testq %rsi, %rsi
je 0x7c4f
movq 0x80(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0x68(%rsp), %rax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2148e(%rip), %rdi     # 0x290fb
leaq 0x2141d(%rip), %rsi     # 0x29091
leaq 0x21423(%rip), %rcx     # 0x2909e
movl $0x65, %edx
callq 0x6210
leaq 0x213f4(%rip), %rdi     # 0x29080
leaq 0x213fe(%rip), %rsi     # 0x29091
leaq 0x21404(%rip), %rcx     # 0x2909e
movl $0x5c, %edx
callq 0x6210
jmp 0x7d96
movq %rax, %rbx
jmp 0x7db1
jmp 0x7d96
jmp 0x7d96
jmp 0x7d96
jmp 0x7d0e
movq %rax, %rbx
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x7cee
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x6310
jmp 0x7cee
jmp 0x7d0e
movq %rax, %rbx
jmp 0x7d52
movq %rax, %rbx
movq 0x170(%rsp), %rsi
testq %rsi, %rsi
je 0x7d11
movq 0x178(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
jmp 0x7d11
movq %rax, %rbx
movq 0xd8(%rsp), %rsi
testq %rsi, %rsi
je 0x7d34
movq 0xe0(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
jmp 0x7d34
movq %rax, %rbx
movq 0x120(%rsp), %rsi
testq %rsi, %rsi
je 0x7d52
movq 0x128(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x7d76
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x6310
jmp 0x7d76
jmp 0x7d73
movq %rax, %rbx
movq 0xa0(%rsp), %rsi
testq %rsi, %rsi
je 0x7d99
movq 0xa8(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
jmp 0x7d99
movq %rax, %rbx
movq 0x48(%rsp), %rsi
testq %rsi, %rsi
je 0x7db1
movq 0x50(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq 0x78(%rsp), %rsi
testq %rsi, %rsi
je 0x7dcc
movq 0x80(%rsp), %rdi
shlq $0x3, %rsi
callq 0x6310
movq %rbx, %rdi
callq 0x65b0