// File: nicklausw[P]snesasm/snesasm.cpp
// Function: solve_unsolveds()
int solve_unsolveds()
{
  unsigned int label_search = 0;
  int match_num = 0;
  
  for (unsigned int counter = 0; counter < unsolveds.size(); counter++) {
    match_num = 0;
    
    for (label_search = 0; label_search < labels.size(); label_search++) {
      if (labels[label_search].name == unsolveds[counter].name) {
        // a match!
        match_num++;
        if (unsolveds[counter].type == tDB) {
          if (labels[label_search].val > 0xFF) {
            // too big!
            cerr << "error: db requires 8-bit args\n";
            return fail;
          }
          
          org = unsolveds[counter].loc;
          write_byte(labels[label_search].val);
        } else if (unsolveds[counter].type == tDW) {
          if (labels[label_search].val > 0xFFFF) {
            // too big!
            cerr << "error: dw requires 16-bit args\n";
            return fail;
          }
          
          org = unsolveds[counter].loc;
          write_byte(labels[label_search].val >> 8);
          write_byte(labels[label_search].val & 0xFF);
        }
      }
    }
    
    if (!match_num) {
      cerr << "error: no such label " << unsolveds[counter].name << '\n';
      return fail;
    }
  }
  
  return success;
}

// Assembly:
subq $0x38, %rsp
movl $0x0, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
movq %rax, 0x20(%rsp)
leaq 0xa7b4(%rip), %rdi      # 0x16310
callq 0xd630
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0xbdc8
movl $0x0, 0x2c(%rsp)
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
movq %rax, 0x18(%rsp)
leaq 0xa75e(%rip), %rdi      # 0x162f0
callq 0xd510
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jae 0xbd61
movl 0x30(%rsp), %eax
movl %eax, %esi
leaq 0xa73b(%rip), %rdi      # 0x162f0
callq 0xd530
movq %rax, 0x10(%rsp)
movl 0x28(%rsp), %eax
movl %eax, %esi
leaq 0xa744(%rip), %rdi      # 0x16310
callq 0xd650
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0xd430
testb $0x1, %al
jne 0xbbe7
jmp 0xbd4f
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
movl 0x28(%rsp), %eax
movl %eax, %esi
leaq 0xa711(%rip), %rdi      # 0x16310
callq 0xd650
movl 0x28(%rax), %eax
cmpl 0xa71b(%rip), %eax      # 0x16328
jne 0xbc8b
movl 0x30(%rsp), %eax
movl %eax, %esi
leaq 0xa6d4(%rip), %rdi      # 0x162f0
callq 0xd530
cmpq $0xff, 0x20(%rax)
jle 0xbc4b
movq 0xa3be(%rip), %rdi      # 0x15ff0
leaq 0x592f(%rip), %rsi      # 0x11568
callq 0x21b0
movl $0x1, 0x34(%rsp)
jmp 0xbdd0
movl 0x28(%rsp), %eax
movl %eax, %esi
leaq 0xa6b8(%rip), %rdi      # 0x16310
callq 0xd650
movq 0x20(%rax), %rax
movq %rax, 0xa5b0(%rip)      # 0x16218
movl 0x30(%rsp), %eax
movl %eax, %esi
leaq 0xa67b(%rip), %rdi      # 0x162f0
callq 0xd530
movq 0x20(%rax), %rax
movzbl %al, %edi
callq 0xc810
jmp 0xbd4d
movl 0x28(%rsp), %eax
movl %eax, %esi
leaq 0xa678(%rip), %rdi      # 0x16310
callq 0xd650
movl 0x28(%rax), %eax
cmpl 0xa57a(%rip), %eax      # 0x16220
jne 0xbd4b
movl 0x30(%rsp), %eax
movl %eax, %esi
leaq 0xa637(%rip), %rdi      # 0x162f0
callq 0xd530
cmpq $0xffff, 0x20(%rax)     # imm = 0xFFFF
jle 0xbce8
movq 0xa321(%rip), %rdi      # 0x15ff0
leaq 0x58b1(%rip), %rsi      # 0x11587
callq 0x21b0
movl $0x1, 0x34(%rsp)
jmp 0xbdd0
movl 0x28(%rsp), %eax
movl %eax, %esi
leaq 0xa61b(%rip), %rdi      # 0x16310
callq 0xd650
movq 0x20(%rax), %rax
movq %rax, 0xa513(%rip)      # 0x16218
movl 0x30(%rsp), %eax
movl %eax, %esi
leaq 0xa5de(%rip), %rdi      # 0x162f0
callq 0xd530
movq 0x20(%rax), %rax
sarq $0x8, %rax
movzbl %al, %edi
callq 0xc810
movl 0x30(%rsp), %eax
movl %eax, %esi
leaq 0xa5bc(%rip), %rdi      # 0x162f0
callq 0xd530
movq 0x20(%rax), %rax
andq $0xff, %rax
movzbl %al, %edi
callq 0xc810
jmp 0xbd4d
jmp 0xbd4f
jmp 0xbd51
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0xbb82
cmpl $0x0, 0x2c(%rsp)
jne 0xbdb6
movq 0xa281(%rip), %rdi      # 0x15ff0
leaq 0x5831(%rip), %rsi      # 0x115a7
callq 0x21b0
movq %rax, 0x8(%rsp)
movl 0x28(%rsp), %eax
movl %eax, %esi
leaq 0xa583(%rip), %rdi      # 0x16310
callq 0xd650
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x2180
movq %rax, %rdi
movl $0xa, %esi
callq 0x21e0
movl $0x1, 0x34(%rsp)
jmp 0xbdd0
jmp 0xbdb8
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0xbb4c
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopl (%rax)