// File: Konano[P]UselessDatabase/Table.cpp
// Function: Table::constraintKey(Key*)
int Table::constraintKey(Key* key) {
    Anys data, _data;
    if (key->ty() == 1) {
        std::map<Anys, bool> m;
        Pointer p(this);
        while (p.next()) {
            data = p.get();
            for (auto i: key->v) {
                if (data[i].isNull()) return -1;
                _data.push_back(data[i]);
            }
            if (m.count(_data)) return -2;
            m[_data] = true;
        }
        // for (uint record_id = 0; record_id < record_num; record_id++) {
        //     if (queryRecord(record_id, data)) continue;
        //     Anys as;
        //     for (auto i: key->v) {
        //         if (data[i].isNull()) return -1;
        //         as.push_back(data[i]);
        //     }
        //     if (m.count(as)) return -2;
        //     m[as] = true;
        // }
    } else {
        Table* p_table = dynamic_cast<ForeignKey*>(key)->p->table;
        if (p_table->index[p_table->p_key_index].fileID == -1) 
            p_table->index[p_table->p_key_index].open();
        Pointer p(this);
        while (p.next()) {
            data = p.get();
            Anys _data;
            bool null = true, non_null = true;
            for (auto i: key->v) {
                null &= data[i].isNull();
                non_null &= !data[i].isNull();
                _data.push_back(data[i]);
            }
            if (null == false && non_null == false) return -1;
            if (p_table->index[p_table->p_key_index].queryRecord(&_data).size() == 0) return -4;
        }
        // for (uint record_id = 0; record_id < record_num; record_id++) {
        //     if (queryRecord(record_id, data)) continue;
        //     Anys as;
        //     bool null = true, non_null = true;
        //     for (auto i: key->v) {
        //         null &= data[i].isNull();
        //         non_null &= !data[i].isNull();
        //         as.push_back(data[i]);
        //     }
        //     if (null == false && non_null == false) return -1;
        //     Anys _data = Anys(data, col_num);
        //     Anys val = chooseData(_data, key->v);
        //     Table* p_table = dynamic_cast<ForeignKey*>(key)->p->table;
        //     if (p_table->index[p_table->p_key_index].queryRecord(&val).size() == 0) return -4;
        // }
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x180(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xc310
leaq -0x48(%rbp), %rdi
callq 0xc310
jmp 0x4cd22
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
movl %eax, -0x184(%rbp)
jmp 0x4cd36
movl -0x184(%rbp), %eax
cmpl $0x1, %eax
jne 0x4cfbd
leaq -0x88(%rbp), %rdi
callq 0x53780
movq -0x180(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x47150
jmp 0x4cd6b
jmp 0x4cd6d
leaq -0xa0(%rbp), %rdi
callq 0x46fe0
movb %al, -0x185(%rbp)
jmp 0x4cd81
movb -0x185(%rbp), %al
testb $0x1, %al
jne 0x4cd90
jmp 0x4cf81
leaq -0xb8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x471c0
jmp 0x4cda5
leaq -0x30(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x42450
leaq -0xb8(%rbp), %rdi
callq 0xc430
movq -0x18(%rbp), %rax
addq $0x30, %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x3d210
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x3d240
movq %rax, -0xd0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3d270
testb $0x1, %al
jne 0x4ce12
jmp 0x4cf20
leaq -0xc8(%rbp), %rdi
callq 0x3d2b0
movl (%rax), %eax
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0xc8f0
movq %rax, -0x190(%rbp)
jmp 0x4ce3e
movq -0x190(%rbp), %rdi
callq 0xc920
movb %al, -0x191(%rbp)
jmp 0x4ce52
movb -0x191(%rbp), %al
testb $0x1, %al
jne 0x4ce5e
jmp 0x4cea7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0xd8(%rbp)
jmp 0x4cf8b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x4d38d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x4d384
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x4cfac
movl -0xd4(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0xc8f0
movq %rax, -0x1a0(%rbp)
jmp 0x4cebf
movq -0x1a0(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0xca00
jmp 0x4ced4
leaq -0x48(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0xc330
jmp 0x4cee6
leaq -0xe0(%rbp), %rdi
callq 0xc3c0
leaq -0xc8(%rbp), %rdi
callq 0x3d3b0
jmp 0x4cdf6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0xc3c0
jmp 0x4cfac
leaq -0x88(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x537a0
movq %rax, -0x1a8(%rbp)
jmp 0x4cf39
movq -0x1a8(%rbp), %rax
cmpq $0x0, %rax
je 0x4cf59
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
movl $0x1, -0xd8(%rbp)
jmp 0x4cf8b
leaq -0x88(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x53800
movq %rax, -0x1b0(%rbp)
jmp 0x4cf72
movq -0x1b0(%rbp), %rax
movb $0x1, (%rax)
jmp 0x4cd6d
movl $0x0, -0xd8(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x538e0
movl -0xd8(%rbp), %eax
testl %eax, %eax
jne 0x4d366
jmp 0x4cfa7
jmp 0x4d355
leaq -0x88(%rbp), %rdi
callq 0x538e0
jmp 0x4d384
movq -0x18(%rbp), %rax
movq %rax, -0x1b8(%rbp)
cmpq $0x0, %rax
je 0x4cff5
movq -0x1b8(%rbp), %rdi
leaq 0x63c54(%rip), %rsi     # 0xb0c30
leaq 0x63c5d(%rip), %rdx     # 0xb0c40
xorl %eax, %eax
movl %eax, %ecx
callq 0x6590
movq %rax, -0x1c0(%rbp)
jmp 0x4d000
xorl %eax, %eax
movq %rax, -0x1c0(%rbp)
jmp 0x4d000
movq -0x1c0(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x48(%rax), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
addq $0x7a0, %rax            # imm = 0x7A0
movq -0xe8(%rbp), %rcx
movslq 0x778(%rcx), %rcx
imulq $0x98, %rcx, %rcx
addq %rcx, %rax
cmpl $-0x1, 0x74(%rax)
jne 0x4d067
movq -0xe8(%rbp), %rax
movslq 0x778(%rax), %rcx
imulq $0x98, %rcx, %rcx
leaq 0x7a0(%rax,%rcx), %rdi
callq 0x36ad0
jmp 0x4d065
jmp 0x4d067
movq -0x180(%rbp), %rsi
leaq -0x100(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x47150
jmp 0x4d081
jmp 0x4d083
leaq -0x100(%rbp), %rdi
callq 0x46fe0
movb %al, -0x1c1(%rbp)
jmp 0x4d097
movb -0x1c1(%rbp), %al
testb $0x1, %al
jne 0x4d0a6
jmp 0x4d353
leaq -0x118(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x471c0
jmp 0x4d0bb
leaq -0x30(%rbp), %rdi
leaq -0x118(%rbp), %rsi
movq %rsi, -0x1d0(%rbp)
callq 0x42450
movq -0x1d0(%rbp), %rdi
callq 0xc430
leaq -0x130(%rbp), %rdi
callq 0xc310
jmp 0x4d0ec
movb $0x1, -0x131(%rbp)
movb $0x1, -0x132(%rbp)
movq -0x18(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rdi
callq 0x3d210
movq %rax, -0x148(%rbp)
movq -0x140(%rbp), %rdi
callq 0x3d240
movq %rax, -0x150(%rbp)
leaq -0x148(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x3d270
testb $0x1, %al
jne 0x4d14b
jmp 0x4d27a
leaq -0x148(%rbp), %rdi
callq 0x3d2b0
movl (%rax), %eax
movl %eax, -0x154(%rbp)
movl -0x154(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0xc8f0
movq %rax, -0x1d8(%rbp)
jmp 0x4d177
movq -0x1d8(%rbp), %rdi
callq 0xc920
movb %al, -0x1d9(%rbp)
jmp 0x4d18b
movb -0x1d9(%rbp), %al
movzbl %al, %eax
movzbl -0x131(%rbp), %ecx
andl %ecx, %eax
andb $0x1, %al
movb %al, -0x131(%rbp)
movl -0x154(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0xc8f0
movq %rax, -0x1e8(%rbp)
jmp 0x4d1bd
movq -0x1e8(%rbp), %rdi
callq 0xc920
movb %al, -0x1e9(%rbp)
jmp 0x4d1d1
movb -0x1e9(%rbp), %al
notb %al
movzbl %al, %eax
movzbl -0x132(%rbp), %ecx
andl %ecx, %eax
andb $0x1, %al
movb %al, -0x132(%rbp)
movl -0x154(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0xc8f0
movq %rax, -0x1f8(%rbp)
jmp 0x4d205
movq -0x1f8(%rbp), %rsi
leaq -0x160(%rbp), %rdi
callq 0xca00
jmp 0x4d21a
leaq -0x130(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0xc330
jmp 0x4d22f
leaq -0x160(%rbp), %rdi
callq 0xc3c0
leaq -0x148(%rbp), %rdi
callq 0x3d3b0
jmp 0x4d12f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x4d345
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x160(%rbp), %rdi
callq 0xc3c0
jmp 0x4d345
movb -0x131(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x4d2ad
movb -0x132(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x4d2ad
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0xd8(%rbp)
jmp 0x4d328
movq -0xe8(%rbp), %rax
movslq 0x778(%rax), %rcx
imulq $0x98, %rcx, %rcx
leaq 0x7a0(%rax,%rcx), %rsi
leaq -0x178(%rbp), %rdi
leaq -0x130(%rbp), %rdx
callq 0x37aa0
jmp 0x4d2df
leaq -0x178(%rbp), %rdi
callq 0x3d860
movq %rax, -0x200(%rbp)
leaq -0x178(%rbp), %rdi
callq 0xde10
movq -0x200(%rbp), %rax
cmpq $0x0, %rax
jne 0x4d31e
movl $0xfffffffc, -0x4(%rbp) # imm = 0xFFFFFFFC
movl $0x1, -0xd8(%rbp)
jmp 0x4d328
movl $0x0, -0xd8(%rbp)
leaq -0x130(%rbp), %rdi
callq 0xc430
movl -0xd8(%rbp), %eax
testl %eax, %eax
jne 0x4d366
jmp 0x4d340
jmp 0x4d083
leaq -0x130(%rbp), %rdi
callq 0xc430
jmp 0x4d384
jmp 0x4d355
movl $0x0, -0x4(%rbp)
movl $0x1, -0xd8(%rbp)
leaq -0x48(%rbp), %rdi
callq 0xc430
leaq -0x30(%rbp), %rdi
callq 0xc430
movl -0x4(%rbp), %eax
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0xc430
leaq -0x30(%rbp), %rdi
callq 0xc430
movq -0x50(%rbp), %rdi
callq 0x6890
nop