// File: /workspace/github_cpp_makefile_star2_O1/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), %r12
movq %rdi, -0x70(%rbp)
cmpq 0x78(%rdi), %r12
sete %r15b
je 0xc302
movq -0x70(%rbp), %rcx
leaq 0xf0(%rcx), %rax
movq %rax, -0x60(%rbp)
leaq 0xf8(%rcx), %rax
movq %rax, -0x68(%rbp)
leaq 0xc0(%rcx), %rax
movq %rax, -0x58(%rbp)
leaq 0xc8(%rcx), %rax
movq %rax, -0x50(%rbp)
leaq 0x90(%rcx), %rax
movq %rax, -0x78(%rbp)
leaq -0x1f0(%rbp), %r13
movl 0x20(%r12), %eax
leal -0x11(%rax), %ecx
cmpl $0x7, %ecx
ja 0xc066
leaq 0x3300(%rip), %rdx      # 0xf320
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq 0x28(%r12), %rsi
movq -0x78(%rbp), %rdi
callq 0xd0d0
movq %rax, %r14
movq %r13, %rdi
callq 0x3280
movl 0x40(%r14), %esi
movq %r13, %rdi
callq 0x3320
leaq -0x48(%rbp), %r14
movq %r14, %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x32d0
jmp 0xc24a
cmpl $0x3, %eax
jne 0xc284
movq 0x28(%r12), %rsi
movq -0x60(%rbp), %rdi
callq 0xd0d0
movq %rax, %r14
cmpq -0x68(%rbp), %rax
je 0xc0e9
movq %r13, %rdi
callq 0x3280
movl 0x40(%r14), %esi
movq %r13, %rdi
callq 0x3320
leaq -0x48(%rbp), %rbx
movq %rbx, %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x32d0
movq 0x28(%r12), %rdi
movq %rbx, %rsi
callq 0x3250
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xc0cc
callq 0x31c0
movq %r13, %rdi
movq 0x7ef2(%rip), %rsi      # 0x13fc8
callq 0x3120
leaq -0x180(%rbp), %rdi
callq 0x30c0
jmp 0xc149
movl $0x16, %edx
movq 0x7edb(%rip), %r13      # 0x13fd0
movq %r13, %rdi
leaq 0x3270(%rip), %rsi      # 0xf36f
callq 0x3230
movq 0x28(%r12), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r13, %rdi
callq 0x3230
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x31d0
movsbl %al, %esi
movq %r13, %rdi
leaq -0x1f0(%rbp), %r13
callq 0x3040
movq %rax, %rdi
callq 0x3150
cmpq -0x68(%rbp), %r14
jne 0xc284
jmp 0xc302
movq 0x28(%r12), %rsi
movq -0x58(%rbp), %rdi
callq 0xd0d0
movq %rax, %rbx
cmpq -0x50(%rbp), %rax
je 0xc29c
movq %r13, %rdi
callq 0x3280
movl 0x40(%rbx), %esi
movq %r13, %rdi
callq 0x3320
leaq -0x48(%rbp), %r14
movq %r14, %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x32d0
movq 0x28(%r12), %rdi
movq %r14, %rsi
callq 0x3250
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xc1b8
callq 0x31c0
movq %r13, %rdi
movq 0x7e06(%rip), %rsi      # 0x13fc8
callq 0x3120
leaq -0x180(%rbp), %rdi
callq 0x30c0
jmp 0xc2fc
movq 0x28(%r12), %rsi
movq -0x60(%rbp), %rdi
callq 0xd0d0
movq %rax, %r14
movq %r13, %rdi
callq 0x3280
movl 0x40(%r14), %esi
movq %r13, %rdi
callq 0x3320
leaq -0x48(%rbp), %r14
movq %r14, %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x32d0
jmp 0xc24a
movq 0x28(%r12), %rsi
movq -0x58(%rbp), %rdi
callq 0xd0d0
movq %rax, %r14
movq %r13, %rdi
callq 0x3280
movl 0x40(%r14), %esi
movq %r13, %rdi
callq 0x3320
leaq -0x48(%rbp), %r14
movq %r14, %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x32d0
movq 0x28(%r12), %rdi
movq %r14, %rsi
callq 0x3250
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xc269
callq 0x31c0
movq %r13, %rdi
movq 0x7d55(%rip), %rsi      # 0x13fc8
callq 0x3120
leaq -0x180(%rbp), %rdi
callq 0x30c0
addq $0x40, %r12
movq -0x70(%rbp), %rax
cmpq 0x78(%rax), %r12
sete %r15b
jne 0xc00c
jmp 0xc302
movl $0x16, %edx
movq 0x7d28(%rip), %r13      # 0x13fd0
movq %r13, %rdi
leaq 0x30a6(%rip), %rsi      # 0xf358
callq 0x3230
movq 0x28(%r12), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r13, %rdi
callq 0x3230
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x31d0
movsbl %al, %esi
movq %r13, %rdi
leaq -0x1f0(%rbp), %r13
callq 0x3040
movq %rax, %rdi
callq 0x3150
cmpq -0x50(%rbp), %rbx
jne 0xc284
andb $0x1, %r15b
movl %r15d, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xc32d
jmp 0xc32d
jmp 0xc32d
jmp 0xc32d
jmp 0xc32d
jmp 0xc32d
jmp 0xc32d
jmp 0xc32d
jmp 0xc32d
movq %rax, %rbx
movq 0x7c91(%rip), %rsi      # 0x13fc8
leaq -0x1f0(%rbp), %rdi
callq 0x3120
leaq -0x180(%rbp), %rdi
callq 0x30c0
movq %rbx, %rdi
callq 0x3330
nop