// File: /workspace/github_cpp_makefile_star2_O3/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, -0x38(%rbp)
movq %rsi, %r13
movq %rdi, %rbx
leaq 0x88(%rdi), %r14
movq 0x98(%rbx), %rdi
movl 0x8(%rbx), %esi
xorl %edx, %edx
movq %r14, %rcx
callq 0x4478
movq %rax, 0x80(%rbx)
movslq 0x6c(%rbx), %rcx
testq %rcx, %rcx
jle 0x266a
movq 0x70(%rbx), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movl (%rdi,%r8,4), %r9d
xorl %edx, %edx
btl %edx, %r9d
jae 0x26a1
incl %edx
cmpl $0x20, %edx
jne 0x2652
incq %r8
addl $-0x20, %esi
cmpq %rcx, %r8
jne 0x264c
incl %ecx
movl %ecx, 0x6c(%rbx)
movl %ecx, 0x10(%rax)
movslq 0x6c(%rbx), %rcx
movl $0x0, 0x14(%rax,%rcx,4)
movq 0x98(%rbx), %rdi
movslq 0x88(%rbx), %rsi
movq 0x20(%rdi), %rax
movb $0x1, (%rax,%rsi)
callq 0x4536
movl 0x6c(%rbx), %edx
shll $0x5, %edx
jmp 0x26a3
subl %esi, %edx
movl %edx, 0x78(%rbx)
movq 0x98(%rbx), %rdi
movl 0x8(%rbx), %esi
movq %r14, %rcx
callq 0x4478
movq %rax, 0x80(%rbx)
movl 0x64(%rbx), %edx
testl %edx, %edx
jle 0x26f6
xorl %r12d, %r12d
movl %r12d, %ecx
shrl $0x5, %ecx
movl $0x7ff, %esi            # imm = 0x7FF
subl %ecx, %esi
movslq %esi, %rsi
movl (%rax,%rsi,4), %edi
movl $0x1, %r8d
movl %r12d, %ecx
shll %cl, %r8d
btl %r12d, %edi
jae 0x2719
incl %r12d
cmpl %r12d, %edx
jne 0x26c9
movl 0x78(%rbx), %ecx
leal 0x1f(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x5, %eax
movq 0x70(%rbx), %rdx
movl $0x1, %esi
shll %cl, %esi
cltq
orl %esi, (%rdx,%rax,4)
jmp 0x261c
movq %r14, -0x40(%rbp)
orl %r8d, %edi
movl %edi, (%rax,%rsi,4)
movslq 0xc(%rbx), %rcx
movslq %r12d, %rdx
imulq %rcx, %rdx
leaq (%rax,%rdx,4), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x2120
movq 0x6894(%rip), %r13      # 0x8fd8
leaq 0x3913(%rip), %rsi      # 0x605e
movl $0x15, %edx
movq %r13, %rdi
callq 0x2170
movl 0x8(%rbx), %esi
movq %r13, %rdi
callq 0x21f0
movq %rax, %r13
leaq 0x3bcd(%rip), %r14      # 0x633a
movl $0x2, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x2170
movl 0x78(%rbx), %esi
movq %r13, %rdi
callq 0x21f0
movq %rax, %r13
movl $0x2, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x2170
movq %r13, %rdi
movl %r12d, %esi
callq 0x21f0
movq %rax, %r14
leaq 0x38c4(%rip), %rsi      # 0x6074
movl $0x2, %edx
movq %rax, %rdi
callq 0x2170
leaq -0x60(%rbp), %r13
leaq -0x29(%rbp), %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x4582
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movq %r14, %rdi
callq 0x2170
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20c0
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2823
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2160
movq 0x98(%rbx), %rdi
movslq 0x88(%rbx), %rsi
movq 0x20(%rdi), %rax
movb $0x1, %r14b
movb %r14b, (%rax,%rsi)
callq 0x4536
movl 0x78(%rbx), %eax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movl %r12d, 0x4(%rcx)
movq 0x98(%rbx), %rdi
movl 0x8(%rbx), %esi
xorl %edx, %edx
movq -0x40(%rbp), %rcx
callq 0x4478
movq %rax, 0x80(%rbx)
movl 0x68(%rbx), %ecx
incl %ecx
movl %ecx, 0x68(%rbx)
movl %ecx, 0xc(%rax)
movq 0x98(%rbx), %rdi
movslq 0x88(%rbx), %rsi
movq 0x20(%rdi), %rax
movb %r14b, (%rax,%rsi)
callq 0x4536
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x28bd
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2160
movq %rbx, %rdi
callq 0x2200
nop