// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq %rdi, %rbx
leaq 0x50a2(%rip), %rsi      # 0x3d59a
callq 0x40e0
testl %eax, %eax
jne 0x3870e
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x42c0
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x30(%rdx)
movaps %xmm0, 0x40(%rdx)
movl %r14d, 0x50(%rdx)
movl $0x15, 0x8(%rsp)
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 0xc(%rsp), %r15
movl $0x54, (%r15)
movq %rbx, %rdi
movq %r12, 0x18(%rsp)
movq %r12, %rsi
movq %r15, %rcx
callq 0x39f1c
movq 0xb8(%rbx), %r12
subq 0xb0(%rbx), %r12
shrq $0x5, %r12
movl %r12d, %eax
shll $0x4, %eax
leal (%rax,%rax,4), %eax
addl $0x4, %eax
movl %eax, (%r15)
movslq %eax, %rdi
callq 0x4030
movq %rax, 0x10(%rsp)
movl %r12d, (%rax)
movl %r14d, 0x8(%rsp)
movq 0xb8(%rbx), %rax
cmpq 0xb0(%rbx), %rax
je 0x38668
movq 0x10(%rsp), %r15
addq $0x4, %r15
movl $0x1, %r12d
leaq 0x8(%rsp), %r13
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x38782
movq 0x20(%rsp), %rbp
movq 0x28(%rsp), %rdx
testq %rdx, %rdx
je 0x38628
movl %r14d, %edi
addq %r15, %rdi
movq %rbp, %rsi
callq 0x44c0
leaq 0x30(%rsp), %rax
cmpq %rax, %rbp
je 0x38642
movq 0x30(%rsp), %rsi
incq %rsi
movq %rbp, %rdi
callq 0x42e0
movl %r12d, 0x8(%rsp)
movl %r12d, %eax
movq 0xb8(%rbx), %rcx
subq 0xb0(%rbx), %rcx
sarq $0x5, %rcx
addl $0x50, %r14d
incl %r12d
cmpq %rax, %rcx
ja 0x385fb
leaq 0xc(%rsp), %rcx
movq %rbx, %rdi
movq 0x18(%rsp), %r14
movq %r14, %rsi
movq 0x10(%rsp), %r15
movq %r15, %rdx
callq 0x39fe2
movl $0x1, %esi
movq %r15, %rdi
callq 0x42e0
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x386a6
callq 0x2577c
movl %eax, 0xc8(%rbx)
leaq 0xc8(%rbx), %rdx
leaq 0xc(%rsp), %rcx
movl $0x4, (%rcx)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3a024
cmpl $0x0, 0x94(%rbx)
jle 0x3870e
movq 0x3b891d(%rip), %rbx    # 0x3f0ff0
leaq 0x595f(%rip), %rsi      # 0x3e039
movl $0x2e, %edx
movq %rbx, %rdi
callq 0x4340
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x42a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41f0
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq