// File: /workspace/github_cpp_makefile_star2_O3/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
subq $0x38, %rsp
movl %edi, %r14d
leaq 0xa9dd9(%rip), %rdi     # 0xb2908
leaq 0xaa1a(%rip), %rsi      # 0x13550
callq 0x2090
testl %eax, %eax
je 0x8d2c
movslq %r14d, %rax
imulq $0xa8, %rax, %rax
leaq 0xb0574(%rip), %rcx     # 0xb90c8
leaq (%rcx,%rax), %r13
addq $0x8, %r13
movq 0xa9dad(%rip), %rdx     # 0xb2910
cmpq 0x88(%r13), %rdx
jne 0x8b90
testq %rdx, %rdx
je 0x8d2c
movq 0x80(%r13), %rsi
movq 0xa9d85(%rip), %rdi     # 0xb2908
callq 0x2240
testl %eax, %eax
je 0x8d2c
movq 0xa9d41(%rip), %rbx     # 0xb28d8
cmpq 0xa9d42(%rip), %rbx     # 0xb28e0
je 0x8d25
leaq 0xa4(%r13), %r14
movq 0xa9d5e(%rip), %rbp     # 0xb2910
movq 0xa9d4f(%rip), %rax     # 0xb2908
movq %rax, 0x28(%rsp)
movq 0xa9cfb(%rip), %rax     # 0xb28c0
movq 0xa9cfc(%rip), %rcx     # 0xb28c8
movq %rcx, (%rsp)
movq 0xa9d09(%rip), %rcx     # 0xb28e0
movq %rcx, 0x30(%rsp)
movq %rax, 0x18(%rsp)
addq $0x30, %rax
movq %rax, 0x8(%rsp)
movq %rbp, 0x20(%rsp)
movq 0x38(%rbx), %rdx
cmpq %rbp, %rdx
jne 0x8d16
testq %rdx, %rdx
je 0x8c17
movq 0x30(%rbx), %rdi
movq 0x28(%rsp), %rsi
callq 0x2240
testl %eax, %eax
jne 0x8d16
movq (%rbx), %r15
movq 0x8(%rbx), %rbp
cmpq %rbp, %r15
je 0x8c68
movq 0x88(%r13), %r12
addq $0x30, %r15
cmpq %r12, -0x28(%r15)
jne 0x8c58
testq %r12, %r12
je 0x8df2
movq 0x80(%r13), %rsi
movq -0x30(%r15), %rdi
movq %r12, %rdx
callq 0x2240
testl %eax, %eax
je 0x8df2
leaq 0x50(%r15), %rax
addq $0x20, %r15
cmpq %rbp, %r15
movq %rax, %r15
jne 0x8c2e
movq %r14, 0x10(%rsp)
movq 0x18(%rbx), %r12
movq 0x20(%rbx), %r14
cmpq %r14, %r12
je 0x8cba
movq 0x88(%r13), %r15
cmpq %r15, 0x8(%r12)
jne 0x8cb1
movl $0x2, %ebp
testq %r15, %r15
je 0x8e91
movq 0x80(%r13), %rsi
movq (%r12), %rdi
movq %r15, %rdx
callq 0x2240
testl %eax, %eax
je 0x8e91
addq $0x50, %r12
cmpq %r14, %r12
jne 0x8c81
movq (%rsp), %rax
cmpq %rax, 0x18(%rsp)
movq 0x10(%rsp), %r14
movq 0x20(%rsp), %rbp
je 0x8d16
movq 0x88(%r13), %r12
movq 0x8(%rsp), %r15
cmpq %r12, -0x28(%r15)
jne 0x8d05
testq %r12, %r12
je 0x8e2e
movq 0x80(%r13), %rsi
movq -0x30(%r15), %rdi
movq %r12, %rdx
callq 0x2240
testl %eax, %eax
je 0x8e2e
leaq 0x50(%r15), %rax
addq $0x20, %r15
cmpq (%rsp), %r15
movq %rax, %r15
jne 0x8cdb
addq $0x60, %rbx
cmpq 0x30(%rsp), %rbx
jne 0x8bef
xorl %ebp, %ebp
jmp 0x8e91
movq 0xa9b8d(%rip), %rbx     # 0xb28c0
movq 0xa9b8e(%rip), %r12     # 0xb28c8
xorl %ebp, %ebp
cmpq %r12, %rbx
je 0x8e91
movslq %r14d, %rax
imulq $0xa8, %rax, %rax
leaq 0xb0372(%rip), %rcx     # 0xb90c8
leaq (%rcx,%rax), %r14
addq $0x8, %r14
movq 0x88(%r14), %r15
addq $0x30, %rbx
cmpq %r15, -0x28(%rbx)
jne 0x8d8b
testq %r15, %r15
je 0x8da0
movq 0x80(%r14), %rsi
movq -0x30(%rbx), %rdi
movq %r15, %rdx
callq 0x2240
testl %eax, %eax
je 0x8da0
leaq 0x50(%rbx), %rax
addq $0x20, %rbx
cmpq %r12, %rbx
movq %rax, %rbx
jne 0x8d69
jmp 0x8e91
movq (%rbx), %rax
movq 0x8(%rbx), %rsi
cmpq %rsi, %rax
je 0x8dcf
movl 0xa4(%r14), %ecx
xorl %edx, %edx
cmpl %ecx, (%rax,%rdx,2)
je 0x8e8c
leaq (%rax,%rdx), %rdi
addq $0x4, %rdi
addq $0x4, %rdx
cmpq %rsi, %rdi
jne 0x8db5
addq $0xa4, %r14
cmpq 0x10(%rbx), %rsi
je 0x8e6a
movl (%r14), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x8e8c
movq (%r15), %rax
movq 0x8(%r15), %rsi
cmpq %rsi, %rax
je 0x8e19
movl (%r14), %ecx
xorl %edx, %edx
cmpl %ecx, (%rax,%rdx,2)
je 0x8e7a
leaq (%rax,%rdx), %rdi
addq $0x4, %rdi
addq $0x4, %rdx
cmpq %rsi, %rdi
jne 0x8e03
cmpq 0x10(%r15), %rsi
je 0x8e6f
movl (%r14), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r15)
jmp 0x8e7a
movq (%r15), %rax
movq 0x8(%r15), %rsi
cmpq %rsi, %rax
je 0x8e55
movl (%r14), %ecx
xorl %edx, %edx
cmpl %ecx, (%rax,%rdx,2)
je 0x8e8c
leaq (%rax,%rdx), %rdi
addq $0x4, %rdi
addq $0x4, %rdx
cmpq %rsi, %rdi
jne 0x8e3f
cmpq 0x10(%r15), %rsi
je 0x8e81
movl (%r14), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r15)
jmp 0x8e8c
movq %rbx, %rdi
jmp 0x8e84
movq %r15, %rdi
movq %r14, %rdx
callq 0xf5d0
movl $0x2, %ebp
jmp 0x8e91
movq %r15, %rdi
movq %r14, %rdx
callq 0xf5d0
movl $0x1, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq