// File: CHEN-Zhihan[P]Compiler/Node.cpp
// Function: VarNode::declare(std::vector<int, std::allocator<int>>&, int) const
void VarNode::declare(vector<int> &sList, int functionID) const {
    if (functionTable.count(i) != 0) {
        cerr << "Redefinition of function: " << reverseLookup[i] << endl;
        exit(-1);
    }
    auto dimension = getDimension();
    int size = accumulate(dimension.begin(), dimension.end(), 1, multiplies<int>());
    if (size <= 0) {
        cerr << "Invalid length of array " << reverseLookup[i] << endl;
        exit(-1);
    }
    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) {
            cerr << "Redeclaration of variable " << reverseLookup[i] << endl;
            exit(-1);
        }
        auto address = findSpace(functionID, size);
        if (address == -1) {
            addressTable[GLOBAL].second[GLOBAL][i] = addressTable[GLOBAL].first;
            addressTable[GLOBAL].first += size;
        } else {
            addressTable[GLOBAL].second[GLOBAL][i] = address;
        }
        variableTable[GLOBAL].insert(i);
        sizeMap[{GLOBAL, i}] = dimension;
    } else {
        int s = getDefinitionScope(sList, functionID);
        if (s != NOT_FOUND) {
            cerr << "Redeclaration of variable " << reverseLookup[i] << endl;
            exit(-1);
        }
        variableTable[sList.back()].insert(i);
        #if DEBUG
            cerr << "add variable " << i << " to " << sList.back() << endl;
        #endif
        auto address = findSpace(functionID, size);
        if (address == -1) {
            addressTable[functionID].second[sList.back()][i] = addressTable[functionID].first;
            addressTable[functionID].first += size;
        } else {
            addressTable[functionID].second[sList.back()][i] = address;
        }
        sizeMap[{sList.back(), i}] = dimension;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rsi
movq %rsi, -0xa8(%rbp)
addq $0x8, %rsi
leaq 0x29544(%rip), %rdi     # 0x51380
callq 0x29f80
cmpq $0x0, %rax
je 0x27ea3
movq 0x291a2(%rip), %rdi     # 0x50ff0
leaq 0xe47b(%rip), %rsi      # 0x362d0
callq 0x4210
movq %rax, %rcx
movq -0xa8(%rbp), %rax
movq %rcx, -0xb0(%rbp)
movslq 0x8(%rax), %rsi
leaq 0x294d2(%rip), %rdi     # 0x51348
callq 0x29680
movq -0xb0(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, %rdi
movq 0x2910c(%rip), %rsi     # 0x50fa0
callq 0x4240
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
movq -0xa8(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x265b0
movq -0xc0(%rbp), %rdi
callq 0x291d0
movq -0xc0(%rbp), %rdi
movq %rax, -0x40(%rbp)
callq 0x29200
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl $0x1, %edx
callq 0x29170
movl %eax, -0xb4(%rbp)
jmp 0x27ef4
movl -0xb4(%rbp), %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jg 0x27f8b
movq 0x290e2(%rip), %rdi     # 0x50ff0
leaq 0xeb1d(%rip), %rsi      # 0x36a32
callq 0x4210
movq %rax, -0xc8(%rbp)
jmp 0x27f23
movq -0xa8(%rbp), %rax
movslq 0x8(%rax), %rsi
leaq 0x29413(%rip), %rdi     # 0x51348
callq 0x29680
movq -0xc8(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, -0xd0(%rbp)
jmp 0x27f52
movq -0xd0(%rbp), %rdi
movq 0x29040(%rip), %rsi     # 0x50fa0
callq 0x4240
jmp 0x27f67
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x16fe0
jmp 0x285dc
movq -0xa8(%rbp), %rax
testb $0x1, 0xc(%rax)
je 0x282ef
movl -0x14(%rbp), %eax
cmpl 0x29537(%rip), %eax     # 0x514dc
jne 0x28031
movq 0x2903e(%rip), %rdi     # 0x50ff0
leaq 0xea92(%rip), %rsi      # 0x36a4b
callq 0x4210
movq %rax, -0xd8(%rbp)
jmp 0x27fc7
movq -0xa8(%rbp), %rax
movslq 0x8(%rax), %rsi
leaq 0x2936f(%rip), %rdi     # 0x51348
callq 0x29680
movq -0xd8(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, -0xe0(%rbp)
jmp 0x27ff6
movq -0xe0(%rbp), %rdi
leaq 0xe8bb(%rip), %rsi      # 0x368bf
callq 0x4210
movq %rax, -0xe8(%rbp)
jmp 0x28012
movq -0xe8(%rbp), %rdi
movq 0x28f80(%rip), %rsi     # 0x50fa0
callq 0x4240
jmp 0x28027
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
leaq 0x29380(%rip), %rdi     # 0x513b8
leaq 0x2949d(%rip), %rsi     # 0x514dc
callq 0x17400
movq %rax, -0xf0(%rbp)
jmp 0x2804d
movq -0xf0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
addq $0x8, %rsi
callq 0x29540
movq %rax, -0xf8(%rbp)
jmp 0x2806d
movq -0xf8(%rbp), %rax
cmpq $0x0, %rax
je 0x280e4
movq 0x28f6f(%rip), %rdi     # 0x50ff0
leaq 0xe9dd(%rip), %rsi      # 0x36a65
callq 0x4210
movq %rax, -0x100(%rbp)
jmp 0x28096
movq -0xa8(%rbp), %rax
movslq 0x8(%rax), %rsi
leaq 0x292a0(%rip), %rdi     # 0x51348
callq 0x29680
movq -0x100(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, -0x108(%rbp)
jmp 0x280c5
movq -0x108(%rbp), %rdi
movq 0x28ecd(%rip), %rsi     # 0x50fa0
callq 0x4240
jmp 0x280da
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
movl -0x14(%rbp), %edi
movl -0x34(%rbp), %esi
callq 0x21ec0
movl %eax, -0x10c(%rbp)
jmp 0x280f7
movl -0x10c(%rbp), %eax
movl %eax, -0x60(%rbp)
cmpl $-0x1, -0x60(%rbp)
jne 0x281d8
leaq 0x29387(%rip), %rdi     # 0x51498
leaq 0x293c4(%rip), %rsi     # 0x514dc
callq 0x16ef0
movq %rax, -0x118(%rbp)
jmp 0x28126
movq -0x118(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x124(%rbp)
leaq 0x2935c(%rip), %rdi     # 0x51498
leaq 0x29399(%rip), %rsi     # 0x514dc
callq 0x16ef0
movq %rax, -0x120(%rbp)
jmp 0x28151
movq -0x120(%rbp), %rdi
addq $0x8, %rdi
leaq 0x29379(%rip), %rsi     # 0x514dc
callq 0x28f40
movq %rax, -0x130(%rbp)
jmp 0x28171
movq -0x130(%rbp), %rdi
movq -0xa8(%rbp), %rsi
addq $0x8, %rsi
callq 0x2a760
movq %rax, -0x138(%rbp)
jmp 0x28191
movq -0x138(%rbp), %rax
movl -0x124(%rbp), %ecx
movl %ecx, (%rax)
movl -0x34(%rbp), %eax
movl %eax, -0x144(%rbp)
leaq 0x292e8(%rip), %rdi     # 0x51498
leaq 0x29325(%rip), %rsi     # 0x514dc
callq 0x16ef0
movq %rax, -0x140(%rbp)
jmp 0x281c5
movq -0x140(%rbp), %rax
movl -0x144(%rbp), %ecx
addl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x2824c
movl -0x60(%rbp), %eax
movl %eax, -0x154(%rbp)
leaq 0x292b0(%rip), %rdi     # 0x51498
leaq 0x292ed(%rip), %rsi     # 0x514dc
callq 0x16ef0
movq %rax, -0x150(%rbp)
jmp 0x281fd
movq -0x150(%rbp), %rdi
addq $0x8, %rdi
leaq 0x292cd(%rip), %rsi     # 0x514dc
callq 0x28f40
movq %rax, -0x160(%rbp)
jmp 0x2821d
movq -0x160(%rbp), %rdi
movq -0xa8(%rbp), %rsi
addq $0x8, %rsi
callq 0x2a760
movq %rax, -0x168(%rbp)
jmp 0x2823d
movq -0x168(%rbp), %rax
movl -0x154(%rbp), %ecx
movl %ecx, (%rax)
leaq 0x29165(%rip), %rdi     # 0x513b8
leaq 0x29282(%rip), %rsi     # 0x514dc
callq 0x17400
movq %rax, -0x170(%rbp)
jmp 0x28268
movq -0x170(%rbp), %rdi
movq -0xa8(%rbp), %rsi
addq $0x8, %rsi
callq 0x174b0
movb %dl, -0x179(%rbp)
movq %rax, -0x178(%rbp)
jmp 0x2828e
movq -0xa8(%rbp), %rdx
movb -0x179(%rbp), %al
movq -0x178(%rbp), %rcx
movq %rcx, -0x70(%rbp)
movb %al, -0x68(%rbp)
addq $0x8, %rdx
leaq 0x29228(%rip), %rsi     # 0x514dc
leaq -0x78(%rbp), %rdi
callq 0x29050
jmp 0x282bf
leaq 0x2921a(%rip), %rdi     # 0x514e0
leaq -0x78(%rbp), %rsi
callq 0x28f70
movq %rax, -0x188(%rbp)
jmp 0x282d8
movq -0x188(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x2a300
jmp 0x282ea
jmp 0x285ca
movq -0xa8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0x230f0
movl %eax, -0x18c(%rbp)
jmp 0x2830a
movl -0x18c(%rbp), %eax
movl %eax, -0x7c(%rbp)
cmpl $0x7d0, -0x7c(%rbp)     # imm = 0x7D0
je 0x28386
movq 0x28ccd(%rip), %rdi     # 0x50ff0
leaq 0xe73b(%rip), %rsi      # 0x36a65
callq 0x4210
movq %rax, -0x198(%rbp)
jmp 0x28338
movq -0xa8(%rbp), %rax
movslq 0x8(%rax), %rsi
leaq 0x28ffe(%rip), %rdi     # 0x51348
callq 0x29680
movq -0x198(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, -0x1a0(%rbp)
jmp 0x28367
movq -0x1a0(%rbp), %rdi
movq 0x28c2b(%rip), %rsi     # 0x50fa0
callq 0x4240
jmp 0x2837c
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
movq -0x10(%rbp), %rdi
callq 0x29d50
movq %rax, %rsi
leaq 0x2901f(%rip), %rdi     # 0x513b8
callq 0x17400
movq %rax, -0x1a8(%rbp)
jmp 0x283a7
movq -0x1a8(%rbp), %rdi
movq -0xa8(%rbp), %rsi
addq $0x8, %rsi
callq 0x174b0
movb %dl, -0x1b1(%rbp)
movq %rax, -0x1b0(%rbp)
jmp 0x283cd
movb -0x1b1(%rbp), %al
movq -0x1b0(%rbp), %rcx
movq %rcx, -0x90(%rbp)
movb %al, -0x88(%rbp)
movl -0x14(%rbp), %edi
movl -0x34(%rbp), %esi
callq 0x21ec0
movl %eax, -0x1b8(%rbp)
jmp 0x283fa
movl -0x1b8(%rbp), %eax
movl %eax, -0x94(%rbp)
cmpl $-0x1, -0x94(%rbp)
jne 0x284ee
leaq 0x2907e(%rip), %rdi     # 0x51498
leaq -0x14(%rbp), %rsi
callq 0x16ef0
movq %rax, -0x1c0(%rbp)
jmp 0x2842c
movq -0x1c0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x1cc(%rbp)
leaq 0x29056(%rip), %rdi     # 0x51498
leaq -0x14(%rbp), %rsi
callq 0x16ef0
movq %rax, -0x1c8(%rbp)
jmp 0x28454
movq -0x1c8(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x1e0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x29d50
movq -0x1e0(%rbp), %rdi
movq %rax, %rsi
callq 0x28f40
movq %rax, -0x1d8(%rbp)
jmp 0x28487
movq -0x1d8(%rbp), %rdi
movq -0xa8(%rbp), %rsi
addq $0x8, %rsi
callq 0x2a760
movq %rax, -0x1e8(%rbp)
jmp 0x284a7
movq -0x1e8(%rbp), %rax
movl -0x1cc(%rbp), %ecx
movl %ecx, (%rax)
movl -0x34(%rbp), %eax
movl %eax, -0x1f4(%rbp)
leaq 0x28fd2(%rip), %rdi     # 0x51498
leaq -0x14(%rbp), %rsi
callq 0x16ef0
movq %rax, -0x1f0(%rbp)
jmp 0x284d8
movq -0x1f0(%rbp), %rax
movl -0x1f4(%rbp), %ecx
addl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x28575
movl -0x94(%rbp), %eax
movl %eax, -0x204(%rbp)
leaq 0x28f97(%rip), %rdi     # 0x51498
leaq -0x14(%rbp), %rsi
callq 0x16ef0
movq %rax, -0x200(%rbp)
jmp 0x28513
movq -0x200(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x218(%rbp)
movq -0x10(%rbp), %rdi
callq 0x29d50
movq -0x218(%rbp), %rdi
movq %rax, %rsi
callq 0x28f40
movq %rax, -0x210(%rbp)
jmp 0x28546
movq -0x210(%rbp), %rdi
movq -0xa8(%rbp), %rsi
addq $0x8, %rsi
callq 0x2a760
movq %rax, -0x220(%rbp)
jmp 0x28566
movq -0x220(%rbp), %rax
movl -0x204(%rbp), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
callq 0x29d50
movq -0xa8(%rbp), %rdx
movq %rax, %rsi
addq $0x8, %rdx
leaq -0x9c(%rbp), %rdi
callq 0x29050
jmp 0x2859a
leaq 0x28f3f(%rip), %rdi     # 0x514e0
leaq -0x9c(%rbp), %rsi
callq 0x28f70
movq %rax, -0x228(%rbp)
jmp 0x285b6
movq -0x228(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x2a300
jmp 0x285c8
jmp 0x285ca
leaq -0x30(%rbp), %rdi
callq 0x16fe0
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x43d0
nopw %cs:(%rax,%rax)