// File: 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
subq $0x8d0, %rsp            # imm = 0x8D0
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x868(%rbp)
addq $0x70, %rdi
callq 0x157f0
movq %rax, -0x18(%rbp)
movq -0x868(%rbp), %rdi
addq $0x70, %rdi
callq 0x15860
movq %rax, -0x20(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x15820
testb $0x1, %al
jne 0x14bb1
jmp 0x15213
leaq -0x18(%rbp), %rdi
callq 0x15890
cmpl $0x11, 0x20(%rax)
je 0x14bd3
leaq -0x18(%rbp), %rdi
callq 0x15890
cmpl $0x12, 0x20(%rax)
jne 0x14cbd
movq -0x868(%rbp), %rax
addq $0x90, %rax
movq %rax, -0x878(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x15890
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x156a0
movq -0x878(%rbp), %rdi
movq %rax, %rsi
callq 0x158b0
movq %rax, -0x28(%rbp)
leaq -0x1a0(%rbp), %rdi
movq %rdi, -0x870(%rbp)
callq 0x3380
leaq -0x28(%rbp), %rdi
callq 0x158e0
movq -0x870(%rbp), %rdi
movl 0x20(%rax), %esi
callq 0x3450
jmp 0x14c40
leaq -0x1d0(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x32a0
jmp 0x14c55
leaq -0x18(%rbp), %rdi
callq 0x15890
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x156a0
movq %rax, %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x3320
leaq -0x1d0(%rbp), %rdi
callq 0x34f8
leaq -0x1a0(%rbp), %rdi
callq 0x30a0
jmp 0x15203
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0x30a0
jmp 0x15225
leaq -0x18(%rbp), %rdi
callq 0x15890
cmpl $0x18, 0x20(%rax)
jne 0x14dc0
movq -0x868(%rbp), %rax
addq $0xc0, %rax
movq %rax, -0x888(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x15890
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x156a0
movq -0x888(%rbp), %rdi
movq %rax, %rsi
callq 0x158b0
movq %rax, -0x1d8(%rbp)
leaq -0x350(%rbp), %rdi
movq %rdi, -0x880(%rbp)
callq 0x3380
leaq -0x1d8(%rbp), %rdi
callq 0x158e0
movq -0x880(%rbp), %rdi
movl 0x20(%rax), %esi
callq 0x3450
jmp 0x14d43
leaq -0x370(%rbp), %rdi
leaq -0x350(%rbp), %rsi
callq 0x32a0
jmp 0x14d58
leaq -0x18(%rbp), %rdi
callq 0x15890
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x156a0
movq %rax, %rdi
leaq -0x370(%rbp), %rsi
callq 0x3320
leaq -0x370(%rbp), %rdi
callq 0x34f8
leaq -0x350(%rbp), %rdi
callq 0x30a0
jmp 0x15201
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
leaq -0x350(%rbp), %rdi
callq 0x30a0
jmp 0x15225
leaq -0x18(%rbp), %rdi
callq 0x15890
cmpl $0x17, 0x20(%rax)
jne 0x14f50
movq -0x868(%rbp), %rax
addq $0xc0, %rax
movq %rax, -0x890(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x15890
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x156a0
movq -0x890(%rbp), %rdi
movq %rax, %rsi
callq 0x158b0
movq -0x868(%rbp), %rdi
movq %rax, -0x378(%rbp)
addq $0xc0, %rdi
callq 0x15940
movq %rax, -0x380(%rbp)
leaq -0x378(%rbp), %rdi
leaq -0x380(%rbp), %rsi
callq 0x15910
testb $0x1, %al
jne 0x14e49
jmp 0x14ea3
movq 0x13180(%rip), %rdi     # 0x27fd0
leaq 0x6665(%rip), %rsi      # 0x1b4bc
callq 0x3280
movq %rax, -0x898(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x15890
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x156a0
movq -0x898(%rbp), %rdi
movq %rax, %rsi
callq 0x3250
movq %rax, %rdi
movq 0x13123(%rip), %rsi     # 0x27fb8
callq 0x32e0
movb $0x0, -0x1(%rbp)
jmp 0x15217
leaq -0x4f8(%rbp), %rdi
movq %rdi, -0x8a0(%rbp)
callq 0x3380
leaq -0x378(%rbp), %rdi
callq 0x158e0
movq -0x8a0(%rbp), %rdi
movl 0x20(%rax), %esi
callq 0x3450
jmp 0x14ed3
leaq -0x518(%rbp), %rdi
leaq -0x4f8(%rbp), %rsi
callq 0x32a0
jmp 0x14ee8
leaq -0x18(%rbp), %rdi
callq 0x15890
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x156a0
movq %rax, %rdi
leaq -0x518(%rbp), %rsi
callq 0x3320
leaq -0x518(%rbp), %rdi
callq 0x34f8
leaq -0x4f8(%rbp), %rdi
callq 0x30a0
jmp 0x151ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
leaq -0x4f8(%rbp), %rdi
callq 0x30a0
jmp 0x15225
leaq -0x18(%rbp), %rdi
callq 0x15890
cmpl $0x14, 0x20(%rax)
jne 0x15053
movq -0x868(%rbp), %rax
addq $0xf0, %rax
movq %rax, -0x8b0(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x15890
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x156a0
movq -0x8b0(%rbp), %rdi
movq %rax, %rsi
callq 0x158b0
movq %rax, -0x520(%rbp)
leaq -0x698(%rbp), %rdi
movq %rdi, -0x8a8(%rbp)
callq 0x3380
leaq -0x520(%rbp), %rdi
callq 0x158e0
movq -0x8a8(%rbp), %rdi
movl 0x20(%rax), %esi
callq 0x3450
jmp 0x14fd6
leaq -0x6b8(%rbp), %rdi
leaq -0x698(%rbp), %rsi
callq 0x32a0
jmp 0x14feb
leaq -0x18(%rbp), %rdi
callq 0x15890
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x156a0
movq %rax, %rdi
leaq -0x6b8(%rbp), %rsi
callq 0x3320
leaq -0x6b8(%rbp), %rdi
callq 0x34f8
leaq -0x698(%rbp), %rdi
callq 0x30a0
jmp 0x151fd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
leaq -0x698(%rbp), %rdi
callq 0x30a0
jmp 0x15225
leaq -0x18(%rbp), %rdi
callq 0x15890
cmpl $0x16, 0x20(%rax)
je 0x15084
leaq -0x18(%rbp), %rdi
callq 0x15890
cmpl $0x15, 0x20(%rax)
je 0x15084
leaq -0x18(%rbp), %rdi
callq 0x15890
cmpl $0x3, 0x20(%rax)
jne 0x151fb
movq -0x868(%rbp), %rax
addq $0xf0, %rax
movq %rax, -0x8b8(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x15890
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x156a0
movq -0x8b8(%rbp), %rdi
movq %rax, %rsi
callq 0x158b0
movq -0x868(%rbp), %rdi
movq %rax, -0x6c0(%rbp)
addq $0xf0, %rdi
callq 0x15940
movq %rax, -0x6c8(%rbp)
leaq -0x6c0(%rbp), %rdi
leaq -0x6c8(%rbp), %rsi
callq 0x15910
testb $0x1, %al
jne 0x150fa
jmp 0x15154
movq 0x12ecf(%rip), %rdi     # 0x27fd0
leaq 0x63cb(%rip), %rsi      # 0x1b4d3
callq 0x3280
movq %rax, -0x8c0(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x15890
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x156a0
movq -0x8c0(%rbp), %rdi
movq %rax, %rsi
callq 0x3250
movq %rax, %rdi
movq 0x12e72(%rip), %rsi     # 0x27fb8
callq 0x32e0
movb $0x0, -0x1(%rbp)
jmp 0x15217
leaq -0x840(%rbp), %rdi
movq %rdi, -0x8c8(%rbp)
callq 0x3380
leaq -0x6c0(%rbp), %rdi
callq 0x158e0
movq -0x8c8(%rbp), %rdi
movl 0x20(%rax), %esi
callq 0x3450
jmp 0x15184
leaq -0x860(%rbp), %rdi
leaq -0x840(%rbp), %rsi
callq 0x32a0
jmp 0x15199
leaq -0x18(%rbp), %rdi
callq 0x15890
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x156a0
movq %rax, %rdi
leaq -0x860(%rbp), %rsi
callq 0x3320
leaq -0x860(%rbp), %rdi
callq 0x34f8
leaq -0x840(%rbp), %rdi
callq 0x30a0
jmp 0x151fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1a8(%rbp)
movl %eax, -0x1ac(%rbp)
leaq -0x840(%rbp), %rdi
callq 0x30a0
jmp 0x15225
jmp 0x151fd
jmp 0x151ff
jmp 0x15201
jmp 0x15203
jmp 0x15205
leaq -0x18(%rbp), %rdi
callq 0x15970
jmp 0x14b87
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x8d0, %rsp            # imm = 0x8D0
popq %rbp
retq
movq -0x1a8(%rbp), %rdi
callq 0x3470
nopw %cs:(%rax,%rax)