// File: /workspace/github_cpp_makefile_star2_O2/xx01cyx[P]LSM-Tree/kvstore.cc
// Function: KVStore::mergeAndWriteToDisk(std::shared_ptr<SSTable> const&, 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::mergeAndWriteToDisk(const SSTPtr& upperLevelSST, const vector<SSTPtr>& lowerLevelSSTs,
                                            TimeStamp maxTimeStamp, const KVPair& data) {

    // Initialize.

    vector<SSTPtr> newSSTs;
    vector<LsmKey> sortedKeys;
    size_t currentSize = HEADER_SIZE + BLOOM_FILTER_SIZE;
    size_t upperKeyNumber = upperLevelSST->getKeyNumber();
    size_t upperIndex = 0;
    size_t lowerKeyNumber;
    size_t lowerIndex;
    size_t lowerLevel = upperLevelSST->getLevel() + 1;
    KeyRefLessThan keyRefLessThan;

    // Function for merging and writing data to the disk. Used in 2-way merge below.

    auto merge = [&](const SSTPtr& sst, size_t& index) {
        LsmKey key = sst->getDataIndexes()[index].key;
        const LsmValue& value = data.at(key);
        if ((lowerLevel != ssTables.size() || value != DELETE_SIGN)
            && !sortedKeys.empty() && sortedKeys.back() != key) {
            size_t sizeIncrement = DATA_INDEX_SIZE + value.size();
            if (currentSize + sizeIncrement > MAX_SSTABLE_SIZE) {
                newSSTs.push_back(generateNewSST(sortedKeys, data, lowerLevel, maxTimeStamp));
                sortedKeys.clear();
                currentSize = HEADER_SIZE + BLOOM_FILTER_SIZE;
            }
            sortedKeys.push_back(key);
            currentSize += sizeIncrement;
        }
        index++;
    };

    // 2-way merge sort and write data to the disk.

    for (const auto& lowerLevelSST : lowerLevelSSTs) {

        lowerIndex = 0;
        lowerKeyNumber = lowerLevelSST->getKeyNumber();

        if (sortedKeys.empty()) {
            LsmKey upperKey = upperLevelSST->getDataIndexes()[0].key;
            LsmKey lowerKey = lowerLevelSST->getDataIndexes()[0].key;
            if ((upperKey < lowerKey) ||
                (upperKey == lowerKey && upperLevelSST->getTimeStamp() < lowerLevelSST->getTimeStamp()))
                sortedKeys.push_back(upperKey);
            else
                sortedKeys.push_back(lowerKey);
        }

        while (upperIndex != upperKeyNumber && lowerIndex != lowerKeyNumber) {
            KeyRef upperKeyRef = make_pair(upperLevelSST, upperIndex);
            KeyRef lowerKeyRef = make_pair(lowerLevelSST, lowerIndex);

            if (keyRefLessThan(upperKeyRef, lowerKeyRef))
                merge(upperLevelSST, upperIndex);
            else
                merge(lowerLevelSST, lowerIndex);
        }

        while (lowerIndex != lowerKeyNumber)
            merge(lowerLevelSST, lowerIndex);
    }

    while (upperIndex != upperKeyNumber)
        merge(upperLevelSST, upperIndex);

    // Pack the remaining data into an SST.

    if (!sortedKeys.empty())
        newSSTs.push_back(generateNewSST(sortedKeys, data, lowerLevel, maxTimeStamp));

    return newSSTs;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r9, %r12
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r14
movq %r8, 0x70(%rsp)
andq $0x0, 0x10(%rdi)
xorps %xmm0, %xmm0
andq $0x0, 0x40(%rsp)
movq %rdi, 0x18(%rsp)
movups %xmm0, (%rdi)
movaps %xmm0, 0x30(%rsp)
movq $0x2820, 0xa8(%rsp) # imm = 0x2820
movq (%rdx), %rdi
callq 0x5804
movq %rax, 0x60(%rsp)
andq $0x0, 0x28(%rsp)
movq %r15, 0x10(%rsp)
movq (%r15), %rdi
callq 0x57ee
incq %rax
leaq 0xa0(%rsp), %rcx
movq %rax, (%rcx)
leaq 0xb0(%rsp), %rdx
movq %r12, 0x90(%rsp)
movq %r12, (%rdx)
movq %rcx, 0x8(%rdx)
movq %r14, 0x10(%rdx)
leaq 0x30(%rsp), %rax
movq %rax, 0x18(%rdx)
leaq 0xa8(%rsp), %rax
movq %rax, 0x20(%rdx)
movq 0x18(%rsp), %rax
movq %rax, 0x28(%rdx)
leaq 0x70(%rsp), %rax
movq %rax, 0x30(%rdx)
movq (%rbx), %rbp
movq 0x8(%rbx), %rax
movq %rax, 0x98(%rsp)
xorl %r13d, %r13d
cmpq 0x98(%rsp), %rbp
je 0x9c3c
andq $0x0, 0x20(%rsp)
movq (%rbp), %rdi
callq 0x5804
movq %rax, %r12
movq 0x30(%rsp), %rax
cmpq 0x38(%rsp), %rax
jne 0x9b6a
movq 0x10(%rsp), %rax
movq (%rax), %rsi
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x580a
movq 0x48(%rsp), %rax
movq (%rax), %rbx
movq %r14, %rdi
callq 0x5a44
movq %rbx, 0x78(%rsp)
movq (%rbp), %rsi
movq %r14, %rdi
callq 0x580a
movq 0x48(%rsp), %rax
movq (%rax), %rbx
movq %r14, %rdi
callq 0x5a44
movq %rbx, 0x68(%rsp)
cmpq %rbx, 0x78(%rsp)
jae 0x9b36
leaq 0x78(%rsp), %rsi
jmp 0x9b60
leaq 0x68(%rsp), %rsi
jne 0x9b60
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x57f2
movq %rax, %rbx
movq (%rbp), %rdi
callq 0x57f2
leaq 0x68(%rsp), %rsi
cmpq %rax, %rbx
jb 0x9b2f
leaq 0x30(%rsp), %rdi
callq 0x4584
xorl %ebx, %ebx
cmpq 0x60(%rsp), %r13
je 0x9c2c
cmpq %r12, %rbx
je 0x9c2c
leaq 0x48(%rsp), %r15
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0xaa4e
movq %r13, 0x58(%rsp)
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0xaa4e
movq %rbx, 0x88(%rsp)
leaq 0xf(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0xa682
testb %al, %al
je 0x9be1
leaq 0xb0(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x9d28
movq 0x28(%rsp), %r13
jmp 0x9bfb
leaq 0xb0(%rsp), %rdi
movq %rbp, %rsi
leaq 0x20(%rsp), %rdx
callq 0x9d28
movq 0x20(%rsp), %rbx
leaq 0x80(%rsp), %rdi
callq 0x6932
leaq 0x50(%rsp), %rdi
callq 0x6932
jmp 0x9b6c
leaq 0xb0(%rsp), %rdi
movq %rbp, %rsi
leaq 0x20(%rsp), %rdx
callq 0x9d28
cmpq %r12, 0x20(%rsp)
jne 0x9c17
addq $0x10, %rbp
jmp 0x9aad
leaq 0xb0(%rsp), %rbx
leaq 0x28(%rsp), %r14
movq 0x90(%rsp), %r12
movq 0x10(%rsp), %r15
movq 0x60(%rsp), %rax
cmpq %rax, 0x28(%rsp)
je 0x9c72
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x9d28
jmp 0x9c56
movq 0x30(%rsp), %rax
cmpq 0x38(%rsp), %rax
je 0x9cb6
movq 0xa0(%rsp), %rcx
movq 0x70(%rsp), %r8
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq %r12, %rdx
callq 0x944a
leaq 0x48(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0xc2e2
leaq 0x50(%rsp), %rdi
callq 0x6932
leaq 0x30(%rsp), %rdi
callq 0x4be4
movq 0x18(%rsp), %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x9d01
jmp 0x9cec
jmp 0x9cec
jmp 0x9cec
jmp 0x9cec
jmp 0x9cec
jmp 0x9cec
jmp 0x9cec
jmp 0x9cec
movq %rax, %rbx
jmp 0x9d0b
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x6932
leaq 0x50(%rsp), %rdi
callq 0x6932
leaq 0x30(%rsp), %rdi
callq 0x4be4
movq 0x18(%rsp), %rdi
callq 0xa552
movq %rbx, %rdi
callq 0x3440
nop