// File: /workspace/github_cpp_makefile_star2_O2/wwdatabase[P]database/./rcdmana/rm_filehandle.h
// Function: RM_FileHandle::insertRec(char const*, RID&)
RC RM_FileHandle::insertRec(const char* data, RID &rid) {

  /* select page id for insert record */
  searchPageID:
    this->bufType = this->bpm->getPage(this->fileID,
                                       0,
                                       this->index);
    BufType b = this->bufType;

    int page_id = 0;

    int pageIndex = 0;
    for (int i = 0; i < bitmapSize; ++i) {
        unsigned int &bitBlock = this->bitmap[i];
        for (int j = 0; j < 32; ++j) {
            if ((bitBlock & (1 << j)) == 0){
                page_id = i * 32 + j + 1;
                break;
            }
        }
        if (page_id != 0)
            break;

        pageIndex = i+1;
    }

    /* current bitmap pages have been full, 
     * ask for more 32 pages.
     */
    if (page_id == 0) {
        ++this->bitmapSize;
        b[4] = this->bitmapSize;
        unsigned int &bitmap = b[5+bitmapSize];
        bitmap = 0;
        this->bpm->markDirty(this->index);

        page_id = this->bitmapSize*32;
    }

    this->pageID = page_id;
    this->bufType = this->bpm->getPage(this->fileID, 
                                 this->pageID,
                                 this->index);
    b = this->bufType;

    /* select tags for insert record */
    int tagID = -1;
    for (int i = 0; i < this->recordNumForEachPage; ++i){
        int tagInt = i / 32;
        unsigned int &tagBlock = b[PAGE_INT_NUM-1-tagInt];
        int tagBit = i % 32;
        if ((tagBlock & (1 << tagBit)) == 0){
            tagID = i;
            tagBlock |= (1 << tagBit);
            break;
        }
    }

    /* the selected page is full exactly,
     * but not labeled full,
     */
    if (tagID == -1) {
        int bitmapInt = pageID / 32;
        unsigned int &bitBlock = this->bitmap[bitmapInt];
        int bitmapBit = pageID % 32;
        bitBlock |= (1 << bitmapBit);

        goto searchPageID;
    }
    
    char *rcd = (char *)(b + recordSize * tagID);
    strcpy(rcd, data);
    cout << ">>> insert record : (" << fileID << ", " << pageID << ", " << tagID << ") "
         << string(rcd) << endl;
    // cout << "insert record : " << string(rcd) << endl;

    bpm->markDirty(this->index);

    rid.pageNum = pageID;
    rid.slotNum = tagID;

    /* 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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, -0x40(%rbp)
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x88(%rdi), %r13
pushq $-0x1
popq %r14
pushq $0x1
popq %r15
movq %r13, -0x38(%rbp)
movq 0x98(%rbx), %rdi
movl 0x8(%rbx), %esi
xorl %edx, %edx
movq %r13, %rcx
callq 0x3b76
movq %rax, 0x80(%rbx)
movl 0x6c(%rbx), %ecx
movq 0x70(%rbx), %rsi
testl %ecx, %ecx
movl $0x0, %edi
cmovgl %ecx, %edi
movl %r14d, %r8d
xorl %r9d, %r9d
cmpq %rdi, %r9
je 0x271c
movl (%rsi,%r9,4), %r10d
xorl %edx, %edx
cmpl $0x20, %edx
je 0x269a
btl %edx, %r10d
jae 0x26a3
incl %edx
jmp 0x268b
incq %r9
addl $-0x20, %r8d
jmp 0x267c
subl %r8d, %edx
movl %edx, 0x78(%rbx)
movq 0x98(%rbx), %rdi
movl 0x8(%rbx), %esi
movq %r13, %rcx
callq 0x3b76
movq %rax, 0x80(%rbx)
movl 0x64(%rbx), %edx
xorl %r13d, %r13d
testl %edx, %edx
cmovlel %r13d, %edx
cmpl %r13d, %edx
je 0x26fa
movl %r13d, %ecx
shrl $0x5, %ecx
movl $0x7ff, %esi            # imm = 0x7FF
subl %ecx, %esi
movslq %esi, %rsi
movl (%rax,%rsi,4), %edi
movl %r15d, %r8d
movl %r13d, %ecx
shll %cl, %r8d
btl %r13d, %edi
jae 0x274a
incl %r13d
jmp 0x26ce
movl 0x78(%rbx), %ecx
movl %ecx, %eax
cltd
pushq $0x20
popq %rsi
idivl %esi
movq 0x70(%rbx), %rdx
movl %r15d, %esi
shll %cl, %esi
cltq
orl %esi, (%rdx,%rax,4)
movq -0x38(%rbp), %r13
jmp 0x264a
incl %ecx
movl %ecx, 0x6c(%rbx)
movl %ecx, 0x10(%rax)
movslq 0x6c(%rbx), %rcx
andl $0x0, 0x14(%rax,%rcx,4)
movq 0x98(%rbx), %rdi
movl 0x88(%rbx), %esi
callq 0x3c52
movl 0x6c(%rbx), %edx
shll $0x5, %edx
jmp 0x26a6
orl %r8d, %edi
movl %edi, (%rax,%rsi,4)
movslq 0xc(%rbx), %rcx
movslq %r13d, %rdx
imulq %rcx, %rdx
leaq (%rax,%rdx,4), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x2160
movq 0x5867(%rip), %rdi      # 0x7fd8
leaq 0x28e6(%rip), %rsi      # 0x505e
callq 0x2170
movl 0x8(%rbx), %esi
movq %rax, %rdi
callq 0x2260
leaq 0x2bab(%rip), %r12      # 0x533a
movq %rax, %rdi
movq %r12, %rsi
callq 0x2170
movl 0x78(%rbx), %esi
movq %rax, %rdi
callq 0x2260
movq %rax, %rdi
movq %r12, %rsi
callq 0x2170
movq %rax, %rdi
movl %r13d, %esi
callq 0x2260
leaq 0x28b2(%rip), %rsi      # 0x5074
movq %rax, %rdi
callq 0x2170
movq %rax, %r12
leaq -0x60(%rbp), %r14
leaq -0x29(%rbp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x3c68
movq %r12, %rdi
movq %r14, %rsi
callq 0x2150
movq %rax, %rdi
callq 0x20b0
leaq -0x60(%rbp), %rdi
callq 0x20a0
movq 0x98(%rbx), %rdi
movl 0x88(%rbx), %esi
callq 0x3c52
movl 0x78(%rbx), %eax
movq -0x40(%rbp), %rcx
movl %eax, (%rcx)
movl %r13d, 0x4(%rcx)
movq 0x98(%rbx), %rdi
movl 0x8(%rbx), %esi
xorl %edx, %edx
movq -0x38(%rbp), %rcx
callq 0x3b76
movq %rax, 0x80(%rbx)
movl 0x68(%rbx), %ecx
incl %ecx
movl %ecx, 0x68(%rbx)
movl %ecx, 0xc(%rax)
movq 0x98(%rbx), %rdi
movl 0x88(%rbx), %esi
callq 0x3c52
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x2270
nop