// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movq 0x48(%rdx), %rax
movq 0x758(%rax), %rdi
movq %rdx, %rsi
callq 0x210d6
testb %al, %al
je 0x1dfb8
pushq $-0x1
jmp 0x1e038
movq 0x48(%rbx), %rcx
movq 0x758(%rcx), %rax
movq %rax, 0x50(%r15)
movl 0x8(%r15), %edx
cmpl 0x8(%rbx), %edx
jne 0x1e023
leaq 0x58(%r14), %rsi
addq $0x58, %rcx
xorl %edi, %edi
pushq $-0x5
popq %rax
cmpq %rdi, %rdx
je 0x1e027
movq 0x30(%r15), %r8
movl (%r8,%rdi,4), %r8d
imulq $0x38, %r8, %r8
movl 0x24(%rsi,%r8), %r8d
leal -0x1(%r8), %r10d
movq 0x30(%rbx), %r9
movl (%r9,%rdi,4), %r9d
imulq $0x38, %r9, %r9
movl 0x24(%rcx,%r9), %r9d
cmpl $0x1, %r10d
ja 0x1e019
decl %r9d
cmpl $0x2, %r9d
jb 0x1e01e
jmp 0x1e039
cmpl %r9d, %r8d
jne 0x1e039
incq %rdi
jmp 0x1dfdd
pushq $-0x3
jmp 0x1e038
movq %r14, %rdi
movq %r15, %rsi
callq 0x1dbd4
testl %eax, %eax
je 0x1e04b
pushq $-0x2
popq %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x58
popq %rdi
callq 0x53e0
movq %rax, %r13
leaq 0x10(%rax), %rdi
addq $0x20, %rax
movq %rax, 0x10(%r13)
andq $0x0, 0x18(%r13)
movb $0x0, 0x20(%r13)
xorps %xmm0, %xmm0
andq $0x0, 0x40(%r13)
movups %xmm0, 0x30(%r13)
movq %r14, 0x18(%rsp)
movq %r14, 0x48(%r13)
leaq 0x31b9f(%rip), %rax     # 0x4fc28
movq %rax, (%r13)
movq %r13, 0x8(%rsp)
movl 0x8(%r15), %eax
movl %eax, 0x8(%r13)
leaq 0x10(%r15), %rsi
callq 0x5260
movq 0x30(%r13), %rax
cmpq %rax, 0x38(%r13)
je 0x1e0b1
movq %rax, 0x38(%r13)
movq %r13, 0x10(%rsp)
addq $0x30, %r13
movq 0x30(%r15), %r14
movq 0x38(%r15), %r12
leaq 0x20(%rsp), %rbp
cmpq %r12, %r14
je 0x1e0e4
movl (%r14), %eax
movl %eax, 0x20(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x17fbe
addq $0x4, %r14
jmp 0x1e0c7
movq 0x48(%r15), %rax
movq 0x10(%rsp), %rcx
movq %rax, 0x48(%rcx)
movq 0x48(%rbx), %rax
movq 0x758(%rax), %rax
movq %rax, 0x50(%rcx)
movq 0x18(%rsp), %r14
leaq 0x760(%r14), %rdi
leaq 0x8(%rsp), %rsi
callq 0x210e0
movq %r14, %rdi
callq 0x1bb26
leaq 0x19f15(%rip), %rsi     # 0x3803a
leaq 0xd8(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x84b8
movq 0x8(%rsp), %rsi
addq $0x10, %rsi
leaq 0xb8(%rsp), %rdi
callq 0x5530
leaq 0x58(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
callq 0xac51
movq 0x768(%r14), %rsi
subq 0x760(%r14), %rsi
sarq $0x3, %rsi
leaq 0x98(%rsp), %rdi
callq 0xaaa3
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x98(%rsp), %rdx
callq 0xac51
leaq 0x98(%rsp), %rdi
callq 0x5200
leaq 0x58(%rsp), %rdi
callq 0x5200
leaq 0xb8(%rsp), %rdi
callq 0x5200
leaq 0xd8(%rsp), %rdi
callq 0x5200
movq 0x8(%rsp), %rsi
addq $0x30, %rsi
leaq 0x40(%rsp), %rdi
callq 0x17f70
leaq 0x78(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x5530
leaq 0x40(%rsp), %rsi
leaq 0x78(%rsp), %rdx
movq %r14, %rdi
callq 0x1bc0e
addq $0x780, %r14            # imm = 0x780
leaq 0x58(%rsp), %rsi
movl %eax, (%rsi)
movq %r14, %rdi
callq 0x18774
leaq 0x78(%rsp), %rdi
callq 0x5200
leaq 0x40(%rsp), %rdi
callq 0x18566
movq 0x48(%rbx), %rax
movq 0x758(%rax), %rdi
addq $0x50, %rdi
leaq 0x8(%rsp), %rsi
callq 0x210e0
leaq 0x20(%rsp), %rdi
callq 0x5200
xorl %eax, %eax
jmp 0x1e039
jmp 0x1e25d
movq %rax, %rbx
jmp 0x1e2b4
movq %rax, %rbx
jmp 0x1e2be
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x5200
jmp 0x1e277
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x5200
jmp 0x1e286
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x5200
jmp 0x1e298
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
jmp 0x1e2c3
movq %rax, %rbx
jmp 0x1e2c8
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x5200
leaq 0x40(%rsp), %rdi
callq 0x18566
leaq 0x20(%rsp), %rdi
callq 0x5200
movq %rbx, %rdi
callq 0x56f0