// File: /workspace/github_cpp_makefile_star2_O3/rfotino[P]consolite-assembler/src/parser.cpp
// Function: Parser::secondPass()
bool Parser::secondPass() {
  // Reset the tokenizer to the start of the input
  // file so that we can do a second pass through it
  _tokenizer->reset();
  if (_tokenizer->hasError()) {
    std::cerr << "Error resetting input stream to start of file for 2nd pass."
              << std::endl;
    return false;
  }

  // Opens up the output file and checks that it succeeded
  _output.open(_outfileName);
  if (!_output.good()) {
    std::cerr << "Error opening file " << _outfileName
              << " for output." << std::endl;
    return false;
  }

  // Read tokens again line by line and output machine code
  std::vector<Token> tokens;
  while (true) {
    // Get the next line of tokens
    _tokenizer->getLineOfTokens(tokens);
    // If there is an error or the tokenizer is
    // empty, break out of the loop
    if (_tokenizer->isEmpty() || _tokenizer->hasError()) {
      break;
    }
    // Skip empty lines with no tokens
    if (0 == tokens.size()) {
      continue;
    }
    // For the second pass we only care about lines starting
    // with an opcode or data.
    if (OPCODE == tokens[0].getType()) {
      const std::string opcode = tokens[0].getData();
      // Output the 4 bytes for this instruction
      size_t bytes_output = 0;
      for (auto token = tokens.begin(); token != tokens.end(); token++) {
        switch (token->getType()) {
        case OPCODE:
          bytes_output += 1;
          _output << opcodes.at(token->getData());
          break;
        case REGISTER:
          bytes_output += 1;
          _output << registers.at(token->getData());
          break;
        case DATA:
          // The RET instruction has only 1 byte of data following
          // it. All other instructions with data have 2 bytes,
          // and must be padded if they are only using one.
          if ("RET" == opcode) {
            bytes_output += 1;
          } else {
            bytes_output += 2;
            if (1 == token->getSize()) {
              _output << '\0';
            }
          }
          _output << token->getData();
          break;
        case LABELREF:
          bytes_output += 2;
          {
            unsigned address = _labels[token->getData()];
            std::string addr_str;
            addr_str.push_back((address >> 8) & 0xff);
            addr_str.push_back(address & 0xff);
            _output << addr_str;
          }
          break;
        case LABELDECL:
        case UNKNOWN:
          break;
        }
      }
      // Output any padding bytes necessary
      while (bytes_output < 4) {
        _output << '\0';
        bytes_output++;
      }
    } else if (DATA == tokens[0].getType()) {
      // Directly output all data, in big endian order
      size_t data_size = 0;
      for (auto token = tokens.begin(); token != tokens.end(); token++) {
        const std::string data = token->getData();
        data_size += token->getSize();
        _output << data;
      }
      // Make sure to pad the data to a multiple of 4 bytes,
      // so that any instructions that follow are aligned properly.
      while (0 != data_size % 4) {
        data_size++;
        _output << '\0';
      }
    }
  }

  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x49c0
movq (%rbx), %rax
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x6a2c
movq 0x45d5(%rip), %rbx      # 0xaff0
leaq 0x19c3(%rip), %rsi      # 0x83e5
movl $0x3b, %edx
movq %rbx, %rdi
jmp 0x6a90
leaq 0x70(%rbx), %r14
leaq 0x50(%rbx), %rsi
movq %r14, %rdi
movl $0x10, %edx
callq 0x20d0
movq 0x70(%rbx), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x90(%rbx,%rax)
je 0x6acd
movq 0x4596(%rip), %r14      # 0xaff0
leaq 0x19c0(%rip), %rsi      # 0x8421
movl $0x13, %edx
movq %r14, %rdi
callq 0x21f0
movq 0x50(%rbx), %rsi
movq 0x58(%rbx), %rdx
movq %r14, %rdi
callq 0x21f0
movq %rax, %rbx
leaq 0x19ad(%rip), %rsi      # 0x8435
movl $0xc, %edx
movq %rax, %rdi
callq 0x21f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rax
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rbx, 0x70(%rsp)
leaq 0x8(%rbx), %rax
movq %rax, 0x68(%rsp)
leaq 0x30(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq 0x70(%rsp), %rbx
movq (%rbx), %rdi
leaq 0x50(%rsp), %rsi
callq 0x4862
movq (%rbx), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rax
movl 0x20(%rcx,%rax), %eax
cmpl $0x0, 0x208(%rcx)
sete %cl
testb $0x5, %al
jne 0x6e73
shrb %al
andb %cl, %al
jne 0x6e73
movq 0x50(%rsp), %rbx
cmpq %rbx, 0x58(%rsp)
je 0x6af8
movl (%rbx), %eax
testl %eax, %eax
je 0x6be9
cmpl $0x2, %eax
jne 0x6af8
xorl %eax, %eax
movq %rax, %r12
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x4c28
movq 0x10(%rbx), %r15
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r14, %rdi
callq 0x21f0
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x6ba3
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq (%r15,%r12), %rax
addq $0x28, %rbx
cmpq 0x58(%rsp), %rbx
jne 0x6b51
testb $0x3, %al
je 0x6af8
leaq (%r12,%r15), %rbx
incq %rbx
movb $0x0, 0x30(%rsp)
movl $0x1, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x21f0
movq %rbx, %rax
incq %rax
testb $0x3, %bl
movq %rax, %rbx
jne 0x6bc1
jmp 0x6af8
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x4c28
movq 0x50(%rsp), %r12
cmpq 0x58(%rsp), %r12
je 0x6e2e
xorl %ebx, %ebx
leaq 0x20(%rsp), %r15
movl (%r12), %eax
cmpq $0x4, %rax
ja 0x6e17
leaq 0x14fa(%rip), %rcx      # 0x812c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r15, 0x10(%rsp)
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x4c28
leaq 0x459c(%rip), %rdi      # 0xb1f8
movq %rbp, %rsi
callq 0x6fdc
movb (%rax), %al
movb %al, 0xf(%rsp)
movl $0x1, %edx
movq %r14, %rdi
leaq 0xf(%rsp), %rsi
callq 0x21f0
jmp 0x6d03
movq %r13, %rdi
leaq 0x13bf(%rip), %rsi      # 0x804a
callq 0x2080
testl %eax, %eax
je 0x6dd1
addq $0x2, %rbx
cmpq $0x1, 0x10(%r12)
jne 0x6dd4
movb $0x0, 0x10(%rsp)
movl $0x1, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x21f0
jmp 0x6dd4
movq %r15, 0x10(%rsp)
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x4c28
leaq 0x4545(%rip), %rdi      # 0xb228
movq %rbp, %rsi
callq 0x7030
movb (%rax), %al
movb %al, 0xf(%rsp)
movl $0x1, %edx
movq %r14, %rdi
leaq 0xf(%rsp), %rsi
callq 0x21f0
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x6d1a
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
incq %rbx
jmp 0x6e17
movq %r15, 0x10(%rsp)
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x4c28
movq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x7084
movl (%rax), %r15d
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x6d68
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movl %r15d, %eax
shrl $0x8, %eax
movsbl %al, %esi
movq %rbp, %rdi
callq 0x2170
movsbl %r15b, %esi
movq %rbp, %rdi
callq 0x2170
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x21f0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %r15
cmpq %r15, %rdi
je 0x6dcb
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
addq $0x2, %rbx
jmp 0x6e17
incq %rbx
movq %r15, 0x10(%rsp)
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x4c28
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x21f0
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x6e17
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
addq $0x28, %r12
cmpq 0x58(%rsp), %r12
jne 0x6c1d
cmpq $0x3, %rbx
jbe 0x6e30
jmp 0x6e4e
xorl %ebx, %ebx
addq $-0x4, %rbx
movb $0x0, 0x10(%rsp)
movl $0x1, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x21f0
incq %rbx
jne 0x6e34
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x6af8
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x6af8
leaq 0x50(%rsp), %rdi
callq 0x6f7e
movb $0x1, %al
jmp 0x6abe
jmp 0x6eed
jmp 0x6ecc
jmp 0x6ecc
jmp 0x6e98
jmp 0x6ecc
jmp 0x6ea7
jmp 0x6ecc
jmp 0x6ecc
jmp 0x6eed
jmp 0x6e98
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
jne 0x6eb9
jmp 0x6ecf
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x6ecf
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x6ecf
jmp 0x6eed
jmp 0x6ecc
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x6ef0
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x6ef0
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x6f7e
movq %rbx, %rdi
callq 0x2290