// File: Konano[P]UselessDatabase/Table.cpp
// Function: Table::rebuild(int, unsigned int)
void Table::rebuild(int ty, uint col_id) {
    if (record_num == 0) return;
    int _main_file;
    fm->createFile(dirPath(db->name, name, "tmp", "usid"));
    fm->openFile(dirPath(db->name, name, "tmp", "usid"), _main_file);
    uint _record_size, _record_onepg;
    int index;
    BufType buf, _buf, buf_st, _buf_st;
    switch (ty) {
    case 0: // del
        _record_size = record_size - col_ty[col_id].size();
        _record_onepg = 0;
        while ((_record_onepg + 1) * _record_size + _record_onepg / 8 + 1 <= PAGE_SIZE) _record_onepg++;
        for (uint i = 0; i < record_num; i++) {
            if (i % record_onepg == 0) {
                buf_st = buf = bpm->getPage(main_file, i / record_onepg);
                buf += (record_onepg - 1) / 8 + 1;
            }
            if (i % _record_onepg == 0) {
                _buf_st = _buf = bpm->getPage(_main_file, i / _record_onepg, index);
                _buf += (_record_onepg - 1) / 8 + 1;
            }
            
            if (exist(buf_st, i % record_onepg))
                exist_set(_buf_st, i % _record_onepg);
            else if (exist(_buf_st, i % _record_onepg))
                exist_reset(_buf_st, i % _record_onepg);
            for (uint j = 0; j < col_num; j++) {
                if (j != col_id) memcpy(_buf, buf, col_ty[j].size());
                buf += col_ty[j].size();
                if (j != col_id) _buf += col_ty[j].size();
            }
            bpm->markDirty(index);
        }
        break;
    case 1: // add
        _record_size = record_size + col_ty[col_id].size();
        _record_onepg = 0;
        while ((_record_onepg + 1) * _record_size + _record_onepg / 8 + 1 <= PAGE_SIZE) _record_onepg++;
        for (uint i = 0; i < record_num; i++) {
            if (i % record_onepg == 0) {
                buf_st = buf = bpm->getPage(main_file, i / record_onepg);
                buf += (record_onepg - 1) / 8 + 1;
            }
            if (i % _record_onepg == 0) {
                _buf_st = _buf = bpm->getPage(_main_file, i / _record_onepg, index);
                _buf += (_record_onepg - 1) / 8 + 1;
            }
            if (exist(buf_st, i % record_onepg)) 
                exist_set(_buf_st, i % _record_onepg);
            else if (exist(_buf_st, i % _record_onepg))
                exist_reset(_buf_st, i % _record_onepg);
            for (uint j = 0; j < col_num - 1; j++) {
                memcpy(_buf, buf, col_ty[j].size());
                buf += col_ty[j].size(); 
                _buf += col_ty[j].size();
            }
            insert(col_ty[col_num - 1].def, col_ty[col_num - 1].ty, col_ty[col_num - 1].size(), _buf);
            bpm->markDirty(index);
        }
        break;
    }
    fm->closeFile(main_file);
    fm->closeFile(_main_file);
    replaceFile(dirPath(db->name, name, "tmp", "usid"), dirPath(db->name, name, "usid"));

    fm->openFile(dirPath(db->name, name, "usid"), main_file);
    record_size = _record_size;
    record_onepg = _record_onepg;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpl $0x0, 0x9fa0(%rax)
jne 0x4b8bb
jmp 0x4c0d8
movq -0x58(%rbp), %rsi
movq 0x38(%rsi), %rdi
movq 0x40(%rsi), %rax
movq %rax, -0x88(%rbp)
addq $0x18, %rsi
movq %rsi, -0x80(%rbp)
leaq 0x38341(%rip), %rdx     # 0x83c1e
movq %rdx, -0x78(%rbp)
leaq 0x3314a(%rip), %rcx     # 0x7ea32
movq %rcx, -0x70(%rbp)
callq 0x353f0
movq -0x88(%rbp), %rdi
movq %rax, %rsi
callq 0x34e40
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rdx
movq -0x70(%rbp), %rcx
movq -0x58(%rbp), %rax
movq 0x38(%rax), %rdi
movq 0x40(%rax), %rax
movq %rax, -0x68(%rbp)
callq 0x353f0
movq -0x68(%rbp), %rdi
movq %rax, %rsi
leaq -0x14(%rbp), %rdx
callq 0x34e90
movl -0xc(%rbp), %eax
movl %eax, -0x5c(%rbp)
testl %eax, %eax
je 0x4b94e
jmp 0x4b93d
movl -0x5c(%rbp), %eax
subl $0x1, %eax
je 0x4bc7c
jmp 0x4c015
movq -0x58(%rbp), %rdi
movl 0x9fa8(%rdi), %eax
movl %eax, -0x8c(%rbp)
addq $0x58, %rdi
movl -0x10(%rbp), %eax
imulq $0x38, %rax, %rax
addq %rax, %rdi
callq 0x3d3d0
movl %eax, %ecx
movl -0x8c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
imull -0x18(%rbp), %eax
movl -0x1c(%rbp), %ecx
shrl $0x3, %ecx
addl %ecx, %eax
addl $0x1, %eax
cmpl $0x2000, %eax           # imm = 0x2000
ja 0x4b9ac
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4b985
movl $0x0, -0x44(%rbp)
movq -0x58(%rbp), %rcx
movl -0x44(%rbp), %eax
cmpl 0x9fa0(%rcx), %eax
jae 0x4bc77
movq -0x58(%rbp), %rcx
movl -0x44(%rbp), %eax
xorl %edx, %edx
divl 0x9fac(%rcx)
cmpl $0x0, %edx
jne 0x4ba25
movq -0x58(%rbp), %rcx
movq 0x48(%rcx), %rdi
movl 0x9fa4(%rcx), %esi
movl -0x44(%rbp), %eax
xorl %edx, %edx
divl 0x9fac(%rcx)
movl %eax, %edx
callq 0x70b0
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq %rcx, -0x38(%rbp)
movl 0x9fac(%rax), %ecx
subl $0x1, %ecx
shrl $0x3, %ecx
addl $0x1, %ecx
movq -0x28(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movl -0x44(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
cmpl $0x0, %edx
jne 0x4ba71
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x14(%rbp), %esi
movl -0x44(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movl %eax, %edx
leaq -0x20(%rbp), %rcx
callq 0x71d0
movq %rax, -0x30(%rbp)
movq %rax, -0x40(%rbp)
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
shrl $0x3, %ecx
addl $0x1, %ecx
movq -0x30(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rax, -0x98(%rbp)
movl -0x44(%rbp), %eax
xorl %edx, %edx
divl 0x9fac(%rcx)
movq -0x98(%rbp), %rax
shrl $0x3, %edx
movl %edx, %edx
movzbl (%rax,%rdx), %eax
movl %eax, -0x90(%rbp)
movl -0x44(%rbp), %eax
xorl %edx, %edx
divl 0x9fac(%rcx)
movl -0x90(%rbp), %eax
movl %edx, %ecx
andl $0x7, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x4bb08
movl -0x44(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movl %edx, %ecx
andl $0x7, %ecx
movl $0x1, %esi
shll %cl, %esi
movq -0x40(%rbp), %rax
movq %rax, -0xa0(%rbp)
movl -0x44(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movq -0xa0(%rbp), %rax
shrl $0x3, %edx
movl %edx, %ecx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
jmp 0x4bb92
movq -0x40(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0x44(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movq -0xb0(%rbp), %rax
shrl $0x3, %edx
movl %edx, %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0xa4(%rbp)
movl -0x44(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movl -0xa4(%rbp), %eax
movl %edx, %ecx
andl $0x7, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x4bb90
movl -0x44(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movl %edx, %ecx
andl $0x7, %ecx
movl $0x1, %esi
shll %cl, %esi
movq -0x40(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl -0x44(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movq -0xb8(%rbp), %rax
shrl $0x3, %edx
movl %edx, %ecx
movzbl (%rax,%rcx), %edx
subl %esi, %edx
movb %dl, (%rax,%rcx)
jmp 0x4bb92
movl $0x0, -0x48(%rbp)
movq -0x58(%rbp), %rcx
movl -0x48(%rbp), %eax
cmpl 0x54(%rcx), %eax
jae 0x4bc59
movl -0x48(%rbp), %eax
cmpl -0x10(%rbp), %eax
je 0x4bbf5
movq -0x58(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xc0(%rbp)
addq $0x58, %rdi
movl -0x48(%rbp), %eax
imulq $0x38, %rax, %rax
addq %rax, %rdi
callq 0x3d3d0
movq -0xc8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movl %eax, %eax
movl %eax, %edx
callq 0x63f0
movq -0x58(%rbp), %rdi
addq $0x58, %rdi
movl -0x48(%rbp), %eax
imulq $0x38, %rax, %rax
addq %rax, %rdi
callq 0x3d3d0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x10(%rbp), %eax
je 0x4bc49
movq -0x58(%rbp), %rdi
addq $0x58, %rdi
movl -0x48(%rbp), %eax
imulq $0x38, %rax, %rax
addq %rax, %rdi
callq 0x3d3d0
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
jmp 0x4bc4b
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x4bb99
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x20(%rbp), %esi
callq 0x7300
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x4b9b3
jmp 0x4c015
movq -0x58(%rbp), %rdi
movl 0x9fa8(%rdi), %eax
movl %eax, -0xcc(%rbp)
addq $0x58, %rdi
movl -0x10(%rbp), %eax
imulq $0x38, %rax, %rax
addq %rax, %rdi
callq 0x3d3d0
movl %eax, %ecx
movl -0xcc(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
imull -0x18(%rbp), %eax
movl -0x1c(%rbp), %ecx
shrl $0x3, %ecx
addl %ecx, %eax
addl $0x1, %eax
cmpl $0x2000, %eax           # imm = 0x2000
ja 0x4bcda
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4bcb3
movl $0x0, -0x4c(%rbp)
movq -0x58(%rbp), %rcx
movl -0x4c(%rbp), %eax
cmpl 0x9fa0(%rcx), %eax
jae 0x4c013
movq -0x58(%rbp), %rcx
movl -0x4c(%rbp), %eax
xorl %edx, %edx
divl 0x9fac(%rcx)
cmpl $0x0, %edx
jne 0x4bd53
movq -0x58(%rbp), %rcx
movq 0x48(%rcx), %rdi
movl 0x9fa4(%rcx), %esi
movl -0x4c(%rbp), %eax
xorl %edx, %edx
divl 0x9fac(%rcx)
movl %eax, %edx
callq 0x70b0
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq %rcx, -0x38(%rbp)
movl 0x9fac(%rax), %ecx
subl $0x1, %ecx
shrl $0x3, %ecx
addl $0x1, %ecx
movq -0x28(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movl -0x4c(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
cmpl $0x0, %edx
jne 0x4bd9f
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x14(%rbp), %esi
movl -0x4c(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movl %eax, %edx
leaq -0x20(%rbp), %rcx
callq 0x71d0
movq %rax, -0x30(%rbp)
movq %rax, -0x40(%rbp)
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
shrl $0x3, %ecx
addl $0x1, %ecx
movq -0x30(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0x4c(%rbp), %eax
xorl %edx, %edx
divl 0x9fac(%rcx)
movq -0xd8(%rbp), %rax
shrl $0x3, %edx
movl %edx, %edx
movzbl (%rax,%rdx), %eax
movl %eax, -0xd0(%rbp)
movl -0x4c(%rbp), %eax
xorl %edx, %edx
divl 0x9fac(%rcx)
movl -0xd0(%rbp), %eax
movl %edx, %ecx
andl $0x7, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x4be36
movl -0x4c(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movl %edx, %ecx
andl $0x7, %ecx
movl $0x1, %esi
shll %cl, %esi
movq -0x40(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl -0x4c(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movq -0xe0(%rbp), %rax
shrl $0x3, %edx
movl %edx, %ecx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
jmp 0x4bec0
movq -0x40(%rbp), %rax
movq %rax, -0xf0(%rbp)
movl -0x4c(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movq -0xf0(%rbp), %rax
shrl $0x3, %edx
movl %edx, %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0xe4(%rbp)
movl -0x4c(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movl -0xe4(%rbp), %eax
movl %edx, %ecx
andl $0x7, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x4bebe
movl -0x4c(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movl %edx, %ecx
andl $0x7, %ecx
movl $0x1, %esi
shll %cl, %esi
movq -0x40(%rbp), %rax
movq %rax, -0xf8(%rbp)
movl -0x4c(%rbp), %eax
xorl %edx, %edx
divl -0x1c(%rbp)
movq -0xf8(%rbp), %rax
shrl $0x3, %edx
movl %edx, %ecx
movzbl (%rax,%rcx), %edx
subl %esi, %edx
movb %dl, (%rax,%rcx)
jmp 0x4bec0
movl $0x0, -0x50(%rbp)
movq -0x58(%rbp), %rcx
movl -0x50(%rbp), %eax
movl 0x54(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0x4bf7a
movq -0x58(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x100(%rbp)
addq $0x58, %rdi
movl -0x50(%rbp), %eax
imulq $0x38, %rax, %rax
addq %rax, %rdi
callq 0x3d3d0
movq -0x108(%rbp), %rdi
movq -0x100(%rbp), %rsi
movl %eax, %eax
movl %eax, %edx
callq 0x63f0
movq -0x58(%rbp), %rdi
addq $0x58, %rdi
movl -0x50(%rbp), %eax
imulq $0x38, %rax, %rax
addq %rax, %rdi
callq 0x3d3d0
movq -0x58(%rbp), %rdi
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
addq $0x58, %rdi
movl -0x50(%rbp), %eax
imulq $0x38, %rax, %rax
addq %rax, %rdi
callq 0x3d3d0
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x4bec7
movq -0x58(%rbp), %rax
movq %rax, %rcx
addq $0x58, %rcx
movl 0x54(%rax), %edx
subl $0x1, %edx
movl %edx, %edx
imulq $0x38, %rdx, %rdx
addq %rdx, %rcx
addq $0x30, %rcx
movq %rcx, -0x118(%rbp)
movq %rax, %rcx
addq $0x58, %rcx
movl 0x54(%rax), %edx
subl $0x1, %edx
movl %edx, %edx
imulq $0x38, %rdx, %rdx
addq %rdx, %rcx
movl 0x24(%rcx), %ecx
movl %ecx, -0x10c(%rbp)
movq %rax, %rdi
addq $0x58, %rdi
movl 0x54(%rax), %eax
subl $0x1, %eax
movl %eax, %eax
imulq $0x38, %rax, %rax
addq %rax, %rdi
callq 0x3d3d0
movq -0x118(%rbp), %rsi
movl -0x10c(%rbp), %edx
movq -0x58(%rbp), %rdi
movl %eax, %ecx
leaq -0x30(%rbp), %r8
callq 0x496a0
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rdi
movl -0x20(%rbp), %esi
callq 0x7300
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x4bce1
jmp 0x4c015
movq -0x58(%rbp), %rax
movq 0x40(%rax), %rdi
movl 0x9fa4(%rax), %esi
callq 0x34df0
movq -0x58(%rbp), %rax
movq 0x40(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x34df0
movq -0x58(%rbp), %rsi
movq 0x38(%rsi), %rdi
addq $0x18, %rsi
leaq 0x37bd3(%rip), %rdx     # 0x83c1e
leaq 0x329e0(%rip), %rcx     # 0x7ea32
callq 0x353f0
movq -0x58(%rbp), %rsi
movq %rax, -0x128(%rbp)
movq 0x38(%rsi), %rdi
addq $0x18, %rsi
leaq 0x329c1(%rip), %rdx     # 0x7ea32
callq 0x35280
movq -0x128(%rbp), %rdi
movq %rax, %rsi
callq 0x357f0
movq -0x58(%rbp), %rsi
movq 0x40(%rsi), %rax
movq %rax, -0x120(%rbp)
movq 0x38(%rsi), %rdi
addq $0x18, %rsi
leaq 0x3298f(%rip), %rdx     # 0x7ea32
callq 0x35280
movq -0x120(%rbp), %rdi
movq -0x58(%rbp), %rdx
movq %rax, %rsi
addq $0x9fa4, %rdx           # imm = 0x9FA4
callq 0x34e90
movq -0x58(%rbp), %rax
movl -0x18(%rbp), %ecx
movl %ecx, 0x9fa8(%rax)
movl -0x1c(%rbp), %ecx
movl %ecx, 0x9fac(%rax)
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)