// File: Konano[P]UselessDatabase/Table.cpp
// Function: Table::createTable(unsigned int, Database*, char const*, unsigned int, Type*, bool)
int Table::createTable(uint table_id, Database* db, const char* name, uint col_num, Type* col_ty, bool create) {
    this->table_id = table_id;
    this->db = db;
    this->fm = db->fm;
    this->bpm = db->bpm;
    strcpy(this->name, name);
    this->col_num = 0;
    vector<uint> tempvec;
    for (uint i = 0; i < col_num; i++) {
        if (this->createColumn(col_ty[i])) return -1;
        if (col_ty[i].key == enumKeyType::Primary)tempvec.push_back(i);
    }

    if(tempvec.size() != 0){
        p_key = new PrimaryKey(this);
        p_key->name = "default primary key name";
        p_key->v.clear();
        for (auto i: tempvec)p_key->v.push_back(i);
        p_key->table = this;
        updateColumns();
        int old = p_key_index;
        if(old != -1)removeIndex(old);
        p_key_index = createIndex(p_key->v, "pk");
        p_key->v_size = p_key->v.size();
    }

    if (create) {
        if (fm->createFile(dirPath(db->name, name, "usid")) == false)
            return -2;
    }
    fm->openFile(dirPath(db->name, name, "usid"), main_file);
    record_size = calDataSize();
    record_onepg = 0;
    while ((record_onepg + 1) * record_size + (record_onepg - 1 + 1) / 8 + 1 <= PAGE_SIZE) record_onepg++;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movb 0x10(%rbp), %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq %r9, -0x38(%rbp)
andb $0x1, %al
movb %al, -0x39(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x110(%rbp)
movl -0x14(%rbp), %eax
movl %eax, 0x50(%rdi)
movq -0x20(%rbp), %rax
movq %rax, 0x38(%rdi)
movq -0x20(%rbp), %rax
movq 0x828(%rax), %rax
movq %rax, 0x40(%rdi)
movq -0x20(%rbp), %rax
movq 0x830(%rax), %rax
movq %rax, 0x48(%rdi)
addq $0x18, %rdi
movq -0x28(%rbp), %rsi
callq 0x6460
movq -0x110(%rbp), %rax
movl $0x0, 0x54(%rax)
leaq -0x58(%rbp), %rdi
callq 0xec40
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x48aab
movq -0x38(%rbp), %rsi
movl -0x5c(%rbp), %eax
imulq $0x38, %rax, %rax
addq %rax, %rsi
leaq -0x98(%rbp), %rdi
callq 0x52cf0
jmp 0x489f4
movq -0x110(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x48ee0
movl %eax, -0x114(%rbp)
jmp 0x48a0f
leaq -0x98(%rbp), %rdi
callq 0xe9f0
movl -0x114(%rbp), %eax
cmpl $0x0, %eax
je 0x48a76
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0xa8(%rbp)
jmp 0x48ea9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x48ebe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x98(%rbp), %rdi
callq 0xe9f0
jmp 0x48ebe
movq -0x38(%rbp), %rax
movl -0x5c(%rbp), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x2c(%rax)
jne 0x48a9b
leaq -0x58(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x3d2d0
jmp 0x48a99
jmp 0x48a9b
jmp 0x48a9d
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x489cc
leaq -0x58(%rbp), %rdi
callq 0x3d1f0
cmpq $0x0, %rax
je 0x48d53
movl $0x68, %edi
callq 0x64a0
movq %rax, -0x120(%rbp)
jmp 0x48ad1
movq -0x110(%rbp), %rsi
movq -0x120(%rbp), %rdi
callq 0x52d40
jmp 0x48ae6
movq -0x110(%rbp), %rax
movq -0x120(%rbp), %rcx
movq %rcx, 0x758(%rax)
movq 0x758(%rax), %rdi
addq $0x10, %rdi
leaq 0x3b0f5(%rip), %rsi     # 0x83c02
callq 0x6690
jmp 0x48b14
movq -0x110(%rbp), %rax
movq 0x758(%rax), %rdi
addq $0x30, %rdi
callq 0x3d020
leaq -0x58(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x3d210
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x3d240
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x3d270
testb $0x1, %al
jne 0x48b75
jmp 0x48be4
leaq -0xb8(%rbp), %rdi
callq 0x3d2b0
movq %rax, %rcx
movq -0x110(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, -0xc4(%rbp)
movq 0x758(%rax), %rdi
addq $0x30, %rdi
leaq -0xc4(%rbp), %rsi
callq 0x3d2d0
jmp 0x48bac
jmp 0x48bae
leaq -0xb8(%rbp), %rdi
callq 0x3d3b0
jmp 0x48b5c
movq -0x120(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
movl $0x68, %esi
callq 0x64b0
jmp 0x48ebe
movq -0x110(%rbp), %rdi
movq 0x758(%rdi), %rax
movq %rdi, 0x48(%rax)
callq 0x48f70
jmp 0x48bfd
movq -0x110(%rbp), %rax
movl 0x778(%rax), %eax
movl %eax, -0xc8(%rbp)
cmpl $-0x1, -0xc8(%rbp)
je 0x48c2f
movq -0x110(%rbp), %rdi
movl -0xc8(%rbp), %esi
callq 0x49120
jmp 0x48c2d
jmp 0x48c2f
movq -0x110(%rbp), %rax
movq 0x758(%rax), %rsi
addq $0x30, %rsi
leaq -0xe0(%rbp), %rdi
callq 0x3d100
jmp 0x48c4f
leaq -0x101(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x68a0
movq -0x128(%rbp), %rdx
leaq 0x3afab(%rip), %rsi     # 0x83c1b
leaq -0x100(%rbp), %rdi
callq 0xc020
jmp 0x48c7e
movq -0x110(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
leaq -0x100(%rbp), %rdx
callq 0x491c0
movl %eax, -0x12c(%rbp)
jmp 0x48ca0
movq -0x110(%rbp), %rax
movl -0x12c(%rbp), %ecx
movl %ecx, 0x778(%rax)
leaq -0x100(%rbp), %rdi
callq 0x6340
leaq -0x101(%rbp), %rdi
callq 0x6530
leaq -0xe0(%rbp), %rdi
callq 0xee40
movq -0x110(%rbp), %rax
movq 0x758(%rax), %rdi
addq $0x30, %rdi
callq 0x3d1f0
movq %rax, %rcx
movq -0x110(%rbp), %rax
movq 0x758(%rax), %rax
movl %ecx, 0x8(%rax)
jmp 0x48d53
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x48d36
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x6340
leaq -0x101(%rbp), %rdi
callq 0x6530
leaq -0xe0(%rbp), %rdi
callq 0xee40
jmp 0x48ebe
testb $0x1, -0x39(%rbp)
je 0x48dcb
movq -0x110(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x140(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq 0x35cb8(%rip), %rdx     # 0x7ea32
callq 0x35280
movq %rax, -0x138(%rbp)
jmp 0x48d88
movq -0x138(%rbp), %rsi
movq -0x140(%rbp), %rdi
callq 0x34e40
movb %al, -0x141(%rbp)
jmp 0x48da3
movb -0x141(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x48dc9
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
movl $0x1, -0xa8(%rbp)
jmp 0x48ea9
jmp 0x48dcb
movq -0x110(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x158(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq 0x35c46(%rip), %rdx     # 0x7ea32
callq 0x35280
movq %rax, -0x150(%rbp)
jmp 0x48dfa
movq -0x150(%rbp), %rsi
movq -0x158(%rbp), %rdi
movq -0x110(%rbp), %rdx
addq $0x9fa4, %rdx           # imm = 0x9FA4
callq 0x34e90
jmp 0x48e1d
movq -0x110(%rbp), %rdi
callq 0x488e0
movl %eax, -0x15c(%rbp)
jmp 0x48e31
movq -0x110(%rbp), %rax
movl -0x15c(%rbp), %ecx
movl %ecx, 0x9fa8(%rax)
movl $0x0, 0x9fac(%rax)
movq -0x110(%rbp), %rcx
movl 0x9fac(%rcx), %eax
addl $0x1, %eax
imull 0x9fa8(%rcx), %eax
movl 0x9fac(%rcx), %ecx
subl $0x1, %ecx
addl $0x1, %ecx
shrl $0x3, %ecx
addl %ecx, %eax
addl $0x1, %eax
cmpl $0x2000, %eax           # imm = 0x2000
ja 0x48e98
movq -0x110(%rbp), %rax
movl 0x9fac(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x9fac(%rax)
jmp 0x48e4e
movl $0x0, -0x4(%rbp)
movl $0x1, -0xa8(%rbp)
leaq -0x58(%rbp), %rdi
callq 0xee40
movl -0x4(%rbp), %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
leaq -0x58(%rbp), %rdi
callq 0xee40
movq -0xa0(%rbp), %rdi
callq 0x6890
nopw %cs:(%rax,%rax)