// File: /workspace/github_cpp_makefile_star2_O2/Luthaf[P]zeopp/networkio.cc
// Function: writeToMOPAC(char*, ATOM_NETWORK*, bool)
bool writeToMOPAC(char *filename, ATOM_NETWORK *cell, bool is_supercell){
  int num_cells = SUPERCELL_SIZE; //defined in networkio.h
  if(!is_supercell) num_cells = 1;
  fstream output;
  output.open(filename, fstream::out);
  if(!output.is_open()){
    cout << "Error: Failed to open .mop output file " << filename << endl;
    //cout << "Exiting ..." << "\n";
    //exit(1);
    return false;
  }
  else{
    cout << "Writing atom network information to " << filename << "\n";

    //Write two empty lines by default
    output << "\n" << "\n";

/*
    // Write information about the atoms
    vector<ATOM>::iterator atomIter = cell->atoms.begin();
    while(atomIter != cell->atoms.end()){
      output << atomIter->type << "  " << atomIter->x << " +1 "
             << atomIter->y << " +1 " << atomIter->z << " +1\n";
      atomIter++;
    }
*/
    for(int i=0; i<cell->numAtoms; i++) {
      //first, determine all the supercell images of this atom as required
      for(int a=0; a<num_cells; a++) {
        for(int b=0; b<num_cells; b++) {
          for(int c=0; c<num_cells; c++) {
            ATOM atom = cell->atoms.at(i);
            atom.a_coord = trans_to_origuc(atom.a_coord)+a;
            atom.b_coord = trans_to_origuc(atom.b_coord)+b;
            atom.c_coord = trans_to_origuc(atom.c_coord)+c;
            Point p = cell->abc_to_xyz(atom.a_coord, atom.b_coord, atom.c_coord);
            output << atom.type << "  " << p[0] << " +1 "
             << p[1] << " +1 " << p[2] << " +1\n";
          }
        }
      }
    }

    //Write unit cell box information
    output << "Tv " << cell->v_a.x*num_cells << " +1 ";
    if(cell->v_a.y==0.0) { output << " 0.0 0 "; } else { output << cell->v_a.y*num_cells << " +1 ";};
    if(cell->v_a.z==0.0) { output << " 0.0 0 \n"; } else { output << cell->v_a.z*num_cells << " +1 \n";};

    output << "Tv ";
    if(cell->v_b.x==0.0) { output << " 0.0 0 "; } else { output << cell->v_b.x*num_cells << " +1 ";};
    output << cell->v_b.y*num_cells << " +1 ";
    if(cell->v_b.z==0.0) { output << " 0.0 0 \n"; } else { output << cell->v_b.z*num_cells << " +1 \n";};

    output << "Tv ";
    if(cell->v_c.x==0.0) { output << " 0.0 0 "; } else { output << cell->v_c.x*num_cells << " +1 ";};
    if(cell->v_c.y==0.0) { output << " 0.0 0 "; } else { output << cell->v_c.y*num_cells << " +1 ";};
    output << cell->v_c.z*num_cells << " +1 \n\n";

  }
  output.close();
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movl %edx, 0x10(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
callq 0x4540
pushq $0x10
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4410
leaq 0x150(%rsp), %rdi
callq 0x4130
movl %eax, %ebp
testb %al, %al
je 0x24168
movq 0x12c062(%rip), %rdi    # 0x14ffc0
leaq 0xd9465(%rip), %rsi     # 0xfd3ca
callq 0x4480
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4480
leaq 0xde602(%rip), %rsi     # 0x10257e
movq %rax, %rdi
callq 0x4480
leaq 0xe0(%rsp), %rdi
leaq 0xde5eb(%rip), %rsi     # 0x10257e
callq 0x4480
leaq 0xde5df(%rip), %rsi     # 0x10257e
movq %rax, %rdi
callq 0x4480
movb %bpl, 0xf(%rsp)
incl 0x10(%rsp)
leaq 0x2f0(%r15), %rax
movq %rax, 0x20(%rsp)
xorl %esi, %esi
leaq 0x2e0(%rsp), %r14
leaq 0xd9882(%rip), %r13     # 0xfd84f
movq %r15, %rbx
movslq 0x110(%r15), %rax
cmpq %rax, %rsi
jge 0x241af
xorl %eax, %eax
cmpl 0x10(%rsp), %eax
je 0x2415d
movl %eax, 0x18(%rsp)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %eax, %eax
cmpl 0x10(%rsp), %eax
je 0x24152
movl %eax, 0x1c(%rsp)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x30(%rsp)
xorl %r12d, %r12d
cmpl %r12d, 0x10(%rsp)
je 0x24147
movq 0x20(%rsp), %rdi
movq %rsi, %r15
callq 0xa73a
leaq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0xa75c
movsd 0x50(%rsp), %xmm0
callq 0x6377f
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x58(%rsp), %xmm0
callq 0x6377f
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x60(%rsp), %xmm0
callq 0x6377f
movapd %xmm0, %xmm2
cvtsi2sd %r12d, %xmm0
addsd %xmm0, %xmm2
movsd %xmm2, 0x60(%rsp)
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3f0e6
leaq 0xe0(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x43f0
movq %rax, %rdi
leaq 0xd8414(%rip), %rsi     # 0xfc4cf
callq 0x4480
movq %rax, %rbp
movq %r14, %rdi
xorl %esi, %esi
callq 0x63c78
movsd (%rax), %xmm0
movq %rbp, %rdi
callq 0x46c0
movq %rax, %rdi
movq %r13, %rsi
callq 0x4480
movq %rax, %rbp
movq %r14, %rdi
pushq $0x1
popq %rsi
callq 0x63c78
movsd (%rax), %xmm0
movq %rbp, %rdi
callq 0x46c0
movq %rax, %rdi
movq %r13, %rsi
callq 0x4480
movq %rax, %rbp
movq %r14, %rdi
pushq $0x2
popq %rsi
callq 0x63c78
movsd (%rax), %xmm0
movq %rbp, %rdi
callq 0x46c0
movq %rax, %rdi
leaq 0xd9727(%rip), %rsi     # 0xfd854
callq 0x4480
leaq 0x38(%rsp), %rdi
callq 0xa808
incl %r12d
movq %r15, %rsi
jmp 0x24017
movl 0x1c(%rsp), %eax
incl %eax
jmp 0x23ffc
movl 0x18(%rsp), %eax
incl %eax
jmp 0x23fe2
incq %rsi
movq %rbx, %r15
jmp 0x23fcd
movq 0x12be51(%rip), %rdi    # 0x14ffc0
leaq 0xd96b1(%rip), %rsi     # 0xfd827
callq 0x4480
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4480
movq %rax, %rdi
callq 0x4230
leaq 0xd0(%rsp), %rdi
callq 0x4120
movl %ebp, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd96a3(%rip), %rsi     # 0xfd859
leaq 0xe0(%rsp), %rdi
callq 0x4480
movb 0xf(%rsp), %bpl
movq %rbx, %r14
cvtsi2sdl 0x10(%rsp), %xmm1
movsd 0x38(%rbx), %xmm0
movsd %xmm1, 0x10(%rsp)
mulsd %xmm1, %xmm0
movq %rax, %rdi
callq 0x46c0
leaq 0xd9660(%rip), %rbx     # 0xfd84f
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4480
movsd 0x40(%r14), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2421d
jp 0x2421d
leaq 0xd964a(%rip), %rbx     # 0xfd85d
leaq 0xe0(%rsp), %rdi
jmp 0x24233
mulsd 0x10(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46c0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4480
movsd 0x48(%r14), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2425e
jp 0x2425e
leaq 0xd9611(%rip), %rsi     # 0xfd865
leaq 0xe0(%rsp), %rdi
jmp 0x2427b
mulsd 0x10(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46c0
movq %rax, %rdi
leaq 0xd95f3(%rip), %rsi     # 0xfd86e
callq 0x4480
leaq 0xd95d2(%rip), %rsi     # 0xfd859
leaq 0xe0(%rsp), %rdi
callq 0x4480
movsd 0x50(%r14), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x242b7
jp 0x242b7
leaq 0xd95b0(%rip), %rsi     # 0xfd85d
leaq 0xe0(%rsp), %rdi
jmp 0x242d4
mulsd 0x10(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46c0
movq %rax, %rdi
leaq 0xd957b(%rip), %rsi     # 0xfd84f
callq 0x4480
movsd 0x58(%r14), %xmm0
mulsd 0x10(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46c0
leaq 0xd9556(%rip), %rbx     # 0xfd84f
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4480
movsd 0x60(%r14), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x24327
jp 0x24327
leaq 0xd9548(%rip), %rsi     # 0xfd865
leaq 0xe0(%rsp), %rdi
jmp 0x24344
mulsd 0x10(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46c0
movq %rax, %rdi
leaq 0xd952a(%rip), %rsi     # 0xfd86e
callq 0x4480
leaq 0xd9509(%rip), %rsi     # 0xfd859
leaq 0xe0(%rsp), %rdi
callq 0x4480
movsd 0x68(%r14), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x24380
jp 0x24380
leaq 0xd94e7(%rip), %rbx     # 0xfd85d
leaq 0xe0(%rsp), %rdi
jmp 0x24396
mulsd 0x10(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46c0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4480
movsd 0x70(%r14), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x243c1
jp 0x243c1
leaq 0xd94a6(%rip), %rsi     # 0xfd85d
leaq 0xe0(%rsp), %rdi
jmp 0x243de
mulsd 0x10(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46c0
movq %rax, %rdi
leaq 0xd9471(%rip), %rsi     # 0xfd84f
callq 0x4480
movsd 0x10(%rsp), %xmm0
mulsd 0x78(%r14), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46c0
leaq 0xd9471(%rip), %rsi     # 0xfd874
movq %rax, %rdi
callq 0x4480
leaq 0xd0(%rsp), %rdi
callq 0x4080
jmp 0x2418e
jmp 0x2441f
movq %rax, %rbx
jmp 0x24433
jmp 0x24426
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0xa808
leaq 0xd0(%rsp), %rdi
callq 0x4120
movq %rbx, %rdi
callq 0x4780