// File: Luthaf[P]zeopp/networkstorage.cc
// Function: build_framework(std::vector<MOLECULE, std::allocator<MOLECULE>>*, ATOM_NETWORK*, std::vector<MOLECULE, std::allocator<MOLECULE>>*, double, double)
ATOM_NETWORK build_framework(vector<MOLECULE> *molecules, ATOM_NETWORK *cell, vector<MOLECULE> *assembled_molecules, double mean_edge_length, double unit_edge_length) {
  //1) set up the unit cell based on the underlying cell and the calculated edge length as a scaling factor
  ATOM_NETWORK alt_framework;
  alt_framework.make(mean_edge_length*cell->a/unit_edge_length, mean_edge_length*cell->b/unit_edge_length, mean_edge_length*cell->c/unit_edge_length, cell->alpha, cell->beta, cell->gamma);
  //2) for each vertex fractional position in the underlying cell, put the appropriate oriented molecule at the corresponding fractional position in the new cell, using the centre of mass of the molecule
  int num_mols = molecules->size();
  int num_vertices = cell->vertices.size();
  if(num_mols!=num_vertices) {
    printf("ERROR: the number of oriented molecules (%d) is not equal to the number of vertices in the underlying cell (%d)\n", num_mols, num_vertices);
  }
  for(int i=0; i<num_mols; i++) {
    XYZ abc = cell->vertices.at(i).abc;
    XYZ new_xyz = alt_framework.abc_to_xyz_returning_XYZ(abc);
    MOLECULE old_m = molecules->at(i);
    XYZ shift = new_xyz - old_m.com;
    assembled_molecules->push_back(translate(old_m, shift));
  }
  //3) put atoms into the cell, converting to fractional coords
  put_atoms_in_atom_network(&alt_framework, assembled_molecules);
  return alt_framework;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2f0, %rsp            # imm = 0x2F0
movq %rdi, -0x2b8(%rbp)
movq %rdi, %rax
movq %rax, -0x2c0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movsd %xmm0, -0x28(%rbp)
movsd %xmm1, -0x30(%rbp)
movb $0x0, -0x31(%rbp)
callq 0x95350
movq -0x2b8(%rbp), %rdi
movsd -0x28(%rbp), %xmm2
movq -0x18(%rbp), %rax
movsd 0x8(%rax), %xmm1
movsd 0x10(%rax), %xmm4
movaps %xmm2, %xmm0
mulsd %xmm1, %xmm0
movsd -0x30(%rbp), %xmm3
divsd %xmm3, %xmm0
movaps %xmm2, %xmm1
mulsd %xmm4, %xmm1
divsd %xmm3, %xmm1
movsd 0x18(%rax), %xmm4
mulsd %xmm4, %xmm2
divsd %xmm3, %xmm2
movsd 0x20(%rax), %xmm3
movsd 0x28(%rax), %xmm4
movsd 0x30(%rax), %xmm5
callq 0x960d0
jmp 0xa4097
movq -0x10(%rbp), %rdi
callq 0xaf3f0
movl %eax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
addq $0x3c0, %rdi            # imm = 0x3C0
callq 0xae470
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x4c(%rbp), %eax
je 0xa40e7
movl -0x48(%rbp), %esi
movl -0x4c(%rbp), %edx
leaq 0x154f38(%rip), %rdi    # 0x1f9003
xorl %eax, %eax
callq 0x5070
jmp 0xa40d4
jmp 0xa40e7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xa42c3
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x48(%rbp), %eax
jge 0xa428b
movq -0x18(%rbp), %rdi
addq $0x3c0, %rdi            # imm = 0x3C0
movslq -0x50(%rbp), %rsi
callq 0xae4a0
movq %rax, -0x2c8(%rbp)
jmp 0xa4117
movq -0x2b8(%rbp), %rsi
movq -0x2c8(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x60(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0x70(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x97310
jmp 0xa4146
movq -0x10(%rbp), %rdi
movslq -0x50(%rbp), %rsi
callq 0xaf120
movq %rax, -0x2d0(%rbp)
jmp 0xa415c
movq -0x2d0(%rbp), %rsi
leaq -0x130(%rbp), %rdi
callq 0xaf160
jmp 0xa4171
leaq -0xa0(%rbp), %rdx
leaq -0x148(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0xeadf0
jmp 0xa418d
movq -0x20(%rbp), %rax
movq %rax, -0x2d8(%rbp)
leaq -0x298(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0xaf160
jmp 0xa41ad
movq -0x138(%rbp), %rax
movq %rax, -0x2a0(%rbp)
movups -0x148(%rbp), %xmm0
movaps %xmm0, -0x2b0(%rbp)
movq -0x2a0(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x10(%rax)
movaps -0x2b0(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x1f0(%rbp), %rdi
leaq -0x298(%rbp), %rsi
callq 0x110ee0
jmp 0xa41f6
movq -0x2d8(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0xafa90
jmp 0xa420b
leaq -0x1f0(%rbp), %rdi
callq 0xaf390
leaq -0x298(%rbp), %rdi
callq 0xaf390
leaq -0x130(%rbp), %rdi
callq 0xaf390
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0xa40ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xa427d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xa4271
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0xaf390
leaq -0x298(%rbp), %rdi
callq 0xaf390
leaq -0x130(%rbp), %rdi
callq 0xaf390
jmp 0xa42c3
movq -0x2b8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xa3b40
jmp 0xa429d
movb $0x1, -0x31(%rbp)
testb $0x1, -0x31(%rbp)
jne 0xa42b3
movq -0x2b8(%rbp), %rdi
callq 0xfde0
movq -0x2c0(%rbp), %rax
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq
movq -0x2b8(%rbp), %rdi
callq 0xfde0
movq -0x40(%rbp), %rdi
callq 0x58f0
nopl (%rax,%rax)