// File: /workspace/github_cpp_makefile_star2_O1/SeanChao[P]KV-Store/kvstore.cc
// Function: KVStore::convertMemTable()
void KVStore::convertMemTable() {
    // get pointer to the head of linked list from SkipList. Beware that the
    // last non-nullptr pointer would be the tail, which contains no meaningful
    // data
    std::shared_ptr<typename SkipList<uint64_t, std::string>::Node> p =
        memTable->exportData();
    // while ((p = p->succ) && memTable->valid(p)) std::clog << p->key << "\n";
    std::string lv = resolvePath(0);
    std::string filename = resolvePath(0, 0);
    std::filesystem::create_directories(lv);
    renameLevel(0, 0);
    std::fstream fs(filename, std::ios::out | std::ios::binary);
    // prepares the cache for index data
    std::vector<Index> indexTable;
    uint64_t offset = 0;
    // writes the data segment
    while ((p = p->succ) && memTable->valid(p)) {
        // std::clog << "write key = " << p->key << std::endl;
        // convert to char * on demand
        uint64_t strLen = p->val.length();
        time_t writeTime = time(nullptr);
        Entry e(p->key, writeTime, strLen, p->val);
        writeEntry(fs, e);
        // caches index data
        indexTable.push_back(Index(p->key, offset));
        offset += sizeof(p->key) + sizeof(writeTime) + sizeof(strLen) +
                  p->val.length();
    }
    // writes index data to file
    for (auto i : indexTable) {
        // std::clog << "write key: " << i.key << " offset: " << i.offset
        //   << " tellp: " << fs.tellp() << std::endl;
        fs.write(reinterpret_cast<char *>(&i.key), sizeof(i.key));
        fs.write(reinterpret_cast<char *>(&i.offset), sizeof(i.offset));
    }
    // writes meta data
    // index of indexTable
    // std::clog << "Meta: offset = " << std::hex << "0x" << offset << std::dec
    //   << std::endl;
    fs.write(reinterpret_cast<char *>(&offset), sizeof(offset));
    indexTableList.insert(indexTableList.begin(), indexTable);
    fs.close();
    renameLevel(0, 1);
    std::clog << "memTable -> " << filename << std::endl;
    // update state
    fileNum[0]++;
    // compaction if the number of files in level 0 is more than 2
    if (fileNum[0] > 2) compaction();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x308, %rsp            # imm = 0x308
movq %rdi, %rbx
movq 0x38(%rdi), %rsi
leaq 0x8(%rsp), %rdi
callq 0x8ff2
leaq 0xd8(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x6646
leaq 0xb8(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x5ea6
leaq 0xf8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
movl $0x2, %edx
callq 0x8f3a
leaq 0xf8(%rsp), %rdi
callq 0x3190
leaq 0xf8(%rsp), %rdi
callq 0x8fba
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x688e
movq %rbx, 0xa0(%rsp)
leaq 0xf8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
movl $0x14, %edx
callq 0x33b0
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rcx
movaps %xmm0, (%rcx)
xorl %eax, %eax
movq %rax, 0x10(%rcx)
movq %rax, 0x18(%rsp)
leaq 0x50(%rsp), %rbp
leaq 0x88(%rsp), %r13
movq 0x8(%rsp), %rsi
movq 0x38(%rsi), %rax
movq %rax, 0x8(%rsp)
addq $0x40, %rsi
leaq 0x10(%rsp), %rdi
callq 0x9478
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x55f0
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0x55de
movq 0x99fa(%rip), %rcx      # 0xefc8
cmpb $0x0, (%rcx)
je 0x55d9
incl 0x8(%r14)
jmp 0x55de
lock
incl 0x8(%r14)
cmpq $0x0, 0x28(%rax)
je 0x55f0
cmpq $0x0, 0x38(%rax)
setne %r12b
jmp 0x55f3
xorl %r12d, %r12d
testq %rax, %rax
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x560b
movq %r14, %rdi
callq 0x9408
testb %r12b, %r12b
je 0x5728
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbx
xorl %edi, %edi
callq 0x3230
movq %rax, %r12
movq 0x8(%rsp), %rax
movq (%rax), %r15
movq %rbp, 0x40(%rsp)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x9582
movq %r15, 0x60(%rsp)
movq %r12, 0x68(%rsp)
movq %rbx, 0x70(%rsp)
movq %r13, 0x78(%rsp)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
leaq 0x78(%rsp), %rdi
callq 0x9582
movq 0x40(%rsp), %rdi
cmpq %rbp, %rdi
je 0x568b
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xf8(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x6f48
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rsp), %rcx
movq %rax, 0xa8(%rsp)
movq %rcx, 0xb0(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x56d9
movups 0xa8(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x28(%rsp)
jmp 0x56eb
leaq 0x20(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0xa150
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rsp), %rcx
addq %rcx, %rax
addq $0x18, %rax
movq %rax, 0x18(%rsp)
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x5597
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x5597
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %r13
cmpq %r13, %r12
je 0x577c
leaq 0x108(%rsp), %rbx
leaq 0x68(%rsp), %r14
leaq 0x60(%rsp), %r15
movups (%r12), %xmm0
movaps %xmm0, 0x60(%rsp)
movl $0x8, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3320
movl $0x8, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3320
addq $0x10, %r12
cmpq %r13, %r12
jne 0x5749
leaq 0x108(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movl $0x8, %edx
callq 0x3320
movq 0xa0(%rsp), %r14
leaq 0x40(%r14), %rdi
movq 0x40(%r14), %rsi
leaq 0x20(%rsp), %rdx
callq 0x9058
leaq 0xf8(%rsp), %rdi
callq 0x3050
movq %r14, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x688e
movq 0x97d8(%rip), %rdi      # 0xefa8
leaq 0x5b54(%rip), %rsi      # 0xb32b
movl $0xc, %edx
callq 0x32b0
movq 0xb8(%rsp), %rsi
movq 0xc0(%rsp), %rdx
movq 0x97b0(%rip), %rdi      # 0xefa8
callq 0x32b0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3250
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
movq 0x60(%r14), %rax
movl (%rax), %ecx
leal 0x1(%rcx), %edx
movl %edx, (%rax)
cmpl $0x2, %ecx
jl 0x5841
movq %r14, %rdi
xorl %esi, %esi
callq 0x6fb8
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x5858
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0xf8(%rsp), %rdi
callq 0x3080
leaq 0xc8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5886
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x58a7
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x58b6
callq 0x9408
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x592e
jmp 0x58e0
jmp 0x58e0
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x8fba
jmp 0x5955
movq %rax, %rbx
jmp 0x5955
movq %rax, %rbx
jmp 0x5976
movq %rax, %rbx
jmp 0x5997
jmp 0x592e
jmp 0x590f
jmp 0x592e
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5931
movq 0x50(%rsp), %rsi
jmp 0x5924
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x5931
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x5931
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x5948
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0xf8(%rsp), %rdi
callq 0x3080
leaq 0xc8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5976
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5997
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x59a6
callq 0x9408
movq %rbx, %rdi
callq 0x33f0