// File: zhengwangsun[P]pas2c/semantics.cpp
// Function: isDefined(int)
int isDefined(int position)
{
    if(currentTable == "main"||currentTable == tree.node[position].value)//若当前符号表为主表,或者要查询的id是子表名字，则查询主表
    {
        for(vector<Tuple>::iterator it = mainTable.begin(); it != mainTable.end();it++)//遍历查重
        {
            if(it->name == tree.node[position].value)
            {
                int i = 0, flag = 0;
                for(vector<int>::iterator it1 = it->useRow.begin(); it1 != it->useRow.end();it1++)
                {
                    if (it1[i] == tree.node[position].row)
                    {
                        flag = 1;
                        break;
                    }
                    i++;
                }
                if(flag == 0)
                    it->useRow.push_back(tree.node[position].row);
                return 1;
            }
        }
        return 0;
    }
    else//若当前符号表是子表且符号不是子表名字
    {
        //先遍历子表序列
        for(vector<subTableItem>::iterator it = subTable.begin(); it != subTable.end();it++)
        {
            if(it->name == currentTable)//找到当前子表
            {
                for(vector<Tuple>::iterator it0 = it->item.begin(); it0 != it->item.end();it0++)//遍历子表中的item
                {
                    if(it0->name == tree.node[position].value)
                    {
                        int i = 0, flag = 0;
                        for(vector<int>::iterator it1 = it0->useRow.begin(); it1 != it0->useRow.end();it1++)
                        {
                            if (it1[i] == tree.node[position].row)
                            {
                                flag = 1;
                                break;
                            }
                            i++;
                        }
                        
                        if (flag == 0)
                            it0->useRow.push_back(tree.node[position].row);
                        return 2;
                    }
                }
                
                for(vector<Tuple>::iterator it1 = it->argument.begin(); it1 != it->argument.end();it1++)//遍历子表中的argument
                {
                    if(it1->name == tree.node[position].value)
                        return 2;
                }
                //还没找到，再遍历主表
                for(vector<Tuple>::iterator it = mainTable.begin(); it != mainTable.end();it++)//遍历查重
                {
                    if(it->name == tree.node[position].value)
                    {
                        int i = 0, flag = 0;
                        for(vector<int>::iterator it1 = it->useRow.begin(); it1 != it->useRow.end();it1++)
                        {
                            if (it1[i] == tree.node[position].row)
                            {
                                flag = 1;
                                break;
                            }
                            i++;
                        }
                        if(flag == 0)
                            it->useRow.push_back(tree.node[position].row);
                        return 1;
                    }
                }
                
            }
        }
        return 0;
    }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movl %edi, -0x8(%rbp)
leaq 0xb1ee3(%rip), %rdi     # 0xbf978
leaq 0x10a1e(%rip), %rsi     # 0x1e4ba
callq 0xc770
testb $0x1, %al
jne 0xdada
movslq -0x8(%rbp), %rax
leaq 0xb8688(%rip), %rsi     # 0xc6138
addq $0x8, %rsi
imulq $0xa8, %rax, %rax
addq %rax, %rsi
addq $0x80, %rsi
leaq 0xb1eac(%rip), %rdi     # 0xbf978
callq 0xc840
testb $0x1, %al
jne 0xdada
jmp 0xdc59
leaq 0xb1e4f(%rip), %rdi     # 0xbf930
callq 0x14c50
movq %rax, -0x10(%rbp)
leaq 0xb1e3f(%rip), %rdi     # 0xbf930
callq 0x14cc0
movq %rax, -0x18(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x14c80
testb $0x1, %al
jne 0xdb10
jmp 0xdc4d
leaq -0x10(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
movslq -0x8(%rbp), %rax
leaq 0xb8611(%rip), %rsi     # 0xc6138
addq $0x8, %rsi
imulq $0xa8, %rax, %rax
addq %rax, %rsi
addq $0x80, %rsi
callq 0xc840
testb $0x1, %al
jne 0xdb4a
jmp 0xdc37
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
addq $0x30, %rdi
callq 0x14fc0
movq %rax, -0x28(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
addq $0x30, %rdi
callq 0x15030
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x14ff0
testb $0x1, %al
jne 0xdb9d
jmp 0xdbf0
movslq -0x1c(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x15060
movl (%rax), %eax
movslq -0x8(%rbp), %rdx
leaq 0xb8581(%rip), %rcx     # 0xc6138
addq $0x8, %rcx
imulq $0xa8, %rdx, %rdx
addq %rdx, %rcx
cmpl 0xa4(%rcx), %eax
jne 0xdbd6
movl $0x1, -0x20(%rbp)
jmp 0xdbf0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
callq 0x15080
movq %rax, -0x38(%rbp)
jmp 0xdb71
cmpl $0x0, -0x20(%rbp)
jne 0xdc2b
leaq -0x10(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
addq $0x30, %rdi
movslq -0x8(%rbp), %rax
leaq 0xb8527(%rip), %rsi     # 0xc6138
addq $0x8, %rsi
imulq $0xa8, %rax, %rax
addq %rax, %rsi
addq $0xa4, %rsi
callq 0x14d90
movl $0x1, -0x4(%rbp)
jmp 0xe0c4
jmp 0xdc39
leaq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x14d30
movq %rax, -0x40(%rbp)
jmp 0xdaea
movl $0x0, -0x4(%rbp)
jmp 0xe0c4
leaq 0xb1ce8(%rip), %rdi     # 0xbf948
callq 0x14b00
movq %rax, -0x48(%rbp)
leaq 0xb1cd8(%rip), %rdi     # 0xbf948
callq 0x14b70
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x14b30
testb $0x1, %al
jne 0xdc8f
jmp 0xe0bd
leaq -0x48(%rbp), %rdi
callq 0x14ba0
movq %rax, %rdi
addq $0x30, %rdi
leaq 0xb1cd2(%rip), %rsi     # 0xbf978
callq 0xc840
testb $0x1, %al
jne 0xdcb4
jmp 0xe0a4
leaq -0x48(%rbp), %rdi
callq 0x14ba0
movq %rax, %rdi
callq 0x14c50
movq %rax, -0x58(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x14ba0
movq %rax, %rdi
callq 0x14cc0
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x14c80
testb $0x1, %al
jne 0xdcf4
jmp 0xde34
leaq -0x58(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
movslq -0x8(%rbp), %rax
leaq 0xb842d(%rip), %rsi     # 0xc6138
addq $0x8, %rsi
imulq $0xa8, %rax, %rax
addq %rax, %rsi
addq $0x80, %rsi
callq 0xc840
testb $0x1, %al
jne 0xdd2e
jmp 0xde1b
movl $0x0, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
addq $0x30, %rdi
callq 0x14fc0
movq %rax, -0x70(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
addq $0x30, %rdi
callq 0x15030
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x14ff0
testb $0x1, %al
jne 0xdd81
jmp 0xddd4
movslq -0x64(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x15060
movl (%rax), %eax
movslq -0x8(%rbp), %rdx
leaq 0xb839d(%rip), %rcx     # 0xc6138
addq $0x8, %rcx
imulq $0xa8, %rdx, %rdx
addq %rdx, %rcx
cmpl 0xa4(%rcx), %eax
jne 0xddba
movl $0x1, -0x68(%rbp)
jmp 0xddd4
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0x15080
movq %rax, -0x80(%rbp)
jmp 0xdd55
cmpl $0x0, -0x68(%rbp)
jne 0xde0f
leaq -0x58(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
addq $0x30, %rdi
movslq -0x8(%rbp), %rax
leaq 0xb8343(%rip), %rsi     # 0xc6138
addq $0x8, %rsi
imulq $0xa8, %rax, %rax
addq %rax, %rsi
addq $0xa4, %rsi
callq 0x14d90
movl $0x2, -0x4(%rbp)
jmp 0xe0c4
jmp 0xde1d
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x14d30
movq %rax, -0x88(%rbp)
jmp 0xdcc9
leaq -0x48(%rbp), %rdi
callq 0x14ba0
movq %rax, %rdi
addq $0x18, %rdi
callq 0x14c50
movq %rax, -0x90(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x14ba0
movq %rax, %rdi
addq $0x18, %rdi
callq 0x14cc0
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x14c80
testb $0x1, %al
jne 0xde85
jmp 0xdee7
leaq -0x90(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
movslq -0x8(%rbp), %rax
leaq 0xb8299(%rip), %rsi     # 0xc6138
addq $0x8, %rsi
imulq $0xa8, %rax, %rax
addq %rax, %rsi
addq $0x80, %rsi
callq 0xc840
testb $0x1, %al
jne 0xdebf
jmp 0xdecb
movl $0x2, -0x4(%rbp)
jmp 0xe0c4
jmp 0xdecd
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x14d30
movq %rax, -0xa0(%rbp)
jmp 0xde50
leaq 0xb1a42(%rip), %rdi     # 0xbf930
callq 0x14c50
movq %rax, -0xa8(%rbp)
leaq 0xb1a2f(%rip), %rdi     # 0xbf930
callq 0x14cc0
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x14c80
testb $0x1, %al
jne 0xdf29
jmp 0xe0a2
leaq -0xa8(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
movslq -0x8(%rbp), %rax
leaq 0xb81f5(%rip), %rsi     # 0xc6138
addq $0x8, %rsi
imulq $0xa8, %rax, %rax
addq %rax, %rsi
addq $0x80, %rsi
callq 0xc840
testb $0x1, %al
jne 0xdf66
jmp 0xe086
movl $0x0, -0xb4(%rbp)
movl $0x0, -0xb8(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
addq $0x30, %rdi
callq 0x14fc0
movq %rax, -0xc0(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
addq $0x30, %rdi
callq 0x15030
movq %rax, -0xc8(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x14ff0
testb $0x1, %al
jne 0xdfd1
jmp 0xe03c
movslq -0xb4(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x15060
movl (%rax), %eax
movslq -0x8(%rbp), %rdx
leaq 0xb8147(%rip), %rcx     # 0xc6138
addq $0x8, %rcx
imulq $0xa8, %rdx, %rdx
addq %rdx, %rcx
cmpl 0xa4(%rcx), %eax
jne 0xe013
movl $0x1, -0xb8(%rbp)
jmp 0xe03c
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
callq 0x15080
movq %rax, -0xd0(%rbp)
jmp 0xdf99
cmpl $0x0, -0xb8(%rbp)
jne 0xe07d
leaq -0xa8(%rbp), %rdi
callq 0x14cf0
movq %rax, %rdi
addq $0x30, %rdi
movslq -0x8(%rbp), %rax
leaq 0xb80d5(%rip), %rsi     # 0xc6138
addq $0x8, %rsi
imulq $0xa8, %rax, %rax
addq %rax, %rsi
addq $0xa4, %rsi
callq 0x14d90
movl $0x1, -0x4(%rbp)
jmp 0xe0c4
jmp 0xe088
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
callq 0x14d30
movq %rax, -0xd8(%rbp)
jmp 0xdefa
jmp 0xe0a4
jmp 0xe0a6
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x14bc0
movq %rax, -0xe0(%rbp)
jmp 0xdc69
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq