// File: /workspace/github_cpp_makefile_star2_O1/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 0x79ba(%rip), %rdi      # 0xc1c8
movl $0x1, %esi
callq 0x7b2c
movq 0x79d1(%rip), %rbx      # 0xc1f0
testq %rbx, %rbx
je 0x4db1
leaq 0x18(%rsp), %r12
xorl %eax, %eax
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x79aa(%rip), %rcx      # 0xc1e8
movzbl (%rcx,%rax), %r15d
cmpl $0x21, %r15d
jg 0x488d
cmpl $0xa, %r15d
je 0x4946
cmpl $0xd, %r15d
je 0x4885
cmpl $0x20, %r15d
jne 0x49b5
testb $0x1, %r14b
je 0x4885
incl %ebp
movslq %ebp, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
leaq 0x794b(%rip), %rdi      # 0xc1c8
callq 0x7b2c
xorl %r14d, %r14d
incl %r13d
jmp 0x4cc4
cmpl $0x22, %r15d
je 0x4979
cmpl $0x2c, %r15d
je 0x4a20
cmpl $0x2e, %r15d
jne 0x49b5
movl 0x78b3(%rip), %eax      # 0xc164
movslq %ebp, %rcx
movq 0x790d(%rip), %rdx      # 0xc1c8
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rbx
movl %eax, (%rdx,%rbx)
incl %r13d
movl %r13d, %edi
movl %ebp, %esi
callq 0x706b
movl %eax, %r13d
movq 0x78eb(%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 0x7be0
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x7132
movq 0x78b0(%rip), %rax      # 0xc1c8
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
movq %r14, %rsi
callq 0x21d0
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x4937
callq 0x2140
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
jmp 0x4bc9
leal 0x1(%rbp), %ebx
testb $0x1, %r14b
jne 0x4bdb
movslq %ebp, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
addq $0x78, %rsi
leaq 0x785f(%rip), %rdi      # 0xc1c8
callq 0x7b2c
movl 0x7800(%rip), %eax      # 0xc174
jmp 0x4a4e
movl 0x77fd(%rip), %eax      # 0xc17c
movslq %ebp, %rcx
movq 0x783f(%rip), %rdx      # 0xc1c8
leaq (%rcx,%rcx,4), %r14
movl %eax, (%rdx,%r14,8)
leal 0x1(%r13), %r12d
movq 0x784c(%rip), %rax      # 0xc1e8
cmpb $0x22, (%rax,%r12)
sete %r15b
jne 0x4a64
leaq 0x18(%rsp), %r12
jmp 0x4c6e
movsbl %r15b, %r14d
movl %r14d, %edi
callq 0x2230
testl %eax, %eax
je 0x4b0a
movl 0x7799(%rip), %eax      # 0xc168
movslq %ebp, %rcx
movq 0x77ef(%rip), %rdx      # 0xc1c8
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rbx
movl %eax, (%rdx,%rbx)
movl %r13d, %edi
movl %ebp, %esi
callq 0x706b
movl %eax, %r13d
movq 0x77d0(%rip), %rax      # 0xc1c8
movq 0x8(%rax,%rbx), %rcx
movq 0x10(%rax,%rbx), %rdx
movb $0x1, %r14b
cmpb $0x3a, -0x1(%rcx,%rdx)
jne 0x4cc4
addq %rbx, %rax
movl 0x7753(%rip), %ecx      # 0xc16c
movl %ecx, (%rax)
jmp 0x4cc4
leal 0x1(%rbp), %ebx
testb $0x1, %r14b
jne 0x4c17
movslq %ebp, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
addq $0x78, %rsi
leaq 0x7785(%rip), %rdi      # 0xc1c8
callq 0x7b2c
movl 0x772a(%rip), %eax      # 0xc178
movslq %ebx, %rcx
movq 0x7770(%rip), %rdx      # 0xc1c8
leaq (%rcx,%rcx,4), %rcx
movl %eax, (%rdx,%rcx,8)
jmp 0x4c64
leal -0x2(%rbx), %ecx
cmpl %ecx, %r13d
je 0x4ae5
addq %r12, %rax
addl $0x2, %r13d
leaq 0x18(%rsp), %r12
movq 0x7749(%rip), %r15      # 0xc1c8
movsbl (%rax), %edx
movq %r12, 0x8(%rsp)
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 %r12, %rdi
je 0x4abc
callq 0x2140
movl %r13d, %ecx
movq 0x7722(%rip), %rax      # 0xc1e8
cmpb $0x22, (%rax,%rcx)
sete %r15b
je 0x4c6b
addq %rcx, %rax
incl %r13d
cmpl %r13d, %ebx
jne 0x4a78
decl %r13d
movl %r13d, %r12d
movl $0x16, %edx
movq 0x74ff(%rip), %rdi      # 0xbff0
leaq 0x493b(%rip), %rsi      # 0x9433
callq 0x2180
movl %r12d, %r13d
leaq 0x18(%rsp), %r12
jmp 0x4cb8
addl $-0x30, %r14d
cmpl $0xa, %r14d
jb 0x4b36
cmpb $0x2d, %r15b
ja 0x4d88
movzbl %r15b, %eax
movabsq $0x203000000000, %rcx   # imm = 0x203000000000
btq %rax, %rcx
jae 0x4cdc
movl 0x7624(%rip), %eax      # 0xc160
movslq %ebp, %rcx
movq 0x7682(%rip), %rdx      # 0xc1c8
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rbx
movl %eax, (%rdx,%rbx)
movl %r13d, %edi
movl %ebp, %esi
callq 0x706b
movl %eax, %r13d
movq 0x7663(%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 0x7be0
leaq 0x8(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x7132
movq 0x7628(%rip), %rax      # 0xc1c8
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x21d0
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x4bbf
callq 0x2140
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x4bd3
callq 0x2140
movb $0x1, %r14b
jmp 0x4cc4
movslq %ebx, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %r14
leaq 0x75db(%rip), %rbx      # 0xc1c8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7b2c
movslq %ebp, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
addq $0x78, %rsi
movq %rbx, %rdi
callq 0x7b2c
movl 0x755f(%rip), %eax      # 0xc174
jmp 0x4c51
movslq %ebx, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %r14
leaq 0x759f(%rip), %rbx      # 0xc1c8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7b2c
movslq %ebp, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
addq $0x78, %rsi
movq %rbx, %rdi
callq 0x7b2c
movl 0x7527(%rip), %eax      # 0xc178
movq 0x7570(%rip), %rcx      # 0xc1c8
movl %eax, (%rcx,%r14)
addl $0x2, %ebp
xorl %r14d, %r14d
movl %ebp, %ebx
incl %r13d
movl %ebx, %ebp
jmp 0x4cc4
decl %r13d
movq 0x7553(%rip), %rbx      # 0xc1c8
movq %r12, 0x8(%rsp)
movl $0x1, %esi
leaq 0x8(%rsp), %rdi
movl $0x22, %edx
callq 0x2190
leaq (%rbx,%r14,8), %rdi
addq $0x8, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x2060
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x4cb4
callq 0x2140
addl $0x2, %r13d
movb $0x1, %r14b
testb %r15b, %r15b
je 0x4dc5
movl %r13d, %eax
movq 0x7522(%rip), %rbx      # 0xc1f0
cmpq %rax, %rbx
ja 0x4837
jmp 0x4db1
movabsq $0x10800000000, %rcx    # imm = 0x10800000000
btq %rax, %rcx
jae 0x4d88
movl 0x747a(%rip), %eax      # 0xc170
movslq %ebp, %rcx
movq 0x74c8(%rip), %rdx      # 0xc1c8
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rbx
movl %eax, (%rdx,%rbx)
movl %r13d, %edi
movl %ebp, %esi
callq 0x706b
movl %eax, %r13d
movq 0x74a9(%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 0x7be0
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x7132
movq 0x746e(%rip), %rax      # 0xc1c8
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
movq %r14, %rsi
callq 0x21d0
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x4d79
callq 0x2140
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
jmp 0x4bc9
movl 0x7452(%rip), %eax      # 0xc1e0
movslq %ebp, %rcx
movq 0x7430(%rip), %rdx      # 0xc1c8
leaq (%rcx,%rcx,4), %rcx
movl %eax, (%rdx,%rcx,8)
movl %r13d, %edi
movl %ebp, %esi
callq 0x706b
movl %eax, %r13d
jmp 0x4bd3
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %eax
jmp 0x4db3
jmp 0x4dd5
jmp 0x4dd0
movq %rax, %rbx
jmp 0x4de7
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x4de7
callq 0x2140
movq %rbx, %rdi
callq 0x2250