// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %rdx, -0x60(%rbp)
movq %rsi, -0x68(%rbp)
movq %rdi, %r15
leaq 0x88(%rdi), %rbx
movq %rbx, -0x58(%rbp)
movq 0x98(%r15), %rdi
movl 0x8(%r15), %esi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x4570
movq %rax, 0x80(%r15)
movslq 0x6c(%r15), %rcx
movl $0x0, %edx
testq %rcx, %rcx
jle 0x2687
movq 0x70(%r15), %rsi
xorl %edi, %edi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %edx, %edx
movl (%rsi,%rdi,4), %r10d
xorl %r9d, %r9d
btl %r9d, %r10d
jae 0x2671
incl %r9d
cmpl $0x20, %r9d
jne 0x2660
jmp 0x2677
subl %r8d, %r9d
movl %r9d, %edx
testl %edx, %edx
jne 0x2687
incq %rdi
addl $-0x20, %r8d
cmpq %rcx, %rdi
jb 0x2659
testl %edx, %edx
jne 0x26c2
incl %ecx
movl %ecx, 0x6c(%r15)
movl %ecx, 0x10(%rax)
movslq 0x6c(%r15), %rcx
movl $0x0, 0x14(%rax,%rcx,4)
movq 0x98(%r15), %rdi
movslq 0x88(%r15), %rsi
movq 0x20(%rdi), %rax
movb $0x1, (%rax,%rsi)
callq 0x462e
movl 0x6c(%r15), %edx
shll $0x5, %edx
movl %edx, 0x78(%r15)
movq 0x98(%r15), %rdi
movl 0x8(%r15), %esi
movq %rbx, %rcx
callq 0x4570
movq %rax, 0x80(%r15)
cmpl $0x0, 0x64(%r15)
jle 0x2734
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0x1, %edx
leal -0x1(%rdx), %ecx
movl %ecx, %esi
shrl $0x5, %esi
movl $0x7ff, %edi            # imm = 0x7FF
subl %esi, %edi
movslq %edi, %rdi
movl (%rax,%rdi,4), %esi
movl $0x1, %r8d
shll %cl, %r8d
btl %ecx, %esi
jb 0x2722
movl %esi, %r9d
orl %r8d, %r9d
movl %r9d, (%rax,%rdi,4)
movl %ecx, %r13d
testl %r8d, %esi
je 0x273a
leal 0x1(%rdx), %ecx
cmpl 0x64(%r15), %edx
movl %ecx, %edx
jl 0x26f2
jmp 0x273a
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl $-0x1, %r13d
je 0x28ba
movslq 0xc(%r15), %rcx
movslq %r13d, %rdx
imulq %rcx, %rdx
leaq (%rax,%rdx,4), %r14
movq %r14, %rdi
movq -0x68(%rbp), %rsi
callq 0x2120
movl $0x15, %edx
movq 0x686d(%rip), %rbx      # 0x8fd8
movq %rbx, %rdi
leaq 0x38e9(%rip), %rsi      # 0x605e
callq 0x2170
movl 0x8(%r15), %esi
movq %rbx, %rdi
callq 0x21f0
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x3ba2(%rip), %r12      # 0x633a
movq %r12, %rsi
callq 0x2170
movl 0x78(%r15), %esi
movq %rbx, %rdi
callq 0x21f0
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2170
movq %rbx, %rdi
movl %r13d, %esi
callq 0x21f0
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x3898(%rip), %rsi      # 0x6074
callq 0x2170
leaq -0x50(%rbp), %rdi
movq %r14, %rsi
leaq -0x29(%rbp), %rdx
callq 0x467e
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x2170
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20c0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
movq -0x58(%rbp), %rbx
je 0x2848
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2160
movq 0x98(%r15), %rdi
movslq 0x88(%r15), %rsi
movq 0x20(%rdi), %rax
movb $0x1, %r14b
movb %r14b, (%rax,%rsi)
callq 0x462e
movl 0x78(%r15), %eax
movq -0x60(%rbp), %rcx
movl %eax, (%rcx)
movl %r13d, 0x4(%rcx)
movq 0x98(%r15), %rdi
movl 0x8(%r15), %esi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x4570
movq %rax, 0x80(%r15)
movl 0x68(%r15), %ecx
incl %ecx
movl %ecx, 0x68(%r15)
movl %ecx, 0xc(%rax)
movq 0x98(%r15), %rdi
movslq 0x88(%r15), %rsi
movq 0x20(%rdi), %rax
movb %r14b, (%rax,%rsi)
callq 0x462e
jmp 0x28d9
movl 0x78(%r15), %ecx
leal 0x1f(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x5, %eax
movq 0x70(%r15), %rdx
movl $0x1, %esi
shll %cl, %esi
cltq
orl %esi, (%rdx,%rax,4)
cmpl $-0x1, %r13d
je 0x2621
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x2910
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2160
movq %rbx, %rdi
callq 0x2200