// File: CHEN-Zhihan[P]Compiler/Node.cpp
// Function: FunctionNode::check(std::vector<int, std::allocator<int>>&, int)
void FunctionNode::check(vector<int>& sList, int functionID) {
    if (sList.back() != GLOBAL) {
        cerr << "Function " << reverseLookup[i] << " can only be defined in the global scope" << endl;
        exit(-1);
    }
    if (functionTable.count(i) != 0) {
        cerr << "Redefinition of function: " << reverseLookup[i] << endl;
        exit(-1);
    }
    variableTable[i] = unordered_set<int>();
    addressTable[i] ={0, unordered_map<int, unordered_map<ID, address> >()};
    int size = 0;
    functionTable[i] = this;
    unordered_set<int> duplicateSet;
    for (auto j = parameters.begin(); j != parameters.end(); ++j) {
        if (functionTable.count((*j)->getID()) != 0) {
            cerr << "Redefinition of function: " << reverseLookup[(*j)->getID()] << endl;
            exit(-1);
        }
        if (duplicateSet.count((*j)->getID()) != 0) {
            cerr << "duplicate parameter '" << reverseLookup[(*j)->getID()] << "' in function definition" << endl;
            exit(-1);
        }
        duplicateSet.insert((*j)->getID());
        variableTable[i].insert((*j)->getID());
        #if DEBUG
            cerr << "add variable " << reverseLookup[(*j)->getID()] << " to " << i << endl;
            cerr << "add variable of size " << (*j)->getDimensions() << " to " << i << " " << (*j)->getID() << endl;
        #endif
        addressTable[i].second[i][(*j)->getID()] = - 3 + (size++) - parameters.size();
        sizeMap[{i, (*j)->getID()}] = vector<int>((*j)->getDimensions());
    }
    #if DEBUG
        cerr << "adding function " << reverseLookup[this->i] << " to " << GLOBAL << endl;
    #endif
    variableTable[GLOBAL].insert(i);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x29d50
movl (%rax), %eax
cmpl 0x2bc9a(%rip), %eax     # 0x514dc
je 0x258af
movq 0x2b7a5(%rip), %rdi     # 0x50ff0
leaq 0x1104f(%rip), %rsi     # 0x368a1
callq 0x4210
movq %rax, %rcx
movq -0x1b0(%rbp), %rax
movq %rcx, -0x1b8(%rbp)
movslq 0x8(%rax), %rsi
leaq 0x2bad5(%rip), %rdi     # 0x51348
callq 0x29680
movq -0x1b8(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, %rdi
leaq 0x1101a(%rip), %rsi     # 0x368ab
callq 0x4210
movq %rax, %rdi
movq 0x2b700(%rip), %rsi     # 0x50fa0
callq 0x4240
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
movq -0x1b0(%rbp), %rsi
addq $0x8, %rsi
leaq 0x2babf(%rip), %rdi     # 0x51380
callq 0x29f80
cmpq $0x0, %rax
je 0x25928
movq 0x2b71d(%rip), %rdi     # 0x50ff0
leaq 0x109f6(%rip), %rsi     # 0x362d0
callq 0x4210
movq %rax, %rcx
movq -0x1b0(%rbp), %rax
movq %rcx, -0x1c0(%rbp)
movslq 0x8(%rax), %rsi
leaq 0x2ba4d(%rip), %rdi     # 0x51348
callq 0x29680
movq -0x1c0(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, %rdi
movq 0x2b687(%rip), %rsi     # 0x50fa0
callq 0x4240
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
xorps %xmm0, %xmm0
movaps %xmm0, -0x30(%rbp)
movaps %xmm0, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x20(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x173e0
movq -0x1b0(%rbp), %rsi
addq $0x8, %rsi
leaq 0x2ba5e(%rip), %rdi     # 0x513b8
callq 0x17400
movq %rax, -0x1c8(%rbp)
jmp 0x25968
movq -0x1c8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movq %rsi, -0x1d8(%rbp)
callq 0x17430
movq -0x1d8(%rbp), %rdi
callq 0x17460
movl $0x0, -0xa4(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movq $0x0, -0xb0(%rbp)
leaq -0xe0(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
callq 0x1ada0
movq -0x1d0(%rbp), %rdx
leaq -0xa0(%rbp), %rdi
leaq -0xa4(%rbp), %rsi
callq 0x29fb0
jmp 0x259e7
movq -0x1b0(%rbp), %rsi
addq $0x8, %rsi
leaq 0x2ba9f(%rip), %rdi     # 0x51498
callq 0x16ef0
movq %rax, -0x1e0(%rbp)
jmp 0x25a07
movq -0x1e0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x17530
leaq -0xa0(%rbp), %rdi
callq 0x17570
leaq -0xe0(%rbp), %rdi
callq 0x1aef0
movq -0x1b0(%rbp), %rsi
movl $0x0, -0xe4(%rbp)
addq $0x8, %rsi
leaq 0x2b932(%rip), %rdi     # 0x51380
callq 0x29ff0
movq -0x1b0(%rbp), %rcx
movq %rcx, (%rax)
leaq -0x120(%rbp), %rdi
callq 0x173e0
movq -0x1b0(%rbp), %rdi
addq $0x10, %rdi
callq 0x2a020
movq %rax, -0x128(%rbp)
movq -0x1b0(%rbp), %rdi
addq $0x10, %rdi
callq 0x2a090
movq %rax, -0x130(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x2a050
testb $0x1, %al
jne 0x25ab3
jmp 0x25fae
leaq -0x128(%rbp), %rdi
callq 0x2a0c0
movq %rax, %rdi
callq 0x292f0
movq %rax, %rdi
callq 0x231c0
movl %eax, -0x134(%rbp)
leaq 0x2b8a4(%rip), %rdi     # 0x51380
leaq -0x134(%rbp), %rsi
callq 0x29f80
movq %rax, -0x1e8(%rbp)
jmp 0x25af1
movq -0x1e8(%rbp), %rax
cmpq $0x0, %rax
je 0x25be2
movq 0x2b4e7(%rip), %rdi     # 0x50ff0
leaq 0x107c0(%rip), %rsi     # 0x362d0
callq 0x4210
movq %rax, -0x1f0(%rbp)
jmp 0x25b1e
leaq -0x128(%rbp), %rdi
callq 0x2a0c0
movq %rax, %rdi
callq 0x292f0
movq %rax, %rdi
callq 0x231c0
movslq %eax, %rsi
leaq 0x2b804(%rip), %rdi     # 0x51348
callq 0x29680
movq -0x1f0(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, -0x1f8(%rbp)
jmp 0x25b61
movq -0x1f8(%rbp), %rdi
movq 0x2b431(%rip), %rsi     # 0x50fa0
callq 0x4240
jmp 0x25b76
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x17460
jmp 0x2602b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x25bc0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x17570
leaq -0xe0(%rbp), %rdi
callq 0x1aef0
jmp 0x2602b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x2601f
leaq -0x128(%rbp), %rdi
callq 0x2a0c0
movq %rax, %rdi
callq 0x292f0
movq %rax, %rdi
callq 0x231c0
movl %eax, -0x138(%rbp)
leaq -0x120(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x29540
movq %rax, -0x200(%rbp)
jmp 0x25c20
movq -0x200(%rbp), %rax
cmpq $0x0, %rax
je 0x25ccb
movq 0x2b3b8(%rip), %rdi     # 0x50ff0
leaq 0x10c95(%rip), %rsi     # 0x368d4
callq 0x4210
movq %rax, -0x208(%rbp)
jmp 0x25c4d
leaq -0x128(%rbp), %rdi
callq 0x2a0c0
movq %rax, %rdi
callq 0x292f0
movq %rax, %rdi
callq 0x231c0
movslq %eax, %rsi
leaq 0x2b6d5(%rip), %rdi     # 0x51348
callq 0x29680
movq -0x208(%rbp), %rdi
movq %rax, %rsi
callq 0x41f0
movq %rax, -0x210(%rbp)
jmp 0x25c90
movq -0x210(%rbp), %rdi
leaq 0x10c4c(%rip), %rsi     # 0x368ea
callq 0x4210
movq %rax, -0x218(%rbp)
jmp 0x25cac
movq -0x218(%rbp), %rdi
movq 0x2b2e6(%rip), %rsi     # 0x50fa0
callq 0x4240
jmp 0x25cc1
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
leaq -0x128(%rbp), %rdi
callq 0x2a0c0
movq %rax, %rdi
callq 0x292f0
movq %rax, %rdi
callq 0x231c0
movl %eax, -0x13c(%rbp)
leaq -0x120(%rbp), %rdi
leaq -0x13c(%rbp), %rsi
callq 0x2a0e0
movb %dl, -0x221(%rbp)
movq %rax, -0x220(%rbp)
jmp 0x25d0f
movq -0x1b0(%rbp), %rsi
movb -0x221(%rbp), %al
movq -0x220(%rbp), %rcx
movq %rcx, -0x150(%rbp)
movb %al, -0x148(%rbp)
addq $0x8, %rsi
leaq 0x2b67d(%rip), %rdi     # 0x513b8
callq 0x17400
movq %rax, -0x230(%rbp)
jmp 0x25d49
leaq -0x128(%rbp), %rdi
callq 0x2a0c0
movq %rax, %rdi
callq 0x292f0
movq %rax, %rdi
callq 0x231c0
movq -0x230(%rbp), %rdi
movl %eax, -0x154(%rbp)
leaq -0x154(%rbp), %rsi
callq 0x2a0e0
movb %dl, -0x239(%rbp)
movq %rax, -0x238(%rbp)
jmp 0x25d8d
movq -0x1b0(%rbp), %rdi
movb -0x239(%rbp), %al
movq -0x238(%rbp), %rcx
movq %rcx, -0x168(%rbp)
movb %al, -0x160(%rbp)
movl -0xe4(%rbp), %eax
movl %eax, -0x250(%rbp)
incl %eax
movl %eax, -0xe4(%rbp)
addq $0x10, %rdi
callq 0x29f60
movq -0x1b0(%rbp), %rsi
movq %rax, %rcx
movl -0x250(%rbp), %eax
subl %ecx, %eax
addl $-0x3, %eax
movl %eax, -0x24c(%rbp)
addq $0x8, %rsi
leaq 0x2b6a7(%rip), %rdi     # 0x51498
callq 0x16ef0
movq %rax, -0x248(%rbp)
jmp 0x25dff
movq -0x1b0(%rbp), %rsi
movq -0x248(%rbp), %rdi
addq $0x8, %rdi
addq $0x8, %rsi
callq 0x28f40
movq %rax, -0x258(%rbp)
jmp 0x25e23
leaq -0x128(%rbp), %rdi
callq 0x2a0c0
movq %rax, %rdi
callq 0x292f0
movq %rax, %rdi
callq 0x231c0
movq -0x258(%rbp), %rdi
movl %eax, -0x16c(%rbp)
leaq -0x16c(%rbp), %rsi
callq 0x2a120
movq %rax, -0x260(%rbp)
jmp 0x25e61
movq -0x260(%rbp), %rax
movl -0x24c(%rbp), %ecx
movl %ecx, (%rax)
leaq -0x128(%rbp), %rdi
callq 0x2a0c0
movq %rax, %rdi
callq 0x292f0
movq %rax, %rdi
callq 0x230d0
movl %eax, -0x264(%rbp)
jmp 0x25e94
movl -0x264(%rbp), %eax
cltq
movq %rax, -0x278(%rbp)
leaq -0x189(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0x16f20
movq -0x278(%rbp), %rsi
movq -0x270(%rbp), %rdx
leaq -0x188(%rbp), %rdi
callq 0x2a150
jmp 0x25ed2
movq -0x1b0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x280(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x2a0c0
movq %rax, %rdi
callq 0x292f0
movq %rax, %rdi
callq 0x231c0
movq -0x280(%rbp), %rsi
movl %eax, -0x198(%rbp)
leaq -0x194(%rbp), %rdi
leaq -0x198(%rbp), %rdx
callq 0x2a1c0
jmp 0x25f22
leaq 0x2b5b7(%rip), %rdi     # 0x514e0
leaq -0x194(%rbp), %rsi
callq 0x28f70
movq %rax, -0x288(%rbp)
jmp 0x25f3e
movq -0x288(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x17650
leaq -0x188(%rbp), %rdi
callq 0x16fe0
leaq -0x189(%rbp), %rdi
callq 0x16fc0
leaq -0x128(%rbp), %rdi
callq 0x2a1f0
jmp 0x25a80
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x25fa0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x16fe0
leaq -0x189(%rbp), %rdi
callq 0x16fc0
jmp 0x2601f
leaq 0x2b403(%rip), %rdi     # 0x513b8
leaq 0x2b520(%rip), %rsi     # 0x514dc
callq 0x17400
movq %rax, -0x290(%rbp)
jmp 0x25fca
movq -0x290(%rbp), %rdi
movq -0x1b0(%rbp), %rsi
addq $0x8, %rsi
callq 0x174b0
movb %dl, -0x299(%rbp)
movq %rax, -0x298(%rbp)
jmp 0x25ff0
movb -0x299(%rbp), %al
movq -0x298(%rbp), %rcx
movq %rcx, -0x1a8(%rbp)
movb %al, -0x1a0(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x17460
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
leaq -0x120(%rbp), %rdi
callq 0x17460
movq -0x58(%rbp), %rdi
callq 0x43d0
nopw %cs:(%rax,%rax)