// File: SeanChao[P]KV-Store/kvstore.cc
// Function: KVStore::readSsTable(int, int)
std::vector<Pair> KVStore::readSsTable(int level, int id) {
    std::vector<Pair> table;
    std::string path = resolvePath(level, id);
    std::ifstream fs(path, std::ios::binary);
    // std::clog << "read ss table from " << path << std::endl;
    if (!fs.is_open()) {
        // std::clog << "error read sstable " << path << std::endl;
        return table;
    }
    fs.seekg(-8, std::ios::end);
    uint64_t offset = 0;
    fs.read(reinterpret_cast<char *>(&offset), sizeof(offset));
    // std::clog << "read: offset of index table at " << std::hex << offset
    //           << std::dec << std::endl;

    fs.seekg(std::ios::beg);
    while (fs.tellg() < (int64_t)offset) {
        // std::clog << "fs pos: " << fs.tellg() << std::endl;
        uint64_t key = 0;
        uint64_t len = 0;
        time_t time = 0;
        fs.read(reinterpret_cast<char *>(&key), sizeof(key));
        fs.read(reinterpret_cast<char *>(&time), sizeof(time));
        fs.read(reinterpret_cast<char *>(&len), sizeof(len));
        char *tmp = new char[len + 1];
        fs.read(tmp, len);
        tmp[len] = '\0';
        std::string val(tmp);
        delete[] tmp;
        table.push_back(Pair(key, time, val));
    }
    fs.close();
    // for (auto i : table)
    // std::clog << "key " << i.key << "\tval " << std::string(i.val, 0, 50)
    // << std::endl;

    return table;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x390, %rsp            # imm = 0x390
movq %rdi, -0x338(%rbp)
movq %rdi, %rax
movq %rax, -0x330(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x328(%rbp)
movb $0x0, -0x19(%rbp)
callq 0xce50
movq -0x328(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
leaq -0x40(%rbp), %rdi
callq 0x8330
jmp 0xa566
leaq -0x258(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movl $0x4, %edx
callq 0x44c0
jmp 0xa57d
leaq -0x258(%rbp), %rdi
callq 0x44b0
movb %al, -0x339(%rbp)
jmp 0xa591
movb -0x339(%rbp), %al
testb $0x1, %al
jne 0xa5e3
jmp 0xa59d
movb $0x1, -0x19(%rbp)
movl $0x1, -0x25c(%rbp)
jmp 0xa8f8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xa948
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xa921
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xa915
leaq -0x258(%rbp), %rdi
movq $-0x8, %rsi
movl $0x2, %edx
callq 0x4430
jmp 0xa5fd
movq $0x0, -0x268(%rbp)
leaq -0x258(%rbp), %rdi
leaq -0x268(%rbp), %rsi
movl $0x8, %edx
callq 0x43e0
jmp 0xa622
xorl %eax, %eax
movl %eax, %esi
leaq -0x278(%rbp), %rdi
callq 0xcda0
jmp 0xa634
movq -0x278(%rbp), %rsi
movq -0x270(%rbp), %rdx
leaq -0x258(%rbp), %rdi
callq 0x43f0
jmp 0xa650
jmp 0xa652
leaq -0x258(%rbp), %rdi
callq 0x4520
movq %rdx, -0x350(%rbp)
movq %rax, -0x348(%rbp)
jmp 0xa66e
movq -0x350(%rbp), %rax
movq -0x348(%rbp), %rcx
movq %rcx, -0x288(%rbp)
movq %rax, -0x280(%rbp)
leaq -0x288(%rbp), %rdi
callq 0xce70
movq %rax, -0x358(%rbp)
jmp 0xa69f
movq -0x358(%rbp), %rax
cmpq -0x268(%rbp), %rax
jge 0xa8dc
movq $0x0, -0x290(%rbp)
movq $0x0, -0x298(%rbp)
movq $0x0, -0x2a0(%rbp)
leaq -0x258(%rbp), %rdi
leaq -0x290(%rbp), %rsi
movl $0x8, %edx
callq 0x43e0
jmp 0xa6ee
leaq -0x258(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
movl $0x8, %edx
callq 0x43e0
jmp 0xa708
leaq -0x258(%rbp), %rdi
leaq -0x298(%rbp), %rsi
movl $0x8, %edx
callq 0x43e0
jmp 0xa722
movq -0x298(%rbp), %rdi
incq %rdi
callq 0x4030
movq %rax, -0x360(%rbp)
jmp 0xa73a
movq -0x360(%rbp), %rax
movq %rax, -0x2a8(%rbp)
movq -0x2a8(%rbp), %rsi
movq -0x298(%rbp), %rdx
leaq -0x258(%rbp), %rdi
callq 0x43e0
jmp 0xa764
movq -0x2a8(%rbp), %rax
movq -0x298(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x2a8(%rbp), %rax
movq %rax, -0x370(%rbp)
leaq -0x2c9(%rbp), %rdi
movq %rdi, -0x368(%rbp)
callq 0x4540
movq -0x370(%rbp), %rsi
movq -0x368(%rbp), %rdx
leaq -0x2c8(%rbp), %rdi
callq 0x5020
jmp 0xa7b3
leaq -0x2c9(%rbp), %rdi
callq 0x4340
movq -0x2a8(%rbp), %rax
movq %rax, -0x378(%rbp)
cmpq $0x0, %rax
je 0xa7df
movq -0x378(%rbp), %rdi
callq 0x4400
movq -0x290(%rbp), %rax
movq %rax, -0x388(%rbp)
movq -0x2a0(%rbp), %rax
movq %rax, -0x380(%rbp)
leaq -0x320(%rbp), %rdi
leaq -0x2c8(%rbp), %rsi
callq 0x4170
jmp 0xa810
movq -0x380(%rbp), %rdx
movq -0x388(%rbp), %rsi
leaq -0x300(%rbp), %rdi
leaq -0x320(%rbp), %rcx
callq 0xcec0
jmp 0xa833
movq -0x338(%rbp), %rdi
leaq -0x300(%rbp), %rsi
callq 0xce90
jmp 0xa848
leaq -0x300(%rbp), %rdi
callq 0xcf10
leaq -0x320(%rbp), %rdi
callq 0x45d8
leaq -0x2c8(%rbp), %rdi
callq 0x45d8
jmp 0xa652
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x2c9(%rbp), %rdi
callq 0x4340
jmp 0xa915
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xa8ce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xa8c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x300(%rbp), %rdi
callq 0xcf10
leaq -0x320(%rbp), %rdi
callq 0x45d8
leaq -0x2c8(%rbp), %rdi
callq 0x45d8
jmp 0xa915
leaq -0x258(%rbp), %rdi
callq 0x4070
jmp 0xa8ea
movb $0x1, -0x19(%rbp)
movl $0x1, -0x25c(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x40c0
leaq -0x40(%rbp), %rdi
callq 0x45d8
testb $0x1, -0x19(%rbp)
jne 0xa938
jmp 0xa92c
leaq -0x258(%rbp), %rdi
callq 0x40c0
leaq -0x40(%rbp), %rdi
callq 0x45d8
jmp 0xa948
movq -0x338(%rbp), %rdi
callq 0xcf30
movq -0x330(%rbp), %rax
addq $0x390, %rsp            # imm = 0x390
popq %rbp
retq
movq -0x338(%rbp), %rdi
callq 0xcf30
movq -0x48(%rbp), %rdi
callq 0x4530
nopl (%rax)