// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x45be
movq (%rbx), %rax
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x6092
movq 0x4f67(%rip), %rdi      # 0xaff0
leaq 0x1386(%rip), %rsi      # 0x7416
jmp 0x60e2
leaq 0x70(%rbx), %r14
leaq 0x50(%rbx), %r15
pushq $0x10
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x20d0
movq 0x70(%rbx), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x90(%rbx,%rax)
je 0x6100
movq 0x4f2f(%rip), %rdi      # 0xaff0
leaq 0x138a(%rip), %rsi      # 0x7452
callq 0x2180
movq %rax, %rdi
movq %r15, %rsi
callq 0x2170
leaq 0x1387(%rip), %rsi      # 0x7466
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20e0
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
movq %rsp, %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rbx, 0x38(%rsp)
leaq 0x8(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0x48(%rsp), %r13
leaq 0x18(%rsp), %rbp
movq 0x38(%rsp), %rbx
movq (%rbx), %rdi
movq %rsp, %rsi
callq 0x44c0
movq (%rbx), %rdi
callq 0x63ae
testb %al, %al
jne 0x633d
movq (%rbx), %rax
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x633d
movq (%rsp), %r15
movq 0x8(%rsp), %rax
cmpq %r15, %rax
je 0x6126
movl (%r15), %ecx
testl %ecx, %ecx
je 0x61c7
cmpl $0x2, %ecx
jne 0x6126
xorl %ebx, %ebx
cmpq %rax, %r15
je 0x61bd
addq $0x8, %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x2210
movq 0x8(%r15), %r12
movq %r14, %rdi
movq %r13, %rsi
callq 0x2170
addq %r12, %rbx
movq %r13, %rdi
callq 0x20c0
movq 0x8(%rsp), %rax
addq $0x20, %r15
jmp 0x6177
incq %rbx
movq %r14, %rdi
xorl %esi, %esi
callq 0x21e0
testb $0x3, %bl
jne 0x61b0
jmp 0x6126
addq $0x8, %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x2210
movq (%rsp), %rbx
xorl %r15d, %r15d
cmpq 0x8(%rsp), %rbx
je 0x632a
movl (%rbx), %eax
cmpq $0x4, %rax
ja 0x6314
leaq 0xfa1(%rip), %rcx       # 0x719c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x8(%rbx), %rsi
movq %rbp, %rdi
callq 0x2210
leaq 0x4fe9(%rip), %rdi      # 0xb200
movq %rbp, %rsi
callq 0x6450
movsbl (%rax), %esi
movq %r14, %rdi
callq 0x21e0
jmp 0x6271
leaq 0xe1c(%rip), %rdi       # 0x704f
movq %r13, %rsi
callq 0x6397
testb %al, %al
je 0x62e0
incq %r15
jmp 0x62f5
leaq 0x8(%rbx), %rsi
movq %rbp, %rdi
callq 0x2210
leaq 0x4fd2(%rip), %rdi      # 0xb230
movq %rbp, %rsi
callq 0x6496
movsbl (%rax), %esi
movq %r14, %rdi
callq 0x21e0
incq %r15
jmp 0x630c
leaq 0x8(%rbx), %rsi
movq %rbp, %rdi
callq 0x2210
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x64dc
movl (%rax), %r12d
movq %rbp, %rdi
callq 0x20c0
leaq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
andq $0x0, 0x20(%rsp)
movb $0x0, 0x28(%rsp)
movl %r12d, %eax
shrl $0x8, %eax
movsbl %al, %esi
movq %rbp, %rdi
callq 0x2150
movsbl %r12b, %esi
movq %rbp, %rdi
callq 0x2150
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2170
addq $0x2, %r15
jmp 0x630c
addq $0x2, %r15
cmpq $0x1, 0x10(%rbx)
jne 0x62f5
movq %r14, %rdi
xorl %esi, %esi
callq 0x21e0
leaq 0x8(%rbx), %rsi
movq %rbp, %rdi
callq 0x2210
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2170
movq %rbp, %rdi
callq 0x20c0
addq $0x28, %rbx
jmp 0x61dd
movq %r14, %rdi
xorl %esi, %esi
callq 0x21e0
incq %r15
cmpq $0x3, %r15
jbe 0x631d
movq %r13, %rdi
callq 0x20c0
jmp 0x6126
movq %rsp, %rdi
callq 0x642e
movb $0x1, %al
jmp 0x60f1
jmp 0x6375
jmp 0x637a
jmp 0x6362
jmp 0x637a
jmp 0x6362
jmp 0x637a
jmp 0x637a
jmp 0x637a
jmp 0x6375
jmp 0x6362
jmp 0x6362
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x20c0
jmp 0x637d
jmp 0x637a
jmp 0x6375
movq %rax, %rbx
jmp 0x6387
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x20c0
movq %rsp, %rdi
callq 0x642e
movq %rbx, %rdi
callq 0x22b0