// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rsi, %r14
movq %rdi, %r12
movq %rdx, %r13
shrq $0x20, %r13
leaq 0xb8(%rsp), %rdi
movq %r12, %rsi
movl %r13d, %ecx
callq 0x5ea6
leaq 0xd8(%rsp), %rdi
movq %r12, %rsi
movl %r15d, %edx
callq 0x6646
leaq 0xf8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
movl $0x2, %edx
callq 0x8f3a
leaq 0xf8(%rsp), %rdi
callq 0x3120
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x8fba
cmpb $-0x1, %bl
je 0x7d92
movzbl %bl, %eax
testl %eax, %eax
jne 0x7dc6
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
leaq 0xf8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
movl $0x14, %edx
callq 0x33b0
leaq 0x178(%rsp), %rdi
callq 0x3090
testb %al, %al
je 0x80f5
movq %r13, 0x90(%rsp)
movq %r12, 0x98(%rsp)
movq %r15, 0xa0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movq %rax, 0x8(%rsp)
movq (%r14), %r12
movq 0x8(%r14), %rbp
cmpq %rbp, %r12
je 0x7f4d
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 0x9582
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 0x9582
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x7eb7
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xf8(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x6f48
movq (%r12), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0xa8(%rsp)
movq %rcx, 0xb0(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x7f01
movups 0xa8(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x18(%rsp)
jmp 0x7f13
leaq 0x10(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0xa150
movq 0x18(%r12), %rax
movq 0x8(%rsp), %rcx
addq %rcx, %rax
addq $0x18, %rax
movq %rax, 0x8(%rsp)
movq 0x68(%rsp), %rdi
cmpq %rbx, %rdi
je 0x7f40
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
addq $0x30, %r12
cmpq %rbp, %r12
jne 0x7e36
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %r14, %rbx
je 0x7f9f
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 0x7f6e
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 0x8014
xorl %r8d, %r8d
xorl %esi, %esi
addl (%rcx,%r8,4), %esi
incq %r8
cmpq %r8, %rdx
jne 0x8006
jmp 0x8016
xorl %esi, %esi
addl %r9d, %esi
movslq %esi, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rsi
leaq 0x10(%rsp), %rdx
callq 0x9058
leaq 0x60(%r14), %r15
movq 0x68(%r14), %rsi
movq %rsi, %rax
subq 0x60(%r14), %rax
shrq $0x2, %rax
cmpl %eax, %ebx
jl 0x8070
movl $0x0, 0x50(%rsp)
cmpq 0x70(%r14), %rsi
je 0x8063
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x68(%r14)
jmp 0x8070
leaq 0x50(%rsp), %rdx
movq %r15, %rdi
callq 0x9766
testl %ebx, %ebx
js 0x8080
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movq (%r15), %rax
incl (%rax,%rbx,4)
movq 0x6f21(%rip), %rdi      # 0xefa8
leaq 0x32be(%rip), %rsi      # 0xb34c
movl $0x17, %edx
callq 0x32b0
movq 0xb8(%rsp), %rsi
movq 0xc0(%rsp), %rdx
movq 0x6ef9(%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 0x80f5
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 0x8123
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xc8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8144
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 0x81ba
jmp 0x81ba
jmp 0x8160
jmp 0x8172
jmp 0x8172
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x8fba
jmp 0x81e1
movq %rax, %rbx
jmp 0x81e1
movq %rax, %rbx
jmp 0x8202
jmp 0x81ba
jmp 0x819e
jmp 0x81ba
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x81bd
movq 0x40(%rsp), %rsi
jmp 0x81b0
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r13, %rdi
je 0x81bd
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x81bd
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x81d4
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 0x8202
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xc8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8223
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x33f0
nop