// File: Konano[P]UselessDatabase/Table.cpp
// Function: Table::createForeignKey(ForeignKey*, PrimaryKey*)
int Table::createForeignKey(ForeignKey* fk, PrimaryKey* pk) {
    if (*pk->table->p_key != *pk) return -1;
    fk->p = pk->table->p_key;
    if (fk->size() != pk->size()) return -3;
    for (uint i = 0; i < fk->size(); i++) {
        if (col_ty[fk->v[i]].ty == CHAR || col_ty[fk->v[i]].ty == VARCHAR) {
            if (pk->table->col_ty[pk->v[i]].ty != CHAR && pk->table->col_ty[pk->v[i]].ty != VARCHAR) return -5;
        }
        else if (col_ty[fk->v[i]].ty != pk->table->col_ty[pk->v[i]].ty) return -5;
    }
    if (constraintKey(fk)) return -2;
    
    ForeignKey* f_keyx = new ForeignKey(this);
    f_keyx->v_size = fk->v_size;
    f_keyx->name = fk->name;
    f_keyx->v.clear();
    for (auto i: fk->v)f_keyx->v.push_back(i);
    f_keyx->table = fk->table;
    f_keyx->p = pk->table->p_key;
    f_key.push_back(f_keyx);
    updateColumns();

    string name = std::string("fk_") + std::string(f_keyx->name) + std::to_string(f_key.size());
    f_key_index.push_back(createIndex(f_keyx->v, name));

    pk->table->p_key->f.push_back(f_keyx);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x758(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x532b0
testb $0x1, %al
jne 0x4d3e0
jmp 0x4d3ec
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4d9c9
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x758(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x18(%rbp), %rdi
callq 0x532e0
movl %eax, -0x154(%rbp)
movq -0x20(%rbp), %rdi
callq 0x532e0
movl %eax, %ecx
movl -0x154(%rbp), %eax
cmpl %ecx, %eax
je 0x4d433
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x4d9c9
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x158(%rbp)
movq -0x18(%rbp), %rdi
callq 0x532e0
movl %eax, %ecx
movl -0x158(%rbp), %eax
cmpl %ecx, %eax
jae 0x4d608
movq -0x150(%rbp), %rax
addq $0x58, %rax
movq %rax, -0x160(%rbp)
movq -0x18(%rbp), %rdi
addq $0x30, %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq %rax, %rcx
movq -0x160(%rbp), %rax
movl (%rcx), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
cmpl $0x1, 0x24(%rax)
je 0x4d4da
movq -0x150(%rbp), %rax
addq $0x58, %rax
movq %rax, -0x168(%rbp)
movq -0x18(%rbp), %rdi
addq $0x30, %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq %rax, %rcx
movq -0x168(%rbp), %rax
movl (%rcx), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
cmpl $0x2, 0x24(%rax)
jne 0x4d567
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
addq $0x58, %rax
movq %rax, -0x170(%rbp)
movq -0x20(%rbp), %rdi
addq $0x30, %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq %rax, %rcx
movq -0x170(%rbp), %rax
movl (%rcx), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
cmpl $0x1, 0x24(%rax)
je 0x4d562
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
addq $0x58, %rax
movq %rax, -0x178(%rbp)
movq -0x20(%rbp), %rdi
addq $0x30, %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq %rax, %rcx
movq -0x178(%rbp), %rax
movl (%rcx), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
cmpl $0x2, 0x24(%rax)
je 0x4d562
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0x4d9c9
jmp 0x4d5f8
movq -0x150(%rbp), %rax
addq $0x58, %rax
movq %rax, -0x190(%rbp)
movq -0x18(%rbp), %rdi
addq $0x30, %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq %rax, %rcx
movq -0x190(%rbp), %rax
movl (%rcx), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x24(%rax), %eax
movl %eax, -0x17c(%rbp)
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
addq $0x58, %rax
movq %rax, -0x188(%rbp)
movq -0x20(%rbp), %rdi
addq $0x30, %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq -0x188(%rbp), %rcx
movq %rax, %rdx
movl -0x17c(%rbp), %eax
movl (%rdx), %edx
imulq $0x38, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x24(%rcx), %eax
je 0x4d5f6
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0x4d9c9
jmp 0x4d5f8
jmp 0x4d5fa
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x4d43a
movq -0x150(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x4ccf0
cmpl $0x0, %eax
je 0x4d629
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x4d9c9
movl $0x58, %edi
callq 0x64a0
movq -0x150(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x198(%rbp)
callq 0x53300
jmp 0x4d64e
movq -0x198(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rsi
addq $0x10, %rsi
movq -0x30(%rbp), %rdi
addq $0x10, %rdi
callq 0x6070
movq -0x30(%rbp), %rdi
addq $0x30, %rdi
callq 0x3d020
movq -0x18(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x3d210
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0x3d240
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x3d270
testb $0x1, %al
jne 0x4d6c2
jmp 0x4d70e
leaq -0x50(%rbp), %rdi
callq 0x3d2b0
movl (%rax), %eax
movl %eax, -0x5c(%rbp)
movq -0x30(%rbp), %rdi
addq $0x30, %rdi
leaq -0x5c(%rbp), %rsi
callq 0x3d2d0
leaq -0x50(%rbp), %rdi
callq 0x3d3b0
jmp 0x4d6af
movq -0x198(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x58, %esi
callq 0x64b0
jmp 0x4d9d5
movq -0x150(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x758(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x50(%rax)
addq $0x760, %rdi            # imm = 0x760
leaq -0x30(%rbp), %rsi
callq 0x53340
movq -0x150(%rbp), %rdi
callq 0x48f70
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x68a0
movq -0x1a0(%rbp), %rdx
leaq 0x364a9(%rip), %rsi     # 0x83c22
leaq -0xc0(%rbp), %rdi
callq 0xc020
jmp 0x4d787
movq -0x30(%rbp), %rsi
addq $0x10, %rsi
leaq -0xe8(%rbp), %rdi
callq 0x6240
jmp 0x4d79d
leaq -0xa0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
leaq -0xe8(%rbp), %rdx
callq 0x15540
jmp 0x4d7b9
movq -0x150(%rbp), %rdi
addq $0x760, %rdi            # imm = 0x760
callq 0x447e0
movq %rax, %rsi
leaq -0x108(%rbp), %rdi
callq 0x15110
jmp 0x4d7dd
leaq -0x80(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
leaq -0x108(%rbp), %rdx
callq 0x15540
jmp 0x4d7f6
leaq -0x108(%rbp), %rdi
callq 0x6340
leaq -0xa0(%rbp), %rdi
callq 0x6340
leaq -0xe8(%rbp), %rdi
callq 0x6340
leaq -0xc0(%rbp), %rdi
callq 0x6340
leaq -0xc1(%rbp), %rdi
callq 0x6530
movq -0x150(%rbp), %rax
addq $0x780, %rax            # imm = 0x780
movq %rax, -0x1a8(%rbp)
movq -0x30(%rbp), %rsi
addq $0x30, %rsi
leaq -0x128(%rbp), %rdi
callq 0x3d100
jmp 0x4d85c
leaq -0x148(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x6240
jmp 0x4d86e
movq -0x150(%rbp), %rdi
leaq -0x128(%rbp), %rsi
leaq -0x148(%rbp), %rdx
callq 0x491c0
movl %eax, -0x1ac(%rbp)
jmp 0x4d890
movq -0x1a8(%rbp), %rdi
movl -0x1ac(%rbp), %eax
movl %eax, -0x10c(%rbp)
leaq -0x10c(%rbp), %rsi
callq 0x3d6c0
jmp 0x4d8b1
leaq -0x148(%rbp), %rdi
callq 0x6340
leaq -0x128(%rbp), %rdi
callq 0xee40
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x758(%rax), %rdi
addq $0x50, %rdi
leaq -0x30(%rbp), %rsi
callq 0x53340
jmp 0x4d8e7
movl $0x0, -0x4(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x6340
jmp 0x4d9c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4d970
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4d964
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4d958
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4d94c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x6340
leaq -0xa0(%rbp), %rdi
callq 0x6340
leaq -0xe8(%rbp), %rdi
callq 0x6340
leaq -0xc0(%rbp), %rdi
callq 0x6340
leaq -0xc1(%rbp), %rdi
callq 0x6530
jmp 0x4d9d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4d9be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4d9b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x6340
leaq -0x128(%rbp), %rdi
callq 0xee40
leaq -0x80(%rbp), %rdi
callq 0x6340
jmp 0x4d9d5
movl -0x4(%rbp), %eax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x6890
nop