// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
leaq 0x50(%rsp), %rbx
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x3370
pushq $-0x8
popq %rsi
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x3330
movq %r14, 0x10(%rsp)
leaq 0x18(%rsp), %rsi
andq $0x0, (%rsi)
leaq 0x50(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x32e0
movq 0x18(%rsp), %rsi
leaq 0x50(%rsp), %rdi
xorl %edx, %edx
callq 0x32f0
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %r14
movaps %xmm0, (%r14)
andq $0x0, 0x10(%r14)
leaq 0x50(%rsp), %r15
leaq 0x8(%rsp), %r12
pushq $0x8
popq %r13
movq %rsp, %rbp
leaq 0x20(%rsp), %rbx
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x70(%rsp,%rax)
jne 0x633e
andq $0x0, 0x8(%rsp)
andq $0x0, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x32e0
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x32e0
cmpq $0x0, 0x58(%rsp)
je 0x633e
movq 0x8(%rsp), %rax
movq (%rsp), %rcx
movq %rax, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8492
jmp 0x62df
leaq 0x50(%rsp), %rdi
callq 0x3050
movq 0x10(%rsp), %rdi
addq $0x40, %rdi
leaq 0x30(%rsp), %rsi
callq 0x74ea
leaq 0x30(%rsp), %rdi
callq 0x845e
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 0x6385
jmp 0x638c
movq %rax, %rbx
jmp 0x6399
jmp 0x638c
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x845e
leaq 0x50(%rsp), %rdi
callq 0x30b0
movq %rbx, %rdi
callq 0x33e0
nop