// File: /workspace/github_cpp_makefile_star2_O2/maplechori[P]stata-reader-writer/State.cpp
// Function: OpenData::process(Context&)
bool OpenData::process(Context &ctx)
{
  char *ctxbuf = (char *)ctx.getCursor(), *start_ctx = NULL;
  int curr = 0, sz = 0;
  
  start_ctx = ctxbuf;
  
  if (ctx.hdr.fileByteorder == LSF)
  {
    char * buf = NULL;
  
    for (int j = 0; j < ctx.hdr.observations; j++)
    {
      int k = 0;
      vector<char *> * v = new vector<char *>(0);
      for (vector<boost::shared_ptr<StataVariables> >::iterator it = ctx.vList.begin(); it != ctx.vList.end(); ++it)
      {

        switch ((*it)->type)
        {
        case ST_STRL:
          cout << "STRL" << endl;
          ctx.strls = true;
          // handle STRLs
          break;
        case ST_DOUBLE:
          buf = new char[8];
          memcpy(buf, ctxbuf, 8); 
          v->push_back(buf);   
          ctxbuf += 8;
          break;
        case ST_FLOAT:
          buf = new char[4];
          memcpy(buf, ctxbuf, 4); 
          v->push_back(buf);
          ctxbuf += 4;
          break;
        case ST_LONG:
          buf = new char[4];
          memcpy(buf, ctxbuf, 4); 
          v->push_back(buf);
          ctxbuf += 4;
          break;
        case ST_INT:
          buf = new char[2];
          memcpy(buf, ctxbuf, 2); 
          v->push_back(buf);
          ctxbuf += 2;
          break;
        case ST_BYTE:
          buf = new char[1];
          memcpy(buf, ctxbuf,1); 
          v->push_back(buf);          
          ctxbuf++;
          break;
        default:
          if ((*it)->type > 0 && (*it)->type <= 2045)
          {
            buf = new char[(*it)->type + 1];
            memset(buf, 0, (*it)->type + 1);
            memcpy(buf, ctxbuf,(*it)->type);
            v->push_back(buf);
            
            ctxbuf += (*it)->type;
          }
          else
            cout << "UNKNOWN TYPE" << endl;
          break;
        }
      }

      ctx.vData.push_back(v);    
    }
  }
  else
  {
    // not implemented yet
  }
 
  ctx.advanceData(ctxbuf-start_ctx);
  ctx.advance();
  ctx.advance();  
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq 0x100(%rsi), %rax
cmpl $0x0, 0x64(%rsi)
movq %rax, %r12
jne 0xb8a3
leaq -0x30(%rbp), %r15
andq $0x0, (%r15)
leaq 0xd8(%rbx), %rcx
movq %rcx, -0x50(%rbp)
xorl %ecx, %ecx
movq %rax, %r12
leaq 0x3e01(%rip), %r14      # 0xf4a4
movq %rax, -0x48(%rbp)
cmpq %rcx, 0x18(%rbx)
jbe 0xb89f
movq %rcx, -0x58(%rbp)
pushq $0x18
popq %rdi
callq 0x7310
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
leaq -0x39(%rbp), %rdx
callq 0xc162
movq %r13, -0x38(%rbp)
movq 0xc0(%rbx), %r13
cmpq 0xc8(%rbx), %r13
je 0xb886
movq %r13, %rdi
callq 0xc136
movl 0x20(%rax), %eax
leal -0xfff6(%rax), %ecx
cmpl $0x4, %ecx
ja 0xb72c
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
pushq $0x4
popq %rdi
callq 0x7030
movq %rax, -0x30(%rbp)
movl (%r12), %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0xc1b4
addq $0x4, %r12
jmp 0xb87d
cmpl $0x8000, %eax           # imm = 0x8000
jne 0xb75a
movq 0xc886(%rip), %rdi      # 0x17fc0
leaq 0x3fef(%rip), %rsi      # 0xf730
callq 0x7300
movq %rax, %rdi
callq 0x7160
movb $0x1, 0xb0(%rbx)
jmp 0xb87d
movq %r13, %rdi
callq 0xc136
cmpl $0x0, 0x20(%rax)
jle 0xb862
movq %r13, %rdi
callq 0xc136
cmpl $0x7fd, 0x20(%rax)      # imm = 0x7FD
jg 0xb862
movq %r13, %rdi
callq 0xc136
movslq 0x20(%rax), %rdi
incq %rdi
callq 0x7030
movq %rax, %r15
movq %rax, -0x30(%rbp)
movq %r13, %rdi
callq 0xc136
movslq 0x20(%rax), %rdx
incq %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x71d0
movq -0x30(%rbp), %r15
movq %r13, %rdi
callq 0xc136
movslq 0x20(%rax), %rdx
movq %r15, %rdi
leaq -0x30(%rbp), %r15
movq %r12, %rsi
callq 0x7280
movq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0xc1b4
movq %r13, %rdi
callq 0xc136
movslq 0x20(%rax), %rax
addq %rax, %r12
jmp 0xb87d
pushq $0x1
popq %rdi
callq 0x7030
movq %rax, -0x30(%rbp)
movb (%r12), %cl
movb %cl, (%rax)
movq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0xc1b4
incq %r12
jmp 0xb87d
pushq $0x8
popq %rdi
callq 0x7030
movq %rax, -0x30(%rbp)
movq (%r12), %rcx
movq %rcx, (%rax)
movq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0xc1b4
addq $0x8, %r12
jmp 0xb87d
pushq $0x2
popq %rdi
callq 0x7030
movq %rax, -0x30(%rbp)
movzwl (%r12), %ecx
movw %cx, (%rax)
movq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0xc1b4
addq $0x2, %r12
jmp 0xb87d
movq 0xc757(%rip), %rdi      # 0x17fc0
leaq 0x3eeb(%rip), %rsi      # 0xf75b
callq 0x7300
movq %rax, %rdi
callq 0x7160
addq $0x10, %r13
jmp 0xb6d9
movq -0x50(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0xc1d8
movq -0x58(%rbp), %rcx
incq %rcx
jmp 0xb6a7
movq -0x48(%rbp), %rax
subl %eax, %r12d
movq %rbx, %rdi
movl %r12d, %esi
callq 0xa730
movq %rbx, %rdi
callq 0xa876
movq %rbx, %rdi
callq 0xa876
movb $0x1, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r13, %rdi
callq 0x7320
movq %rbx, %rdi
callq 0x75b0