// File: /workspace/github_cpp_makefile_star2_O2/Luthaf[P]zeopp/networkstorage.cc
// Function: parse_edge(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>*, int, ATOM_NETWORK*, int, bool)
void parse_edge(vector<string> *token, int first_index, ATOM_NETWORK *cell, int atom_index, bool uses_node) {
  bool verbose = false;
  if(!uses_node) {
    printf("NET ERROR: parsed unexpected \"edge\" field, when edges where expected to be provided with atoms\n");
    exit(EXIT_FAILURE);
  }
  XYZ start(convertToDouble(token->at(first_index+0)), convertToDouble(token->at(first_index+1)), convertToDouble(token->at(first_index+2)));
  XYZ start_xyz = cell->abc_to_xyz_returning_XYZ(start);
  XYZ end(convertToDouble(token->at(first_index+3)), convertToDouble(token->at(first_index+4)), convertToDouble(token->at(first_index+5)));
  XYZ end_xyz = cell->abc_to_xyz_returning_XYZ(end);
  //1) detect which vertex it starts at
  int v_index = -1;
  for(int i=0; i<atom_index && v_index==-1; i++) {
    if((start_xyz-cell->abc_to_xyz_returning_XYZ(cell->vertices.at(i).abc)).magnitude()<DISTANCE_TOLERANCE) { //match to parsed vertex
      v_index = i;
    }
  }
  if(v_index==-1) {
    printf("NET ERROR: could not match this edge start position to a previously parsed vertex\n");
    exit(EXIT_FAILURE);
  }
  //2) update the cell accordingly
  cell->vertices.at(v_index).edges.push_back(end);
  if(verbose) printf("parsed edge assigned to vertex %d at %.3f %.3f %.3f\n", v_index+1, end.x, end.y, end.z);
  //3) detect which vertex it ends at
  v_index = -1;
  for(int i=0; i<atom_index && v_index==-1; i++) {
    if((end_xyz-cell->abc_to_xyz_returning_XYZ(cell->vertices.at(i).abc)).magnitude()<DISTANCE_TOLERANCE) { //match to parsed vertex
      v_index = i;
    }
  }
  if(v_index==-1) {
/*
    printf("ERROR: could not match this edge end position to a previously parsed vertex\n");
    exit(EXIT_FAILURE);
*/
    if(verbose) printf("DEBUG: could not match this edge end position to a previously parsed vertex - creating an orphan edge to temporarily store this start/end until its true position by symmetry is known\n");
    //this is a specific scenario - the cgd file contains a definition of an edge, for which the end point is not a basic vertex - normally this is the default case, but for this cgd format the edges are not given exhaustively and so we need to know of each edge in both directions in order to construct the complete net including symmetry operations upon these edges
    //accordingly, we need to store this orphan edge, by saving its start and end in the cell as an orphan - this vertex is the symmetry image of some basic vertex, and we will assign this orphaned edge to that vertex when we know it
    cell->orphan_edge_starts.push_back(end);
    cell->orphan_edge_ends.push_back(start);
  } else {
    //4) update the cell accordingly
    cell->vertices.at(v_index).edges.push_back(start);
    if(verbose) printf("parsed edge assigned to vertex %d at %.3f %.3f %.3f\n", v_index+1, start.x, start.y, start.z);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
testl %r8d, %r8d
je 0x483c0
movl %ecx, %ebp
movq %rdx, %rbx
movq %rdi, %r14
movslq %esi, %r12
movq %r12, %rsi
callq 0x4bb9a
movq %rax, %rdi
callq 0x38473
movsd %xmm0, 0x8(%rsp)
leal 0x1(%r12), %eax
movslq %eax, %rsi
movq %r14, %rdi
callq 0x4bb9a
movq %rax, %rdi
callq 0x38473
movsd %xmm0, 0x10(%rsp)
leal 0x2(%r12), %eax
movslq %eax, %rsi
movq %r14, %rdi
callq 0x4bb9a
movq %rax, %rdi
callq 0x38473
movaps %xmm0, %xmm2
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0x6402e
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x3f1b2
leal 0x3(%r12), %eax
movslq %eax, %rsi
movq %r14, %rdi
callq 0x4bb9a
movq %rax, %rdi
callq 0x38473
movsd %xmm0, 0x8(%rsp)
leal 0x4(%r12), %eax
movslq %eax, %rsi
movq %r14, %rdi
callq 0x4bb9a
movq %rax, %rdi
callq 0x38473
movsd %xmm0, 0x10(%rsp)
addl $0x5, %r12d
movslq %r12d, %rsi
movq %r14, %rdi
callq 0x4bb9a
movq %rax, %rdi
callq 0x38473
movaps %xmm0, %xmm2
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0x6402e
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3f1b2
movq %rbx, 0x8(%rsp)
leaq 0x3c0(%rbx), %r14
movslq %ebp, %rbx
pushq $-0x1
popq %r15
xorl %r12d, %r12d
leaq 0x30(%rsp), %r13
leaq 0x48(%rsp), %rbp
movl %r15d, %eax
cmpl $-0x1, %eax
jne 0x482e0
cmpq %rbx, %r12
jge 0x482e0
movq %r14, %rdi
movq %r12, %rsi
callq 0x4b63c
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq %rax, %rdx
callq 0x3f1b2
movq %rbp, %rdi
leaq 0x78(%rsp), %rsi
movq %r13, %rdx
callq 0x642ae
movq %rbp, %rdi
callq 0x63758
movsd 0xb93f8(%rip), %xmm1    # 0x1016c8
ucomisd %xmm0, %xmm1
movl %r15d, %eax
cmoval %r12d, %eax
incq %r12
jmp 0x4828b
cmpl $-0x1, %eax
je 0x483c9
movslq %eax, %rsi
movq %r14, %rdi
callq 0x4b63c
leaq 0x20(%rax), %rdi
leaq 0x18(%rsp), %rsi
callq 0x4beaa
pushq $-0x1
popq %rax
xorl %r15d, %r15d
leaq 0x30(%rsp), %r12
leaq 0x48(%rsp), %r13
movl %eax, %ebp
cmpl $-0x1, %eax
jne 0x48368
cmpq %rbx, %r15
jge 0x48368
movq %r14, %rdi
movq %r15, %rsi
callq 0x4b63c
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movq %rax, %rdx
callq 0x3f1b2
movq %r13, %rdi
leaq 0x60(%rsp), %rsi
movq %r12, %rdx
callq 0x642ae
movq %r13, %rdi
callq 0x63758
movsd 0xb936f(%rip), %xmm1    # 0x1016c8
ucomisd %xmm0, %xmm1
movl %ebp, %eax
cmoval %r15d, %eax
incq %r15
jmp 0x48314
cmpl $-0x1, %eax
je 0x48381
movslq %eax, %rsi
movq %r14, %rdi
callq 0x4b63c
movq %rax, %rbx
addq $0x20, %rbx
jmp 0x4839e
movq 0x8(%rsp), %rbx
leaq 0x3d8(%rbx), %rdi
leaq 0x18(%rsp), %rsi
callq 0x4beaa
addq $0x3f0, %rbx            # imm = 0x3F0
leaq 0x90(%rsp), %rsi
movq %rbx, %rdi
callq 0x4beaa
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbad49(%rip), %rdi     # 0x103110
jmp 0x483d0
leaq 0xbad9f(%rip), %rdi     # 0x10316f
callq 0x4670
pushq $0x1
popq %rdi
callq 0x524c