// File: /workspace/github_cpp_makefile_star2_O2/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 0x7e56(%rip), %rdi      # 0xc1d8
pushq $0x1
popq %rsi
callq 0x69ea
leaq 0x8(%rsp), %r13
xorl %ebp, %ebp
xorl %edx, %edx
xorl %r14d, %r14d
movl %ebp, %ebp
movq 0x7e61(%rip), %r15      # 0xc200
cmpq %rbp, %r15
jbe 0x47c5
movq 0x7e49(%rip), %rax      # 0xc1f8
movzbl (%rax,%rbp), %r12d
movslq %edx, %rbx
cmpl $0x2e, %r12d
je 0x4560
cmpl $0xd, %r12d
je 0x4439
cmpl $0x20, %r12d
je 0x4415
cmpl $0x22, %r12d
je 0x447f
cmpl $0x2c, %r12d
je 0x4440
cmpl $0xa, %r12d
jne 0x44f7
leal 0x1(%rdx), %ebx
movslq %ebx, %r15
testb $0x1, %r14b
jne 0x46ee
addl $0x3, %edx
movslq %edx, %rax
imulq $0x28, %rax, %rsi
leaq 0x7dd0(%rip), %rdi      # 0xc1d8
callq 0x69ea
movl 0x7d71(%rip), %eax      # 0xc184
jmp 0x446c
testb $0x1, %r14b
je 0x4439
incl %edx
movslq %edx, %rax
imulq $0x28, %rax, %rsi
leaq 0x7dad(%rip), %rdi      # 0xc1d8
movq %rdx, %rbx
callq 0x69ea
movq %rbx, %rdx
xorl %r14d, %r14d
incl %ebp
jmp 0x4396
leal 0x1(%rdx), %ebx
movslq %ebx, %r15
testb $0x1, %r14b
jne 0x46bb
addl $0x3, %edx
movslq %edx, %rax
imulq $0x28, %rax, %rsi
leaq 0x7d77(%rip), %rdi      # 0xc1d8
callq 0x69ea
movl 0x7d1c(%rip), %eax      # 0xc188
movq 0x7d65(%rip), %rcx      # 0xc1d8
imulq $0x28, %r15, %rdx
movl %eax, (%rcx,%rdx)
jmp 0x4733
movq %rdx, (%rsp)
movl 0x7d03(%rip), %ecx      # 0xc18c
movq 0x7d48(%rip), %rdx      # 0xc1d8
imulq $0x28, %rbx, %rbx
movl %ecx, (%rdx,%rbx)
addl $0x2, %ebp
pushq $0x1
popq %r12
leal -0x1(%rbp), %ecx
movb (%rax,%rcx), %al
cmpb $0x22, %al
je 0x45d3
cmpl %ebp, %r15d
je 0x47ad
movq 0x7d1c(%rip), %r14      # 0xc1d8
leaq 0x18(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movsbl %al, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x2160
leaq (%r14,%rbx), %rdi
addq $0x8, %rdi
movq %r13, %rsi
callq 0x22b0
movq %r13, %rdi
callq 0x22c8
movq 0x7d05(%rip), %rax      # 0xc1f8
incl %ebp
jmp 0x449e
movq %rdx, %r15
movsbl %r12b, %r14d
movl %r14d, %edi
callq 0x2240
testl %eax, %eax
je 0x4616
movl 0x7c64(%rip), %eax      # 0xc178
movq 0x7cbd(%rip), %rcx      # 0xc1d8
imulq $0x28, %rbx, %rbx
movl %eax, (%rcx,%rbx)
movl %ebp, %edi
movl %r15d, %esi
callq 0x6198
movl %eax, %ebp
movq 0x7ca3(%rip), %rax      # 0xc1d8
movq 0x8(%rax,%rbx), %rcx
movq 0x10(%rax,%rbx), %rdx
movb $0x1, %r14b
cmpb $0x3a, -0x1(%rcx,%rdx)
movq %r15, %rdx
jne 0x4396
addq %rbx, %rax
movl 0x7c23(%rip), %ecx      # 0xc17c
movl %ecx, (%rax)
jmp 0x4396
movl 0x7c0e(%rip), %eax      # 0xc174
movq 0x7c6b(%rip), %rcx      # 0xc1d8
imulq $0x28, %rbx, %r14
movl %eax, (%rcx,%r14)
incl %ebp
movl %ebp, %edi
movl %ebx, %esi
movq %rdx, %r15
callq 0x6198
movl %eax, %ebp
movq 0x7c4c(%rip), %rax      # 0xc1d8
leaq (%rax,%r14), %rsi
addq $0x8, %rsi
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0x21d0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x6253
movq 0x7c25(%rip), %rax      # 0xc1d8
leaq (%rax,%r14), %rdi
addq $0x8, %rdi
movq %r13, %rsi
callq 0x21a0
movq %r13, %rdi
callq 0x22c8
movq %rbx, %rdi
jmp 0x46ab
movq 0x7bfe(%rip), %r14      # 0xc1d8
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rdi
movq %r12, %rsi
pushq $0x22
popq %rdx
callq 0x2160
leaq (%r14,%rbx), %rdi
addq $0x8, %rdi
movq %r13, %rsi
callq 0x22b0
movq %r13, %rdi
callq 0x22c8
movb $0x1, %r14b
movq (%rsp), %rdx
jmp 0x4396
addl $-0x30, %r14d
cmpl $0xa, %r14d
movq %r15, %rsi
jb 0x4645
cmpb $0x2d, %r12b
ja 0x4782
movzbl %r12b, %eax
movabsq $0x203000000000, %rcx   # imm = 0x203000000000
btq %rax, %rcx
jae 0x473c
movl 0x7b25(%rip), %eax      # 0xc170
movq 0x7b86(%rip), %rcx      # 0xc1d8
imulq $0x28, %rbx, %rbx
movl %eax, (%rcx,%rbx)
movl %ebp, %edi
callq 0x6198
movl %eax, %ebp
movq 0x7b6f(%rip), %rax      # 0xc1d8
leaq (%rax,%rbx), %rsi
addq $0x8, %rsi
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x21d0
movq %r13, %rdi
movq %r14, %rsi
callq 0x6253
movq 0x7b48(%rip), %rax      # 0xc1d8
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
movq %r13, %rsi
callq 0x21a0
movq %r13, %rdi
callq 0x22c8
movq %r14, %rdi
callq 0x22c8
movq %r15, %rdx
movb $0x1, %r14b
jmp 0x4396
imulq $0x28, %r15, %rbx
leaq 0x7b12(%rip), %r14      # 0xc1d8
movq %r14, %rdi
movq %rbx, %rsi
movq %rdx, %r15
callq 0x69ea
leal 0x3(%r15), %eax
cltq
imulq $0x28, %rax, %rsi
movq %r14, %rdi
callq 0x69ea
movl 0x7a9c(%rip), %eax      # 0xc188
jmp 0x471f
imulq $0x28, %r15, %rbx
leaq 0x7adf(%rip), %r14      # 0xc1d8
movq %r14, %rdi
movq %rbx, %rsi
movq %rdx, %r15
callq 0x69ea
leal 0x3(%r15), %eax
cltq
imulq $0x28, %rax, %rsi
movq %r14, %rdi
callq 0x69ea
movl 0x7a65(%rip), %eax      # 0xc184
movq 0x7ab2(%rip), %rcx      # 0xc1d8
movl %eax, (%rcx,%rbx)
addl $0x2, %r15d
xorl %r14d, %r14d
movl %r15d, %ebx
incl %ebp
movl %ebx, %edx
jmp 0x4396
movabsq $0x10800000000, %rcx    # imm = 0x10800000000
btq %rax, %rcx
jae 0x4782
movl 0x7a2e(%rip), %eax      # 0xc180
movq 0x7a7f(%rip), %rcx      # 0xc1d8
imulq $0x28, %rbx, %rbx
movl %eax, (%rcx,%rbx)
movl %ebp, %edi
callq 0x6198
movl %eax, %ebp
movq 0x7a68(%rip), %rax      # 0xc1d8
leaq (%rax,%rbx), %rsi
addq $0x8, %rsi
leaq 0x28(%rsp), %r14
jmp 0x4676
movl 0x7a68(%rip), %eax      # 0xc1f0
movq 0x7a49(%rip), %rcx      # 0xc1d8
imulq $0x28, %rbx, %rdx
movl %eax, (%rcx,%rdx)
movl %ebp, %edi
movl %r15d, %esi
callq 0x6198
movq %r15, %rdx
movl %eax, %ebp
movb $0x1, %r14b
jmp 0x4396
movq 0x783c(%rip), %rdi      # 0xbff0
leaq 0x3c88(%rip), %rsi      # 0x8443
callq 0x2140
pushq $0x1
popq %rax
jmp 0x47c7
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x47e2
jmp 0x47dd
movq %rax, %rbx
jmp 0x47ef
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x22c8
movq %rbx, %rdi
callq 0x2270