// File: zimpha[P]MiniSQL/RecordManager.cc
// Function: RecordManager::RecordManagerRecordInsert(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<element, std::allocator<element>> const&, Table&)
void RecordManager::RecordManagerRecordInsert(std::string dbName, const std::vector<element> & entry, Table & nt)
{
    long offset = (nt.blockCount - 1) * BLOCKSIZE;
    bufferIter pBlock;
    std::vector<element> queryTuple;
    int tupleSize = nt.entrySize + 1;

    pBlock = bm.BufferManagerRead(dbName, offset);
    Block & block = *pBlock;
    int endPoint = getInt(block, BLOCKSIZE - 4);
    if (endPoint + tupleSize + 4 >= BLOCKSIZE) {
        // The block is full;
        setInt(block, BLOCKSIZE - 4, BLOCKSIZE);
        offset += BLOCKSIZE;
        Block newBlock;
        newBlock.fileName = dbName;
        newBlock.offset = offset;
        InputRecord(newBlock, 0, entry, nt);
        setInt(newBlock, BLOCKSIZE - 4, tupleSize);
        bm.BufferManagerWrite(newBlock);

        nt.blockCount++;
        nt.write();
    }
    else {
        // The block is not full;
        InputRecord(block, endPoint, entry, nt);
        endPoint += tupleSize;
        setInt(block, BLOCKSIZE - 4, endPoint);
    }
    std::string tableName = dbName.substr(0, dbName.find('.'));
    for (int i = 0; i < nt.attributes.size(); i++) {
        for (auto index : nt.attributes[i].indices) {
            im.insert(tableName+"."+nt.attributes[i].name, entry[i], offset);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x11c0, %rsp           # imm = 0x11C0
movq %rsi, -0x1198(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x11a0(%rbp)
movq -0x20(%rbp), %rax
movl 0x40(%rax), %eax
shll $0xc, %eax
addl $0xfffff000, %eax       # imm = 0xFFFFF000
cltq
movq %rax, -0x28(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x23de0
leaq -0x48(%rbp), %rdi
callq 0x21a10
movq -0x11a0(%rbp), %rax
movq -0x1198(%rbp), %rsi
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
incl %ecx
movl %ecx, -0x4c(%rbp)
movq (%rax), %rdi
movq -0x28(%rbp), %rdx
callq 0xefc0
movq %rax, -0x1190(%rbp)
jmp 0x22c32
movq -0x1190(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xfa80
movq -0x11a0(%rbp), %rdi
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rsi
movl $0xffc, %edx            # imm = 0xFFC
callq 0x227d0
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
addl -0x4c(%rbp), %eax
addl $0x4, %eax
cmpl $0x1000, %eax           # imm = 0x1000
jl 0x22d80
movq -0x11a0(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl $0xffc, %edx            # imm = 0xFFC
movl $0x1000, %ecx           # imm = 0x1000
callq 0x22830
movq -0x28(%rbp), %rax
addq $0x1000, %rax           # imm = 0x1000
movq %rax, -0x28(%rbp)
leaq -0x10a8(%rbp), %rdi
callq 0xfc90
jmp 0x22cb4
movq -0x1198(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x3050
jmp 0x22cc9
movq -0x11a0(%rbp), %rdi
movq -0x28(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
leaq -0x10a8(%rbp), %rsi
xorl %edx, %edx
callq 0x23030
jmp 0x22cf3
movq -0x11a0(%rbp), %rdi
movl -0x4c(%rbp), %ecx
leaq -0x10a8(%rbp), %rsi
movq %rsi, -0x11a8(%rbp)
movl $0xffc, %edx            # imm = 0xFFC
callq 0x22830
movq -0x11a0(%rbp), %rax
movq -0x11a8(%rbp), %rsi
movq (%rax), %rdi
callq 0xf440
jmp 0x22d2d
movq -0x20(%rbp), %rax
movl 0x40(%rax), %ecx
incl %ecx
movl %ecx, 0x40(%rax)
movq -0x20(%rbp), %rdi
callq 0x240c0
jmp 0x22d44
leaq -0x10a8(%rbp), %rdi
callq 0xfc60
jmp 0x22dbe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x2301c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x10a8(%rbp), %rdi
callq 0xfc60
jmp 0x2301c
movq -0x11a0(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl -0x74(%rbp), %edx
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
callq 0x23030
jmp 0x22d9d
movq -0x11a0(%rbp), %rdi
movl -0x4c(%rbp), %eax
addl -0x74(%rbp), %eax
movl %eax, -0x74(%rbp)
movq -0x70(%rbp), %rsi
movl -0x74(%rbp), %ecx
movl $0xffc, %edx            # imm = 0xFFC
callq 0x22830
movq -0x1198(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x11b0(%rbp)
movl $0x2e, %esi
callq 0x33c0
movq -0x1198(%rbp), %rsi
movq -0x11b0(%rbp), %rdx
movq %rax, %rcx
leaq -0x10c8(%rbp), %rdi
callq 0x32f0
jmp 0x22df9
movl $0x0, -0x10cc(%rbp)
movslq -0x10cc(%rbp), %rax
movq %rax, -0x11b8(%rbp)
movq -0x20(%rbp), %rdi
addq $0x28, %rdi
callq 0x7750
movq %rax, %rcx
movq -0x11b8(%rbp), %rax
cmpq %rcx, %rax
jae 0x22ff2
movq -0x20(%rbp), %rdi
addq $0x28, %rdi
movslq -0x10cc(%rbp), %rsi
callq 0x7810
addq $0x30, %rax
movq %rax, -0x10d8(%rbp)
movq -0x10d8(%rbp), %rdi
callq 0x1c060
movq %rax, -0x10e0(%rbp)
movq -0x10d8(%rbp), %rdi
callq 0x1c090
movq %rax, -0x10e8(%rbp)
leaq -0x10e0(%rbp), %rdi
leaq -0x10e8(%rbp), %rsi
callq 0xc8c0
testb $0x1, %al
jne 0x22e92
jmp 0x22fdc
leaq -0x10e0(%rbp), %rdi
callq 0x1c0c0
movq %rax, %rsi
leaq -0x1108(%rbp), %rdi
callq 0x3130
jmp 0x22eaf
movq -0x11a0(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x11c0(%rbp)
leaq 0x43c2(%rip), %rdx      # 0x2728a
leaq -0x1188(%rbp), %rdi
leaq -0x10c8(%rbp), %rsi
callq 0x76d0
jmp 0x22edd
movq -0x20(%rbp), %rdi
addq $0x28, %rdi
movslq -0x10cc(%rbp), %rsi
callq 0x7810
movq %rax, %rdx
leaq -0x1168(%rbp), %rdi
leaq -0x1188(%rbp), %rsi
callq 0x78a0
jmp 0x22f09
movq -0x18(%rbp), %rdi
movslq -0x10cc(%rbp), %rsi
callq 0x8260
movq -0x11c0(%rbp), %rsi
movq %rax, %rcx
movq -0x28(%rbp), %r8
leaq -0x1148(%rbp), %rdi
leaq -0x1168(%rbp), %rdx
callq 0x13a40
jmp 0x22f3c
leaq -0x1148(%rbp), %rdi
callq 0x78f0
leaq -0x1168(%rbp), %rdi
callq 0x35b8
leaq -0x1188(%rbp), %rdi
callq 0x35b8
leaq -0x1108(%rbp), %rdi
callq 0x35b8
leaq -0x10e0(%rbp), %rdi
callq 0x1c0e0
jmp 0x22e76
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x23010
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x22fce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x22fc2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x1168(%rbp), %rdi
callq 0x35b8
leaq -0x1188(%rbp), %rdi
callq 0x35b8
leaq -0x1108(%rbp), %rdi
callq 0x35b8
jmp 0x23010
jmp 0x22fde
movl -0x10cc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10cc(%rbp)
jmp 0x22e03
leaq -0x10c8(%rbp), %rdi
callq 0x35b8
leaq -0x48(%rbp), %rdi
callq 0xb6d0
addq $0x11c0, %rsp           # imm = 0x11C0
popq %rbp
retq
leaq -0x10c8(%rbp), %rdi
callq 0x35b8
leaq -0x48(%rbp), %rdi
callq 0xb6d0
movq -0x60(%rbp), %rdi
callq 0x34f0
nop