// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edi, %ebx
leaq 0xa6e0c(%rip), %rdi     # 0xad8d8
leaq 0x7aad(%rip), %rsi      # 0xe580
callq 0x5f92
movslq %ebx, %r14
testb %al, %al
jne 0x6b0f
imulq $0xa8, %r14, %rbx
leaq 0xad5ab(%rip), %r12     # 0xb4098
leaq (%r12,%rbx), %r15
addq $0x88, %r15
leaq 0xa6dd9(%rip), %rdi     # 0xad8d8
movq %r15, %rsi
callq 0x5f9f
testb %al, %al
je 0x6b93
movq 0xa6d7a(%rip), %rbx     # 0xad890
imulq $0xa8, %r14, %rax
leaq 0xad574(%rip), %rcx     # 0xb4098
leaq (%rcx,%rax), %r14
addq $0x8, %r14
leaq (%rcx,%rax), %r12
addq $0x88, %r12
addq $0x30, %rbx
leaq -0x30(%rbx), %r15
movq 0xa6d52(%rip), %r13     # 0xad898
cmpq %r13, %r15
je 0x6c51
movq %r15, %rdi
movq %r12, %rsi
callq 0x5f9f
testb %al, %al
jne 0x6b64
addq $0x50, %rbx
jmp 0x6b3b
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
addq $0xa4, %r14
xorl %edx, %edx
leaq (%rax,%rdx), %rsi
cmpq %rcx, %rsi
je 0x6c46
movl (%rax,%rdx,2), %esi
cmpl (%r14), %esi
je 0x6c51
addq $0x4, %rdx
jmp 0x6b74
leaq (%r12,%rbx), %r14
addq $0x8, %r14
movq 0xa6d06(%rip), %rbp     # 0xad8a8
addq $0xa4, %r14
leaq 0xa6d28(%rip), %r12     # 0xad8d8
pushq $0x2
popq %rbx
cmpq 0xa6cf6(%rip), %rbp     # 0xad8b0
je 0x6cbc
leaq 0x30(%rbp), %rdi
movq %r12, %rsi
callq 0x5f9f
testb %al, %al
je 0x6c3d
movq (%rbp), %r13
addq $0x30, %r13
leaq -0x30(%r13), %rdi
cmpq 0x8(%rbp), %rdi
je 0x6bf4
movq %r15, %rsi
callq 0x5f9f
testb %al, %al
jne 0x6c6a
addq $0x50, %r13
jmp 0x6bd8
movq 0x18(%rbp), %r13
cmpq 0x20(%rbp), %r13
je 0x6c13
movq %r13, %rdi
movq %r15, %rsi
callq 0x5f9f
addq $0x50, %r13
testb %al, %al
je 0x6bf8
jmp 0x6c59
movq 0xa6c76(%rip), %r13     # 0xad890
addq $0x30, %r13
leaq -0x30(%r13), %rdi
cmpq 0xa6c6f(%rip), %rdi     # 0xad898
je 0x6c3d
movq %r15, %rsi
callq 0x5f9f
testb %al, %al
jne 0x6c8b
addq $0x50, %r13
jmp 0x6c1e
addq $0x60, %rbp
jmp 0x6bb3
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa628
xorl %ebx, %ebx
cmpq %r13, %r15
setne %bl
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r13), %rax
movq 0x8(%r13), %rcx
xorl %edx, %edx
leaq (%rax,%rdx), %rsi
cmpq %rcx, %rsi
je 0x6caf
movl (%rax,%rdx,2), %esi
cmpl (%r14), %esi
je 0x6c59
addq $0x4, %rdx
jmp 0x6c74
movq (%r13), %rax
movq 0x8(%r13), %rcx
xorl %edx, %edx
pushq $0x1
popq %rbx
leaq (%rax,%rdx), %rsi
cmpq %rcx, %rsi
je 0x6caf
movl (%rax,%rdx,2), %esi
cmpl (%r14), %esi
je 0x6c59
addq $0x4, %rdx
jmp 0x6c98
movq %r13, %rdi
movq %r14, %rsi
callq 0xa628
jmp 0x6c59
xorl %ebx, %ebx
jmp 0x6c59