// File: maplechori[P]stata-reader-writer/State.cpp
// Function: OpenVarTypes::process(Context&)
bool OpenVarTypes::process(Context &ctx)
{
  char *ctxbuf = (char *)ctx.advance();
  int curr = 0;
  StataVariablesImpl<double> *sta_double;
  StataVariablesImpl<float> *sta_float;
  StataVariablesImpl<long> *sta_long;
  StataVariablesImpl<short> *sta_integer;
  StataVariablesImpl<int8_t> *sta_byte;
  StataVariablesImpl<string> *sta_char;

  if (ctx.hdr.fileByteorder == LSF)
  {

    while (*ctxbuf != '<')
    {
      int stataType = GetLSF<unsigned int>(ctxbuf, 2);

      switch (stataType)
      {
      case ST_STRL:
        cout << "STRL" << endl;

        // handle STRLs
        break;
      case ST_DOUBLE:
        sta_double = new StataVariablesImpl<double>();
        sta_double->type = stataType;
        ctx.vList.push_back(boost::shared_ptr<StataVariables>(sta_double));
        break;
      case ST_FLOAT:
        sta_float = new StataVariablesImpl<float>();
        sta_float->type = stataType;
        ctx.vList.push_back(boost::shared_ptr<StataVariables>(sta_float));
        break;
      case ST_LONG:
        sta_long = new StataVariablesImpl<long>();
        sta_long->type = stataType;
        ctx.vList.push_back(boost::shared_ptr<StataVariables>(sta_long));
        break;
      case ST_INT:
        sta_integer = new StataVariablesImpl<short>();
        sta_integer->type = stataType;
        ctx.vList.push_back(boost::shared_ptr<StataVariables>(sta_integer));
        break;
      case ST_BYTE:
        sta_byte = new StataVariablesImpl<int8_t>();
        sta_byte->type = stataType;
        ctx.vList.push_back(boost::shared_ptr<StataVariables>(sta_byte));
        break;
      default:
        if (stataType > 0 && stataType <= 2045)
        {
          sta_char = new StataVariablesImpl<string>();
          sta_char->type = stataType;
          ctx.vList.push_back(boost::shared_ptr<StataVariables>(sta_char));
        }
        else
          cout << "UNKNOWN TYPE " << hex << stataType << endl;
        break;
      }

      ctxbuf += 2;
      curr++;
    }
  }
  else
  {
    // not implemented yet
  }

  ctx.advance();
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x15460
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x64(%rax)
jne 0x16b81
jmp 0x165e7
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3c, %eax
je 0x16b7f
movq -0x18(%rbp), %rdi
movl $0x2, %esi
callq 0x18690
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0xcc(%rbp)
subl $0x8000, %eax           # imm = 0x8000
je 0x16678
jmp 0x1661a
movl -0xcc(%rbp), %eax
subl $0xfff6, %eax           # imm = 0xFFF6
je 0x1669f
jmp 0x16629
movl -0xcc(%rbp), %eax
subl $0xfff7, %eax           # imm = 0xFFF7
je 0x16756
jmp 0x1663c
movl -0xcc(%rbp), %eax
subl $0xfff8, %eax           # imm = 0xFFF8
je 0x16816
jmp 0x1664f
movl -0xcc(%rbp), %eax
subl $0xfff9, %eax           # imm = 0xFFF9
je 0x168d6
jmp 0x16662
movl -0xcc(%rbp), %eax
subl $0xfffa, %eax           # imm = 0xFFFA
je 0x16996
jmp 0x16a56
movq 0x1c941(%rip), %rdi     # 0x32fc0
leaq 0xb357(%rip), %rsi      # 0x219dd
callq 0x8420
movq %rax, %rdi
movq 0x1c8e3(%rip), %rsi     # 0x32f78
callq 0x8480
jmp 0x16b65
movl $0x98, %edi
callq 0x8430
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd8(%rbp)
callq 0x18ab0
jmp 0x166bd
movq -0xd8(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0x54(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x10(%rbp), %rax
addq $0xc0, %rax
movq %rax, -0xe8(%rbp)
movq -0x28(%rbp), %rsi
leaq -0x78(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x18b20
movq -0xe8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x18af0
jmp 0x1670c
leaq -0x78(%rbp), %rdi
callq 0x18bb0
jmp 0x16b65
movq -0xd8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movl $0x98, %esi
callq 0x8440
jmp 0x16b99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x18bb0
jmp 0x16b99
movl $0x98, %edi
callq 0x8430
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xf0(%rbp)
callq 0x18bd0
jmp 0x16774
movq -0xf0(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x54(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x10(%rbp), %rax
addq $0xc0, %rax
movq %rax, -0x100(%rbp)
movq -0x30(%rbp), %rsi
leaq -0x88(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x18c10
movq -0x100(%rbp), %rdi
movq -0xf8(%rbp), %rsi
callq 0x18af0
jmp 0x167c6
leaq -0x88(%rbp), %rdi
callq 0x18bb0
jmp 0x16b65
movq -0xf0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movl $0x98, %esi
callq 0x8440
jmp 0x16b99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x18bb0
jmp 0x16b99
movl $0x98, %edi
callq 0x8430
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x108(%rbp)
callq 0x18ca0
jmp 0x16834
movq -0x108(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x54(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x10(%rbp), %rax
addq $0xc0, %rax
movq %rax, -0x118(%rbp)
movq -0x38(%rbp), %rsi
leaq -0x98(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x18ce0
movq -0x118(%rbp), %rdi
movq -0x110(%rbp), %rsi
callq 0x18af0
jmp 0x16886
leaq -0x98(%rbp), %rdi
callq 0x18bb0
jmp 0x16b65
movq -0x108(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movl $0x98, %esi
callq 0x8440
jmp 0x16b99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x18bb0
jmp 0x16b99
movl $0x98, %edi
callq 0x8430
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x120(%rbp)
callq 0x18d70
jmp 0x168f4
movq -0x120(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0x54(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x10(%rbp), %rax
addq $0xc0, %rax
movq %rax, -0x130(%rbp)
movq -0x40(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x18db0
movq -0x130(%rbp), %rdi
movq -0x128(%rbp), %rsi
callq 0x18af0
jmp 0x16946
leaq -0xa8(%rbp), %rdi
callq 0x18bb0
jmp 0x16b65
movq -0x120(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movl $0x98, %esi
callq 0x8440
jmp 0x16b99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x18bb0
jmp 0x16b99
movl $0x98, %edi
callq 0x8430
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x138(%rbp)
callq 0x18e40
jmp 0x169b4
movq -0x138(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x54(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x10(%rbp), %rax
addq $0xc0, %rax
movq %rax, -0x148(%rbp)
movq -0x48(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x18e80
movq -0x148(%rbp), %rdi
movq -0x140(%rbp), %rsi
callq 0x18af0
jmp 0x16a06
leaq -0xb8(%rbp), %rdi
callq 0x18bb0
jmp 0x16b65
movq -0x138(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movl $0x98, %esi
callq 0x8440
jmp 0x16b99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x18bb0
jmp 0x16b99
cmpl $0x0, -0x54(%rbp)
jle 0x16b27
cmpl $0x7fd, -0x54(%rbp)     # imm = 0x7FD
jg 0x16b27
movl $0xb0, %edi
callq 0x8430
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x150(%rbp)
callq 0x18f10
jmp 0x16a8b
movq -0x150(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x54(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x10(%rbp), %rax
addq $0xc0, %rax
movq %rax, -0x160(%rbp)
movq -0x50(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x18f60
movq -0x160(%rbp), %rdi
movq -0x158(%rbp), %rsi
callq 0x18af0
jmp 0x16add
leaq -0xc8(%rbp), %rdi
callq 0x18bb0
jmp 0x16b63
movq -0x150(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movl $0xb0, %esi
callq 0x8440
jmp 0x16b99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x18bb0
jmp 0x16b99
movq 0x1c492(%rip), %rdi     # 0x32fc0
leaq 0xaead(%rip), %rsi      # 0x219e2
callq 0x8420
movq %rax, %rdi
leaq 0x24ac(%rip), %rsi      # 0x18ff0
callq 0x8130
movq %rax, %rdi
movl -0x54(%rbp), %esi
callq 0x8780
movq %rax, %rdi
movq 0x1c41a(%rip), %rsi     # 0x32f78
callq 0x8480
jmp 0x16b65
movq -0x18(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x18(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x165e7
jmp 0x16b83
jmp 0x16b83
movq -0x10(%rbp), %rdi
callq 0x15460
movb $0x1, %al
andb $0x1, %al
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x87c0
nopw %cs:(%rax,%rax)