// File: 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
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x19290
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x64(%rax)
jne 0x17a2a
movq $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movslq -0x34(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x17a28
movl $0x0, -0x38(%rbp)
movl $0x18, %edi
callq 0x8430
movq %rax, -0x78(%rbp)
movq %rax, -0x80(%rbp)
movb $0x1, -0x55(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0x70(%rbp)
callq 0x19390
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x193b0
jmp 0x1767a
movb $0x0, -0x55(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x19420
movq -0x80(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
addq $0xc0, %rdi
callq 0x19440
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
addq $0xc0, %rdi
callq 0x194b0
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x19470
testb $0x1, %al
jne 0x176cd
jmp 0x17a06
leaq -0x60(%rbp), %rdi
callq 0x194e0
movq %rax, %rdi
callq 0x190a0
movl 0x20(%rax), %eax
movl %eax, -0x84(%rbp)
subl $0x8000, %eax           # imm = 0x8000
je 0x17786
jmp 0x176f4
movl -0x84(%rbp), %eax
subl $0xfff6, %eax           # imm = 0xFFF6
je 0x177b8
jmp 0x17707
movl -0x84(%rbp), %eax
subl $0xfff7, %eax           # imm = 0xFFF7
je 0x177f2
jmp 0x1771a
movl -0x84(%rbp), %eax
subl $0xfff8, %eax           # imm = 0xFFF8
je 0x1782a
jmp 0x1772d
movl -0x84(%rbp), %eax
subl $0xfff9, %eax           # imm = 0xFFF9
je 0x17862
jmp 0x17740
movl -0x84(%rbp), %eax
subl $0xfffa, %eax           # imm = 0xFFFA
je 0x1789c
jmp 0x178d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x19420
testb $0x1, -0x55(%rbp)
jne 0x17773
jmp 0x17781
movq -0x80(%rbp), %rdi
movl $0x18, %esi
callq 0x8440
jmp 0x17a61
movq 0x1b833(%rip), %rdi     # 0x32fc0
leaq 0xa249(%rip), %rsi      # 0x219dd
callq 0x8420
movq %rax, %rdi
movq 0x1b7d5(%rip), %rsi     # 0x32f78
callq 0x8480
movq -0x10(%rbp), %rax
movb $0x1, 0xb0(%rax)
jmp 0x179f6
movl $0x8, %edi
callq 0x8030
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x19500
movq -0x18(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x18(%rbp)
jmp 0x179f6
movl $0x4, %edi
callq 0x8030
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
movq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x19500
movq -0x18(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x18(%rbp)
jmp 0x179f6
movl $0x4, %edi
callq 0x8030
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
movq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x19500
movq -0x18(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x18(%rbp)
jmp 0x179f6
movl $0x2, %edi
callq 0x8030
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movw (%rcx), %cx
movw %cx, (%rax)
movq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x19500
movq -0x18(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x18(%rbp)
jmp 0x179f6
movl $0x1, %edi
callq 0x8030
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movb (%rcx), %cl
movb %cl, (%rax)
movq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x19500
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x179f6
leaq -0x60(%rbp), %rdi
callq 0x194e0
movq %rax, %rdi
callq 0x190a0
cmpl $0x0, 0x20(%rax)
jle 0x179d2
leaq -0x60(%rbp), %rdi
callq 0x194e0
movq %rax, %rdi
callq 0x190a0
cmpl $0x7fd, 0x20(%rax)      # imm = 0x7FD
jg 0x179d2
leaq -0x60(%rbp), %rdi
callq 0x194e0
movq %rax, %rdi
callq 0x190a0
movl 0x20(%rax), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x8030
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xa0(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x194e0
movq %rax, %rdi
callq 0x190a0
movq -0xa0(%rbp), %rdi
movl 0x20(%rax), %eax
addl $0x1, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x8240
movq -0x30(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x194e0
movq %rax, %rdi
callq 0x190a0
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rsi
movslq 0x20(%rax), %rdx
callq 0x8380
movq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x19500
leaq -0x60(%rbp), %rdi
callq 0x194e0
movq %rax, %rdi
callq 0x190a0
movl 0x20(%rax), %ecx
movq -0x18(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
jmp 0x179f4
movq 0x1b5e7(%rip), %rdi     # 0x32fc0
leaq 0xa028(%rip), %rsi      # 0x21a08
callq 0x8420
movq %rax, %rdi
movq 0x1b589(%rip), %rsi     # 0x32f78
callq 0x8480
jmp 0x179f6
jmp 0x179f8
leaq -0x60(%rbp), %rdi
callq 0x19570
jmp 0x176a3
movq -0x10(%rbp), %rdi
addq $0xd8, %rdi
leaq -0x40(%rbp), %rsi
callq 0x19590
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1762b
jmp 0x17a2c
jmp 0x17a2c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movl %eax, %esi
callq 0x15210
movq -0x10(%rbp), %rdi
callq 0x15460
movq -0x10(%rbp), %rdi
callq 0x15460
movb $0x1, %al
andb $0x1, %al
addq $0xa0, %rsp
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x87c0
nopw (%rax,%rax)