// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x41e0(%rip), %rsi      # 0x2e502
callq 0x4100
testl %eax, %eax
jne 0x2a4e0
xorl %r14d, %r14d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4310
cmpq $0x15, %r14
je 0x2a34f
andl $0x0, 0x40(%rsp,%r14,4)
incq %r14
jmp 0x2a33e
movq %rsp, %rax
movl $0x15, (%rax)
leaq 0x40(%rsp), %rdx
movl $0x44524f43, (%rdx)     # imm = 0x44524F43
movups 0x78(%rbx), %xmm0
movups %xmm0, 0x4(%rdx)
movl 0x88(%rbx), %eax
movl %eax, 0x14(%rdx)
movq 0x90(%rbx), %rax
movq %rax, 0x20(%rdx)
movss 0x98(%rbx), %xmm0
movss %xmm0, 0x28(%rdx)
movups 0x9c(%rbx), %xmm0
movups %xmm0, 0x2c(%rdx)
movl 0xac(%rbx), %eax
movl %eax, 0x50(%rdx)
leaq 0x4(%rsp), %r12
movl $0x54, (%r12)
movq %rbx, %rdi
movq %r15, 0x10(%rsp)
movq %r15, %rsi
movq %r12, %rcx
callq 0x2ba86
movq 0xb0(%rbx), %rbp
movq 0xb8(%rbx), %r15
movq %r15, %r14
subq %rbp, %r14
shrq $0x5, %r14
imull $0x50, %r14d, %eax
orl $0x4, %eax
movl %eax, (%r12)
movslq %eax, %rdi
callq 0x4030
movl %r14d, (%rax)
movq %rax, 0x8(%rsp)
addq $0x4, %rax
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
leaq 0x20(%rsp), %r13
xorl %r12d, %r12d
subq %rbp, %r15
movl %r12d, (%rsp)
movl %r12d, %eax
sarq $0x5, %r15
cmpq %rax, %r15
jbe 0x2a463
movq %r13, %rdi
movq %rbx, %rsi
movq %rsp, %rdx
callq 0x2a554
movq 0x28(%rsp), %rdx
testq %rdx, %rdx
je 0x2a444
movq 0x20(%rsp), %rsi
movl %r14d, %edi
addq 0x18(%rsp), %rdi
callq 0x44e0
movq %r13, %rdi
callq 0x4170
incl %r12d
movq 0xb0(%rbx), %rbp
movq 0xb8(%rbx), %r15
addl $0x50, %r14d
jmp 0x2a407
leaq 0x4(%rsp), %rcx
movq %rbx, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
movq 0x8(%rsp), %r15
movq %r15, %rdx
callq 0x2bb64
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x4330
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x2a49f
callq 0x1b6a8
movl %eax, 0xc8(%rbx)
leaq 0xc8(%rbx), %rdx
leaq 0x4(%rsp), %rcx
movl $0x4, (%rcx)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2bbae
cmpl $0x0, 0x94(%rbx)
jle 0x2a4e0
movq 0x3bab24(%rip), %rdi    # 0x3e4ff0
leaq 0x4a86(%rip), %rsi      # 0x2ef59
callq 0x4300
movq %rax, %rdi
callq 0x4180
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq