// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
callq 0x4520
movq %r15, %rdi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x43d0
leaq 0x150(%rsp), %rdi
callq 0x4120
movl %eax, %ebp
testb %al, %al
je 0x2ee58
movq 0x16819d(%rip), %r15    # 0x196fc0
leaq 0x121530(%rip), %rsi    # 0x15035a
movl $0x24, %edx
movq %r15, %rdi
callq 0x44e0
testq %rbx, %rbx
je 0x2ee9b
movq %rbx, %rdi
callq 0x41e0
movq 0x168175(%rip), %rdi    # 0x196fc0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x44e0
jmp 0x2eeb3
movq 0x168161(%rip), %r14    # 0x196fc0
leaq 0x121951(%rip), %rsi    # 0x1507b7
movl $0x27, %edx
movq %r14, %rdi
callq 0x44e0
testq %rbx, %rbx
je 0x2f254
movq %rbx, %rdi
callq 0x41e0
movq 0x168135(%rip), %rdi    # 0x196fc0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x44e0
jmp 0x2f26c
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x4710
movq 0x168106(%rip), %rdi    # 0x196fc0
leaq 0x12666d(%rip), %rsi    # 0x15552e
movl $0x1, %edx
callq 0x44e0
leaq 0xe0(%rsp), %rdi
leaq 0x126654(%rip), %rsi    # 0x15552e
movl $0x1, %edx
callq 0x44e0
leaq 0x126643(%rip), %rsi    # 0x15552e
movl $0x1, %edx
leaq 0xe0(%rsp), %rdi
callq 0x44e0
movb %bpl, 0x7(%rsp)
incl 0x8(%rsp)
movq 0x10(%rsp), %rax
cmpl $0x0, 0x110(%rax)
jle 0x2f1cc
leaq 0x70(%rsp), %r13
xorl %ecx, %ecx
leaq 0x2e0(%rsp), %r15
imulq $0x98, %rcx, %r12
xorl %eax, %eax
movl %eax, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %eax, %eax
movl %eax, 0x1c(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
movq 0x2f0(%rax), %rbp
movq 0x2f8(%rax), %rdx
subq %rbp, %rdx
sarq $0x3, %rdx
movabsq $-0x79435e50d79435e5, %rax # imm = 0x86BCA1AF286BCA1B
imulq %rax, %rdx
movq %rcx, %r14
cmpq %rcx, %rdx
jbe 0x2f51f
movupd (%rbp,%r12), %xmm0
movupd 0x10(%rbp,%r12), %xmm1
movupd 0x20(%rbp,%r12), %xmm2
movups 0x30(%rbp,%r12), %xmm3
movaps %xmm3, 0x60(%rsp)
movapd %xmm2, 0x50(%rsp)
movapd %xmm1, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x40(%rbp,%r12), %rsi
movq 0x48(%rbp,%r12), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0xf32a
addq %r12, %rbp
leaq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x60(%rbp), %rsi
movq 0x68(%rbp), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %rdi
callq 0xf32a
movb 0x90(%rbp), %al
movb %al, 0x50(%r13)
movups 0x80(%rbp), %xmm0
movups %xmm0, 0x40(%r13)
movsd 0x48(%rsp), %xmm0
callq 0x84e6b
addsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x50(%rsp), %xmm0
callq 0x84e6b
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x58(%rsp), %xmm0
callq 0x84e6b
movapd %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
addsd %xmm0, %xmm2
movsd %xmm2, 0x58(%rsp)
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x53092
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rdx
leaq 0xe0(%rsp), %rdi
callq 0x44e0
movq %rax, %rbp
movl $0x2, %edx
movq %rax, %rdi
leaq 0x1203e0(%rip), %rsi    # 0x14f48f
callq 0x44e0
movq %r15, %rdi
xorl %esi, %esi
callq 0x85478
movsd (%rax), %xmm0
movq %rbp, %rdi
callq 0x46b0
movq %rax, %rbp
movl $0x4, %edx
movq %rax, %rdi
leaq 0x121703(%rip), %rsi    # 0x1507df
callq 0x44e0
movq %r15, %rdi
movl $0x1, %esi
callq 0x85478
movsd (%rax), %xmm0
movq %rbp, %rdi
callq 0x46b0
movq %rax, %rbp
movl $0x4, %edx
movq %rax, %rdi
leaq 0x1216d3(%rip), %rsi    # 0x1507df
callq 0x44e0
movq %r15, %rdi
movl $0x2, %esi
callq 0x85478
movsd (%rax), %xmm0
movq %rbp, %rdi
callq 0x46b0
movl $0x4, %edx
movq %rax, %rdi
leaq 0x1216ab(%rip), %rsi    # 0x1507e4
callq 0x44e0
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x2f163
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x4470
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x2f185
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x4470
incl %ebx
cmpl %ebx, 0x8(%rsp)
movq %r14, %rcx
jne 0x2ef56
movl 0x1c(%rsp), %eax
incl %eax
cmpl 0x8(%rsp), %eax
jne 0x2ef43
movl 0x18(%rsp), %eax
incl %eax
cmpl 0x8(%rsp), %eax
jne 0x2ef30
incq %rcx
movq 0x10(%rsp), %rax
movslq 0x110(%rax), %rax
cmpq %rax, %rcx
jl 0x2ef27
leaq 0x121616(%rip), %rsi    # 0x1507e9
movl $0x3, %edx
leaq 0xe0(%rsp), %rdi
callq 0x44e0
movb 0x7(%rsp), %bpl
cvtsi2sdl 0x8(%rsp), %xmm1
movq 0x10(%rsp), %rax
movsd 0x38(%rax), %xmm0
movsd %xmm1, 0x8(%rsp)
mulsd %xmm1, %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46b0
leaq 0x1215c7(%rip), %rbx    # 0x1507df
movl $0x4, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x44e0
movq 0x10(%rsp), %rax
movsd 0x40(%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2f29c
jp 0x2f29c
movl $0x7, %edx
leaq 0x1215a3(%rip), %rbx    # 0x1507ed
leaq 0xe0(%rsp), %rdi
jmp 0x2f2b7
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x4710
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x4420
movsbl %al, %esi
movq 0x167d36(%rip), %rdi    # 0x196fc0
callq 0x4040
movq %rax, %rdi
callq 0x42c0
jmp 0x2f4fe
mulsd 0x8(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46b0
movq %rax, %rdi
movl $0x4, %edx
movq %rbx, %rsi
callq 0x44e0
movq 0x10(%rsp), %rax
movsd 0x48(%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2f2eb
jp 0x2f2eb
movl $0x8, %edx
leaq 0x121514(%rip), %rsi    # 0x1507f5
leaq 0xe0(%rsp), %rdi
jmp 0x2f30d
mulsd 0x8(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46b0
movq %rax, %rdi
movl $0x5, %edx
leaq 0x1214f1(%rip), %rsi    # 0x1507fe
callq 0x44e0
leaq 0x1214d0(%rip), %rsi    # 0x1507e9
movl $0x3, %edx
leaq 0xe0(%rsp), %rdi
callq 0x44e0
movq 0x10(%rsp), %rax
movsd 0x50(%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2f357
jp 0x2f357
movl $0x7, %edx
leaq 0x1214a0(%rip), %rsi    # 0x1507ed
leaq 0xe0(%rsp), %rdi
jmp 0x2f379
mulsd 0x8(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46b0
movq %rax, %rdi
movl $0x4, %edx
leaq 0x121466(%rip), %rsi    # 0x1507df
callq 0x44e0
movq 0x10(%rsp), %rax
movsd 0x58(%rax), %xmm0
mulsd 0x8(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46b0
leaq 0x12143d(%rip), %rbx    # 0x1507df
movl $0x4, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x44e0
movq 0x10(%rsp), %rax
movsd 0x60(%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2f3de
jp 0x2f3de
movl $0x8, %edx
leaq 0x121421(%rip), %rsi    # 0x1507f5
leaq 0xe0(%rsp), %rdi
jmp 0x2f400
mulsd 0x8(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46b0
movq %rax, %rdi
movl $0x5, %edx
leaq 0x1213fe(%rip), %rsi    # 0x1507fe
callq 0x44e0
leaq 0x1213dd(%rip), %rsi    # 0x1507e9
movl $0x3, %edx
leaq 0xe0(%rsp), %rdi
callq 0x44e0
movq 0x10(%rsp), %rax
movsd 0x68(%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2f44a
jp 0x2f44a
movl $0x7, %edx
leaq 0x1213ad(%rip), %rbx    # 0x1507ed
leaq 0xe0(%rsp), %rdi
jmp 0x2f465
mulsd 0x8(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46b0
movq %rax, %rdi
movl $0x4, %edx
movq %rbx, %rsi
callq 0x44e0
movq 0x10(%rsp), %rax
movsd 0x70(%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2f499
jp 0x2f499
movl $0x7, %edx
leaq 0x12135e(%rip), %rsi    # 0x1507ed
leaq 0xe0(%rsp), %rdi
jmp 0x2f4bb
mulsd 0x8(%rsp), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46b0
movq %rax, %rdi
movl $0x4, %edx
leaq 0x121324(%rip), %rsi    # 0x1507df
callq 0x44e0
movsd 0x8(%rsp), %xmm0
movq 0x10(%rsp), %rax
mulsd 0x78(%rax), %xmm0
leaq 0xe0(%rsp), %rdi
callq 0x46b0
leaq 0x121320(%rip), %rsi    # 0x150804
movl $0x6, %edx
movq %rax, %rdi
callq 0x44e0
leaq 0xd0(%rsp), %rdi
callq 0x4090
leaq 0xd0(%rsp), %rdi
callq 0x4110
movl %ebp, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12017f(%rip), %rdi    # 0x14f6a5
movq %r14, %rsi
xorl %eax, %eax
callq 0x4630
jmp 0x2f534
jmp 0x2f534
movq %rax, %rbx
jmp 0x2f58a
movq %rax, %rbx
jmp 0x2f568
jmp 0x2f540
movq %rax, %rbx
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x2f568
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x4470
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x2f58a
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x4470
leaq 0xd0(%rsp), %rdi
callq 0x4110
movq %rbx, %rdi
callq 0x4780