// File: /workspace/github_cpp_makefile_star2_O3/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 0x901c
leaq 0xd8(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x66b8
leaq 0xb8(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x5f1a
leaq 0xf8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
movl $0x2, %edx
callq 0x8f64
leaq 0xf8(%rsp), %rdi
callq 0x3190
leaq 0xf8(%rsp), %rdi
callq 0x8fe4
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x6900
leaq 0xf8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
movl $0x14, %edx
callq 0x33b0
movq %rbx, 0xa0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movq %rax, 0x18(%rsp)
leaq 0x10(%rsp), %r14
movq -0x8(%r14), %rsi
movq 0x38(%rsi), %rax
movq %rax, -0x8(%r14)
addq $0x40, %rsi
movq %r14, %rdi
callq 0x94a6
movq -0x8(%r14), %rax
testq %rax, %rax
je 0x57c0
leaq 0x50(%rsp), %r13
leaq 0x88(%rsp), %r12
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x564e
movq 0x9984(%rip), %rcx      # 0xefc8
cmpb $0x0, (%rcx)
je 0x5665
incl 0x8(%rdi)
jmp 0x5669
cmpq $0x0, 0x28(%rax)
je 0x57c0
cmpq $0x0, 0x38(%rax)
jne 0x568b
jmp 0x57c0
lock
incl 0x8(%rdi)
cmpq $0x0, 0x28(%rax)
je 0x57bb
movq 0x38(%rax), %rbx
callq 0x9436
testq %rbx, %rbx
je 0x57c0
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbp
xorl %edi, %edi
callq 0x3230
movq %rax, %r15
movq 0x8(%rsp), %rax
movq (%rax), %rbx
movq %r13, 0x40(%rsp)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x95b2
movq %rbx, 0x60(%rsp)
movq %r15, 0x68(%rsp)
movq %rbp, 0x70(%rsp)
movq %r12, 0x78(%rsp)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
leaq 0x78(%rsp), %rdi
callq 0x95b2
movq 0x40(%rsp), %rdi
cmpq %r13, %rdi
je 0x56fd
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xf8(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x6fba
movq 0x8(%rsp), %rax
movq (%rax), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0xa8(%rsp)
movq %rax, 0xb0(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x574b
movups 0xa8(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x28(%rsp)
jmp 0x5762
leaq 0x20(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0xa1b4
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rcx
addq %rcx, %rax
addq $0x18, %rax
movq %rax, 0x18(%rsp)
movq 0x78(%rsp), %rdi
cmpq %r12, %rdi
je 0x5796
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x8(%rsp), %rsi
movq 0x38(%rsi), %rax
movq %rax, 0x8(%rsp)
addq $0x40, %rsi
movq %r14, %rdi
callq 0x94a6
movq 0x8(%rsp), %rax
testq %rax, %rax
jne 0x5633
jmp 0x57c0
callq 0x9436
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rbp
cmpq %rbp, %r13
je 0x5814
leaq 0x108(%rsp), %r14
leaq 0x68(%rsp), %r15
leaq 0x60(%rsp), %r12
movups (%r13), %xmm0
movaps %xmm0, 0x60(%rsp)
movl $0x8, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x3320
movl $0x8, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x3320
addq $0x10, %r13
cmpq %rbp, %r13
jne 0x57e1
leaq 0x108(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movl $0x8, %edx
callq 0x3320
movq 0xa0(%rsp), %rbx
leaq 0x40(%rbx), %rdi
movq 0x40(%rbx), %rsi
leaq 0x20(%rsp), %rdx
callq 0x9084
leaq 0xf8(%rsp), %rdi
callq 0x3050
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x6900
movq 0x9740(%rip), %rdi      # 0xefa8
leaq 0x5abc(%rip), %rsi      # 0xb32b
movl $0xc, %edx
callq 0x32b0
movq 0xb8(%rsp), %rsi
movq 0xc0(%rsp), %rdx
movq 0x9718(%rip), %rdi      # 0xefa8
callq 0x32b0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3250
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
movq 0x60(%rbx), %rax
movl (%rax), %ecx
leal 0x1(%rcx), %edx
movl %edx, (%rax)
cmpl $0x2, %ecx
jl 0x58d9
movq %rbx, %rdi
xorl %esi, %esi
callq 0x702a
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x58f0
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 0x591e
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x593f
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x594e
callq 0x9436
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x59c6
jmp 0x5978
jmp 0x5978
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x8fe4
jmp 0x59ed
movq %rax, %rbx
jmp 0x59ed
movq %rax, %rbx
jmp 0x5a0e
movq %rax, %rbx
jmp 0x5a2f
jmp 0x59a5
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r13, %rdi
je 0x59c9
movq 0x50(%rsp), %rsi
jmp 0x59ba
jmp 0x59c6
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r12, %rdi
je 0x59c9
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x59c9
jmp 0x59c6
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x59e0
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 0x5a0e
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5a2f
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5a3e
callq 0x9436
movq %rbx, %rdi
callq 0x33f0