// File: /workspace/github_cpp_makefile_star2_O2/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 0x742c
leaq 0xb8(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x5694
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x51ec
leaq 0xf8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x73a6
leaq 0xf8(%rsp), %rdi
callq 0x31a0
leaq 0xf8(%rsp), %rdi
callq 0x740e
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x579c
movq %rbx, 0x38(%rsp)
leaq 0xf8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
pushq $0x14
popq %rdx
callq 0x33b0
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
andq $0x0, 0x18(%rsp)
xorl %r13d, %r13d
leaq 0x8(%rsp), %rbx
movq 0x8(%rsp), %rsi
addq $0x38, %rsi
movq %rbx, %rdi
callq 0x787c
cmpq $0x0, 0x8(%rsp)
je 0x4c68
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x78e2
movq 0x40(%rsp), %rax
testq %rax, %rax
je 0x4c5e
cmpq $0x0, 0x28(%rax)
je 0x4c5e
movq 0x38(%rax), %r14
leaq 0x48(%rsp), %rdi
callq 0x77e6
testq %r14, %r14
je 0x4c68
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r15
xorl %edi, %edi
callq 0x3250
movq %rax, %r12
movq 0x8(%rsp), %rsi
movq (%rsi), %r14
addq $0x8, %rsi
leaq 0xd8(%rsp), %rbp
movq %rbp, %rdi
callq 0x3340
movq %r14, 0x60(%rsp)
movq %r12, 0x68(%rsp)
movq %r15, 0x70(%rsp)
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3340
movq %rbp, %rdi
callq 0x3438
leaq 0xf8(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x5bfa
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x50(%rsp)
movq %r13, 0x58(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x8492
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
addq %rax, %r13
addq $0x18, %r13
movq %r13, 0x18(%rsp)
movq %r14, %rdi
callq 0x3438
jmp 0x4b59
leaq 0x48(%rsp), %rdi
callq 0x77e6
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rbp
leaq 0x108(%rsp), %rbx
leaq 0x68(%rsp), %r14
leaq 0x60(%rsp), %r15
pushq $0x8
popq %r12
cmpq %rbp, %r13
je 0x4cb9
movups (%r13), %xmm0
movaps %xmm0, 0x60(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3320
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x3320
addq $0x10, %r13
jmp 0x4c88
leaq 0x18(%rsp), %rsi
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x3320
movq 0x38(%rsp), %rbx
leaq 0x40(%rbx), %rdi
movq 0x40(%rbx), %rsi
leaq 0x20(%rsp), %rdx
callq 0x7452
leaq 0xf8(%rsp), %rdi
callq 0x3040
pushq $0x1
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x579c
movq 0xa2a7(%rip), %rdi      # 0xefa8
leaq 0x5623(%rip), %rsi      # 0xa32b
callq 0x3270
leaq 0x98(%rsp), %rsi
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
callq 0x3160
movq 0x60(%rbx), %rax
movl (%rax), %ecx
leal 0x1(%rcx), %edx
movl %edx, (%rax)
cmpl $0x2, %ecx
jl 0x4d3f
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5c64
leaq 0x20(%rsp), %rdi
callq 0x845e
leaq 0xf8(%rsp), %rdi
callq 0x3070
leaq 0x98(%rsp), %rdi
callq 0x3438
leaq 0xb8(%rsp), %rdi
callq 0x3438
leaq 0x10(%rsp), %rdi
callq 0x77e6
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4dd5
jmp 0x4da4
jmp 0x4da4
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x740e
jmp 0x4def
movq %rax, %rbx
jmp 0x4def
movq %rax, %rbx
jmp 0x4dfc
movq %rax, %rbx
jmp 0x4e09
jmp 0x4dd5
jmp 0x4db7
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
jmp 0x4dcc
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x3438
jmp 0x4dd8
jmp 0x4dd5
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x845e
leaq 0xf8(%rsp), %rdi
callq 0x3070
leaq 0x98(%rsp), %rdi
callq 0x3438
leaq 0xb8(%rsp), %rdi
callq 0x3438
leaq 0x10(%rsp), %rdi
callq 0x77e6
movq %rbx, %rdi
callq 0x33e0
nop