// File: Luthaf[P]zeopp/networkstorage.cc
// Function: determine_edge_length(std::vector<MOLECULE, std::allocator<MOLECULE>>*, ATOM_NETWORK*, std::vector<MOLECULE, std::allocator<MOLECULE>>*, std::vector<CONNECTION, std::allocator<CONNECTION>>*)
double determine_edge_length(vector<MOLECULE> *molecules, ATOM_NETWORK *cell, vector<MOLECULE> *assembled_molecules, vector<CONNECTION> *two_way_connections) {
  bool verbose = false;
  int num_placed = 0;
  bool placed_ok = true;
  double sum_edge_lengths = 0;
  vector<bool> placed;
  int num_mols = molecules->size();
  for(int i=0; i<num_mols; i++) placed.push_back(false);
  vector<bool> connected;
  int num_cons = two_way_connections->size();
  for(int i=0; i<num_cons; i++) connected.push_back(false);
  for(int i=0; i<num_mols; i++) assembled_molecules->push_back(molecules->at(i)); //this vector starts out the same as the molecules vector, but the molecules are then each shifted into their relative positions to form the assembled_molecules
  while(num_placed<2 && placed_ok) {
    if(verbose) printf("DEBUG: num_placed = %d; placing...\n", num_placed);
    int new_num_placed = place_molecule(molecules, two_way_connections, cell, &placed, &connected, assembled_molecules, num_placed, &sum_edge_lengths);
    if(verbose) printf("DEBUG: after calling place_molecule, %d molecules have been placed\n", new_num_placed);
    if(new_num_placed==num_placed+1) {
      num_placed = new_num_placed;
    } else placed_ok = false;
  }
  if(!placed_ok || num_placed!=2) {
//    printf("ERROR: could not find a molecule which has not been placed and which is adjacent to an already placed molecule in order to connected two building blocks together to determine edge length - is the net valid?\n");
//    exit(EXIT_FAILURE);
if(verbose) printf("NOTICE: could not place any molecules - returning invalid distance\n");
    return -1;
  }
  return sum_edge_lengths;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movb $0x0, -0x29(%rbp)
movl $0x0, -0x30(%rbp)
movb $0x1, -0x31(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xf670
movq -0x10(%rbp), %rdi
callq 0xaf3f0
movl %eax, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jge 0xa4361
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
callq 0xf700
jmp 0xa4343
jmp 0xa4345
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0xa432e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xa4572
leaq -0xa8(%rbp), %rdi
callq 0xf670
movq -0x28(%rbp), %rdi
callq 0xaf420
movl %eax, -0xac(%rbp)
movl $0x0, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
cmpl -0xac(%rbp), %eax
jge 0xa43d4
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
callq 0xf700
jmp 0xa43a4
jmp 0xa43a6
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0xa4386
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0xf9b0
jmp 0xa4572
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jge 0xa4435
movq -0x20(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x10(%rbp), %rdi
movslq -0xb4(%rbp), %rsi
callq 0xaf120
movq %rax, -0xc8(%rbp)
jmp 0xa440d
movq -0xc8(%rbp), %rsi
movq -0xd0(%rbp), %rdi
callq 0xaf710
jmp 0xa4422
jmp 0xa4424
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0xa43de
jmp 0xa4437
xorl %eax, %eax
cmpl $0x2, -0x30(%rbp)
movb %al, -0xd1(%rbp)
jge 0xa444e
movb -0x31(%rbp), %al
movb %al, -0xd1(%rbp)
movb -0xd1(%rbp), %al
testb $0x1, %al
jne 0xa445d
jmp 0xa44fe
testb $0x1, -0x29(%rbp)
je 0xa4478
movl -0x30(%rbp), %esi
leaq 0x154c07(%rip), %rdi    # 0x1f9074
xorl %eax, %eax
callq 0x5070
jmp 0xa4476
jmp 0xa4478
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %r9
movl -0x30(%rbp), %ecx
movq %rsp, %rax
leaq -0x40(%rbp), %r8
movq %r8, 0x8(%rax)
movl %ecx, (%rax)
leaq -0x68(%rbp), %rcx
leaq -0xa8(%rbp), %r8
callq 0x9fa00
movl %eax, -0xd8(%rbp)
jmp 0xa44b0
movl -0xd8(%rbp), %eax
movl %eax, -0xb8(%rbp)
testb $0x1, -0x29(%rbp)
je 0xa44da
movl -0xb8(%rbp), %esi
leaq 0x154bc9(%rip), %rdi    # 0x1f9098
xorl %eax, %eax
callq 0x5070
jmp 0xa44d8
jmp 0xa44da
movl -0xb8(%rbp), %eax
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0xa44f5
movl -0xb8(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0xa44f9
movb $0x0, -0x31(%rbp)
jmp 0xa4437
testb $0x1, -0x31(%rbp)
je 0xa450a
cmpl $0x2, -0x30(%rbp)
je 0xa453b
testb $0x1, -0x29(%rbp)
je 0xa4522
leaq 0x154bc5(%rip), %rdi    # 0x1f90dc
xorl %eax, %eax
callq 0x5070
jmp 0xa4520
jmp 0xa4522
movsd 0x14e24e(%rip), %xmm0   # 0x1f2778
movsd %xmm0, -0x8(%rbp)
movl $0x1, -0xbc(%rbp)
jmp 0xa454f
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
movl $0x1, -0xbc(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0xf9b0
leaq -0x68(%rbp), %rdi
callq 0xf9b0
movsd -0x8(%rbp), %xmm0
addq $0xf0, %rsp
popq %rbp
retq
leaq -0x68(%rbp), %rdi
callq 0xf9b0
movq -0x78(%rbp), %rdi
callq 0x58f0
nopw %cs:(%rax,%rax)