// File: 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
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x290(%rbp)
leaq -0x218(%rbp), %rdi
movq %rdi, -0x288(%rbp)
movl $0x4, %edx
callq 0x44c0
movq -0x288(%rbp), %rdi
callq 0x44b0
movb %al, -0x279(%rbp)
jmp 0xa31a
movb -0x279(%rbp), %al
testb $0x1, %al
jne 0xa33f
jmp 0xa326
jmp 0xa33f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0xa4f8
leaq -0x218(%rbp), %rdi
movq $-0x8, %rsi
movl $0x2, %edx
callq 0x4430
jmp 0xa359
movq $0x0, -0x230(%rbp)
leaq -0x218(%rbp), %rdi
leaq -0x230(%rbp), %rsi
movl $0x8, %edx
callq 0x43e0
jmp 0xa37e
movq -0x230(%rbp), %rsi
leaq -0x240(%rbp), %rdi
callq 0xcda0
jmp 0xa393
movq -0x240(%rbp), %rsi
movq -0x238(%rbp), %rdx
leaq -0x218(%rbp), %rdi
callq 0x43f0
jmp 0xa3af
leaq -0x258(%rbp), %rdi
callq 0xc790
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x4570
movb %al, -0x291(%rbp)
jmp 0xa3db
movb -0x291(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xa3ec
jmp 0xa4b0
movq $0x0, -0x260(%rbp)
movq $0x0, -0x268(%rbp)
leaq -0x218(%rbp), %rdi
leaq -0x260(%rbp), %rsi
movl $0x8, %edx
callq 0x43e0
jmp 0xa41c
leaq -0x218(%rbp), %rdi
leaq -0x268(%rbp), %rsi
movl $0x8, %edx
callq 0x43e0
jmp 0xa436
leaq -0x218(%rbp), %rdi
callq 0x45c0
movq %rax, -0x2a0(%rbp)
jmp 0xa44b
movq -0x2a0(%rbp), %rax
cmpq $0x0, %rax
jne 0xa47a
jmp 0xa4b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x258(%rbp), %rdi
callq 0xcd20
jmp 0xa4f8
movq -0x260(%rbp), %rsi
movq -0x268(%rbp), %rdx
leaq -0x278(%rbp), %rdi
callq 0xc980
jmp 0xa496
leaq -0x258(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0xc950
jmp 0xa4ab
jmp 0xa3bb
leaq -0x218(%rbp), %rdi
callq 0x4070
jmp 0xa4be
movq -0x290(%rbp), %rdi
addq $0x40, %rdi
leaq -0x258(%rbp), %rsi
callq 0xcde0
jmp 0xa4d7
leaq -0x258(%rbp), %rdi
callq 0xcd20
leaq -0x218(%rbp), %rdi
callq 0x40c0
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
leaq -0x218(%rbp), %rdi
callq 0x40c0
movq -0x220(%rbp), %rdi
callq 0x4530