// File: /workspace/github_cpp_makefile_star2_O1/KJTang[P]SimpleCompiler/Assembler.cpp
// Function: Assembler::init()
bool Assembler::init() {
    std::istringstream iss(asmcode);
    std::string curToken;
    while (iss>>curToken) {
        auto it = this->opList.find(curToken);
        if (it == opList.end()) {
            // Label
            if (curToken[curToken.size()-1] == ':') {
                this->labelMap.insert(std::make_pair(curToken.substr(0, curToken.size()-1), line));
                continue;
            }
            // Invalid Operator
            std::cout<<"Invalid Operator: "<<curToken<<std::endl;
            return false;
        }
        Instruction newIns;
        newIns.opName = curToken;   // TODO: only used for test here, delete this later
        newIns.op = (*it).second.first;
        std::string arg;
        for (int i = 0; i != (*it).second.second; i++) {
            iss>>arg;    
            newIns.args.push_back(arg);
            ++line;
        }
        this->instructions.push_back(newIns);
        ++line;
        // function declaration
        if (newIns.op == OP::FUNC) {
            this->funcMap.insert(std::make_pair(newIns.args[0], line));
        }
        // variable declaration
        if (newIns.op == OP::INT) {
            this->varMap.insert(std::make_pair(newIns.args[0], varID++));
        }
    }
    this->instructions.push_back({"CALL", OP::CALL, std::vector<std::string> {"main"}});
    this->instructions.push_back({"EXIT", OP::EXIT, std::vector<std::string> {}});
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movq %rdi, %r14
leaq -0x288(%rbp), %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x3310
leaq -0xb0(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x40(%r14), %rax
movq %rax, -0x108(%rbp)
leaq 0x48(%r14), %rax
movq %rax, -0x100(%rbp)
leaq -0x68(%rbp), %r13
leaq 0x70(%r14), %rax
movq %rax, -0x98(%rbp)
leaq 0xc0(%r14), %rax
movq %rax, -0xf8(%rbp)
leaq 0xf0(%r14), %rax
movq %rax, -0xf0(%rbp)
leaq 0x90(%r14), %rax
movq %rax, -0xe8(%rbp)
leaq -0xe0(%rbp), %r12
leaq -0x288(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x3260
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x20(%rax,%rcx), %eax
movl %eax, -0x9c(%rbp)
testb $0x5, %al
jne 0xbd2e
movq -0x108(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0xc5a0
movq %rax, %rbx
cmpq -0x100(%rbp), %rax
je 0xbc00
leaq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
xorl %eax, %eax
movq %rax, -0x88(%rbp)
movb $0x0, -0x80(%rbp)
movq %rax, 0x10(%r13)
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
leaq -0x90(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x3130
movl 0x40(%rbx), %eax
movl %eax, -0x70(%rbp)
leaq -0xd0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq $0x0, -0xd8(%rbp)
movb $0x0, -0xd0(%rbp)
cmpl $0x0, 0x44(%rbx)
je 0xbafa
xorl %r15d, %r15d
leaq -0x288(%rbp), %rdi
movq %r12, %rsi
callq 0x3260
movq %r13, %rdi
movq %r12, %rsi
callq 0xc4ae
incl 0x88(%r14)
incl %r15d
cmpl 0x44(%rbx), %r15d
jne 0xbad0
movq -0x98(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xc4fa
incl 0x88(%r14)
cmpl $0x18, -0x70(%rbp)
jne 0xbb67
movq -0x68(%rbp), %rax
leaq -0x40(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x42ce
movl 0x88(%r14), %eax
movl %eax, -0x30(%rbp)
movq -0xf8(%rbp), %rdi
movq %rbx, %rsi
callq 0xc63e
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xbb67
callq 0x31c0
cmpl $0x14, -0x70(%rbp)
jne 0xbbc4
movq -0x68(%rbp), %rax
movl 0x8c(%r14), %ebx
leal 0x1(%rbx), %ecx
movl %ecx, 0x8c(%r14)
leaq -0x40(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0x42ce
movl %ebx, -0x30(%rbp)
movq -0xf0(%rbp), %rdi
movq %r15, %rsi
callq 0xc63e
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xbbc4
callq 0x31c0
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0xbbdc
callq 0x31c0
movq %r13, %rdi
callq 0x4dfc
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0xbbf9
callq 0x31c0
xorl %ebx, %ebx
jmp 0xbd20
movq -0xc0(%rbp), %rax
movq -0xb8(%rbp), %rcx
cmpb $0x3a, -0x1(%rax,%rcx)
jne 0xbc53
decq %rcx
leaq -0x50(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
xorl %edx, %edx
callq 0x3200
leaq -0x80(%rbp), %rcx
movq %rcx, -0x90(%rbp)
movq -0x50(%rbp), %rax
leaq -0x40(%rbp), %rdx
cmpq %rdx, %rax
je 0xbcb6
movq %rax, -0x90(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0xbcbc
movl $0x12, %edx
movq 0x8371(%rip), %rdi      # 0x13fd0
leaq 0x36da(%rip), %rsi      # 0xf340
callq 0x3230
movq -0xc0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
movq 0x8350(%rip), %rdi      # 0x13fd0
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
movl $0x1, %ebx
movq %rax, %rdi
callq 0x3150
jmp 0xbd20
movups (%rdx), %xmm0
movups %xmm0, (%rcx)
movq -0x48(%rbp), %rax
movq %rax, -0x88(%rbp)
movq %rdx, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movb $0x0, -0x40(%rbp)
movl 0x88(%r14), %eax
movl %eax, -0x70(%rbp)
movq -0xe8(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xc63e
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0xbd09
callq 0x31c0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xbd1b
callq 0x31c0
movl $0x2, %ebx
testb $0x1, %bl
je 0xba1b
jmp 0xbe36
leaq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
movq %rax, (%rdi)
leaq 0x3359(%rip), %rsi      # 0xf09c
leaq 0x3356(%rip), %rdx      # 0xf0a0
callq 0x47f6
movl $0x17, -0x70(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
movq %rax, (%rdi)
leaq 0x35eb(%rip), %rsi      # 0xf353
leaq 0x35e8(%rip), %rdx      # 0xf357
callq 0x47f6
leaq -0x68(%rbp), %rbx
leaq -0x50(%rbp), %rsi
leaq -0xe0(%rbp), %rcx
movl $0x1, %edx
movq %rbx, %rdi
callq 0xc55e
leaq -0x90(%rbp), %rsi
movq -0x98(%rbp), %rdi
callq 0xcfca
movq %rbx, %rdi
callq 0x4dfc
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0xbdc0
callq 0x31c0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xbdd2
callq 0x31c0
leaq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
movq %rax, (%rdi)
leaq 0x325b(%rip), %rsi      # 0xf042
leaq 0x3258(%rip), %rdx      # 0xf046
callq 0x47f6
leaq -0x90(%rbp), %rsi
movl $0x0, 0x20(%rsi)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rsi)
movq $0x0, 0x38(%rsi)
movq -0x98(%rbp), %rdi
callq 0xcfca
movq %rbx, %rdi
callq 0x4dfc
movq -0x28(%rbx), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0xbe36
callq 0x31c0
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0xbe4e
callq 0x31c0
testb $0x5, -0x9c(%rbp)
setne %bl
movq 0x8161(%rip), %rsi      # 0x13fc0
leaq -0x288(%rbp), %rdi
callq 0x3290
leaq -0x210(%rbp), %rdi
callq 0x30c0
movl %ebx, %eax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xbf2e
jmp 0xbf35
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0xc532
xorl %r14d, %r14d
jmp 0xbeaf
movq %rax, %rbx
movb $0x1, %r14b
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xbec9
callq 0x31c0
jmp 0xbec9
movq %rax, %rbx
movb $0x1, %r14b
testb %r14b, %r14b
je 0xbf61
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
jmp 0xbf01
jmp 0xbf35
movq %rax, %rbx
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0xbef9
callq 0x31c0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xbf61
callq 0x31c0
jmp 0xbf61
jmp 0xbf35
jmp 0xbf13
jmp 0xbf3a
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xbf3d
callq 0x31c0
jmp 0xbf3d
jmp 0xbf3a
jmp 0xbf3a
movq %rax, %rbx
jmp 0xbf55
jmp 0xbf35
movq %rax, %rbx
jmp 0xbf61
movq %rax, %rbx
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0xbf55
callq 0x31c0
leaq -0x90(%rbp), %rdi
callq 0xc532
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0xbf79
callq 0x31c0
movq 0x8040(%rip), %rsi      # 0x13fc0
leaq -0x288(%rbp), %rdi
callq 0x3290
leaq -0x210(%rbp), %rdi
callq 0x30c0
movq %rbx, %rdi
callq 0x3330