// File: /workspace/github_cpp_makefile_star2_O2/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), %r14
leaq 0xf0(%rdi), %rax
movq %rax, -0x38(%rbp)
leaq 0xf8(%rdi), %rax
movq %rax, -0x50(%rbp)
leaq 0xc0(%rdi), %rax
movq %rax, -0x30(%rbp)
leaq 0xc8(%rdi), %rax
movq %rax, -0x40(%rbp)
movq %rdi, -0x58(%rbp)
leaq 0x90(%rdi), %rax
movq %rax, -0x48(%rbp)
addq $0x28, %r14
leaq -0x1f0(%rbp), %r12
leaq -0x28(%r14), %rbx
movq -0x58(%rbp), %rax
movq 0x78(%rax), %r15
cmpq %r15, %rbx
je 0x9609
movl 0x20(%rbx), %eax
leal -0x11(%rax), %ecx
cmpl $0x7, %ecx
ja 0x94a8
leaq 0x2e9f(%rip), %rdx      # 0xc300
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq (%r14), %rsi
movq -0x48(%rbp), %rdi
callq 0xa1e0
movq %rax, %r13
leaq -0x1d0(%rbp), %rbx
movq %rbx, %rdi
callq 0x3280
movl 0x40(%r13), %esi
movq %rbx, %rdi
callq 0x3340
movq %r12, %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x32e0
jmp 0x95b2
cmpl $0x3, %eax
jne 0x95d1
movq (%r14), %rsi
movq -0x38(%rbp), %rdi
callq 0xa1e0
cmpq -0x50(%rbp), %rax
je 0x95da
movq %rax, %r13
leaq -0x1d0(%rbp), %rbx
movq %rbx, %rdi
callq 0x3280
movl 0x40(%r13), %esi
movq %rbx, %rdi
callq 0x3340
movq %r12, %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x32e0
jmp 0x95b2
movq (%r14), %rsi
movq -0x30(%rbp), %rdi
callq 0xa1e0
cmpq -0x40(%rbp), %rax
je 0x95e3
movq %rax, %r13
leaq -0x1d0(%rbp), %rbx
movq %rbx, %rdi
callq 0x3280
movl 0x40(%r13), %esi
movq %rbx, %rdi
callq 0x3340
movq %r12, %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x32e0
jmp 0x95b2
movq (%r14), %rsi
movq -0x38(%rbp), %rdi
callq 0xa1e0
movq %rax, %r13
leaq -0x1d0(%rbp), %rbx
movq %rbx, %rdi
callq 0x3280
movl 0x40(%r13), %esi
movq %rbx, %rdi
callq 0x3340
movq %r12, %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x32e0
jmp 0x95b2
movq (%r14), %rsi
movq -0x30(%rbp), %rdi
callq 0xa1e0
movq %rax, %r13
leaq -0x1d0(%rbp), %rbx
movq %rbx, %rdi
callq 0x3280
movl 0x40(%r13), %esi
movq %rbx, %rdi
callq 0x3340
movq %r12, %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x32e0
movq (%r14), %rdi
movq %r12, %rsi
callq 0x3230
movq %r12, %rdi
callq 0x33a8
leaq -0x1d0(%rbp), %rdi
callq 0x3070
addq $0x40, %r14
jmp 0x943a
leaq 0x2d6e(%rip), %rsi      # 0xc34f
jmp 0x95ea
leaq 0x2d4e(%rip), %rsi      # 0xc338
movq 0x89df(%rip), %rdi      # 0x11fd0
callq 0x31d0
movq (%r14), %rsi
movq %rax, %rdi
callq 0x31b0
movq %rax, %rdi
callq 0x3110
cmpq %r15, %rbx
sete %al
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9633
jmp 0x9633
jmp 0x9633
jmp 0x9633
jmp 0x9633
jmp 0x9633
jmp 0x9633
jmp 0x9633
jmp 0x9633
movq %rax, %rbx
leaq -0x1d0(%rbp), %rdi
callq 0x3070
movq %rbx, %rdi
callq 0x3350