// File: Konano[P]UselessDatabase/Index.cpp
// Function: Index::convert_buf_to_BtreeNode(int)
BtreeNode* Index::convert_buf_to_BtreeNode(int index) {
    BtreeNode* ans = new BtreeNode();
    ans->child.clear();
    ans->child_ptr.clear();
    ans->record.clear();
    ans->left_page = nullptr;
    ans->right_page = nullptr;
    
    ans->index = index;

    BufType buf = table->bpm->getPage(fileID, index);

    ans->left_page_index = *(uint32_t *)(buf + 4);
    ans->right_page_index = *(uint32_t *)(buf + 8);
    ans->fa_index = *(uint32_t *)(buf + 12);
    ans->record_cnt = *(uint16_t *)(buf + 16);

    if (ans->record_cnt != 0) {
        ans->child.push_back(*(uint32_t *)(buf + 18)); 
        if (ans->child[0] != -1) ans->is_leaf = false;
    }
    else ans->child.push_back(-1);

    for (int i = 0; i < ans->record_cnt; i++) {
        BtreeRecord temp;
        temp.record_id = *(uint32_t *)(buf + 22 + i * record_size);
        for (uint j = 0; j < this->key_num; j++) {
            if (this->ty[j] == enumType::INT) {
                bool flag = true;
                for (uint k = 0; k < 4; k++) {
                    if (*(uint8_t*)(buf + 22 + i * record_size + 4 + this->offset[j] + k) != 255) {
                        flag = false;
                        break;
                    }
                }
                if(flag) temp.key.push_back(Any());
                else temp.key.push_back(Any(*(int*)(buf + 22 + i * record_size + 4 + this->offset[j])));
            }
            if (this->ty[j] == enumType::CHAR) {
                bool flag = true;
                for (uint k = 0; k < this->offset[j] - (j == 0 ? 0 : this->offset[j - 1]); k++) {
                    if (*(uint8_t*)(buf + 22 + i * record_size + 4 + this->offset[j] + k) != 255) {
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    char* str = new char[record_size - 8 + 1];
                    memcpy(str, buf + 22 + i * record_size + 4 + this->offset[j], this->offset[j] - (j == 0 ? 0 : this->offset[j - 1]));
                    str[record_size - 8] = '\0';
                    temp.key.push_back(Any(str));
                }
                else{
                    temp.key.push_back(Any());
                }
            }
        }
        ans->child.push_back(*(uint32_t *)(buf + 22 + (i + 1) * record_size - 4));
        ans->record.push_back(temp);
    }
    return ans;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl $0x70, %edi
callq 0x64a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xb0(%rbp)
callq 0x3d5a0
jmp 0x3700b
movq -0xb0(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0x3d660
movq -0x18(%rbp), %rdi
addq $0x38, %rdi
callq 0x3d680
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
callq 0x3d6a0
movq -0xb8(%rbp), %rax
movq -0x18(%rbp), %rcx
movq $0x0, 0x8(%rcx)
movq -0x18(%rbp), %rcx
movq $0x0, 0x10(%rcx)
movl -0xc(%rbp), %edx
movq -0x18(%rbp), %rcx
movl %edx, 0x18(%rcx)
movq (%rax), %rcx
movq 0x48(%rcx), %rdi
movl 0x74(%rax), %esi
movl -0xc(%rbp), %edx
callq 0x70b0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x30(%rbp), %rax
movl 0xc(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x6c(%rax)
movq -0x30(%rbp), %rax
movzwl 0x10(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x3711d
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
movq -0x30(%rbp), %rax
movl 0x12(%rax), %eax
movl %eax, -0x34(%rbp)
leaq -0x34(%rbp), %rsi
callq 0x3d6c0
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3d6f0
cmpl $-0x1, (%rax)
je 0x3711b
movq -0x18(%rbp), %rax
movb $0x0, 0x68(%rax)
jmp 0x3711b
movq -0xb0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x70, %esi
callq 0x64b0
jmp 0x37695
jmp 0x37135
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
leaq -0x38(%rbp), %rsi
callq 0x3d6c0
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jge 0x37688
leaq -0x60(%rbp), %rdi
callq 0x3d710
movq -0xb8(%rbp), %rdx
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
movzwl 0x6c(%rdx), %edx
imull %edx, %ecx
movslq %ecx, %rcx
movl 0x16(%rax,%rcx), %eax
movl %eax, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movq -0xb8(%rbp), %rcx
movl -0x64(%rbp), %eax
cmpl 0x28(%rcx), %eax
jae 0x37619
movq -0xb8(%rbp), %rdi
addq $0x78, %rdi
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x3cfb0
cmpl $0x0, (%rax)
jne 0x37319
movb $0x1, -0x65(%rbp)
movl $0x0, -0x6c(%rbp)
cmpl $0x4, -0x6c(%rbp)
jae 0x37225
movq -0xb8(%rbp), %rdi
movq -0x30(%rbp), %rax
addq $0x16, %rax
movl -0x3c(%rbp), %ecx
movzwl 0x6c(%rdi), %edx
imull %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
addq $0x4, %rax
movq %rax, -0xc0(%rbp)
addq $0x48, %rdi
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq %rax, %rcx
movq -0xc0(%rbp), %rax
movl (%rcx), %ecx
addq %rcx, %rax
movl -0x6c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0xff, %eax
je 0x37218
movb $0x0, -0x65(%rbp)
jmp 0x37225
jmp 0x3721a
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x371b7
testb $0x1, -0x65(%rbp)
je 0x3728c
leaq -0x58(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0x78(%rbp), %rdi
callq 0xd240
jmp 0x37241
movq -0xc8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xc330
jmp 0x37253
leaq -0x78(%rbp), %rdi
callq 0xc3c0
jmp 0x37317
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x3767d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x78(%rbp), %rdi
callq 0xc3c0
jmp 0x3767d
movq -0xb8(%rbp), %rdi
leaq -0x58(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
movzwl 0x6c(%rdi), %edx
imull %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
addq $0x48, %rdi
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq -0xd0(%rbp), %rcx
movl (%rax), %eax
leaq 0x1a(%rax,%rcx), %rsi
leaq -0x80(%rbp), %rdi
callq 0xd1d0
jmp 0x372e0
movq -0xd8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xc330
jmp 0x372f2
leaq -0x80(%rbp), %rdi
callq 0xc3c0
jmp 0x37317
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xc3c0
jmp 0x3767d
jmp 0x37319
movq -0xb8(%rbp), %rdi
addq $0x78, %rdi
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x3cfb0
cmpl $0x1, (%rax)
jne 0x37609
movb $0x1, -0x81(%rbp)
movl $0x0, -0x88(%rbp)
movq -0xb8(%rbp), %rdi
movl -0x88(%rbp), %eax
movl %eax, -0xe0(%rbp)
addq $0x48, %rdi
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movl (%rax), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0x64(%rbp)
jne 0x37381
xorl %eax, %eax
movl %eax, -0xe4(%rbp)
jmp 0x373a3
movq -0xb8(%rbp), %rdi
addq $0x48, %rdi
movl -0x64(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x3cf40
movl (%rax), %eax
movl %eax, -0xe4(%rbp)
movl -0xe0(%rbp), %eax
movl -0xdc(%rbp), %ecx
movl -0xe4(%rbp), %edx
subl %edx, %ecx
cmpl %ecx, %eax
jae 0x37432
movq -0xb8(%rbp), %rdi
movq -0x30(%rbp), %rax
addq $0x16, %rax
movl -0x3c(%rbp), %ecx
movzwl 0x6c(%rdi), %edx
imull %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
addq $0x4, %rax
movq %rax, -0xf0(%rbp)
addq $0x48, %rdi
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq %rax, %rcx
movq -0xf0(%rbp), %rax
movl (%rcx), %ecx
addq %rcx, %rax
movl -0x88(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0xff, %eax
je 0x3741c
movb $0x0, -0x81(%rbp)
jmp 0x37432
jmp 0x3741e
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x37348
testb $0x1, -0x81(%rbp)
je 0x375b1
movq -0xb8(%rbp), %rax
movzwl 0x6c(%rax), %eax
addl $-0x7, %eax
movslq %eax, %rdi
callq 0x6040
movq %rax, -0xf8(%rbp)
jmp 0x3745e
movq -0xb8(%rbp), %rdi
movq -0xf8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x30(%rbp), %rax
addq $0x16, %rax
movl -0x3c(%rbp), %ecx
movzwl 0x6c(%rdi), %edx
imull %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
addq $0x4, %rax
movq %rax, -0x110(%rbp)
addq $0x48, %rdi
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movq -0xb8(%rbp), %rdi
movq %rax, %rcx
movq -0x110(%rbp), %rax
movl (%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x108(%rbp)
addq $0x48, %rdi
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x3cf40
movl (%rax), %eax
movl %eax, -0xfc(%rbp)
cmpl $0x0, -0x64(%rbp)
jne 0x374f5
xorl %eax, %eax
movl %eax, -0x11c(%rbp)
jmp 0x37517
movq -0xb8(%rbp), %rdi
addq $0x48, %rdi
movl -0x64(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x3cf40
movl (%rax), %eax
movl %eax, -0x11c(%rbp)
movq -0x108(%rbp), %rsi
movq -0x118(%rbp), %rdi
movl -0xfc(%rbp), %eax
movl -0x11c(%rbp), %ecx
subl %ecx, %eax
movl %eax, %edx
callq 0x63f0
movq -0xb8(%rbp), %rax
movq -0x90(%rbp), %rcx
movzwl 0x6c(%rax), %eax
movb $0x0, -0x8(%rax,%rcx)
leaq -0x58(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xc350
jmp 0x37571
movq -0x128(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xc330
jmp 0x37586
leaq -0x98(%rbp), %rdi
callq 0xc3c0
jmp 0x37607
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x98(%rbp), %rdi
callq 0xc3c0
jmp 0x3767d
leaq -0x58(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xd240
jmp 0x375ca
movq -0x130(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xc330
jmp 0x375df
leaq -0xa0(%rbp), %rdi
callq 0xc3c0
jmp 0x37607
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xc3c0
jmp 0x3767d
jmp 0x37609
jmp 0x3760b
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x3717b
movq -0xb8(%rbp), %rdx
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
incl %ecx
movzwl 0x6c(%rdx), %edx
imull %edx, %ecx
movslq %ecx, %rcx
movl 0x12(%rax,%rcx), %eax
movl %eax, -0xa4(%rbp)
leaq -0xa4(%rbp), %rsi
callq 0x3d6c0
jmp 0x37653
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
leaq -0x60(%rbp), %rsi
callq 0x3d730
jmp 0x37666
leaq -0x60(%rbp), %rdi
callq 0x3d560
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x3713c
leaq -0x60(%rbp), %rdi
callq 0x3d560
jmp 0x37695
movq -0x18(%rbp), %rax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x6890
nop