// File: /workspace/github_cpp_makefile_star2_O3/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 $0x308, %rsp            # imm = 0x308
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movq %rdx, %r13
shrq $0x20, %r13
leaq 0xb8(%rsp), %rdi
movq %r12, %rsi
movl %r13d, %ecx
callq 0x5f1a
leaq 0xd8(%rsp), %rdi
movq %r12, %rsi
movl %r14d, %edx
callq 0x66b8
leaq 0xf8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
movl $0x2, %edx
callq 0x8f64
leaq 0xf8(%rsp), %rdi
callq 0x3120
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x8fe4
cmpb $-0x1, %bl
je 0x7db2
movzbl %bl, %eax
testl %eax, %eax
jne 0x7de6
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
leaq 0xf8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
movl $0x14, %edx
callq 0x33b0
leaq 0x178(%rsp), %rdi
callq 0x3090
testb %al, %al
je 0x8115
movq %r13, 0x90(%rsp)
movq %r12, 0x98(%rsp)
movq %r14, 0xa0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movq %rax, 0x8(%rsp)
movq (%r15), %r12
movq 0x8(%r15), %rbp
cmpq %rbp, %r12
je 0x7fbf
leaq 0x78(%rsp), %rbx
movq %rbx, %r13
movq 0x18(%r12), %r15
xorl %edi, %edi
callq 0x3230
movq %rax, %rbx
movq (%r12), %r14
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x10(%r12), %rsi
movq 0x18(%r12), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %rdi
callq 0x95b2
movq %r14, 0x50(%rsp)
movq %rbx, 0x58(%rsp)
movq %r15, 0x60(%rsp)
movq %r13, %rbx
movq %r13, 0x68(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
addq %rsi, %rdx
leaq 0x68(%rsp), %rdi
callq 0x95b2
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x7ed7
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xf8(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x6fba
movq (%r12), %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0xa8(%rsp)
movq %rax, 0xb0(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x7f21
movups 0xa8(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x18(%rsp)
jmp 0x7f38
leaq 0x10(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0xa1b4
movq 0x8(%rsp), %rax
movq 0x18(%r12), %rcx
addq %rcx, %rax
addq $0x18, %rax
movq %rax, 0x8(%rsp)
movq 0x68(%rsp), %rdi
cmpq %rbx, %rdi
je 0x7f60
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
addq $0x30, %r12
cmpq %rbp, %r12
jne 0x7e56
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %r14, %rbx
je 0x7fbf
leaq 0x108(%rsp), %r15
leaq 0x58(%rsp), %r12
leaq 0x50(%rsp), %r13
movups (%rbx), %xmm0
movaps %xmm0, 0x50(%rsp)
movl $0x8, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x3320
movl $0x8, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x3320
addq $0x10, %rbx
cmpq %r14, %rbx
jne 0x7f8e
leaq 0x108(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x8, %edx
callq 0x3320
leaq 0xf8(%rsp), %rdi
callq 0x3050
movq 0x98(%rsp), %r14
leaq 0x40(%r14), %rdi
movq 0x40(%r14), %rax
movq 0x60(%r14), %rcx
movq 0x68(%r14), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
movq 0xa0(%rsp), %rbx
movslq %ebx, %rsi
cmpq %rdx, %rsi
cmovbq %rsi, %rdx
testq %rdx, %rdx
movq 0x90(%rsp), %r9
je 0x8034
xorl %r8d, %r8d
xorl %esi, %esi
addl (%rcx,%r8,4), %esi
incq %r8
cmpq %r8, %rdx
jne 0x8026
jmp 0x8036
xorl %esi, %esi
addl %r9d, %esi
movslq %esi, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rsi
leaq 0x10(%rsp), %rdx
callq 0x9084
leaq 0x60(%r14), %r15
movq 0x68(%r14), %rsi
movq %rsi, %rax
subq 0x60(%r14), %rax
shrq $0x2, %rax
cmpl %eax, %ebx
jl 0x8090
movl $0x0, 0x50(%rsp)
cmpq 0x70(%r14), %rsi
je 0x8083
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x68(%r14)
jmp 0x8090
leaq 0x50(%rsp), %rdx
movq %r15, %rdi
callq 0x97a2
testl %ebx, %ebx
js 0x80a0
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movq (%r15), %rax
incl (%rax,%rbx,4)
movq 0x6f01(%rip), %rdi      # 0xefa8
leaq 0x329e(%rip), %rsi      # 0xb34c
movl $0x17, %edx
callq 0x32b0
movq 0xb8(%rsp), %rsi
movq 0xc0(%rsp), %rdx
movq 0x6ed9(%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 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x8115
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0xf8(%rsp), %rdi
callq 0x3080
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8143
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xc8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8164
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x3280
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x81da
jmp 0x81da
jmp 0x8180
jmp 0x8192
jmp 0x8192
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x8fe4
jmp 0x8201
movq %rax, %rbx
jmp 0x8201
movq %rax, %rbx
jmp 0x8222
jmp 0x81da
jmp 0x81be
jmp 0x81da
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x81dd
movq 0x40(%rsp), %rsi
jmp 0x81d0
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r13, %rdi
je 0x81dd
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x81dd
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x81f4
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0xf8(%rsp), %rdi
callq 0x3080
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8222
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xc8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8243
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x33f0
nop