// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movaps %xmm1, 0x20(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
callq 0x3db30
movsd 0x18(%r15), %xmm0
movapd 0x40(%rsp), %xmm3
mulsd %xmm3, %xmm0
movapd 0x20(%rsp), %xmm4
divsd %xmm4, %xmm0
movsd 0x30(%r15), %xmm1
movupd 0x8(%r15), %xmm2
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
mulpd %xmm2, %xmm3
unpcklpd %xmm4, %xmm4            # xmm4 = xmm4[0,0]
divpd %xmm4, %xmm3
movupd %xmm3, 0x8(%rbx)
movsd %xmm0, 0x18(%rbx)
movups 0x20(%r15), %xmm0
movups %xmm0, 0x20(%rbx)
movsd %xmm1, 0x30(%rbx)
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
callq 0x3e5a4
movq 0x8(%r14), %rax
movq %r14, 0x58(%rsp)
subq (%r14), %rax
movl $0xa8, %ecx
cqto
idivq %rcx
movq %rax, %rbx
movq 0x3c8(%r15), %rax
subq 0x3c0(%r15), %rax
pushq $0x70
popq %rcx
cqto
idivq %rcx
cmpl %eax, %ebx
je 0x459cd
leaq 0xbcada(%rip), %rdi     # 0x10249c
movl %ebx, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x4050
addq $0x3c0, %r15            # imm = 0x3C0
movq %r15, 0x40(%rsp)
xorl %ebp, %ebp
testl %ebx, %ebx
cmovlel %ebp, %ebx
leaq 0x98(%rsp), %r13
leaq 0xb0(%rsp), %r14
leaq 0x158(%rsp), %r12
leaq 0x200(%rsp), %r15
cmpq %rbp, %rbx
je 0x45ac5
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x4b63c
movq 0x10(%rax), %rcx
movq %rcx, 0x70(%rsp)
movups (%rax), %xmm0
movaps %xmm0, 0x60(%rsp)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x3f1b2
movq 0x58(%rsp), %rdi
movq %rbp, %rsi
callq 0x4b9e8
movq %r14, %rdi
movq %rax, %rsi
callq 0x4ba0a
leaq 0x80(%rsp), %rdi
movq %r13, %rsi
leaq 0x140(%rsp), %rdx
callq 0x642ae
movq %r12, %rdi
movq %r14, %rsi
callq 0x4ba0a
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x80(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x76138
movq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x4dee8
movq %r15, %rdi
callq 0x4bb60
movq %r12, %rdi
callq 0x4bb60
movq %r14, %rdi
callq 0x4bb60
incq %rbp
jmp 0x45a00
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
callq 0x4558a
movq %rbx, %rax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x45b2c
jmp 0x45b2c
jmp 0x45af2
movq %rax, %rbx
jmp 0x45b19
movq %rax, %rbx
jmp 0x45b0c
movq %rax, %rbx
leaq 0x200(%rsp), %rdi
callq 0x4bb60
leaq 0x158(%rsp), %rdi
callq 0x4bb60
leaq 0xb0(%rsp), %rdi
callq 0x4bb60
jmp 0x45b2f
jmp 0x45b2c
jmp 0x45b2c
movq %rax, %rbx
movq 0x20(%rsp), %rdi
callq 0xaab6
movq %rbx, %rdi
callq 0x4780