// File: /workspace/github_cpp_makefile_star2_O2/SeanChao[P]KV-Store/kvstore.cc
// Function: KVStore::writeSsTable(std::vector<Pair, std::allocator<Pair>>, Location, bool)
void KVStore::writeSsTable(std::vector<Pair> table, Location loc, bool tmp) {
    std::string path = resolvePath(loc.level, loc.id);
    std::string folder = resolvePath(loc.level);
    if (!std::filesystem::exists(folder))
        std::filesystem::create_directories(folder);
    std::fstream fs(path, std::ios::out | std::ios::binary);
    if (!fs.is_open()) {
        // std::clog << "[writeSsTable] Failed to open sstable file " << path
        // << std::endl;
        return;
    }
    // prepares the cache for index data
    std::vector<Index> indexTable;
    uint64_t offset = 0;
    // writes the data segment
    for (auto &p : table) {
        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
        // << 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));
    fs.close();
    // update state
    indexTableList.insert(indexTableList.begin() + getIndex(loc), indexTable);
    if ((int)fileNum.size() <= loc.level) fileNum.push_back(0);
    if (loc.level >= 0) fileNum[loc.level]++;
    std::clog << "write ss table done -> " << path << std::endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq %rdx, 0x8(%rsp)
movq %rdx, %rcx
shrq $0x20, %rcx
leaq 0xa8(%rsp), %rdi
movq %r15, %rsi
callq 0x51ec
leaq 0x88(%rsp), %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x5694
leaq 0xe8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x73a6
leaq 0xe8(%rsp), %rdi
callq 0x738a
movl %eax, %ebx
leaq 0xe8(%rsp), %rdi
callq 0x740e
testb %bl, %bl
jne 0x66d6
leaq 0xe8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x73a6
leaq 0xe8(%rsp), %rdi
callq 0x31a0
leaq 0xe8(%rsp), %rdi
callq 0x740e
leaq 0xe8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
pushq $0x14
popq %rdx
callq 0x33b0
leaq 0x168(%rsp), %rdi
callq 0x3080
testb %al, %al
je 0x68e6
movq %r15, 0x30(%rsp)
leaq 0x10(%rsp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
andq $0x0, (%rsp)
movaps %xmm0, (%rax)
movq (%r14), %r15
movq 0x8(%r14), %rax
movq %rax, 0x38(%rsp)
leaq 0x68(%rsp), %r13
leaq 0xc8(%rsp), %r12
cmpq 0x38(%rsp), %r15
je 0x67df
movq %r13, %rbp
leaq 0x10(%r15), %rbx
movq 0x18(%r15), %r14
xorl %edi, %edi
callq 0x3250
movq %rax, %r13
movq (%r15), %r15
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3340
movq %r15, 0x50(%rsp)
movq %r13, 0x58(%rsp)
movq %r14, 0x60(%rsp)
movq %rbp, %r13
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3340
movq %r12, %rdi
callq 0x3438
leaq 0xe8(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x5bfa
movq -0x10(%rbx), %rax
movq (%rsp), %r14
movq %rax, 0x40(%rsp)
movq %r14, 0x48(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x8492
movq 0x8(%rbx), %rax
addq %r14, %rax
addq $0x18, %rax
movq %rax, (%rsp)
movq %r13, %rdi
callq 0x3438
addq $0x20, %rbx
movq %rbx, %r15
jmp 0x6736
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbp
leaq 0xf8(%rsp), %rbx
leaq 0x58(%rsp), %r14
leaq 0x50(%rsp), %r15
pushq $0x8
popq %r12
cmpq %rbp, %r13
je 0x6830
movups (%r13), %xmm0
movaps %xmm0, 0x50(%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 0x67ff
movq %rsp, %rsi
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x3320
leaq 0xe8(%rsp), %rdi
callq 0x3040
movq 0x30(%rsp), %r15
leaq 0x40(%r15), %rbx
movq 0x40(%r15), %r14
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x69a2
cltq
imulq $0x18, %rax, %rsi
addq %r14, %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x7452
movq 0x68(%r15), %rax
subq 0x60(%r15), %rax
leaq 0x60(%r15), %rbx
shrq $0x2, %rax
movl 0x8(%rsp), %r14d
cmpl %eax, %r14d
jl 0x68a5
leaq 0x50(%rsp), %rsi
andl $0x0, (%rsi)
movq %rbx, %rdi
callq 0x7bee
testl %r14d, %r14d
js 0x68b1
movq (%rbx), %rax
incl (%rax,%r14,4)
movq 0x86f0(%rip), %rdi      # 0xefa8
leaq 0x3a8d(%rip), %rsi      # 0xa34c
callq 0x3270
leaq 0xa8(%rsp), %rsi
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
callq 0x3160
leaq 0x10(%rsp), %rdi
callq 0x845e
leaq 0xe8(%rsp), %rdi
callq 0x3070
leaq 0x88(%rsp), %rdi
callq 0x3438
leaq 0xa8(%rsp), %rdi
callq 0x3438
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6965
jmp 0x6965
jmp 0x6929
jmp 0x693b
jmp 0x693b
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x740e
jmp 0x697f
movq %rax, %rbx
jmp 0x697f
movq %rax, %rbx
jmp 0x698c
jmp 0x6965
jmp 0x6965
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
jmp 0x695e
jmp 0x6958
movq %rax, %rbx
movq %rbp, %rdi
callq 0x3438
jmp 0x6968
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x845e
leaq 0xe8(%rsp), %rdi
callq 0x3070
leaq 0x88(%rsp), %rdi
callq 0x3438
leaq 0xa8(%rsp), %rdi
callq 0x3438
movq %rbx, %rdi
callq 0x33e0
nop