// File: xx01cyx[P]LSM-Tree/kvstore.cc
// Function: KVStore::merge0AndWriteToDisk(std::vector<std::shared_ptr<SSTable>, std::allocator<std::shared_ptr<SSTable>>> const&, unsigned long, std::unordered_map<unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<unsigned long>, std::equal_to<unsigned long>, std::allocator<std::pair<unsigned long const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>> const&)
vector<SSTPtr> KVStore::merge0AndWriteToDisk(const vector<SSTPtr> &SSTs, TimeStamp maxTimeStamp, const KVPair& data) {

    vector<SSTPtr> newSSTs;
    priority_queue<KeyRef, vector<KeyRef>, KeyRefGreaterThan> pq;
    vector<LsmKey> sortedKeys;
    KeyRef currentRef;
    size_t currentSize = HEADER_SIZE + BLOOM_FILTER_SIZE;

    for (const auto & SST : SSTs)
        pq.push(make_pair(SST, 0));

    while (!pq.empty()) {
        currentRef = pq.top();
        SSTPtr currentSST = currentRef.first;
        size_t currentIndex = currentRef.second;
        vector<DataIndex> currentDataIndexes = currentSST->getDataIndexes();
        LsmKey currentKey = currentDataIndexes[currentIndex].key;
        LsmValue currentValue = data.at(currentKey);

        if (sortedKeys.empty())
            sortedKeys.push_back(currentKey);

        if ((ssTables.size() != 2 || currentValue != DELETE_SIGN)
            && !sortedKeys.empty() && sortedKeys.back() != currentKey) {
            size_t sizeIncrement = DATA_INDEX_SIZE + currentValue.size();
            if (currentSize + sizeIncrement > MAX_SSTABLE_SIZE) {
                newSSTs.push_back(generateNewSST(sortedKeys, data, 1, maxTimeStamp));
                sortedKeys.clear();
                currentSize = HEADER_SIZE + BLOOM_FILTER_SIZE;
            }
            sortedKeys.push_back(currentKey);
            currentSize += sizeIncrement;
        }

        pq.pop();
        if (currentIndex != currentDataIndexes.size() - 1)
            pq.push(make_pair(currentSST, currentIndex + 1));
    }

    // Pack the remaining data into an SST.
    if (!sortedKeys.empty())
        newSSTs.push_back(generateNewSST(sortedKeys, data, 1, maxTimeStamp));

    return newSSTs;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x1a8(%rbp)
movq %rdi, %rax
movq %rax, -0x1a0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x198(%rbp)
movb $0x0, -0x29(%rbp)
callq 0x15ef0
leaq -0x50(%rbp), %rdi
callq 0x16970
jmp 0x128cf
leaq -0x78(%rbp), %rdi
callq 0x6410
leaq -0x90(%rbp), %rdi
callq 0x169a0
jmp 0x128e6
movq $0x2820, -0x98(%rbp) # imm = 0x2820
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x169d0
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x16a00
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x16a30
testb $0x1, %al
jne 0x1293e
jmp 0x12a31
leaq -0xa8(%rbp), %rdi
callq 0x16a70
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rsi
movl $0x0, -0xec(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xec(%rbp), %rdx
callq 0x16ae0
jmp 0x12977
leaq -0xd0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x16b20
jmp 0x1298c
leaq -0x50(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x16a90
jmp 0x1299e
leaq -0xd0(%rbp), %rdi
callq 0x16b60
leaq -0xe8(%rbp), %rdi
callq 0x16b80
leaq -0xa8(%rbp), %rdi
callq 0x16ba0
jmp 0x12922
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x12e50
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x12e20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x12e14
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x12a20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x16b60
leaq -0xe8(%rbp), %rdi
callq 0x16b80
jmp 0x12e14
jmp 0x12a33
leaq -0x50(%rbp), %rdi
callq 0x16bc0
movb %al, -0x1a9(%rbp)
jmp 0x12a44
movb -0x1a9(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x12a55
jmp 0x12d81
leaq -0x50(%rbp), %rdi
callq 0x16be0
movq %rax, -0x1b8(%rbp)
jmp 0x12a67
movq -0x1b8(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x16c00
jmp 0x12a7c
leaq -0x100(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
leaq -0x90(%rbp), %rsi
callq 0x16190
movq -0x1c0(%rbp), %rdi
movq -0x80(%rbp), %rax
movq %rax, -0x108(%rbp)
callq 0xd330
movq %rax, %rsi
leaq -0x120(%rbp), %rdi
callq 0x9940
jmp 0x12abe
movq -0x108(%rbp), %rsi
leaq -0x120(%rbp), %rdi
callq 0x9b90
movq (%rax), %rax
movq %rax, -0x128(%rbp)
movq -0x28(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x16c40
movq %rax, -0x1c8(%rbp)
jmp 0x12af4
movq -0x1c8(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x4120
jmp 0x12b09
leaq -0x78(%rbp), %rdi
callq 0x16c70
testb $0x1, %al
jne 0x12b18
jmp 0x12b5f
leaq -0x78(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x6430
jmp 0x12b2a
jmp 0x12b5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x12d70
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x12d64
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x12d58
movq -0x198(%rbp), %rdi
addq $0x20, %rdi
callq 0x16520
cmpq $0x2, %rax
jne 0x12b9f
leaq 0x1071e(%rip), %rsi     # 0x2329a
leaq -0x148(%rbp), %rdi
callq 0x15e80
movb %al, -0x1c9(%rbp)
jmp 0x12b90
movb -0x1c9(%rbp), %al
testb $0x1, %al
jne 0x12b9f
jmp 0x12c90
leaq -0x78(%rbp), %rdi
callq 0x16c70
testb $0x1, %al
jne 0x12c90
leaq -0x78(%rbp), %rdi
callq 0x16cb0
movq (%rax), %rax
cmpq -0x128(%rbp), %rax
je 0x12c90
leaq -0x148(%rbp), %rdi
callq 0x41e0
addq $0xc, %rax
movq %rax, -0x150(%rbp)
movq -0x98(%rbp), %rax
addq -0x150(%rbp), %rax
cmpq $0x200000, %rax         # imm = 0x200000
jbe 0x12c69
movq -0x28(%rbp), %rdx
movq -0x20(%rbp), %r8
leaq -0x160(%rbp), %rdi
leaq -0x78(%rbp), %rsi
movl $0x1, %ecx
callq 0x13980
jmp 0x12c15
movq -0x1a8(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x168d0
jmp 0x12c2a
leaq -0x160(%rbp), %rdi
callq 0xd350
leaq -0x78(%rbp), %rdi
callq 0x16cf0
movq $0x2820, -0x98(%rbp) # imm = 0x2820
jmp 0x12c69
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x160(%rbp), %rdi
callq 0xd350
jmp 0x12d58
leaq -0x78(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x6430
jmp 0x12c7b
movq -0x150(%rbp), %rax
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x16d10
jmp 0x12c9b
movq -0x108(%rbp), %rax
movq %rax, -0x1d8(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x9bf0
movq %rax, %rcx
movq -0x1d8(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
je 0x12d2f
movq -0x108(%rbp), %rax
incq %rax
movq %rax, -0x180(%rbp)
leaq -0x178(%rbp), %rdi
leaq -0x100(%rbp), %rsi
leaq -0x180(%rbp), %rdx
callq 0x16d60
jmp 0x12cf5
leaq -0x50(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x16a90
jmp 0x12d07
leaq -0x178(%rbp), %rdi
callq 0x16b60
jmp 0x12d2f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x16b60
jmp 0x12d58
leaq -0x148(%rbp), %rdi
callq 0x4608
leaq -0x120(%rbp), %rdi
callq 0x9b30
leaq -0x100(%rbp), %rdi
callq 0xd350
jmp 0x12a33
leaq -0x148(%rbp), %rdi
callq 0x4608
leaq -0x120(%rbp), %rdi
callq 0x9b30
leaq -0x100(%rbp), %rdi
callq 0xd350
jmp 0x12e14
leaq -0x78(%rbp), %rdi
callq 0x16c70
testb $0x1, %al
jne 0x12dea
movq -0x28(%rbp), %rdx
movq -0x20(%rbp), %r8
leaq -0x190(%rbp), %rdi
leaq -0x78(%rbp), %rsi
movl $0x1, %ecx
callq 0x13980
jmp 0x12dad
movq -0x1a8(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x168d0
jmp 0x12dc2
leaq -0x190(%rbp), %rdi
callq 0xd350
jmp 0x12dea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x190(%rbp), %rdi
callq 0xd350
jmp 0x12e14
movb $0x1, -0x29(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x16b60
leaq -0x78(%rbp), %rdi
callq 0x69d0
leaq -0x50(%rbp), %rdi
callq 0x16da0
testb $0x1, -0x29(%rbp)
jne 0x12e40
jmp 0x12e34
leaq -0x90(%rbp), %rdi
callq 0x16b60
leaq -0x78(%rbp), %rdi
callq 0x69d0
leaq -0x50(%rbp), %rdi
callq 0x16da0
jmp 0x12e50
movq -0x1a8(%rbp), %rdi
callq 0x163c0
movq -0x1a0(%rbp), %rax
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
movq -0x1a8(%rbp), %rdi
callq 0x163c0
movq -0x58(%rbp), %rdi
callq 0x4560
nopw %cs:(%rax,%rax)