// File: Konano[P]UselessDatabase/Index.cpp
// Function: Index::Index(Table*, char const*, std::vector<unsigned int, std::allocator<unsigned int>>, int)
Index::Index(Table* table, const char* name, vector<uint> key, int btree_max_per_node) : table(table), key(key), btree_max_per_node(btree_max_per_node) {
    strcpy(this->name, name);
    table->fm->createFile(dirPath(table->db->name, table->name, name, "usid"));
    page_num = 1;
    next_del_page = -1;
    record_size = 8;
    this->key.clear();
    this->offset.clear();
    this->ty.clear();
    this->key_num = key.size();
    for (auto i : key) {
        this->key.push_back(i);
        this->ty.push_back(table->col_ty[i].ty);
    }
    for (uint i = 0; i < key.size(); i++) {
        this->offset.push_back(i == 0 ? 0 : this->offset[i - 1]);
        this->offset[i] += table->col_ty[key[i]].size();
    }
    for (uint i = 0; i < key.size(); i++) this->offset[i] -= table->col_ty[key[i]].size();
    for (uint i = 0; i < key.size(); i++)
        record_size += table->col_ty[key[i]].size();
    max_recond_num = (PAGE_SIZE - 18) / record_size;

    open();

    int index;
    BufType buf = table->bpm->getPage(fileID, 0, index);
    *(uint32_t *)(buf+0) = -1;
    *(uint32_t *)(buf+4) = -1;
    *(uint32_t *)(buf+8) = -1;
    *(uint32_t *)(buf+12) = -1;
    *(uint16_t *)(buf+16) = 0;
    table->bpm->markDirty(index);
    
    root_page = 0;
    close();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rcx, -0xb0(%rbp)
movq %rsi, %rax
movq -0xb0(%rbp), %rsi
movq %rsi, -0xa8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rax, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq %rax, (%rdi)
addq $0x30, %rdi
movq %rdi, -0xa0(%rbp)
callq 0x3d100
movq -0x88(%rbp), %rdi
addq $0x48, %rdi
movq %rdi, -0x98(%rbp)
callq 0xec40
movq -0x88(%rbp), %rdi
addq $0x78, %rdi
movq %rdi, -0x90(%rbp)
callq 0xec60
movq -0x88(%rbp), %rdi
movl -0x24(%rbp), %eax
movl %eax, 0x90(%rdi)
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
callq 0x6460
movq -0x10(%rbp), %rsi
movq 0x38(%rsi), %rdi
movq 0x40(%rsi), %rax
movq %rax, -0x80(%rbp)
addq $0x18, %rsi
movq -0x18(%rbp), %rdx
leaq 0x4837f(%rip), %rcx     # 0x7ea32
callq 0x353f0
movq %rax, -0x78(%rbp)
jmp 0x366be
movq -0x78(%rbp), %rsi
movq -0x80(%rbp), %rdi
callq 0x34e40
jmp 0x366cd
movq -0x88(%rbp), %rdi
movl $0x1, 0x60(%rdi)
movl $0xffffffff, 0x68(%rdi) # imm = 0xFFFFFFFF
movw $0x8, 0x6c(%rdi)
addq $0x30, %rdi
callq 0x3d020
movq -0x88(%rbp), %rdi
addq $0x48, %rdi
callq 0x3d020
movq -0x88(%rbp), %rdi
addq $0x78, %rdi
callq 0x3d040
movq -0xa8(%rbp), %rdi
callq 0x3d1f0
movq -0x88(%rbp), %rcx
movq %rax, %rdx
movq -0xa8(%rbp), %rax
movl %edx, 0x28(%rcx)
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x3d210
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
callq 0x3d240
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x3d270
testb $0x1, %al
jne 0x36765
jmp 0x367ed
leaq -0x48(%rbp), %rdi
callq 0x3d2b0
movq -0x88(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x54(%rbp)
addq $0x30, %rdi
leaq -0x54(%rbp), %rsi
callq 0x3d2d0
jmp 0x36789
movq -0x88(%rbp), %rdi
addq $0x78, %rdi
movq -0x10(%rbp), %rax
movl -0x54(%rbp), %ecx
imulq $0x38, %rcx, %rcx
leaq 0x7c(%rax,%rcx), %rsi
callq 0x3d340
jmp 0x367ab
jmp 0x367ad
leaq -0x48(%rbp), %rdi
callq 0x3d3b0
jmp 0x3674f
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0xede0
movq -0x98(%rbp), %rdi
callq 0xee40
movq -0xa0(%rbp), %rdi
callq 0xee40
jmp 0x36ac3
movl $0x0, -0x58(%rbp)
movq -0xa8(%rbp), %rdi
movl -0x58(%rbp), %eax
movq %rax, -0xb8(%rbp)
callq 0x3d1f0
movq %rax, %rcx
movq -0xb8(%rbp), %rax
cmpq %rcx, %rax
jae 0x368e7
movq -0x88(%rbp), %rax
addq $0x48, %rax
movq %rax, -0xc0(%rbp)
cmpl $0x0, -0x58(%rbp)
jne 0x3683f
xorl %eax, %eax
movl %eax, -0xc4(%rbp)
jmp 0x36861
movq -0x88(%rbp), %rdi
addq $0x48, %rdi
movl -0x58(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x3cf40
movl (%rax), %eax
movl %eax, -0xc4(%rbp)
movq -0xc0(%rbp), %rdi
movl -0xc4(%rbp), %eax
movl %eax, -0x5c(%rbp)
leaq -0x5c(%rbp), %rsi
callq 0x3d060
jmp 0x3687c
movq -0xa8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, -0xd0(%rbp)
movl -0x58(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq %rax, %rcx
movq -0xd0(%rbp), %rax
movl (%rcx), %ecx
imulq $0x38, %rcx, %rcx
leaq 0x58(%rax,%rcx), %rdi
callq 0x3d3d0
movl %eax, -0xc8(%rbp)
jmp 0x368ba
movq -0x88(%rbp), %rdi
addq $0x48, %rdi
movl -0x58(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movl -0xc8(%rbp), %ecx
addl (%rax), %ecx
movl %ecx, (%rax)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x367f4
movl $0x0, -0x60(%rbp)
movq -0xa8(%rbp), %rdi
movl -0x60(%rbp), %eax
movq %rax, -0xd8(%rbp)
callq 0x3d1f0
movq %rax, %rcx
movq -0xd8(%rbp), %rax
cmpq %rcx, %rax
jae 0x36980
movq -0xa8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl -0x60(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq %rax, %rcx
movq -0xe8(%rbp), %rax
movl (%rcx), %ecx
imulq $0x38, %rcx, %rcx
leaq 0x58(%rax,%rcx), %rdi
callq 0x3d3d0
movl %eax, -0xdc(%rbp)
jmp 0x36951
movq -0x88(%rbp), %rdi
addq $0x48, %rdi
movl -0x60(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movl -0xdc(%rbp), %edx
movl (%rax), %ecx
subl %edx, %ecx
movl %ecx, (%rax)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x368ee
movl $0x0, -0x64(%rbp)
movq -0xa8(%rbp), %rdi
movl -0x64(%rbp), %eax
movq %rax, -0xf0(%rbp)
callq 0x3d1f0
movq %rax, %rcx
movq -0xf0(%rbp), %rax
cmpq %rcx, %rax
jae 0x36a0f
movq -0xa8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, -0x100(%rbp)
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq %rax, %rcx
movq -0x100(%rbp), %rax
movl (%rcx), %ecx
imulq $0x38, %rcx, %rcx
leaq 0x58(%rax,%rcx), %rdi
callq 0x3d3d0
movl %eax, -0xf4(%rbp)
jmp 0x369ea
movq -0x88(%rbp), %rax
movl -0xf4(%rbp), %edx
movzwl 0x6c(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x6c(%rax)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x36987
movq -0x88(%rbp), %rdi
movzwl 0x6c(%rdi), %ecx
xorl %edx, %edx
movl $0x1fee, %eax           # imm = 0x1FEE
divl %ecx
movl %eax, 0x70(%rdi)
callq 0x36ad0
jmp 0x36a2d
movq -0x88(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x48(%rcx), %rdi
movl 0x74(%rax), %esi
xorl %edx, %edx
leaq -0x68(%rbp), %rcx
callq 0x71d0
movq %rax, -0x108(%rbp)
jmp 0x36a53
movq -0x108(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x70(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movq -0x70(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movq -0x70(%rbp), %rax
movl $0xffffffff, 0xc(%rax)  # imm = 0xFFFFFFFF
movq -0x70(%rbp), %rax
movw $0x0, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x68(%rbp), %esi
callq 0x7300
jmp 0x36aa5
movq -0x88(%rbp), %rdi
movl $0x0, 0x64(%rdi)
callq 0x36b30
jmp 0x36aba
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x6890
nopl (%rax)