// File: /workspace/github_cpp_makefile_star2_O3/KJTang[P]SimpleCompiler/Assembler.cpp
// Function: Assembler::redirect()
bool Assembler::redirect() {
    for (auto it = this->instructions.begin(); it != this->instructions.end(); ++it) {
        // label
        if (it->op == OP::JZ || it->op == OP::JMP) {
            auto lableIt = this->labelMap.find(it->args[0]);
            std::ostringstream num;
            num<<lableIt->second;
            it->args[0] = num.str();
        } 
        // function
        else if (it->op == OP::FUNC) {
            auto funcIt = this->funcMap.find(it->args[0]);
            std::ostringstream num;
            num<<funcIt->second;
            it->args[0] = num.str();
        } else if (it->op == OP::CALL) {
            auto funcIt = this->funcMap.find(it->args[0]);
            if (funcIt == this->funcMap.end()) {
                std::cout<<"cannot find function: "<<it->args[0]<<std::endl;
                return false;
            }
            std::ostringstream num;
            num<<funcIt->second;
            it->args[0] = num.str();
        } 
        // variable
        else if (it->op == OP::INT) {
            auto varIt = this->varMap.find(it->args[0]);
            std::ostringstream num;
            num<<varIt->second;
            it->args[0] = num.str();
        } else if (it->op == OP::SAVE || it->op == OP::LOAD || it->op == OP::SCAN) {
            auto varIt = this->varMap.find(it->args[0]);
            if (varIt == this->varMap.end()) {
                std::cout<<"cannot find variable: "<<it->args[0]<<std::endl;
                return false;
            }
            std::ostringstream num;
            num<<varIt->second;
            it->args[0] = num.str();
        }
    }
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq 0x70(%rdi), %rbx
movb $0x1, %al
cmpq 0x78(%rdi), %rbx
je 0xc1b7
movq %rdi, %r12
leaq 0xf0(%rdi), %rax
movq %rax, -0x38(%rbp)
leaq 0xf8(%rdi), %rax
movq %rax, -0x70(%rbp)
leaq 0xc0(%rdi), %rax
movq %rax, -0x30(%rbp)
leaq 0xc8(%rdi), %rax
movq %rax, -0x60(%rbp)
addq $0x28, %rbx
leaq 0x90(%rdi), %rax
movq %rax, -0x68(%rbp)
leaq -0x1e8(%rbp), %r14
leaq -0x58(%rbp), %r15
movl -0x8(%rbx), %eax
leal -0x11(%rax), %ecx
cmpl $0x7, %ecx
ja 0xc015
leaq 0x334b(%rip), %rdx      # 0xf320
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq (%rbx), %rsi
movq -0x68(%rbp), %rdi
callq 0xcf42
movq %rax, %r13
movq %r14, %rdi
callq 0x3280
movl 0x40(%r13), %esi
movq %r14, %rdi
callq 0x3320
movq %r15, %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x32d0
jmp 0xc103
cmpl $0x3, %eax
jne 0xc13b
movq (%rbx), %rsi
movq -0x38(%rbp), %rdi
callq 0xcf42
cmpq -0x70(%rbp), %rax
je 0xc155
movq %rax, %r13
movq %r14, %rdi
callq 0x3280
movl 0x40(%r13), %esi
movq %r14, %rdi
callq 0x3320
movq %r15, %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x32d0
jmp 0xc103
movq (%rbx), %rsi
movq -0x30(%rbp), %rdi
callq 0xcf42
cmpq -0x60(%rbp), %rax
je 0xc15e
movq %rax, %r13
movq %r14, %rdi
callq 0x3280
movl 0x40(%r13), %esi
movq %r14, %rdi
callq 0x3320
movq %r15, %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x32d0
jmp 0xc103
movq (%rbx), %rsi
movq -0x38(%rbp), %rdi
callq 0xcf42
movq %rax, %r13
movq %r14, %rdi
callq 0x3280
movl 0x40(%r13), %esi
movq %r14, %rdi
callq 0x3320
movq %r15, %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x32d0
jmp 0xc103
movq (%rbx), %rsi
movq -0x30(%rbp), %rdi
callq 0xcf42
movq %rax, %r13
movq %r14, %rdi
callq 0x3280
movl 0x40(%r13), %esi
movq %r14, %rdi
callq 0x3320
movq %r15, %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x32d0
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x3250
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0xc120
callq 0x31c0
movq %r14, %rdi
movq 0x7e9e(%rip), %rsi      # 0x13fc8
callq 0x3120
leaq -0x178(%rbp), %rdi
callq 0x30c0
leaq 0x40(%rbx), %rax
addq $0x18, %rbx
cmpq 0x78(%r12), %rbx
movq %rax, %rbx
jne 0xbfc3
movb $0x1, %al
jmp 0xc1b7
leaq 0x3213(%rip), %rsi      # 0xf36f
jmp 0xc165
leaq 0x31f3(%rip), %rsi      # 0xf358
movq 0x7e64(%rip), %r14      # 0x13fd0
movl $0x16, %edx
movq %r14, %rdi
callq 0x3230
movq (%rbx), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r14, %rdi
callq 0x3230
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x31d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3150
xorl %eax, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xc1db
jmp 0xc1db
jmp 0xc1db
jmp 0xc1db
jmp 0xc1db
jmp 0xc1db
jmp 0xc1db
jmp 0xc1db
jmp 0xc1db
movq %rax, %rbx
movq 0x7de3(%rip), %rsi      # 0x13fc8
leaq -0x1e8(%rbp), %rdi
callq 0x3120
leaq -0x178(%rbp), %rdi
callq 0x30c0
movq %rbx, %rdi
callq 0x3330
nop