// File: atfrank[P]PCASA/lib/Trajectory.cpp
// Function: Trajectory::writeHeader(std::basic_ofstream<char, std::char_traits<char>>&)
void Trajectory::writeHeader(std::ofstream &trjout){
  if (format.compare("CHARMM") == 0){
    trjout.seekp(0, std::ios::beg);

    binbuf icntrl[21];
    binbuf *buffer=reinterpret_cast<binbuf *>(&icntrl[0]);
    int *ibuffer;
    int length;
    unsigned int i;

    for (i=0; i< 21; i++){
      buffer[i].i=0;
    }

    buffer[0].c[0]='C';
    buffer[0].c[1]='O';
    buffer[0].c[2]='R';
    buffer[0].c[3]='D';

    buffer[1].i=getNFrame();
    buffer[2].i=getNPriv();
    buffer[3].i=getNSavc();
    buffer[4].i=getNStep();
    icntrl[5].i=getQVelocity();

    buffer[8].i=getDOF();
    buffer[9].i=getNFixed();
    buffer[10].f=getTStepAKMA();
    buffer[11].i=getQCrystal();
    buffer[12].i=getQ4D();
    buffer[13].i=getQCharge();
    buffer[14].i=getQCheck();

    buffer[20].i=getVersion();

    length=sizeof(int)*21;

    writeFortran(trjout, buffer, length);

   
    length=sizeof(int)+title.size()*80;
    char *otitle=new char [length];
    unsigned int *ntitle=reinterpret_cast<unsigned int *>(otitle);
    *ntitle=title.size();
    for (i=0; i< title.size(); i++){
      //We need to use a temporary/intermediate string because
      //getTitle(i) returns a copy of the title. Multiple calls
      //to the same element returns two separate copies with different
      //begin() and end()
      std::string tmpTitle=getTitle(i); 
      std::copy(tmpTitle.begin(), tmpTitle.end() , otitle+sizeof(int)+i*80);
    }

    writeFortran(trjout, otitle, length);
    delete otitle;

    //binbuf natm=natom;
    if (mol != NULL){
      natom=mol->getNAtom();
    }
    ibuffer=&natom;
    length=sizeof(int);
    writeFortran(trjout, ibuffer, length);

    if (nfixed > 0){
      std::cerr << "Warning: Fixed atoms has yet to be implemented" << std::endl;
      //for (i=0; i< length; i++){
      //  std::cerr << buffer[i].i << ":";
      //  fixinx.push_back(buffer[i].i);
      //}
    }

  }
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
movq 0xf0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
leaq 0xa23b(%rip), %rsi      # 0x7c4d6
callq 0x5140
cmpl $0x0, %eax
jne 0x7265a
movq 0xe8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x5400
leaq 0x90(%rsp), %rax
movq %rax, 0x88(%rsp)
movl $0x0, 0x78(%rsp)
cmpl $0x15, 0x78(%rsp)
jae 0x722fb
movq 0x88(%rsp), %rax
movl 0x78(%rsp), %ecx
movl $0x0, (%rax,%rcx,4)
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x722d4
movq 0x20(%rsp), %rdi
movq 0x88(%rsp), %rax
movb $0x43, (%rax)
movq 0x88(%rsp), %rax
movb $0x4f, 0x1(%rax)
movq 0x88(%rsp), %rax
movb $0x52, 0x2(%rax)
movq 0x88(%rsp), %rax
movb $0x44, 0x3(%rax)
callq 0x72670
movq 0x20(%rsp), %rdi
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x4(%rax)
callq 0x72680
movq 0x20(%rsp), %rdi
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x8(%rax)
callq 0x72690
movq 0x20(%rsp), %rdi
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0xc(%rax)
callq 0x726b0
movq 0x20(%rsp), %rdi
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x10(%rax)
callq 0x726d0
movq 0x20(%rsp), %rdi
movl %eax, 0xa4(%rsp)
callq 0x726f0
movq 0x20(%rsp), %rdi
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x20(%rax)
callq 0x72710
movq 0x20(%rsp), %rdi
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x24(%rax)
callq 0x72730
movq 0x20(%rsp), %rdi
movq 0x88(%rsp), %rax
movss %xmm0, 0x28(%rax)
callq 0x72750
movq 0x20(%rsp), %rdi
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x2c(%rax)
callq 0x72770
movq 0x20(%rsp), %rdi
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x30(%rax)
callq 0x72790
movq 0x20(%rsp), %rdi
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x34(%rax)
callq 0x727b0
movq 0x20(%rsp), %rdi
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x38(%rax)
callq 0x727d0
movq 0x20(%rsp), %rdi
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x50(%rax)
movl $0x54, 0x7c(%rsp)
movq 0xe8(%rsp), %rsi
movq 0x88(%rsp), %rdx
leaq 0x7c(%rsp), %rcx
callq 0x74e80
movq 0x20(%rsp), %rdi
addq $0xb0, %rdi
callq 0x78e0
imulq $0x50, %rax, %rax
addq $0x4, %rax
movl %eax, 0x7c(%rsp)
movslq 0x7c(%rsp), %rdi
callq 0x5030
movq 0x20(%rsp), %rdi
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x68(%rsp)
addq $0xb0, %rdi
callq 0x78e0
movl %eax, %ecx
movq 0x68(%rsp), %rax
movl %ecx, (%rax)
movl $0x0, 0x78(%rsp)
movq 0x20(%rsp), %rdi
movl 0x78(%rsp), %eax
movq %rax, 0x18(%rsp)
addq $0xb0, %rdi
callq 0x78e0
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jae 0x7258e
movq 0x20(%rsp), %rsi
leaq 0x48(%rsp), %rdi
movq %rdi, 0x10(%rsp)
leaq 0x78(%rsp), %rdx
callq 0x727f0
movq 0x10(%rsp), %rdi
callq 0x5370
movq 0x10(%rsp), %rdi
movq %rax, 0x40(%rsp)
callq 0x5210
movq %rax, 0x38(%rsp)
movq 0x70(%rsp), %rax
movl 0x78(%rsp), %edx
shll $0x4, %edx
movl %edx, %ecx
leal (%rcx,%rcx,4), %ecx
leaq 0x4(%rax,%rcx), %rdx
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x75000
jmp 0x72557
leaq 0x48(%rsp), %rdi
callq 0x5250
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x724ce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x30(%rsp)
movl %eax, 0x2c(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x5250
jmp 0x72662
movq 0x20(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq 0x70(%rsp), %rdx
leaq 0x7c(%rsp), %rcx
callq 0x74fa0
movq 0x70(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x725c9
movq 0x8(%rsp), %rdi
movl $0x1, %esi
callq 0x5420
movq 0x20(%rsp), %rax
cmpq $0x0, 0x28(%rax)
je 0x725f0
movq 0x20(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0x44a80
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0xc8(%rax)
movq 0x20(%rsp), %rdi
movq %rdi, %rax
addq $0xc8, %rax
movq %rax, 0x80(%rsp)
movl $0x4, 0x7c(%rsp)
movq 0xe8(%rsp), %rsi
movq 0x80(%rsp), %rdx
leaq 0x7c(%rsp), %rcx
callq 0x75060
movq 0x20(%rsp), %rax
cmpl $0x0, 0x94(%rax)
jle 0x72658
movq 0x3c89b3(%rip), %rdi    # 0x43aff0
leaq 0xa8cd(%rip), %rsi      # 0x7cf11
callq 0x53f0
movq %rax, %rdi
movq 0x3c8955(%rip), %rsi    # 0x43afa8
callq 0x5460
jmp 0x7265a
addq $0xf8, %rsp
retq
movq 0x30(%rsp), %rdi
callq 0x5720
nopl (%rax)