// File: nicklausw[P]snesasm/snesasm.cpp
// Function: parse_num(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
long parse_num(string num)
{
  unsigned int label_search;
  string without_sym = num; // declare without symbol
  
  // make 'without symbol' true
  if (!isdigit(without_sym[0])) {
    without_sym.erase(without_sym.begin());
  }
  
  if (isalpha(num[0])) {
    for (label_search = 0; label_search < labels.size(); label_search++) { // look for a label!
      if (labels[label_search].name == num) {
        return labels[label_search].val; // it's a match!
      }
    }
  }
  
  // check for invalid symbols
  for (unsigned int checkn = 0; checkn < without_sym.length(); checkn++) {
    if (isdigit(without_sym[checkn])) continue;
    
    // not a digit...
    if (num[0] != '$') {
      cerr << "error: invalid symbol in number\n";
      exit(fail);
    } else {
      switch (without_sym[checkn]) {
        case 'a': continue;
        case 'b': continue;
        case 'c': continue;
        case 'd': continue;
        case 'e': continue;
        case 'f': continue;
        default:
          // invalid symbol
          cerr << "error: invalid symbol in number\n";
          exit(fail);
      }
    }
  }
  
  
  char *chararray = const_cast<char*>(without_sym.c_str()); // turn without_sym into char array
  
  switch (num[0]) {
    case '%':
      // binary
      return strtol(chararray, &chararray, 2);
    case '$':
      // hex
      return strtol(chararray, &chararray, 16);
  }
  
  return strtol(chararray, &chararray, 10);
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, %rsi
movq %rsi, 0x60(%rsp)
movq %rsi, 0xd8(%rsp)
leaq 0xb0(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x20d0
movq 0x68(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2390
movq %rax, 0x70(%rsp)
jmp 0xc99e
movq 0x70(%rsp), %rax
movsbl (%rax), %edi
callq 0x2190
cmpl $0x0, %eax
jne 0xca2d
leaq 0xb0(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x2160
movq %rax, 0x90(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xd550
movq 0x50(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x2220
movq %rax, 0x58(%rsp)
jmp 0xc9f8
movq 0x58(%rsp), %rax
movq %rax, 0x88(%rsp)
jmp 0xca2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x23a8
jmp 0xcd0a
movq 0x60(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2390
movq %rax, 0x48(%rsp)
jmp 0xca42
movq 0x48(%rsp), %rax
movsbl (%rax), %edi
callq 0x22e0
cmpl $0x0, %eax
je 0xcafb
movl $0x0, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
movq %rax, 0x40(%rsp)
leaq 0x987a(%rip), %rdi      # 0x162f0
callq 0xd510
movq %rax, %rcx
movq 0x40(%rsp), %rax
cmpq %rcx, %rax
jae 0xcaf9
movl 0xd4(%rsp), %eax
movl %eax, %esi
leaq 0x9858(%rip), %rdi      # 0x162f0
callq 0xd530
movq 0x60(%rsp), %rsi
movq %rax, %rdi
callq 0xd430
testb $0x1, %al
jne 0xcab0
jmp 0xcae1
movl 0xd4(%rsp), %eax
movl %eax, %esi
leaq 0x9830(%rip), %rdi      # 0x162f0
callq 0xd530
movq 0x20(%rax), %rax
movq %rax, 0xe0(%rsp)
movl $0x1, 0x84(%rsp)
jmp 0xcced
jmp 0xcae3
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0xca63
jmp 0xcafb
movl $0x0, 0x80(%rsp)
movl 0x80(%rsp), %eax
movq %rax, 0x38(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x22c0
movq %rax, %rcx
movq 0x38(%rsp), %rax
cmpq %rcx, %rax
jae 0xcc2f
movl 0x80(%rsp), %eax
movl %eax, %esi
leaq 0xb0(%rsp), %rdi
callq 0x2390
movq %rax, 0x30(%rsp)
jmp 0xcb4d
movq 0x30(%rsp), %rax
movsbl (%rax), %edi
callq 0x2190
cmpl $0x0, %eax
je 0xcb64
jmp 0xcc19
movq 0x60(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2390
movq %rax, 0x28(%rsp)
jmp 0xcb79
movq 0x28(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x24, %eax
je 0xcba5
movq 0x9463(%rip), %rdi      # 0x15ff0
leaq 0x4952(%rip), %rsi      # 0x114e6
callq 0x21b0
jmp 0xcb9b
movl $0x1, %edi
callq 0x21f0
movl 0x80(%rsp), %eax
movl %eax, %esi
leaq 0xb0(%rsp), %rdi
callq 0x2390
movq %rax, 0x20(%rsp)
jmp 0xcbc2
movq 0x20(%rsp), %rax
movsbl (%rax), %eax
addl $-0x61, %eax
movl %eax, %ecx
movq %rcx, 0x18(%rsp)
subl $0x5, %eax
ja 0xcbfa
movq 0x18(%rsp), %rax
leaq 0x441f(%rip), %rcx      # 0x11004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0xcc19
jmp 0xcc19
jmp 0xcc19
jmp 0xcc19
jmp 0xcc19
jmp 0xcc19
movq 0x93ef(%rip), %rdi      # 0x15ff0
leaq 0x48de(%rip), %rsi      # 0x114e6
callq 0x21b0
jmp 0xcc0f
movl $0x1, %edi
callq 0x21f0
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0xcb06
leaq 0xb0(%rsp), %rdi
callq 0x20c0
movq 0x60(%rsp), %rdi
movq %rax, 0x78(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x2390
movq %rax, 0x10(%rsp)
jmp 0xcc56
movq 0x10(%rsp), %rax
movsbl (%rax), %eax
movl %eax, 0xc(%rsp)
subl $0x24, %eax
je 0xcc9d
jmp 0xcc69
movl 0xc(%rsp), %eax
subl $0x25, %eax
jne 0xccc6
jmp 0xcc74
movq 0x78(%rsp), %rdi
leaq 0x78(%rsp), %rsi
movl $0x2, %edx
callq 0x2280
movq %rax, 0xe0(%rsp)
movl $0x1, 0x84(%rsp)
jmp 0xcced
movq 0x78(%rsp), %rdi
leaq 0x78(%rsp), %rsi
movl $0x10, %edx
callq 0x2280
movq %rax, 0xe0(%rsp)
movl $0x1, 0x84(%rsp)
jmp 0xcced
movq 0x78(%rsp), %rdi
leaq 0x78(%rsp), %rsi
movl $0xa, %edx
callq 0x2280
movq %rax, 0xe0(%rsp)
movl $0x1, 0x84(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x23a8
movq 0xe0(%rsp), %rax
addq $0xe8, %rsp
retq
movq 0xa8(%rsp), %rdi
callq 0x2320
nopw (%rax,%rax)