// File: /workspace/github_cpp_makefile_star2_O2/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:
cmpl $0x0, 0x9fa0(%rdi)
je 0x1d643
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, %r14d
movq %rdi, %rbx
movl %edx, (%rsp)
movq 0x38(%rdi), %rdi
movq 0x40(%rbx), %r15
leaq 0x18(%rbx), %rbp
leaq 0x1ae9f(%rip), %r12     # 0x38036
leaq 0x16550(%rip), %r13     # 0x336ee
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x149e5
movq %r15, %rdi
movq %rax, %rsi
callq 0x146c2
movq 0x38(%rbx), %rdi
movq 0x40(%rbx), %r15
movq %rbp, 0x40(%rsp)
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x149e5
leaq 0xc(%rsp), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x146ea
cmpl $0x1, %r14d
movq %rbx, 0x18(%rsp)
je 0x1d3bf
testl %r14d, %r14d
jne 0x1d5a2
movl 0x9fa8(%rbx), %ebp
leaq 0x58(%rbx), %rax
movq %rax, 0x50(%rsp)
movl (%rsp), %r15d
imulq $0x38, %r15, %rax
leaq (%rbx,%rax), %rdi
addq $0x58, %rdi
callq 0x17ffa
movl %ebp, %edx
subl %eax, %edx
pushq $-0x1
popq %rbp
pushq $0x1
popq %rax
incl %ebp
addl %edx, %eax
movl %ebp, %ecx
shrl $0x3, %ecx
addl %eax, %ecx
cmpl $0x2001, %ecx           # imm = 0x2001
jb 0x1d228
movl %edx, 0x24(%rsp)
leal -0x1(%rbp), %eax
shrl $0x3, %eax
movq %rax, 0x48(%rsp)
xorl %r14d, %r14d
movq %rbp, 0x30(%rsp)
cmpl 0x9fa0(%rbx), %r14d
jae 0x1d5a2
movl 0x9fac(%rbx), %ecx
movl %r14d, %eax
xorl %edx, %edx
divl %ecx
testl %edx, %edx
jne 0x1d299
movq 0x48(%rbx), %rdi
movl 0x9fa4(%rbx), %esi
movl %eax, %edx
callq 0x5c8e
movl 0x9fac(%rbx), %ecx
leal -0x1(%rcx), %edx
shrl $0x3, %edx
movq %rax, 0x38(%rsp)
leaq (%rax,%rdx), %r12
incq %r12
movl %r14d, %eax
xorl %edx, %edx
divl %ebp
movl %edx, %ebp
testl %edx, %edx
jne 0x1d2d7
movq 0x48(%rbx), %rdi
movl 0xc(%rsp), %esi
movl %eax, %edx
leaq 0x8(%rsp), %rcx
callq 0x5d50
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, (%rsp)
addq %rcx, %rax
incq %rax
movq %rax, 0x10(%rsp)
movl 0x9fac(%rbx), %ecx
movl %r14d, %eax
xorl %edx, %edx
divl %ecx
movl %edx, %eax
shrl $0x3, %eax
movq 0x38(%rsp), %rcx
movzbl (%rcx,%rax), %eax
andl $0x7, %edx
btl %edx, %eax
movq %r14, 0x28(%rsp)
jae 0x1d30f
movl %ebp, %ecx
andb $0x7, %cl
pushq $0x1
popq %rax
shll %cl, %eax
shrl $0x3, %ebp
movq (%rsp), %rcx
orb %al, (%rcx,%rbp)
jmp 0x1d337
movl %ebp, %eax
movl %ebp, %ecx
andb $0x7, %cl
pushq $0x1
popq %rdx
shll %cl, %edx
shrl $0x3, %eax
movq (%rsp), %rcx
movzbl (%rcx,%rax), %ecx
andl $0x7, %ebp
btl %ebp, %ecx
jae 0x1d337
subb %dl, %cl
movq (%rsp), %rdx
movb %cl, (%rdx,%rax)
movq 0x10(%rsp), %rbp
movq 0x50(%rsp), %r14
xorl %ebx, %ebx
movq 0x18(%rsp), %rax
movl 0x54(%rax), %eax
cmpq %rax, %rbx
jae 0x1d39b
cmpq %rbx, %r15
je 0x1d36a
movq %r14, %rdi
callq 0x17ffa
movl %eax, %edx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x5350
movq %r14, %rdi
callq 0x17ffa
movl %eax, %r13d
cmpq %rbx, %r15
je 0x1d38c
movq %r14, %rdi
callq 0x17ffa
movl %eax, %eax
addq %rax, %rbp
movq %rbp, 0x10(%rsp)
movl %r13d, %eax
addq %rax, %r12
incq %rbx
addq $0x38, %r14
jmp 0x1d343
movq 0x18(%rsp), %rbx
movq 0x48(%rbx), %rdi
movl 0x8(%rsp), %esi
callq 0x5e0a
movq 0x28(%rsp), %r14
incl %r14d
movq 0x30(%rsp), %rbp
jmp 0x1d252
movq %rbx, %rcx
movl 0x9fa8(%rbx), %ebx
leaq 0x58(%rcx), %rax
movq %rax, 0x28(%rsp)
movl (%rsp), %eax
imulq $0x38, %rax, %rax
leaq (%rcx,%rax), %rdi
addq $0x58, %rdi
callq 0x17ffa
addl %ebx, %eax
pushq $-0x1
popq %rbp
pushq $0x1
popq %rdx
incl %ebp
addl %eax, %edx
movl %ebp, %ecx
shrl $0x3, %ecx
addl %edx, %ecx
cmpl $0x2001, %ecx           # imm = 0x2001
jb 0x1d3ed
movl %eax, 0x24(%rsp)
leal -0x1(%rbp), %eax
shrl $0x3, %eax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
movq 0x18(%rsp), %rbx
cmpl 0x9fa0(%rbx), %r14d
jae 0x1d5a2
movl 0x9fac(%rbx), %ecx
movl %r14d, %eax
xorl %edx, %edx
divl %ecx
testl %edx, %edx
jne 0x1d45e
movq 0x48(%rbx), %rdi
movl 0x9fa4(%rbx), %esi
movl %eax, %edx
callq 0x5c8e
movl 0x9fac(%rbx), %ecx
leal -0x1(%rcx), %edx
shrl $0x3, %edx
movq %rax, 0x38(%rsp)
leaq (%rax,%rdx), %r13
incq %r13
movl %r14d, %eax
xorl %edx, %edx
movq %rbp, %r15
divl %ebp
movl %edx, %ebp
testl %edx, %edx
jne 0x1d49f
movq 0x48(%rbx), %rdi
movl 0xc(%rsp), %esi
movl %eax, %edx
leaq 0x8(%rsp), %rcx
callq 0x5d50
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, (%rsp)
addq %rcx, %rax
incq %rax
movq %rax, 0x10(%rsp)
movl 0x9fac(%rbx), %ecx
movl %r14d, %eax
xorl %edx, %edx
divl %ecx
movl %edx, %eax
shrl $0x3, %eax
movq 0x38(%rsp), %rcx
movzbl (%rcx,%rax), %eax
andl $0x7, %edx
btl %edx, %eax
jae 0x1d4d2
movl %ebp, %ecx
andb $0x7, %cl
pushq $0x1
popq %rax
shll %cl, %eax
shrl $0x3, %ebp
movq (%rsp), %rcx
orb %al, (%rcx,%rbp)
jmp 0x1d4fa
movl %ebp, %eax
movl %ebp, %ecx
andb $0x7, %cl
pushq $0x1
popq %rdx
shll %cl, %edx
shrl $0x3, %eax
movq (%rsp), %rcx
movzbl (%rcx,%rax), %ecx
andl $0x7, %ebp
btl %ebp, %ecx
jae 0x1d4fa
subb %dl, %cl
movq (%rsp), %rdx
movb %cl, (%rdx,%rax)
movq 0x10(%rsp), %rbp
movq 0x28(%rsp), %r12
xorl %ebx, %ebx
movq 0x18(%rsp), %rax
movl 0x54(%rax), %eax
decl %eax
cmpq %rax, %rbx
jae 0x1d552
movq %r12, %rdi
callq 0x17ffa
movl %eax, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5350
movq %r12, %rdi
callq 0x17ffa
movl %eax, %eax
addq %rax, %r13
movq %r12, %rdi
callq 0x17ffa
movl %eax, %eax
addq %rax, %rbp
movq %rbp, 0x10(%rsp)
incq %rbx
addq $0x38, %r12
jmp 0x1d506
imulq $0x38, %rax, %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax), %rdi
leaq (%rcx,%rax), %r12
addq $0x30, %r12
movl -0xc(%r12), %ebp
callq 0x17ffa
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movl %ebp, %edx
movl %eax, %ecx
leaq 0x10(%rsp), %r8
callq 0x1bed4
movq 0x48(%rbx), %rdi
movl 0x8(%rsp), %esi
callq 0x5e0a
incl %r14d
movq %r15, %rbp
jmp 0x1d417
movq 0x40(%rbx), %rdi
leaq 0x9fa4(%rbx), %r14
movl 0x9fa4(%rbx), %esi
callq 0x14678
movq 0x40(%rbx), %rdi
movl 0xc(%rsp), %esi
callq 0x14678
movq 0x38(%rbx), %rdi
leaq 0x1aa66(%rip), %rdx     # 0x38036
leaq 0x16117(%rip), %r13     # 0x336ee
movq 0x40(%rsp), %r15
movq %r15, %rsi
movq %r13, %rcx
callq 0x149e5
movq %rax, %r12
movq 0x38(%rbx), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x14944
movq %r12, %rdi
movq %rax, %rsi
callq 0x14bdf
movq 0x38(%rbx), %rdi
movq 0x40(%rbx), %r12
movq %r15, %rsi
movq %r13, %rdx
callq 0x14944
movq %r12, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x146ea
movl 0x24(%rsp), %eax
movl %eax, 0x9fa8(%rbx)
movl %ebp, 0x9fac(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq