// File: 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
movq %rsp, %rbp
subq $0x400, %rsp            # imm = 0x400
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movl $0x2, -0x20(%rbp)
testb $0x1, -0x19(%rbp)
jne 0x5894e
movl $0x1, -0x20(%rbp)
leaq -0x230(%rbp), %rdi
movq %rdi, -0x308(%rbp)
callq 0x5660
movq -0x308(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x10, %edx
callq 0x54b0
jmp 0x58978
leaq -0x230(%rbp), %rdi
callq 0x58b0
movb %al, -0x309(%rbp)
jmp 0x5898c
movb -0x309(%rbp), %al
testb $0x1, %al
jne 0x58a0c
jmp 0x58998
movq 0x21a621(%rip), %rdi    # 0x272fc0
leaq 0x19b05a(%rip), %rsi    # 0x1f3a00
callq 0x5540
movq %rax, -0x318(%rbp)
jmp 0x589b4
movq -0x318(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x5540
movq %rax, -0x320(%rbp)
jmp 0x589cd
movq -0x320(%rbp), %rdi
movq 0x21a5bd(%rip), %rsi    # 0x272f98
callq 0x55b0
jmp 0x589e2
movb $0x0, -0x1(%rbp)
movl $0x1, -0x240(%rbp)
jmp 0x5912d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x59147
movq 0x21a5ad(%rip), %rdi    # 0x272fc0
leaq 0x19ab89(%rip), %rsi    # 0x1f35a3
callq 0x5540
movq %rax, -0x328(%rbp)
jmp 0x58a28
movq -0x328(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x5540
movq %rax, -0x330(%rbp)
jmp 0x58a41
movq -0x330(%rbp), %rdi
leaq 0x1a0bd6(%rip), %rsi    # 0x1f9625
callq 0x5540
jmp 0x58a56
leaq -0x220(%rbp), %rdi
leaq 0x1a0bc1(%rip), %rsi    # 0x1f9625
callq 0x5540
movq %rax, -0x338(%rbp)
jmp 0x58a72
movq -0x338(%rbp), %rdi
leaq 0x1a0ba5(%rip), %rsi    # 0x1f9625
callq 0x5540
jmp 0x58a87
movl $0x0, -0x244(%rbp)
movl -0x244(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x110(%rcx), %eax
jge 0x58d9f
movl $0x0, -0x248(%rbp)
movl -0x248(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x58d89
movl $0x0, -0x24c(%rbp)
movl -0x24c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x58d73
movl $0x0, -0x250(%rbp)
movl -0x250(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x58d5d
movq -0x18(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x244(%rbp), %rsi
callq 0xf090
movq %rax, -0x340(%rbp)
jmp 0x58b12
movq -0x340(%rbp), %rsi
leaq -0x2e8(%rbp), %rdi
callq 0xf0d0
jmp 0x58b27
movsd -0x2d0(%rbp), %xmm0
callq 0xe9290
movsd %xmm0, -0x348(%rbp)
jmp 0x58b3e
movsd -0x348(%rbp), %xmm0
cvtsi2sdl -0x248(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x2d0(%rbp)
movsd -0x2c8(%rbp), %xmm0
callq 0xe9290
movsd %xmm0, -0x350(%rbp)
jmp 0x58b71
movsd -0x350(%rbp), %xmm0
cvtsi2sdl -0x24c(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x2c8(%rbp)
movsd -0x2c0(%rbp), %xmm0
callq 0xe9290
movsd %xmm0, -0x358(%rbp)
jmp 0x58ba4
movsd -0x358(%rbp), %xmm0
cvtsi2sdl -0x250(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x2c0(%rbp)
movq -0x18(%rbp), %rsi
movsd -0x2d0(%rbp), %xmm0
movsd -0x2c8(%rbp), %xmm1
movsd -0x2c0(%rbp), %xmm2
leaq -0x300(%rbp), %rdi
callq 0x97170
jmp 0x58bea
leaq -0x220(%rbp), %rdi
leaq -0x2a8(%rbp), %rsi
callq 0x5480
movq %rax, -0x360(%rbp)
jmp 0x58c06
movq -0x360(%rbp), %rdi
leaq 0x199874(%rip), %rsi    # 0x1f2488
callq 0x5540
movq %rax, -0x368(%rbp)
jmp 0x58c22
leaq -0x300(%rbp), %rdi
xorl %esi, %esi
callq 0xe9d70
movq %rax, -0x370(%rbp)
jmp 0x58c39
movq -0x368(%rbp), %rdi
movq -0x370(%rbp), %rax
movsd (%rax), %xmm0
callq 0x5980
movq %rax, -0x378(%rbp)
jmp 0x58c59
movq -0x378(%rbp), %rdi
leaq 0x19adc1(%rip), %rsi    # 0x1f3a28
callq 0x5540
movq %rax, -0x380(%rbp)
jmp 0x58c75
leaq -0x300(%rbp), %rdi
movl $0x1, %esi
callq 0xe9d70
movq %rax, -0x388(%rbp)
jmp 0x58c8f
movq -0x380(%rbp), %rdi
movq -0x388(%rbp), %rax
movsd (%rax), %xmm0
callq 0x5980
movq %rax, -0x390(%rbp)
jmp 0x58caf
movq -0x390(%rbp), %rdi
leaq 0x19ad6b(%rip), %rsi    # 0x1f3a28
callq 0x5540
movq %rax, -0x398(%rbp)
jmp 0x58ccb
leaq -0x300(%rbp), %rdi
movl $0x2, %esi
callq 0xe9d70
movq %rax, -0x3a0(%rbp)
jmp 0x58ce5
movq -0x398(%rbp), %rdi
movq -0x3a0(%rbp), %rax
movsd (%rax), %xmm0
callq 0x5980
movq %rax, -0x3a8(%rbp)
jmp 0x58d05
movq -0x3a8(%rbp), %rdi
leaq 0x19ad1a(%rip), %rsi    # 0x1f3a2d
callq 0x5540
jmp 0x58d1a
leaq -0x2e8(%rbp), %rdi
callq 0xf200
movl -0x250(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x250(%rbp)
jmp 0x58ae3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x2e8(%rbp), %rdi
callq 0xf200
jmp 0x59147
jmp 0x58d5f
movl -0x24c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24c(%rbp)
jmp 0x58aca
jmp 0x58d75
movl -0x248(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x248(%rbp)
jmp 0x58ab1
jmp 0x58d8b
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x58a91
leaq -0x220(%rbp), %rdi
leaq 0x19ac85(%rip), %rsi    # 0x1f3a32
callq 0x5540
movq %rax, -0x3b0(%rbp)
jmp 0x58dbb
movq -0x3b0(%rbp), %rdi
movq -0x18(%rbp), %rax
movsd 0x38(%rax), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x5980
movq %rax, -0x3b8(%rbp)
jmp 0x58de2
movq -0x3b8(%rbp), %rdi
leaq 0x19ac38(%rip), %rsi    # 0x1f3a28
callq 0x5540
jmp 0x58df7
movq -0x18(%rbp), %rax
movsd 0x40(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x58e22
jp 0x58e22
leaq -0x220(%rbp), %rdi
leaq 0x19ac1d(%rip), %rsi    # 0x1f3a36
callq 0x5540
jmp 0x58e20
jmp 0x58e60
leaq -0x220(%rbp), %rdi
movq -0x18(%rbp), %rax
movsd 0x40(%rax), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x5980
movq %rax, -0x3c0(%rbp)
jmp 0x58e49
movq -0x3c0(%rbp), %rdi
leaq 0x19abd1(%rip), %rsi    # 0x1f3a28
callq 0x5540
jmp 0x58e5e
jmp 0x58e60
movq -0x18(%rbp), %rax
movsd 0x48(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x58e8b
jp 0x58e8b
leaq -0x220(%rbp), %rdi
leaq 0x19abbc(%rip), %rsi    # 0x1f3a3e
callq 0x5540
jmp 0x58e89
jmp 0x58ec9
leaq -0x220(%rbp), %rdi
movq -0x18(%rbp), %rax
movsd 0x48(%rax), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x5980
movq %rax, -0x3c8(%rbp)
jmp 0x58eb2
movq -0x3c8(%rbp), %rdi
leaq 0x19ab87(%rip), %rsi    # 0x1f3a47
callq 0x5540
jmp 0x58ec7
jmp 0x58ec9
leaq -0x220(%rbp), %rdi
leaq 0x19ab5b(%rip), %rsi    # 0x1f3a32
callq 0x5540
jmp 0x58ede
movq -0x18(%rbp), %rax
movsd 0x50(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x58f09
jp 0x58f09
leaq -0x220(%rbp), %rdi
leaq 0x19ab36(%rip), %rsi    # 0x1f3a36
callq 0x5540
jmp 0x58f07
jmp 0x58f47
leaq -0x220(%rbp), %rdi
movq -0x18(%rbp), %rax
movsd 0x50(%rax), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x5980
movq %rax, -0x3d0(%rbp)
jmp 0x58f30
movq -0x3d0(%rbp), %rdi
leaq 0x19aaea(%rip), %rsi    # 0x1f3a28
callq 0x5540
jmp 0x58f45
jmp 0x58f47
leaq -0x220(%rbp), %rdi
movq -0x18(%rbp), %rax
movsd 0x58(%rax), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x5980
movq %rax, -0x3d8(%rbp)
jmp 0x58f6e
movq -0x3d8(%rbp), %rdi
leaq 0x19aaac(%rip), %rsi    # 0x1f3a28
callq 0x5540
jmp 0x58f83
movq -0x18(%rbp), %rax
movsd 0x60(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x58fae
jp 0x58fae
leaq -0x220(%rbp), %rdi
leaq 0x19aa99(%rip), %rsi    # 0x1f3a3e
callq 0x5540
jmp 0x58fac
jmp 0x58fec
leaq -0x220(%rbp), %rdi
movq -0x18(%rbp), %rax
movsd 0x60(%rax), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x5980
movq %rax, -0x3e0(%rbp)
jmp 0x58fd5
movq -0x3e0(%rbp), %rdi
leaq 0x19aa64(%rip), %rsi    # 0x1f3a47
callq 0x5540
jmp 0x58fea
jmp 0x58fec
leaq -0x220(%rbp), %rdi
leaq 0x19aa38(%rip), %rsi    # 0x1f3a32
callq 0x5540
jmp 0x59001
movq -0x18(%rbp), %rax
movsd 0x68(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x5902c
jp 0x5902c
leaq -0x220(%rbp), %rdi
leaq 0x19aa13(%rip), %rsi    # 0x1f3a36
callq 0x5540
jmp 0x5902a
jmp 0x5906a
leaq -0x220(%rbp), %rdi
movq -0x18(%rbp), %rax
movsd 0x68(%rax), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x5980
movq %rax, -0x3e8(%rbp)
jmp 0x59053
movq -0x3e8(%rbp), %rdi
leaq 0x19a9c7(%rip), %rsi    # 0x1f3a28
callq 0x5540
jmp 0x59068
jmp 0x5906a
movq -0x18(%rbp), %rax
movsd 0x70(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x59095
jp 0x59095
leaq -0x220(%rbp), %rdi
leaq 0x19a9aa(%rip), %rsi    # 0x1f3a36
callq 0x5540
jmp 0x59093
jmp 0x590d3
leaq -0x220(%rbp), %rdi
movq -0x18(%rbp), %rax
movsd 0x70(%rax), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x5980
movq %rax, -0x3f0(%rbp)
jmp 0x590bc
movq -0x3f0(%rbp), %rdi
leaq 0x19a95e(%rip), %rsi    # 0x1f3a28
callq 0x5540
jmp 0x590d1
jmp 0x590d3
leaq -0x220(%rbp), %rdi
movq -0x18(%rbp), %rax
movsd 0x78(%rax), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x5980
movq %rax, -0x3f8(%rbp)
jmp 0x590fa
movq -0x3f8(%rbp), %rdi
leaq 0x19a945(%rip), %rsi    # 0x1f3a4d
callq 0x5540
jmp 0x5910f
jmp 0x59111
leaq -0x230(%rbp), %rdi
callq 0x50a0
jmp 0x5911f
movb $0x1, -0x1(%rbp)
movl $0x1, -0x240(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x5150
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x400, %rsp            # imm = 0x400
popq %rbp
retq
leaq -0x230(%rbp), %rdi
callq 0x5150
movq -0x238(%rbp), %rdi
callq 0x58f0
nop