// File: /workspace/github_cpp_makefile_star2_O1/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 0x4a42
movq (%rbx), %rax
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
je 0x6b7c
movq 0x4485(%rip), %rbx      # 0xaff0
leaq 0x1873(%rip), %rsi      # 0x83e5
movl $0x3b, %edx
movq %rbx, %rdi
jmp 0x6be0
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 0x6c1d
movq 0x4446(%rip), %r14      # 0xaff0
leaq 0x1870(%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 0x185d(%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)
leaq 0x20(%rsp), %r12
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 0x48e0
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 0x6fc4
shrb %al
andb %cl, %al
jne 0x6fc4
movq 0x50(%rsp), %r15
cmpq %r15, 0x58(%rsp)
je 0x6c4d
movl (%r15), %eax
testl %eax, %eax
je 0x6d10
cmpl $0x2, %eax
jne 0x6c4d
cmpq 0x58(%rsp), %r15
je 0x6f3d
xorl %ebx, %ebx
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x8(%r15), %rsi
movq 0x10(%r15), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x4caa
movq 0x10(%r15), %r12
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 0x6cfd
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
addq %r12, %rbx
addq $0x28, %r15
cmpq 0x58(%rsp), %r15
jne 0x6cae
jmp 0x6f3f
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x8(%r15), %rsi
movq 0x10(%r15), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x4caa
movq 0x50(%rsp), %rbx
cmpq 0x58(%rsp), %rbx
je 0x6f78
xorl %r15d, %r15d
movl (%rbx), %eax
cmpq $0x4, %rax
ja 0x6f2c
leaq 0x13d9(%rip), %rcx      # 0x812c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r12, 0x10(%rsp)
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x4caa
leaq 0x447d(%rip), %rdi      # 0xb1f8
movq %rbp, %rsi
callq 0x712c
movb (%rax), %al
movb %al, 0xf(%rsp)
movl $0x1, %edx
movq %r14, %rdi
leaq 0xf(%rsp), %rsi
callq 0x21f0
jmp 0x6e1c
movq %r13, %rdi
leaq 0x12a3(%rip), %rsi      # 0x804a
callq 0x2080
testl %eax, %eax
je 0x6ee8
addq $0x2, %r15
cmpq $0x1, 0x10(%rbx)
jne 0x6eeb
movb $0x0, 0x10(%rsp)
movl $0x1, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x21f0
jmp 0x6eeb
movq %r12, 0x10(%rsp)
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x4caa
leaq 0x442c(%rip), %rdi      # 0xb228
movq %rbp, %rsi
callq 0x7180
movb (%rax), %al
movb %al, 0xf(%rsp)
movl $0x1, %edx
movq %r14, %rdi
leaq 0xf(%rsp), %rsi
callq 0x21f0
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x6e33
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
incq %r15
jmp 0x6f2c
movq %r12, 0x10(%rsp)
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x4caa
movq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x71d4
movl (%rax), %r12d
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x6e7f
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 %r12d, %eax
shrl $0x8, %eax
movsbl %al, %esi
movq %rbp, %rdi
callq 0x2170
movsbl %r12b, %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), %r12
cmpq %r12, %rdi
je 0x6ee2
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
addq $0x2, %r15
jmp 0x6f2c
incq %r15
movq %r12, 0x10(%rsp)
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x4caa
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x21f0
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x6f2c
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
addq $0x28, %rbx
cmpq 0x58(%rsp), %rbx
jne 0x6d40
jmp 0x6f7b
xorl %ebx, %ebx
testb $0x3, %bl
leaq 0x20(%rsp), %r12
je 0x6c4d
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 0x6f50
jmp 0x6c4d
xorl %r15d, %r15d
cmpq $0x3, %r15
ja 0x6f9f
addq $-0x4, %r15
movb $0x0, 0x10(%rsp)
movl $0x1, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x21f0
incq %r15
jne 0x6f85
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x6c4d
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x6c4d
leaq 0x50(%rsp), %rdi
callq 0x70d0
movb $0x1, %al
jmp 0x6c0e
jmp 0x701d
jmp 0x7022
jmp 0x7022
jmp 0x6fe9
jmp 0x7022
jmp 0x7022
jmp 0x6ff8
jmp 0x7022
jmp 0x701d
jmp 0x6fe9
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
jne 0x700a
jmp 0x7025
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7025
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0x7025
jmp 0x7022
jmp 0x701d
movq %rax, %rbx
jmp 0x7041
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x7041
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x50(%rsp), %rdi
callq 0x70d0
movq %rbx, %rdi
callq 0x2290
nop