// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe8, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq 0x48(%rdx), %rdx
movq 0x758(%rdx), %rsi
movl 0x8(%rsi), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x8(%rbx), %ecx
jne 0x24789
movq %rdi, %r14
testq %rcx, %rcx
je 0x24298
movq 0x30(%rsi), %rdi
movq 0x30(%rbx), %r8
xorl %r9d, %r9d
movl (%rdi,%r9,4), %r10d
movslq %r9d, %r11
cmpl (%r8,%r11,4), %r10d
jne 0x24789
incq %r9
cmpl %r9d, %ecx
jne 0x24214
movq %rsi, 0x50(%r15)
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
cmpl %ecx, 0x8(%r15)
jne 0x24789
leaq 0x58(%r14), %rax
movq 0x30(%r15), %rsi
addq $0x58, %rdx
movq 0x30(%rbx), %rdi
xorl %r8d, %r8d
movl (%rsi,%r8,4), %r9d
imulq $0x38, %r9, %r9
movl 0x24(%rax,%r9), %r9d
leal -0x1(%r9), %r11d
movl (%rdi,%r8,4), %r10d
imulq $0x38, %r10, %r10
movl 0x24(%rdx,%r10), %r10d
cmpl $0x1, %r11d
ja 0x24285
decl %r10d
cmpl $0x2, %r10d
jb 0x2428e
jmp 0x243b7
cmpl %r10d, %r9d
jne 0x243b7
incq %r8
cmpq %r8, %rcx
jne 0x24253
jmp 0x242ab
movq %rsi, 0x50(%r15)
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
cmpl %ecx, 0x8(%r15)
jne 0x24789
movq %r14, %rdi
movq %r15, %rsi
callq 0x23d70
movl %eax, %ecx
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
testl %ecx, %ecx
jne 0x24789
movl $0x58, %edi
callq 0x5390
movq %rax, %r12
leaq 0x10(%rax), %rdi
addq $0x20, %rax
movq %rax, 0x10(%r12)
xorl %eax, %eax
movq %rax, 0x18(%r12)
movb $0x0, 0x20(%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%r12)
movq %rax, 0x40(%r12)
movq %r14, 0x48(%r12)
leaq 0x2e852(%rip), %rax     # 0x52b58
movq %rax, (%r12)
movq %r12, 0x8(%rsp)
movl 0x8(%r15), %eax
movl %eax, 0x8(%r12)
leaq 0x10(%r15), %rsi
callq 0x5230
movq 0x30(%r12), %rax
cmpq %rax, 0x38(%r12)
je 0x24332
movq %rax, 0x38(%r12)
movq 0x30(%r15), %r13
movq 0x38(%r15), %rbp
cmpq %rbp, %r13
je 0x24381
leaq 0x30(%rsp), %r12
movl (%r13), %eax
movl %eax, 0x30(%rsp)
movq 0x8(%rsp), %rdi
movq 0x38(%rdi), %rsi
cmpq 0x40(%rdi), %rsi
je 0x24367
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rdi)
jmp 0x24373
addq $0x30, %rdi
movq %r12, %rdx
callq 0x1d0aa
addq $0x4, %r13
cmpq %rbp, %r13
jne 0x24344
movq 0x8(%rsp), %r12
movq 0x48(%r15), %rax
movq %rax, 0x48(%r12)
movq 0x48(%rbx), %rax
movq 0x758(%rax), %rax
movq %rax, 0x50(%r12)
movq 0x768(%r14), %rsi
cmpq 0x770(%r14), %rsi
je 0x243c1
movq %r12, (%rsi)
addq $0x8, 0x768(%r14)
jmp 0x243d2
movl $0xfffffffb, %eax       # imm = 0xFFFFFFFB
jmp 0x24789
leaq 0x760(%r14), %rdi
leaq 0x8(%rsp), %rdx
callq 0x28b36
movq %r14, %rdi
callq 0x21208
leaq 0xa0(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x1c864(%rip), %rsi     # 0x40c52
leaq 0x1c860(%rip), %rdx     # 0x40c55
leaq 0x90(%rsp), %rdi
callq 0xa960
movq 0x8(%rsp), %rax
leaq 0x80(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0xa5ec
movq 0x90(%rsp), %rcx
movq 0x98(%rsp), %r8
movq 0x78(%rsp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %edi
cmpq %r12, %rcx
je 0x24453
movq 0xa0(%rsp), %rdi
movq 0x70(%rsp), %rsi
cmpq %rdi, %rax
jbe 0x24474
movl $0xf, %edi
cmpq %r13, %rsi
je 0x2446f
movq 0x80(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x24483
leaq 0x90(%rsp), %rdi
callq 0x5160
jmp 0x24491
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5420
leaq 0x20(%rsp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rax), %rsi
leaq 0x10(%rax), %rcx
cmpq %rcx, %rsi
je 0x244b5
movq %rsi, 0x10(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x20(%rsp)
jmp 0x244bb
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x18(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
movq 0x768(%r14), %r15
subq 0x760(%r14), %r15
sarq $0x3, %r15
movl $0x1, %esi
cmpq $0xa, %r15
jb 0x24544
movl $0x4, %esi
movabsq $0x346dc5d63886594b, %rdi # imm = 0x346DC5D63886594B
movq %r15, %rcx
cmpq $0x63, %rcx
jbe 0x2453d
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x24542
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x24544
movq %rcx, %rax
mulq %rdi
shrq $0xb, %rdx
addl $0x4, %esi
cmpq $0x1869f, %rcx          # imm = 0x1869F
movq %rdx, %rcx
ja 0x24507
addl $-0x3, %esi
jmp 0x24544
addl $-0x2, %esi
jmp 0x24544
decl %esi
movl %esi, %esi
leaq 0x60(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x50(%rsp), %rdi
xorl %edx, %edx
callq 0x5460
movq 0x50(%rsp), %rdi
movl 0x58(%rsp), %esi
movq %r15, %rdx
callq 0xc2b1
leaq 0x20(%rsp), %r15
movq -0x10(%r15), %rcx
movq -0x8(%r15), %r8
movq 0x58(%rsp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %edi
cmpq %r15, %rcx
je 0x24591
movq 0x20(%rsp), %rdi
movq 0x50(%rsp), %rsi
cmpq %rdi, %rax
jbe 0x245af
movl $0xf, %edi
cmpq %rbp, %rsi
je 0x245aa
movq 0x60(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x245bb
leaq 0x10(%rsp), %rdi
callq 0x5160
jmp 0x245c9
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5420
leaq 0x40(%rsp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rax), %rsi
leaq 0x10(%rax), %rcx
cmpq %rcx, %rsi
je 0x245ed
movq %rsi, 0x30(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x40(%rsp)
jmp 0x245f3
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x38(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
movq 0x50(%rsp), %rdi
cmpq %rbp, %rdi
je 0x24627
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x53a0
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x2463e
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x53a0
movq 0x70(%rsp), %rdi
cmpq %r13, %rdi
je 0x24658
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x53a0
movq 0x90(%rsp), %rdi
cmpq %r12, %rdi
je 0x24675
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x53a0
movq 0x8(%rsp), %rsi
addq $0x30, %rsi
leaq 0xb0(%rsp), %rdi
callq 0x1c59a
leaq 0xd8(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
addq %rsi, %rdx
leaq 0xc8(%rsp), %rdi
callq 0xa5ec
leaq 0xb0(%rsp), %rsi
leaq 0xc8(%rsp), %rdx
movq %r14, %rdi
callq 0x21478
movl %eax, 0x10(%rsp)
movq 0x788(%r14), %rsi
cmpq 0x790(%r14), %rsi
je 0x246ec
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x788(%r14)
jmp 0x24700
addq $0x780, %r14            # imm = 0x780
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x1cbbc
movq 0xc8(%rsp), %rdi
cmpq %r15, %rdi
je 0x2471d
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x53a0
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x2473a
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x53a0
movq 0x48(%rbx), %rax
movq 0x758(%rax), %rdi
movq 0x58(%rdi), %rsi
cmpq 0x60(%rdi), %rsi
je 0x2475e
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x58(%rdi)
jmp 0x2476c
addq $0x50, %rdi
leaq 0x8(%rsp), %rdx
callq 0x28b36
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x24787
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x53a0
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x247a5
movq %rax, %rbx
jmp 0x24847
movq %rax, %rbx
jmp 0x24864
movq %rax, %rbx
movq 0x50(%rsp), %rdi
cmpq %rbp, %rdi
je 0x247cc
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x53a0
jmp 0x247cc
movq %rax, %rbx
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x247ec
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x53a0
jmp 0x247ec
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r13, %rdi
je 0x2480b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x53a0
jmp 0x2480b
movq %rax, %rbx
movq 0x90(%rsp), %rdi
cmpq %r12, %rdi
je 0x2487f
movq 0xa0(%rsp), %rsi
jmp 0x24877
movq %rax, %rbx
jmp 0x2487f
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
cmpq %r15, %rdi
je 0x24847
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x53a0
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x24864
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x53a0
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2487f
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x53a0
movq %rbx, %rdi
callq 0x56e0
nop