// File: /workspace/github_cpp_makefile_star2_O1/SeanChao[P]KV-Store/kvstore.cc
// Function: KVStore::readSsTable(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void KVStore::readSsTable(std::string path) {
    // load index table into indexTableList
    std::ifstream fs(path, std::ios::binary);
    // std::clog << "read ss table from " << path << std::endl;
    // reads the offest of the begining of the index part
    if (!fs.is_open()) {
        // std::clog << "Error open file " << path << std::endl;
    }
    fs.seekg(-8, std::ios::end);
    uint64_t offset = 0;
    fs.read(reinterpret_cast<char *>(&offset), sizeof(offset));
    // std::clog << "read: get offset of index table at " << std::hex << offset
    //           << std::dec << std::endl;
    // reads keys and indices and saves them in indexTable
    fs.seekg(offset);
    std::vector<Index> indexTable;
    while (!fs.eof()) {
        uint64_t key = 0;
        uint64_t off = 0;
        // int64_t time = 0;
        fs.read(reinterpret_cast<char *>(&key), sizeof(key));
        // fs.read(reinterpret_cast<char *>(&time), sizeof(time));
        fs.read(reinterpret_cast<char *>(&off), sizeof(off));
        if (fs.gcount() == 0)
            break;  // if fs comes to meta data then break, this condition may
                    // fail if the meta data changes
        // std::clog << "read key: " << key << std::hex << " @0x" << off << " "
        // << std::dec << fs.gcount() << std::endl;
        indexTable.push_back(Index(key, off));
    }
    fs.close();
    // for(auto i: indexTable) {
    //     std::clog << "key " << i.key << "offset "
    // }
    indexTableList.push_back(indexTable);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rdi, %rbx
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movl $0x4, %edx
callq 0x3370
movq %r14, %rdi
movq $-0x8, %rsi
movl $0x2, %edx
callq 0x3330
movq %rbx, 0x30(%rsp)
leaq 0x48(%rsp), %rsi
movq $0x0, (%rsi)
leaq 0x50(%rsp), %rdi
movl $0x8, %edx
callq 0x32f0
movq 0x48(%rsp), %rsi
leaq 0x50(%rsp), %rdi
xorl %edx, %edx
callq 0x3300
xorps %xmm0, %xmm0
movq %rsp, %r14
movaps %xmm0, (%r14)
movq $0x0, 0x10(%r14)
xorl %ebx, %ebx
leaq 0x50(%rsp), %r15
leaq 0x28(%rsp), %r12
leaq 0x20(%rsp), %r13
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x70(%rsp,%rax)
jne 0x793a
movq %rbx, 0x28(%rsp)
movq %rbx, 0x20(%rsp)
movl $0x8, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x32f0
movl $0x8, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x32f0
movq 0x58(%rsp), %rbp
testq %rbp, %rbp
je 0x793a
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rax, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x7924
movups 0x38(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x8(%rsp)
jmp 0x7931
movq %r14, %rdi
leaq 0x38(%rsp), %rdx
callq 0xa150
testq %rbp, %rbp
jne 0x78b0
leaq 0x50(%rsp), %rdi
callq 0x3060
movq 0x30(%rsp), %rdi
addq $0x40, %rdi
movq %rsp, %rsi
callq 0x910e
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x796b
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x50(%rsp), %rdi
callq 0x30b0
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x798b
jmp 0x7992
movq %rax, %rbx
jmp 0x79ab
jmp 0x7992
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x79ab
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x50(%rsp), %rdi
callq 0x30b0
movq %rbx, %rdi
callq 0x33f0
nop