// File: wwdatabase[P]database/./rcdmana/rm_filehandle.h
// Function: RM_FileHandle::deleteRec(RID const&)
RC RM_FileHandle::deleteRec(const RID &rid) {
    rid.getPageNum(this->pageID);
    this->bufType = bpm->getPage(this->fileID,
                                 this->pageID,
                                 this->index);
    BufType b = this->bufType;
    int tagID ;
    rid.getSlotNum(tagID);

    int tagInt = tagID / 32;
    int tagBit = tagID % 32;
    unsigned int &tagBlock = b[PAGE_INT_NUM-1-tagInt];

    // check bit correct
    if ((tagBlock & (1 << tagBit)) == 0) {
        cout << "error delete" << endl;
        return 108;
    }

    cout << ">>> delete record : (" << fileID << ", " << pageID << ", " << tagID << ")" << endl;
    tagBlock &= ~(1 << tagBit);

    bpm->markDirty(index);

    /* fresh table info with page index 0
     */
    this->bufType = this->bpm->getPage(this->fileID,
                                 0,
                                 this->index);
    b = this->bufType;
    --recordNumForAllPages;
    b[3] = recordNumForAllPages;
    this->bpm->markDirty(this->index);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rsi
movq %rsi, -0x40(%rbp)
movq -0x18(%rbp), %rdi
addq $0x78, %rsi
callq 0x4e80
movq -0x40(%rbp), %rcx
movq 0x98(%rcx), %rdi
movl 0x8(%rcx), %esi
movl 0x78(%rcx), %edx
addq $0x88, %rcx
callq 0x4ea0
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x80(%rax)
movq 0x80(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x24(%rbp), %rsi
callq 0x4f50
movl -0x24(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %edx, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movl $0x7ff, %ecx            # imm = 0x7FF
subl -0x28(%rbp), %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl (%rax), %eax
movl -0x2c(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0x2e25
movq 0xb1da(%rip), %rdi      # 0xdfd8
leaq 0x628e(%rip), %rsi      # 0x9093
callq 0x21c0
movq %rax, %rdi
movq 0xb1b4(%rip), %rsi      # 0xdfc8
callq 0x2200
movl $0x6c, -0x4(%rbp)
jmp 0x2f29
movq 0xb1ac(%rip), %rdi      # 0xdfd8
leaq 0x626d(%rip), %rsi      # 0x90a0
callq 0x21c0
movq %rax, %rdi
movq -0x40(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x2300
movq %rax, %rdi
leaq 0x6518(%rip), %rsi      # 0x9369
callq 0x21c0
movq %rax, %rdi
movq -0x40(%rbp), %rax
movl 0x78(%rax), %esi
callq 0x2300
movq %rax, %rdi
leaq 0x64fa(%rip), %rsi      # 0x9369
callq 0x21c0
movq %rax, %rdi
movl -0x24(%rbp), %esi
callq 0x2300
movq %rax, %rdi
leaq 0x61ef(%rip), %rsi      # 0x9078
callq 0x21c0
movq %rax, %rdi
movq 0xb130(%rip), %rsi      # 0xdfc8
callq 0x2200
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
xorl $-0x1, %edx
movq -0x38(%rbp), %rcx
andl (%rcx), %edx
movl %edx, (%rcx)
movq 0x98(%rax), %rdi
movl 0x88(%rax), %esi
callq 0x5040
movq -0x40(%rbp), %rcx
movq 0x98(%rcx), %rdi
movl 0x8(%rcx), %esi
addq $0x88, %rcx
xorl %edx, %edx
callq 0x4ea0
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x80(%rax)
movq 0x80(%rax), %rcx
movq %rcx, -0x20(%rbp)
movl 0x68(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x68(%rax)
movl 0x68(%rax), %edx
movq -0x20(%rbp), %rcx
movl %edx, 0xc(%rcx)
movq 0x98(%rax), %rdi
movl 0x88(%rax), %esi
callq 0x5040
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)