// File: xx01cyx[P]LSM-Tree/kvstore.cc
// Function: KVStore::getValueFromDisk[abi:cxx11](unsigned long)
LsmValue KVStore::getValueFromDisk(LsmKey key) {

    size_t levelNumber = ssTables.size();
    if (levelNumber == 0)   // All data are stored in memTable.
        return "";

    TimeStamp maxTimeStamp = 0;
    LsmValue newestValue;

    auto getNewestValue = [&](const SSTPtr& sst, LsmKey key) {
        LsmValue newValue = sst->get(key);
        TimeStamp newTimeStamp = sst->getTimeStamp();
        if (newValue.length() != 0 && newTimeStamp > maxTimeStamp) {
            newestValue = newValue;
            maxTimeStamp = newTimeStamp;
        }
    };

    // Read from L0.
    vector<SSTPtr> L0SSTs = *ssTables[0];
    uint32_t L0SSTNumber = L0SSTs.size();
    for (int i = L0SSTNumber - 1; i >= 0; --i) {
        SSTPtr sst = L0SSTs[i];
        getNewestValue(sst, key);

        if (newestValue.length() != 0)
            return newestValue;
    }

    // Read from the rest levels.
    for (size_t n = 1; n < levelNumber; n++) {
        vector<SSTPtr> levelSSTs = *ssTables[n];
        if (key < levelSSTs.front()->getMinKey() || key > levelSSTs.back()->getMaxKey())
            continue;
        uint32_t sstIndex = sstBinarySearch(levelSSTs, key, 0, levelSSTs.size() - 1);
        SSTPtr targetSST = levelSSTs[sstIndex];
        getNewestValue(targetSST, key);

        if (newestValue.length() != 0)
            return newestValue;
    }

    return newestValue;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0xe8(%rbp)
movq %rdi, %rax
movq %rax, -0xe0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
addq $0x20, %rdi
callq 0x16520
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x10dfb
leaq -0x21(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x4570
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rdx
leaq 0x12242(%rip), %rsi     # 0x2300e
callq 0x4b40
jmp 0x10dd3
leaq -0x21(%rbp), %rdi
callq 0x4320
jmp 0x111c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x21(%rbp), %rdi
callq 0x4320
jmp 0x111d8
movq -0xe8(%rbp), %rdi
movq $0x0, -0x40(%rbp)
movb $0x0, -0x41(%rbp)
callq 0x4410
movq -0xe8(%rbp), %rax
movq -0xd8(%rbp), %rdi
leaq -0x40(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movq %rax, -0x50(%rbp)
addq $0x20, %rdi
movq $0x0, -0x78(%rbp)
leaq -0x78(%rbp), %rsi
callq 0x15de0
movq %rax, -0xf8(%rbp)
jmp 0x10e4b
movq -0xf8(%rbp), %rdi
callq 0x16540
movq %rax, %rsi
leaq -0x70(%rbp), %rdi
callq 0x16570
jmp 0x10e65
leaq -0x70(%rbp), %rdi
callq 0x16120
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x80(%rbp)
cmpl $0x0, -0x80(%rbp)
jl 0x10f43
movslq -0x80(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x16170
movq %rax, %rsi
leaq -0x90(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x16190
movq -0x100(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x58(%rbp), %rdi
callq 0x11fe0
jmp 0x10ebd
movq -0xe8(%rbp), %rdi
callq 0x4480
cmpq $0x0, %rax
je 0x10f0d
movb $0x1, -0x41(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0x10f17
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x111ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xd350
jmp 0x111a1
movl $0x0, -0x94(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xd350
movl -0x94(%rbp), %eax
testl %eax, %eax
jne 0x11190
jmp 0x10f33
jmp 0x10f35
movl -0x80(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x10e7a
movq $0x1, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x11182
movq -0xd8(%rbp), %rdi
addq $0x20, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x16370
movq %rax, -0x108(%rbp)
jmp 0x10f7f
movq -0x108(%rbp), %rdi
callq 0x16540
movq %rax, %rsi
leaq -0xb8(%rbp), %rdi
callq 0x16570
jmp 0x10f9c
movq -0x18(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x16140
movq %rax, %rdi
callq 0xd330
movq %rax, %rdi
callq 0x98e0
movq %rax, -0x110(%rbp)
jmp 0x10fcc
movq -0x118(%rbp), %rax
movq -0x110(%rbp), %rcx
cmpq %rcx, %rax
jb 0x11022
movq -0x18(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x16700
movq %rax, %rdi
callq 0xd330
movq %rax, %rdi
callq 0x9900
movq %rax, -0x120(%rbp)
jmp 0x1100f
movq -0x128(%rbp), %rax
movq -0x120(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x11053
movl $0x7, -0x94(%rbp)
jmp 0x11130
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x111a1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x11174
movq -0x18(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x16120
movq -0x140(%rbp), %rdi
movq -0x138(%rbp), %rsi
movl %eax, %ecx
decl %ecx
xorl %edx, %edx
callq 0x120b0
movl %eax, -0x12c(%rbp)
jmp 0x11092
movl -0x12c(%rbp), %eax
movl %eax, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, %esi
leaq -0xb8(%rbp), %rdi
callq 0x16170
movq %rax, %rsi
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x16190
movq -0x148(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x58(%rbp), %rdi
callq 0x11fe0
jmp 0x110de
movq -0xe8(%rbp), %rdi
callq 0x4480
cmpq $0x0, %rax
je 0x1111a
movb $0x1, -0x41(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0x11124
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0xd350
jmp 0x11174
movl $0x0, -0x94(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0xd350
leaq -0xb8(%rbp), %rdi
callq 0x163c0
movl -0x94(%rbp), %eax
movl %eax, -0x14c(%rbp)
testl %eax, %eax
je 0x1115b
jmp 0x1114e
movl -0x14c(%rbp), %eax
subl $0x7, %eax
je 0x1115d
jmp 0x11190
jmp 0x1115d
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0x10f4e
leaq -0xb8(%rbp), %rdi
callq 0x163c0
jmp 0x111a1
movb $0x1, -0x41(%rbp)
movl $0x1, -0x94(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x163c0
testb $0x1, -0x41(%rbp)
jne 0x111b8
jmp 0x111ac
leaq -0x70(%rbp), %rdi
callq 0x163c0
jmp 0x111ba
movq -0xe8(%rbp), %rdi
callq 0x4608
jmp 0x111c8
movq -0xe8(%rbp), %rdi
callq 0x4608
jmp 0x111d8
movq -0xe0(%rbp), %rax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x4560
nopw %cs:(%rax,%rax)