// File: /workspace/github_cpp_makefile_star2_O3/nicklausw[P]snesasm/snesasm.cpp
// Function: lexer()
int lexer()
{
  // the crazy part, tokens
  unsigned int counter = 0; // counter again
  int current_token = 0; // vector location value
  bool ct_used = false; // current token usage
  
  // we need the token vector big enough
  // for the first token, obviously
  tokens.resize(1);
  
  
  while (counter < ins.length()) {
    // handle newlines
    if (ins[counter] == '\n') {
      if (ct_used == false) {
        counter++;
        current_token++;
        tokens.resize((current_token+2)*sizeof(token));
        tokens[current_token].token_type = tkNL;
      } else {
        // handle vector size
        tokens.resize((current_token+1)*sizeof(token));
        
        // get ready for another loop
        ct_used = false;
        counter++;
        current_token++;
        tokens.resize((current_token+2)*sizeof(token));
        tokens[current_token].token_type = tkNL;
        current_token++;
      }
      continue;
    }
    
    if (ins[counter] == ',') {
      if (ct_used == false) {
        counter++;
        current_token++;
        tokens.resize((current_token+2)*sizeof(token));
        tokens[current_token].token_type = tkCOMMA;
      } else {
        // handle vector size
        tokens.resize((current_token+1)*sizeof(token));
        
        // get ready for another loop
        ct_used = false;
        counter++;
        current_token++;
        tokens.resize((current_token+2)*sizeof(token));
        tokens[current_token].token_type = tkCOMMA;
        current_token++;
      }
      continue;
    }
    
    if (ins[counter] == ' ') {
      if (ct_used == false) {
        counter++;
      } else {
        // handle vector size
        tokens.resize((current_token+1)*sizeof(token));
        
        // get ready for another loop
        ct_used = false;
        counter++;
        current_token++;
      }
      continue;
    }
    
    
    // no newline means do a token
    
    if (ins[counter] == 0x0D) {
      // CR LF
      counter++;
      continue;
    }
    
    
    if (ins[counter] == '.') {
      // directive
      ct_used = true;
      tokens[current_token].token_type = tkDIR;
      counter++; // skip period
      
      counter = append_token(counter, current_token);
      
      // no case sensitivity
      tokens[current_token].token_i = str_tolower(tokens[current_token].token_i);
      
      // no need for a counter++ here, it's handled above.
      continue;
    } else if (ins[counter] == '\"') {
      // quote!
      ct_used = true;
      tokens[current_token].token_type = tkQUOTE;
      
      // quotes get their own append function
      
      unsigned int str_length = ins.length() - 1;
      
      counter++;
  
      while (ins[counter] != '\"') {
        if (counter == str_length) {
          cerr << "error: unclosed quote\n";
          return fail;
        }
    
        tokens[current_token].token_i.append(string(1, ins[counter]));
        counter++;
      }
      
      tokens[current_token].token_i.append(string(1, '\"'));
      
      counter++;
      
      
      // no need for a counter++ here, it's handled above.
      continue;
    } else if (isalpha(ins[counter])) {
      // opcode
      ct_used = true;
      tokens[current_token].token_type = tkOP;
      
      counter = append_token(counter, current_token);
      
      if (tokens[current_token].token_i[tokens[current_token].token_i.length() - 1] == ':')
        tokens[current_token].token_type = tkLB; // it's a label
      
      // no need for a counter++ here, it's handled above.
      continue;
    } else if (isdigit(ins[counter]) || ins[counter] == '$' || ins[counter] == '%' || ins[counter] == '-') {
      // number
      ct_used = true;
      tokens[current_token].token_type = tkNUM;
      
      counter = append_token(counter, current_token);
      
      // no case sensitivity
      tokens[current_token].token_i = str_tolower(tokens[current_token].token_i);
      
      // no need for a counter++ here, it's handled above.
      continue;
    } else if (ins[counter] == '#' || ins[counter] == '(') {
      // opcode arg
      ct_used = true;
      tokens[current_token].token_type = tkARG;
      
      counter = append_token(counter, current_token);
      
      // no case sensitivity
      tokens[current_token].token_i = str_tolower(tokens[current_token].token_i);
      
      // no need for a counter++ here, it's handled above.
      continue;
    } else {
      // unknown
      ct_used = true;
      tokens[current_token].token_type = tkUNDEF;
      
      counter = append_token(counter, current_token);
      
      // no need for a counter++ here, it's handled above.
      continue;
    }
    
    // be sure to move on to next symbol.
    counter++;
  }
  
  return success;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
leaq 0x79b4(%rip), %rdi      # 0xc1c8
movl $0x1, %esi
callq 0x7a1a
movq 0x79cb(%rip), %rbx      # 0xc1f0
testq %rbx, %rbx
je 0x4d48
leaq 0x18(%rsp), %r13
xorl %eax, %eax
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x79a4(%rip), %rcx      # 0xc1e8
movzbl (%rcx,%rax), %r15d
cmpl $0x21, %r15d
jg 0x4893
cmpl $0xa, %r15d
je 0x494a
cmpl $0xd, %r15d
je 0x488c
cmpl $0x20, %r15d
jne 0x4a54
testb $0x1, %r14b
je 0x488c
incl %r12d
movslq %r12d, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
leaq 0x7944(%rip), %rdi      # 0xc1c8
callq 0x7a1a
xorl %r14d, %r14d
incl %ebp
jmp 0x4c5e
cmpl $0x22, %r15d
je 0x4982
cmpl $0x2c, %r15d
je 0x4abe
cmpl $0x2e, %r15d
jne 0x4a54
movl 0x78ad(%rip), %eax      # 0xc164
movslq %r12d, %rcx
movq 0x7907(%rip), %rdx      # 0xc1c8
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rbx
movl %eax, (%rdx,%rbx)
incl %ebp
movl %ebp, %edi
movl %r12d, %esi
callq 0x6f46
movl %eax, %ebp
movq 0x78e7(%rip), %rax      # 0xc1c8
leaq 0x78(%rsp), %rcx
movq %rcx, 0x68(%rsp)
movq 0x8(%rax,%rbx), %rsi
movq 0x10(%rax,%rbx), %rdx
addq %rsi, %rdx
leaq 0x68(%rsp), %r15
movq %r15, %rdi
callq 0x7ace
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x7012
movq 0x78ac(%rip), %rax      # 0xc1c8
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
movq %r14, %rsi
callq 0x21d0
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x493b
callq 0x2140
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
jmp 0x4bc2
leal 0x1(%r12), %ebx
movslq %ebx, %r15
testb $0x1, %r14b
jne 0x4bd4
movslq %r12d, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
addq $0x78, %rsi
leaq 0x7856(%rip), %rdi      # 0xc1c8
callq 0x7a1a
movl 0x77f7(%rip), %eax      # 0xc174
jmp 0x4af1
movl 0x77f4(%rip), %eax      # 0xc17c
movslq %r12d, %rdx
movq 0x7836(%rip), %r15      # 0xc1c8
leaq (%rdx,%rdx,4), %r14
movl %eax, (%r15,%r14,8)
leal 0x1(%rbp), %eax
movb (%rcx,%rax), %al
cmpb $0x22, %al
je 0x4a0d
addl $-0x2, %ebx
cmpl %ebp, %ebx
je 0x4d4c
movq 0x7812(%rip), %r15      # 0xc1c8
movq %r13, 0x8(%rsp)
movsbl %al, %edx
movl $0x1, %esi
leaq 0x8(%rsp), %rdi
callq 0x2190
leaq (%r15,%r14,8), %rdi
addq $0x8, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x2060
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x49f3
callq 0x2140
leal 0x2(%rbp), %eax
movq 0x77eb(%rip), %rcx      # 0xc1e8
movb (%rcx,%rax), %al
incl %ebp
cmpb $0x22, %al
jne 0x49a7
movq 0x77bb(%rip), %r15      # 0xc1c8
movq %r13, 0x8(%rsp)
movl $0x1, %esi
leaq 0x8(%rsp), %rdi
movl $0x22, %edx
callq 0x2190
leaq (%r15,%r14,8), %rdi
addq $0x8, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x2060
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x4a4c
callq 0x2140
addl $0x2, %ebp
jmp 0x4bcc
movsbl %r15b, %r14d
movl %r14d, %edi
callq 0x2230
testl %eax, %eax
je 0x4b04
movl 0x76fa(%rip), %eax      # 0xc168
movslq %r12d, %rcx
movq 0x7750(%rip), %rdx      # 0xc1c8
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rbx
movl %eax, (%rdx,%rbx)
movl %ebp, %edi
movl %r12d, %esi
callq 0x6f46
movl %eax, %ebp
movq 0x7732(%rip), %rax      # 0xc1c8
movq 0x8(%rax,%rbx), %rcx
movq 0x10(%rax,%rbx), %rdx
movb $0x1, %r14b
cmpb $0x3a, -0x1(%rcx,%rdx)
jne 0x4c5e
addq %rbx, %rax
movl 0x76b5(%rip), %ecx      # 0xc16c
movl %ecx, (%rax)
jmp 0x4c5e
leal 0x1(%r12), %ebx
movslq %ebx, %r15
testb $0x1, %r14b
jne 0x4c0d
movslq %r12d, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
addq $0x78, %rsi
leaq 0x76e2(%rip), %rdi      # 0xc1c8
callq 0x7a1a
movl 0x7687(%rip), %eax      # 0xc178
movq 0x76d0(%rip), %rcx      # 0xc1c8
leaq (%r15,%r15,4), %rdx
movl %eax, (%rcx,%rdx,8)
jmp 0x4c59
addl $-0x30, %r14d
cmpl $0xa, %r14d
jb 0x4b30
cmpb $0x2d, %r15b
ja 0x4d20
movzbl %r15b, %eax
movabsq $0x203000000000, %rcx   # imm = 0x203000000000
btq %rax, %rcx
jae 0x4c75
movl 0x762a(%rip), %eax      # 0xc160
movslq %r12d, %rcx
movq 0x7688(%rip), %rdx      # 0xc1c8
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rbx
movl %eax, (%rdx,%rbx)
movl %ebp, %edi
movl %r12d, %esi
callq 0x6f46
movl %eax, %ebp
movq 0x766a(%rip), %rax      # 0xc1c8
leaq 0x58(%rsp), %rcx
movq %rcx, 0x48(%rsp)
movq 0x8(%rax,%rbx), %rsi
movq 0x10(%rax,%rbx), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x7ace
leaq 0x8(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x7012
movq 0x762f(%rip), %rax      # 0xc1c8
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x21d0
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x4bb8
callq 0x2140
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x4bcc
callq 0x2140
movb $0x1, %r14b
jmp 0x4c5e
shlq $0x3, %r15
leaq (%r15,%r15,4), %r14
leaq 0x75e5(%rip), %rbx      # 0xc1c8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7a1a
movslq %r12d, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
addq $0x78, %rsi
movq %rbx, %rdi
callq 0x7a1a
movl 0x7569(%rip), %eax      # 0xc174
jmp 0x4c44
shlq $0x3, %r15
leaq (%r15,%r15,4), %r14
leaq 0x75ac(%rip), %rbx      # 0xc1c8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7a1a
movslq %r12d, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
addq $0x78, %rsi
movq %rbx, %rdi
callq 0x7a1a
movl 0x7534(%rip), %eax      # 0xc178
movq 0x757d(%rip), %rcx      # 0xc1c8
movl %eax, (%rcx,%r14)
addl $0x2, %r12d
xorl %r14d, %r14d
movl %r12d, %ebx
incl %ebp
movl %ebx, %r12d
movl %ebp, %eax
movq 0x7589(%rip), %rbx      # 0xc1f0
cmpq %rax, %rbx
ja 0x483d
jmp 0x4d48
movabsq $0x10800000000, %rcx    # imm = 0x10800000000
btq %rax, %rcx
jae 0x4d20
movl 0x74e1(%rip), %eax      # 0xc170
movslq %r12d, %rcx
movq 0x752f(%rip), %rdx      # 0xc1c8
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rbx
movl %eax, (%rdx,%rbx)
movl %ebp, %edi
movl %r12d, %esi
callq 0x6f46
movl %eax, %ebp
movq 0x7511(%rip), %rax      # 0xc1c8
leaq 0x38(%rsp), %rcx
movq %rcx, 0x28(%rsp)
movq 0x8(%rax,%rbx), %rsi
movq 0x10(%rax,%rbx), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x7ace
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x7012
movq 0x74d6(%rip), %rax      # 0xc1c8
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
movq %r14, %rsi
callq 0x21d0
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x4d11
callq 0x2140
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
jmp 0x4bc2
movl 0x74ba(%rip), %eax      # 0xc1e0
movslq %r12d, %rcx
movq 0x7498(%rip), %rdx      # 0xc1c8
leaq (%rcx,%rcx,4), %rcx
movl %eax, (%rdx,%rcx,8)
movl %ebp, %edi
movl %r12d, %esi
callq 0x6f46
movl %eax, %ebp
jmp 0x4bcc
xorl %eax, %eax
jmp 0x4d69
movq 0x729d(%rip), %rdi      # 0xbff0
leaq 0x46d9(%rip), %rsi      # 0x9433
movl $0x16, %edx
callq 0x2180
movl $0x1, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4d84
jmp 0x4d7f
movq %rax, %rbx
jmp 0x4d96
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x4d96
callq 0x2140
movq %rbx, %rdi
callq 0x2250