// File: Luthaf[P]zeopp/networkstorage.cc
// Function: add_2c_vertices_and_normal_edges(ATOM_NETWORK*)
void add_2c_vertices_and_normal_edges(ATOM_NETWORK *cell) { //just add the 2-c vertices and normal edges - dummy edges are handled later, once we know all the vertices by symmetry, and can make a better guess of the orthogonal direction of the 2-c vertices
  bool verbose = false;
  if(verbose) printf("augmenting underlying net with 2-c vertices\n");
  int num_v = cell->vertices.size();
  vector<XYZ> candidates;
  vector<XYZ> from_abc_vector, to_abc_vector; //, orthogonal_abc_vector;
  for(int i=0; i<num_v; i++) {
    XYZ v_abc = cell->vertices.at(i).abc;
    XYZ v_xyz = cell->abc_to_xyz_returning_XYZ(v_abc);
    int num_e = cell->vertices.at(i).edges.size();
    for(int j=0; j<num_e; j++) {
      XYZ e_abc = cell->vertices.at(i).edges.at(j);
      XYZ e_xyz = cell->abc_to_xyz_returning_XYZ(e_abc);
      XYZ this_edge_vector = get_vector_from_to(v_xyz, e_xyz);
      double this_edge_length = this_edge_vector.magnitude();
      XYZ this_candidate_abc = v_abc+(e_abc-v_abc).scale(0.5);
      XYZ this_candidate_xyz = cell->abc_to_xyz_returning_XYZ(this_candidate_abc);
      candidates.push_back(this_candidate_abc);
      from_abc_vector.push_back(v_abc);
      to_abc_vector.push_back(e_abc);
      //forget dummy edges for now - they are handled later
    }
  }
  int num_candidates = candidates.size();
  //find and remove any duplicates
  vector<bool> keep_vector;
  int num_new_vertices = 0;
  for(int i=0; i<num_candidates; i++) {
    //check against all previous candidates to see if we should reject this one
    bool keep = true;
    for(int j=0; j<i && keep; j++) {
      if(overlaps_abc(candidates.at(i),candidates.at(j),cell)) keep = false;
    }
    keep_vector.push_back(keep);
    if(keep) num_new_vertices++;
  }
  if(num_new_vertices>0) {
    //halve the edge lengths of all previous vertices to match these new short edges!
    for(int i=0; i<num_v; i++) {
      XYZ v_abc = cell->vertices.at(i).abc;
      int num_e = cell->vertices.at(i).edges.size();
      for(int j=0; j<num_e; j++) {
        XYZ e_abc = cell->vertices.at(i).edges.at(j);
        XYZ new_e_abc = v_abc+(e_abc-v_abc).scale(0.5);
        cell->vertices.at(i).edges.at(j) = new_e_abc;
      }
    }
    //set up final vertices
    for(int i=0; i<num_candidates; i++) {
      if(keep_vector.at(i)) {
        if(verbose) printf("accepted candidate ID %d: %.3f %.3f %.3f\n", i, candidates.at(i).x, candidates.at(i).y, candidates.at(i).z);
        VERTEX v(candidates.at(i).x, candidates.at(i).y, candidates.at(i).z);
        v.expected_edges = 2; //it is a 2-c vertex
        v.edges.push_back(from_abc_vector.at(i));
        v.edges.push_back(to_abc_vector.at(i));
        cell->vertices.push_back(v);
      }
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x460, %rsp            # imm = 0x460
movq %rdi, -0x8(%rbp)
movb $0x0, -0x9(%rbp)
testb $0x1, -0x9(%rbp)
je 0xa9ec7
leaq 0x150546(%rip), %rdi    # 0x1fa406
movb $0x0, %al
callq 0x5070
movq -0x8(%rbp), %rdi
addq $0x3c0, %rdi            # imm = 0x3C0
callq 0xae470
movl %eax, -0x10(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xada60
leaq -0x40(%rbp), %rdi
callq 0xada60
leaq -0x58(%rbp), %rdi
callq 0xada60
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0xaa184
movq -0x8(%rbp), %rdi
addq $0x3c0, %rdi            # imm = 0x3C0
movslq -0x5c(%rbp), %rsi
callq 0xae4a0
movq %rax, -0x338(%rbp)
jmp 0xa9f25
movq -0x338(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x70(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0x80(%rbp)
movq -0x8(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
leaq -0x80(%rbp), %rdx
callq 0x97310
jmp 0xa9f51
movq -0x8(%rbp), %rdi
addq $0x3c0, %rdi            # imm = 0x3C0
movslq -0x5c(%rbp), %rsi
callq 0xae4a0
movq %rax, -0x340(%rbp)
jmp 0xa9f6e
movq -0x340(%rbp), %rdi
addq $0x20, %rdi
callq 0xae590
movl %eax, -0xac(%rbp)
movl $0x0, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
cmpl -0xac(%rbp), %eax
jge 0xaa174
movq -0x8(%rbp), %rdi
addq $0x3c0, %rdi            # imm = 0x3C0
movslq -0x5c(%rbp), %rsi
callq 0xae4a0
movq %rax, -0x348(%rbp)
jmp 0xa9fbd
movq -0x348(%rbp), %rdi
addq $0x20, %rdi
movslq -0xb0(%rbp), %rsi
callq 0xae5c0
movq %rax, -0x350(%rbp)
jmp 0xa9fdd
movq -0x350(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0xc0(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0xd0(%rbp)
movq -0x8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
callq 0x97310
jmp 0xaa012
movq -0x98(%rbp), %rax
movq %rax, -0x110(%rbp)
movups -0xa8(%rbp), %xmm0
movaps %xmm0, -0x120(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x130(%rbp)
movups -0xe8(%rbp), %xmm0
movaps %xmm0, -0x140(%rbp)
movq -0x130(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x28(%rax)
movaps -0x140(%rbp), %xmm0
movups %xmm0, 0x18(%rax)
movq -0x110(%rbp), %rcx
movq %rcx, 0x10(%rax)
movaps -0x120(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x100(%rbp), %rdi
callq 0xeaf90
jmp 0xaa086
leaq -0x100(%rbp), %rdi
callq 0xe9240
movsd %xmm0, -0x358(%rbp)
jmp 0xaa09c
movsd -0x358(%rbp), %xmm0
movsd %xmm0, -0x148(%rbp)
leaq -0x190(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0xeadf0
jmp 0xaa0c5
leaq -0x178(%rbp), %rdi
leaq -0x190(%rbp), %rsi
movsd 0x147f9d(%rip), %xmm0   # 0x1f2078
callq 0xea9f0
jmp 0xaa0e2
leaq -0x160(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0x178(%rbp), %rdx
callq 0xeae50
jmp 0xaa0fb
movq -0x8(%rbp), %rsi
leaq -0x1a8(%rbp), %rdi
leaq -0x160(%rbp), %rdx
callq 0x97310
jmp 0xaa114
leaq -0x28(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0xafc20
jmp 0xaa126
leaq -0x40(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xafc20
jmp 0xaa135
leaq -0x58(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0xafc20
jmp 0xaa147
jmp 0xaa149
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0xa9f8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0xaa80f
jmp 0xaa176
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xa9efc
leaq -0x28(%rbp), %rdi
callq 0xae590
movl %eax, -0x1ac(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0xf670
movl $0x0, -0x1dc(%rbp)
movl $0x0, -0x1e0(%rbp)
movl -0x1e0(%rbp), %eax
cmpl -0x1ac(%rbp), %eax
jge 0xaa338
movb $0x1, -0x1e1(%rbp)
movl $0x0, -0x1e8(%rbp)
movl -0x1e8(%rbp), %ecx
xorl %eax, %eax
cmpl -0x1e0(%rbp), %ecx
movb %al, -0x359(%rbp)
jge 0xaa1f8
movb -0x1e1(%rbp), %al
movb %al, -0x359(%rbp)
movb -0x359(%rbp), %al
testb $0x1, %al
jne 0xaa207
jmp 0xaa2f2
movslq -0x1e0(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xae5c0
movq %rax, -0x368(%rbp)
jmp 0xaa220
movq -0x368(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x1f0(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0x200(%rbp)
movslq -0x1e8(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xae5c0
movq %rax, -0x370(%rbp)
jmp 0xaa255
movq -0x370(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x210(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0x220(%rbp)
movq -0x8(%rbp), %rdi
movq -0x210(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x28(%rax)
movaps -0x220(%rbp), %xmm0
movups %xmm0, 0x18(%rax)
movq -0x1f0(%rbp), %rcx
movq %rcx, 0x10(%rax)
movaps -0x200(%rbp), %xmm0
movups %xmm0, (%rax)
callq 0x9f840
movb %al, -0x371(%rbp)
jmp 0xaa2b0
movb -0x371(%rbp), %al
testb $0x1, %al
jne 0xaa2bc
jmp 0xaa2dc
movb $0x0, -0x1e1(%rbp)
jmp 0xaa2dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0xaa803
jmp 0xaa2de
movl -0x1e8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e8(%rbp)
jmp 0xaa1d6
movzbl -0x1e1(%rbp), %esi
andl $0x1, %esi
leaq -0x1d8(%rbp), %rdi
callq 0xf700
jmp 0xaa30a
testb $0x1, -0x1e1(%rbp)
je 0xaa322
movl -0x1dc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1dc(%rbp)
jmp 0xaa324
movl -0x1e0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e0(%rbp)
jmp 0xaa1b3
cmpl $0x0, -0x1dc(%rbp)
jle 0xaa7d3
movl $0x0, -0x224(%rbp)
movl -0x224(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0xaa52e
movq -0x8(%rbp), %rdi
addq $0x3c0, %rdi            # imm = 0x3C0
movslq -0x224(%rbp), %rsi
callq 0xae4a0
movq %rax, -0x380(%rbp)
jmp 0xaa37e
movq -0x380(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x230(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0x240(%rbp)
movq -0x8(%rbp), %rdi
addq $0x3c0, %rdi            # imm = 0x3C0
movslq -0x224(%rbp), %rsi
callq 0xae4a0
movq %rax, -0x388(%rbp)
jmp 0xaa3ba
movq -0x388(%rbp), %rdi
addq $0x20, %rdi
callq 0xae590
movl %eax, -0x244(%rbp)
movl $0x0, -0x248(%rbp)
movl -0x248(%rbp), %eax
cmpl -0x244(%rbp), %eax
jge 0xaa518
movq -0x8(%rbp), %rdi
addq $0x3c0, %rdi            # imm = 0x3C0
movslq -0x224(%rbp), %rsi
callq 0xae4a0
movq %rax, -0x390(%rbp)
jmp 0xaa40c
movq -0x390(%rbp), %rdi
addq $0x20, %rdi
movslq -0x248(%rbp), %rsi
callq 0xae5c0
movq %rax, -0x398(%rbp)
jmp 0xaa42c
movq -0x398(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x250(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0x260(%rbp)
leaq -0x2a8(%rbp), %rdi
leaq -0x260(%rbp), %rsi
leaq -0x240(%rbp), %rdx
callq 0xeadf0
jmp 0xaa464
leaq -0x290(%rbp), %rdi
leaq -0x2a8(%rbp), %rsi
movsd 0x147bfe(%rip), %xmm0   # 0x1f2078
callq 0xea9f0
jmp 0xaa481
leaq -0x278(%rbp), %rdi
leaq -0x240(%rbp), %rsi
leaq -0x290(%rbp), %rdx
callq 0xeae50
jmp 0xaa49d
movq -0x8(%rbp), %rdi
addq $0x3c0, %rdi            # imm = 0x3C0
movslq -0x224(%rbp), %rsi
callq 0xae4a0
movq %rax, -0x3a0(%rbp)
jmp 0xaa4bd
movq -0x3a0(%rbp), %rdi
addq $0x20, %rdi
movslq -0x248(%rbp), %rsi
callq 0xae5c0
movq %rax, -0x3a8(%rbp)
jmp 0xaa4dd
movq -0x3a8(%rbp), %rax
movq -0x278(%rbp), %rcx
movq %rcx, (%rax)
movq -0x270(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x268(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0x248(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x248(%rbp)
jmp 0xaa3da
jmp 0xaa51a
movl -0x224(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x224(%rbp)
jmp 0xaa34f
movl $0x0, -0x2ac(%rbp)
movl -0x2ac(%rbp), %eax
cmpl -0x1ac(%rbp), %eax
jge 0xaa7d1
movslq -0x2ac(%rbp), %rsi
leaq -0x1d8(%rbp), %rdi
callq 0xfff0
movq %rdx, -0x3b8(%rbp)
movq %rax, -0x3b0(%rbp)
jmp 0xaa56d
movq -0x3b8(%rbp), %rax
movq -0x3b0(%rbp), %rcx
movq %rcx, -0x2c0(%rbp)
movq %rax, -0x2b8(%rbp)
leaq -0x2c0(%rbp), %rdi
callq 0x101a0
testb $0x1, %al
jne 0xaa59e
jmp 0xaa7bb
testb $0x1, -0x9(%rbp)
je 0xaa656
movl -0x2ac(%rbp), %eax
movl %eax, -0x3c4(%rbp)
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
callq 0xae5c0
movq %rax, -0x3c0(%rbp)
jmp 0xaa5c9
movq -0x3c0(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x3d8(%rbp)
movslq -0x2ac(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xae5c0
movq %rax, -0x3d0(%rbp)
jmp 0xaa5f5
movq -0x3d0(%rbp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x3e8(%rbp)
movslq -0x2ac(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xae5c0
movq %rax, -0x3e0(%rbp)
jmp 0xaa622
movsd -0x3e8(%rbp), %xmm1
movsd -0x3d8(%rbp), %xmm0
movl -0x3c4(%rbp), %esi
movq -0x3e0(%rbp), %rax
movsd 0x10(%rax), %xmm2
leaq 0x14fde8(%rip), %rdi    # 0x1fa433
movb $0x3, %al
callq 0x5070
jmp 0xaa654
jmp 0xaa656
movslq -0x2ac(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xae5c0
movq %rax, -0x3f0(%rbp)
jmp 0xaa66f
movq -0x3f0(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x400(%rbp)
movslq -0x2ac(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xae5c0
movq %rax, -0x3f8(%rbp)
jmp 0xaa69b
movq -0x3f8(%rbp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x410(%rbp)
movslq -0x2ac(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xae5c0
movq %rax, -0x408(%rbp)
jmp 0xaa6c8
movsd -0x410(%rbp), %xmm1
movsd -0x400(%rbp), %xmm0
movq -0x408(%rbp), %rax
movsd 0x10(%rax), %xmm2
leaq -0x330(%rbp), %rdi
callq 0x110910
jmp 0xaa6f2
movl $0x2, -0x318(%rbp)
leaq -0x310(%rbp), %rax
movq %rax, -0x420(%rbp)
movslq -0x2ac(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0xae5c0
movq %rax, -0x418(%rbp)
jmp 0xaa723
movq -0x418(%rbp), %rsi
movq -0x420(%rbp), %rdi
callq 0xafc20
jmp 0xaa738
leaq -0x310(%rbp), %rax
movq %rax, -0x430(%rbp)
movslq -0x2ac(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0xae5c0
movq %rax, -0x428(%rbp)
jmp 0xaa75f
movq -0x428(%rbp), %rsi
movq -0x430(%rbp), %rdi
callq 0xafc20
jmp 0xaa774
movq -0x8(%rbp), %rdi
addq $0x3c0, %rdi            # imm = 0x3C0
leaq -0x330(%rbp), %rsi
callq 0xafc90
jmp 0xaa78d
leaq -0x330(%rbp), %rdi
callq 0x12650
jmp 0xaa7bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x330(%rbp), %rdi
callq 0x12650
jmp 0xaa803
jmp 0xaa7bd
movl -0x2ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2ac(%rbp)
jmp 0xaa538
jmp 0xaa7d3
leaq -0x1d8(%rbp), %rdi
callq 0xf9b0
leaq -0x58(%rbp), %rdi
callq 0x12210
leaq -0x40(%rbp), %rdi
callq 0x12210
leaq -0x28(%rbp), %rdi
callq 0x12210
addq $0x460, %rsp            # imm = 0x460
popq %rbp
retq
leaq -0x1d8(%rbp), %rdi
callq 0xf9b0
leaq -0x58(%rbp), %rdi
callq 0x12210
leaq -0x40(%rbp), %rdi
callq 0x12210
leaq -0x28(%rbp), %rdi
callq 0x12210
movq -0x88(%rbp), %rdi
callq 0x58f0
nopw %cs:(%rax,%rax)