// File: 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
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rcx
movq 0x98(%rcx), %rdi
movl 0x8(%rcx), %esi
addq $0x88, %rcx
xorl %edx, %edx
callq 0x4ea0
movq %rax, %rcx
movq -0xc0(%rbp), %rax
movq %rcx, 0x80(%rax)
movq 0x80(%rax), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0xc0(%rbp), %rcx
movl -0x2c(%rbp), %eax
cmpl 0x6c(%rcx), %eax
jge 0x298e
movq -0xc0(%rbp), %rax
movq 0x70(%rax), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x20, -0x3c(%rbp)
jge 0x296f
movq -0x38(%rbp), %rax
movl (%rax), %eax
movl -0x3c(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0x2962
movl -0x2c(%rbp), %eax
shll $0x5, %eax
addl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x296f
jmp 0x2964
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2932
cmpl $0x0, -0x24(%rbp)
je 0x2977
jmp 0x298e
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x2902
cmpl $0x0, -0x24(%rbp)
jne 0x29f2
movq -0xc0(%rbp), %rax
movl 0x6c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x6c(%rax)
movl 0x6c(%rax), %edx
movq -0x20(%rbp), %rcx
movl %edx, 0x10(%rcx)
movq -0x20(%rbp), %rcx
movl 0x6c(%rax), %edx
addl $0x5, %edx
movslq %edx, %rdx
shlq $0x2, %rdx
addq %rdx, %rcx
movq %rcx, -0x48(%rbp)
movq -0x48(%rbp), %rcx
movl $0x0, (%rcx)
movq 0x98(%rax), %rdi
movl 0x88(%rax), %esi
callq 0x5040
movq -0xc0(%rbp), %rax
movl 0x6c(%rax), %eax
shll $0x5, %eax
movl %eax, -0x24(%rbp)
movq -0xc0(%rbp), %rcx
movl -0x24(%rbp), %eax
movl %eax, 0x78(%rcx)
movq 0x98(%rcx), %rdi
movl 0x8(%rcx), %esi
movl 0x78(%rcx), %edx
addq $0x88, %rcx
callq 0x4ea0
movq %rax, %rcx
movq -0xc0(%rbp), %rax
movq %rcx, 0x80(%rax)
movq 0x80(%rax), %rax
movq %rax, -0x20(%rbp)
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x50(%rbp)
movq -0xc0(%rbp), %rcx
movl -0x50(%rbp), %eax
cmpl 0x64(%rcx), %eax
jge 0x2acc
movl -0x50(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %eax, -0x54(%rbp)
movq -0x20(%rbp), %rax
movl $0x7ff, %ecx            # imm = 0x7FF
subl -0x54(%rbp), %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movl -0x50(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %edx, -0x64(%rbp)
movq -0x60(%rbp), %rax
movl (%rax), %eax
movl -0x64(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0x2abc
movl -0x50(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x64(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movq -0x60(%rbp), %rax
orl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x2acc
jmp 0x2abe
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x2a42
cmpl $-0x1, -0x4c(%rbp)
jne 0x2b2a
movq -0xc0(%rbp), %rax
movl 0x78(%rax), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0xc0(%rbp), %rax
movl %ecx, -0x68(%rbp)
movq 0x70(%rax), %rcx
movslq -0x68(%rbp), %rdx
shlq $0x2, %rdx
addq %rdx, %rcx
movq %rcx, -0x70(%rbp)
movl 0x78(%rax), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %edx, -0x74(%rbp)
movl -0x74(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movq -0x70(%rbp), %rax
orl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x28b2
movq -0xc0(%rbp), %rcx
movq -0x20(%rbp), %rax
movl 0xc(%rcx), %ecx
movl -0x4c(%rbp), %edx
imull %edx, %ecx
movslq %ecx, %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21b0
movq 0xb47b(%rip), %rdi      # 0xdfd8
leaq 0x6516(%rip), %rsi      # 0x907a
callq 0x21c0
movq %rax, %rdi
movq -0xc0(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x2300
movq %rax, %rdi
leaq 0x67e4(%rip), %rsi      # 0x9369
movq %rsi, -0xe0(%rbp)
callq 0x21c0
movq %rax, %rdi
movq -0xc0(%rbp), %rax
movl 0x78(%rax), %esi
callq 0x2300
movq -0xe0(%rbp), %rsi
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
movl -0x4c(%rbp), %esi
callq 0x2300
movq %rax, %rdi
leaq 0x64c9(%rip), %rsi      # 0x9090
callq 0x21c0
movq %rax, -0xd8(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0xa1(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x2320
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
leaq -0xa0(%rbp), %rdi
callq 0x5070
jmp 0x2c0d
movq -0xd8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x21a0
movq %rax, -0xe8(%rbp)
jmp 0x2c29
movq -0xe8(%rbp), %rdi
movq 0xb391(%rip), %rsi      # 0xdfc8
callq 0x2200
jmp 0x2c3e
leaq -0xa0(%rbp), %rdi
callq 0x2110
leaq -0xa1(%rbp), %rdi
callq 0x2220
movq -0xc0(%rbp), %rax
movq 0x98(%rax), %rdi
movl 0x88(%rax), %esi
callq 0x5040
movq -0xc0(%rbp), %rcx
movl 0x78(%rcx), %edx
movq -0x18(%rbp), %rax
movl %edx, (%rax)
movl -0x4c(%rbp), %edx
movq -0x18(%rbp), %rax
movl %edx, 0x4(%rax)
movq 0x98(%rcx), %rdi
movl 0x8(%rcx), %esi
addq $0x88, %rcx
xorl %edx, %edx
callq 0x4ea0
movq %rax, %rcx
movq -0xc0(%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
xorl %eax, %eax
addq $0xf0, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x2d1f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x2110
leaq -0xa1(%rbp), %rdi
callq 0x2220
movq -0xb0(%rbp), %rdi
callq 0x2310
nopw (%rax,%rax)