// File: CHEN-Zhihan[P]Compiler/Node.cpp
// Function: VarNode::assign(std::vector<int, std::allocator<int>>&, int) const
void VarNode::assign(vector<int> & sList, int functionID) const {
    if (functionTable.count(i) != 0) {
        cerr << "Redefinition of function: " << reverseLookup[i] << endl;
        exit(-1);
    }
    for (auto & i : subscriptions) {
        i->check(sList, functionID);
        i->evaluate();
    }
    if (global) {
        if (functionID == GLOBAL) {
            cerr << "Refer to global variable " << reverseLookup[i] << " in the global scope" << endl;
            exit(-1);
        }
        if (variableTable[GLOBAL].count(i) != 0 and sizeMap[{GLOBAL, i}].size() != subscriptions.size()) {
            cerr << "Invalid assignment to array " << reverseLookup[i] << endl;
            exit(-1);
        }
        if (variableTable[GLOBAL].count(i) == 0) {
            if (subscriptions.size() != 0) {
                cerr << "Invalid assignment to array " << reverseLookup[i] << endl;
                exit(-1);
            }
            variableTable[GLOBAL].insert(i);
            addressTable[GLOBAL].second[GLOBAL][i] = addressTable[GLOBAL].first++;
            sizeMap[{GLOBAL, i}] = vector<int>();
        }
    } else {
        int s = getDefinitionScope(sList, functionID);
        if (s != NOT_FOUND and sizeMap[{s, i}].size() != subscriptions.size()) {
            cerr << "Invalid assignment to array " << reverseLookup[i] << endl;
            exit(-1);
        }
        if (s == NOT_FOUND) {
            if (subscriptions.size() != 0) {
                cerr << "Invalid assignment to array " << reverseLookup[i] << endl;
                exit(-1);
            }
            auto address = findSpace(functionID, 1);
            if (address == -1) {
                addressTable[functionID].second[sList.back()][i] = addressTable[functionID].first++;
            } else {
                addressTable[functionID].second[sList.back()][i] = address;
            }
            variableTable[sList.back()].insert(i);
            #if DEBUG
                cerr << "add variable " << i << " to " << sList.back() << endl;
            #endif
            sizeMap[{sList.back(), i}] = vector<int>();
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rsi
movq %rsi, -0xd0(%rbp)
addq $0x8, %rsi
leaq 0x2ddc4(%rip), %rdi     # 0x51380
callq 0x29f80
cmpq $0x0, %rax
je 0x23623
movq 0x2da22(%rip), %rdi     # 0x50ff0
leaq 0x12cfb(%rip), %rsi     # 0x362d0
callq 0x4210
movq %rax, %rcx
movq -0xd0(%rbp), %rax
movq %rcx, -0xd8(%rbp)
movslq 0x8(%rax), %rsi
leaq 0x2dd52(%rip), %rdi     # 0x51348
callq 0x29680
movq -0xd8(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, %rdi
movq 0x2d98c(%rip), %rsi     # 0x50fa0
callq 0x4240
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
movq -0xd0(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2a680
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2a6b0
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x2a6e0
testb $0x1, %al
jne 0x2365f
jmp 0x236a1
leaq -0x28(%rbp), %rdi
callq 0x2a720
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x299a0
movq %rax, %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x38(%rbp), %rdi
callq 0x299a0
movq %rax, %rdi
callq 0x23d40
leaq -0x28(%rbp), %rdi
callq 0x2a740
jmp 0x2364c
movq -0xd0(%rbp), %rax
testb $0x1, 0xc(%rax)
je 0x239ee
movl -0x14(%rbp), %eax
cmpl 0x2de21(%rip), %eax     # 0x514dc
jne 0x23728
movq 0x2d92c(%rip), %rdi     # 0x50ff0
leaq 0x13380(%rip), %rsi     # 0x36a4b
callq 0x4210
movq %rax, %rcx
movq -0xd0(%rbp), %rax
movq %rcx, -0xe0(%rbp)
movslq 0x8(%rax), %rsi
leaq 0x2dc5c(%rip), %rdi     # 0x51348
callq 0x29680
movq -0xe0(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, %rdi
leaq 0x131b5(%rip), %rsi     # 0x368bf
callq 0x4210
movq %rax, %rdi
movq 0x2d887(%rip), %rsi     # 0x50fa0
callq 0x4240
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
leaq 0x2dc89(%rip), %rdi     # 0x513b8
leaq 0x2dda6(%rip), %rsi     # 0x514dc
callq 0x17400
movq -0xd0(%rbp), %rsi
movq %rax, %rdi
addq $0x8, %rsi
callq 0x29540
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, -0xe1(%rbp)
je 0x237bf
movq -0xd0(%rbp), %rdx
addq $0x8, %rdx
leaq -0x40(%rbp), %rdi
leaq 0x2dd67(%rip), %rsi     # 0x514dc
callq 0x29050
leaq 0x2dd5f(%rip), %rdi     # 0x514e0
leaq -0x40(%rbp), %rsi
callq 0x28f70
movq %rax, %rdi
callq 0x29660
movq -0xd0(%rbp), %rdi
movq %rax, -0xf0(%rbp)
addq $0x10, %rdi
callq 0x2a210
movq %rax, %rcx
movq -0xf0(%rbp), %rax
cmpq %rcx, %rax
setne %al
movb %al, -0xe1(%rbp)
movb -0xe1(%rbp), %al
testb $0x1, %al
jne 0x237cb
jmp 0x23827
movq 0x2d81e(%rip), %rdi     # 0x50ff0
leaq 0x132a7(%rip), %rsi     # 0x36a80
callq 0x4210
movq %rax, %rcx
movq -0xd0(%rbp), %rax
movq %rcx, -0xf8(%rbp)
movslq 0x8(%rax), %rsi
leaq 0x2db4e(%rip), %rdi     # 0x51348
callq 0x29680
movq -0xf8(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, %rdi
movq 0x2d788(%rip), %rsi     # 0x50fa0
callq 0x4240
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
leaq 0x2db8a(%rip), %rdi     # 0x513b8
leaq 0x2dca7(%rip), %rsi     # 0x514dc
callq 0x17400
movq -0xd0(%rbp), %rsi
movq %rax, %rdi
addq $0x8, %rsi
callq 0x29540
cmpq $0x0, %rax
jne 0x239e9
movq -0xd0(%rbp), %rdi
addq $0x10, %rdi
callq 0x2a210
cmpq $0x0, %rax
je 0x238c9
movq 0x2d77c(%rip), %rdi     # 0x50ff0
leaq 0x13205(%rip), %rsi     # 0x36a80
callq 0x4210
movq %rax, %rcx
movq -0xd0(%rbp), %rax
movq %rcx, -0x100(%rbp)
movslq 0x8(%rax), %rsi
leaq 0x2daac(%rip), %rdi     # 0x51348
callq 0x29680
movq -0x100(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, %rdi
movq 0x2d6e6(%rip), %rsi     # 0x50fa0
callq 0x4240
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
leaq 0x2dae8(%rip), %rdi     # 0x513b8
leaq 0x2dc05(%rip), %rsi     # 0x514dc
movq %rsi, -0x110(%rbp)
callq 0x17400
movq -0xd0(%rbp), %rsi
movq %rax, %rdi
addq $0x8, %rsi
movq %rsi, -0x108(%rbp)
callq 0x174b0
movq -0x110(%rbp), %rsi
movq %rax, -0x50(%rbp)
movb %dl, -0x48(%rbp)
leaq 0x2db86(%rip), %rdi     # 0x51498
movq %rdi, -0x120(%rbp)
callq 0x16ef0
movq -0x120(%rbp), %rdi
movq -0x110(%rbp), %rsi
movl (%rax), %ecx
movl %ecx, -0x114(%rbp)
incl %ecx
movl %ecx, (%rax)
callq 0x16ef0
movq -0x110(%rbp), %rsi
movq %rax, %rdi
addq $0x8, %rdi
callq 0x28f40
movq -0x108(%rbp), %rsi
movq %rax, %rdi
callq 0x2a760
movl -0x114(%rbp), %ecx
movl %ecx, (%rax)
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x299f0
movq -0x110(%rbp), %rsi
movq -0x108(%rbp), %rdx
leaq -0x78(%rbp), %rdi
callq 0x29050
jmp 0x23998
leaq 0x2db41(%rip), %rdi     # 0x514e0
leaq -0x78(%rbp), %rsi
callq 0x28f70
movq %rax, -0x128(%rbp)
jmp 0x239b1
movq -0x128(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x17650
leaq -0x70(%rbp), %rdi
callq 0x16fe0
jmp 0x239e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x16fe0
jmp 0x23d34
jmp 0x23d2b
movq -0xd0(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0x230f0
movl %eax, -0x88(%rbp)
xorl %eax, %eax
cmpl $0x7d0, -0x88(%rbp)     # imm = 0x7D0
movb %al, -0x129(%rbp)
je 0x23a81
movq -0xd0(%rbp), %rdx
addq $0x8, %rdx
leaq -0x90(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x29050
leaq 0x2daa0(%rip), %rdi     # 0x514e0
leaq -0x90(%rbp), %rsi
callq 0x28f70
movq %rax, %rdi
callq 0x29660
movq -0xd0(%rbp), %rdi
movq %rax, -0x138(%rbp)
addq $0x10, %rdi
callq 0x2a210
movq %rax, %rcx
movq -0x138(%rbp), %rax
cmpq %rcx, %rax
setne %al
movb %al, -0x129(%rbp)
movb -0x129(%rbp), %al
testb $0x1, %al
jne 0x23a8d
jmp 0x23ae9
movq 0x2d55c(%rip), %rdi     # 0x50ff0
leaq 0x12fe5(%rip), %rsi     # 0x36a80
callq 0x4210
movq %rax, %rcx
movq -0xd0(%rbp), %rax
movq %rcx, -0x140(%rbp)
movslq 0x8(%rax), %rsi
leaq 0x2d88c(%rip), %rdi     # 0x51348
callq 0x29680
movq -0x140(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, %rdi
movq 0x2d4c6(%rip), %rsi     # 0x50fa0
callq 0x4240
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
cmpl $0x7d0, -0x88(%rbp)     # imm = 0x7D0
jne 0x23d29
movq -0xd0(%rbp), %rdi
addq $0x10, %rdi
callq 0x2a210
cmpq $0x0, %rax
je 0x23b6b
movq 0x2d4da(%rip), %rdi     # 0x50ff0
leaq 0x12f63(%rip), %rsi     # 0x36a80
callq 0x4210
movq %rax, %rcx
movq -0xd0(%rbp), %rax
movq %rcx, -0x148(%rbp)
movslq 0x8(%rax), %rsi
leaq 0x2d80a(%rip), %rdi     # 0x51348
callq 0x29680
movq -0x148(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, %rdi
movq 0x2d444(%rip), %rsi     # 0x50fa0
callq 0x4240
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
movl -0x14(%rbp), %edi
movl $0x1, %esi
callq 0x21ec0
movl %eax, -0x94(%rbp)
cmpl $-0x1, -0x94(%rbp)
jne 0x23bf4
leaq 0x2d90a(%rip), %rdi     # 0x51498
leaq -0x14(%rbp), %rsi
callq 0x16ef0
movl (%rax), %ecx
movl %ecx, -0x14c(%rbp)
addl $0x1, %ecx
movl %ecx, (%rax)
leaq 0x2d8ed(%rip), %rdi     # 0x51498
leaq -0x14(%rbp), %rsi
callq 0x16ef0
addq $0x8, %rax
movq %rax, -0x158(%rbp)
movq -0x10(%rbp), %rdi
callq 0x29d50
movq -0x158(%rbp), %rdi
movq %rax, %rsi
callq 0x28f40
movq -0xd0(%rbp), %rsi
movq %rax, %rdi
addq $0x8, %rsi
callq 0x2a760
movl -0x14c(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x23c4e
movl -0x94(%rbp), %eax
movl %eax, -0x15c(%rbp)
leaq 0x2d891(%rip), %rdi     # 0x51498
leaq -0x14(%rbp), %rsi
callq 0x16ef0
addq $0x8, %rax
movq %rax, -0x168(%rbp)
movq -0x10(%rbp), %rdi
callq 0x29d50
movq -0x168(%rbp), %rdi
movq %rax, %rsi
callq 0x28f40
movq -0xd0(%rbp), %rsi
movq %rax, %rdi
addq $0x8, %rsi
callq 0x2a760
movl -0x15c(%rbp), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
callq 0x29d50
movq %rax, %rsi
leaq 0x2d757(%rip), %rdi     # 0x513b8
callq 0x17400
movq -0xd0(%rbp), %rsi
movq %rax, %rdi
addq $0x8, %rsi
movq %rsi, -0x170(%rbp)
callq 0x174b0
movq %rax, -0xa8(%rbp)
movb %dl, -0xa0(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xc0(%rbp)
movq $0x0, -0xb0(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x299f0
movq -0x10(%rbp), %rdi
callq 0x29d50
movq -0x170(%rbp), %rdx
movq %rax, %rsi
leaq -0xc8(%rbp), %rdi
callq 0x29050
jmp 0x23ccf
leaq 0x2d80a(%rip), %rdi     # 0x514e0
leaq -0xc8(%rbp), %rsi
callq 0x28f70
movq %rax, -0x178(%rbp)
jmp 0x23ceb
movq -0x178(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x17650
leaq -0xc0(%rbp), %rdi
callq 0x16fe0
jmp 0x23d29
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x16fe0
jmp 0x23d34
jmp 0x23d2b
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x43d0
nopl (%rax)