// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r8, 0x30(%rsp)
movq %rcx, 0x58(%rsp)
movq %rsi, 0xb0(%rsp)
xorps %xmm0, %xmm0
andq $0x0, 0x10(%rdi)
leaq 0xd0(%rsp), %rax
andq $0x0, 0x10(%rax)
movq %rdi, 0x20(%rsp)
movups %xmm0, (%rdi)
andq $0x0, 0x10(%rsp)
movq %rax, %r14
movaps %xmm0, (%rax)
andq $0x0, 0xa0(%rsp)
movaps %xmm0, (%rsp)
movaps %xmm0, 0x90(%rsp)
movq (%rdx), %rbp
movq 0x8(%rdx), %rbx
leaq 0x68(%rsp), %r12
leaq 0x40(%rsp), %r13
leaq 0x60(%rsp), %r15
cmpq %rbx, %rbp
je 0x8ae7
movq %r13, %rdi
movq %rbp, %rsi
callq 0xaa4e
andl $0x0, 0x50(%rsp)
movaps 0x40(%rsp), %xmm0
movaps %xmm0, 0x60(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x70(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa630
movq %r12, %rdi
callq 0x6932
leaq 0x48(%rsp), %rdi
callq 0x6932
addq $0x10, %rbp
jmp 0x8a97
movl $0x2820, %eax           # imm = 0x2820
movq %rax, 0x38(%rsp)
leaq 0x40(%rsp), %rbp
leaq 0x60(%rsp), %rbx
leaq 0xb8(%rsp), %r14
movq 0x30(%rsp), %r12
movq 0xd0(%rsp), %rsi
cmpq 0xd8(%rsp), %rsi
je 0x8cdf
leaq 0x90(%rsp), %r15
movq %r15, %rdi
callq 0xa646
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
callq 0xaa4e
movq 0xa0(%rsp), %r13
movq 0x80(%rsp), %rsi
movq %rbp, %rdi
callq 0x580a
movq 0x40(%rsp), %rax
movq %r13, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rax
movq %rax, 0x28(%rsp)
movq %r12, %rdi
leaq 0x28(%rsp), %rsi
callq 0xcf9a
movq %rbx, %rdi
movq %rax, %rsi
callq 0x32f0
movq (%rsp), %rax
cmpq 0x8(%rsp), %rax
jne 0x8b9b
movq %rsp, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4584
movq 0xb0(%rsp), %rax
cmpq $0x2, 0x38(%rax)
jne 0x8bc1
movq %rbx, %rdi
leaq 0x56e6(%rip), %rsi      # 0xe29a
callq 0xa465
testb %al, %al
je 0x8c62
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
je 0x8c62
movq -0x8(%rax), %rax
cmpq 0x28(%rsp), %rax
je 0x8c62
movq 0x68(%rsp), %r12
movq 0x38(%rsp), %rax
addq %r12, %rax
addq $0xc, %rax
cmpq $0x200001, %rax         # imm = 0x200001
jb 0x8c47
movq %r14, %r15
movq %r14, %rdi
movq %rsp, %rsi
movq 0x30(%rsp), %rdx
pushq $0x1
popq %rcx
movq 0x58(%rsp), %r8
callq 0x944a
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0xc2e2
leaq 0xc0(%rsp), %rdi
callq 0x6932
movq (%rsp), %rax
movl $0x2820, %ecx           # imm = 0x2820
movq %rcx, 0x38(%rsp)
cmpq %rax, 0x8(%rsp)
je 0x8c47
movq %rax, 0x8(%rsp)
movq %rsp, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4584
addq $0xc, %r12
addq %r12, 0x38(%rsp)
movq 0x30(%rsp), %r12
leaq 0xd0(%rsp), %rdi
callq 0xa668
movq 0x48(%rsp), %rax
subq 0x40(%rsp), %rax
sarq $0x4, %rax
decq %rax
cmpq %rax, %r13
je 0x8cbd
incq %r13
movq %r14, %rdi
leaq 0x80(%rsp), %rsi
callq 0xaa4e
movq %r13, 0xc8(%rsp)
leaq 0xd0(%rsp), %rdi
movq %r14, %rsi
callq 0xa630
leaq 0xc0(%rsp), %rdi
callq 0x6932
movq %rbx, %rdi
callq 0x34a8
movq %rbp, %rdi
callq 0x5a44
leaq 0x88(%rsp), %rdi
callq 0x6932
jmp 0x8b08
movq (%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0x8d1b
leaq 0x60(%rsp), %rdi
movq %rsp, %rsi
pushq $0x1
popq %rcx
movq %r12, %rdx
movq 0x58(%rsp), %r8
callq 0x944a
leaq 0x60(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0xc2e2
leaq 0x68(%rsp), %rdi
callq 0x6932
leaq 0x98(%rsp), %rdi
callq 0x6932
movq %rsp, %rdi
callq 0x4be4
leaq 0xd0(%rsp), %rdi
callq 0xab00
movq 0x20(%rsp), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
jmp 0x8db6
movq %rax, %rbx
jmp 0x8dbb
jmp 0x8d92
jmp 0x8d69
jmp 0x8d92
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x6932
jmp 0x8d95
movq %rax, %rbx
jmp 0x8dae
movq %rax, %rbx
movq %r12, %rdi
callq 0x6932
leaq 0x48(%rsp), %rdi
jmp 0x8db6
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x34a8
jmp 0x8da4
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x5a44
leaq 0x88(%rsp), %rdi
callq 0x6932
leaq 0x98(%rsp), %rdi
callq 0x6932
movq %rsp, %rdi
callq 0x4be4
leaq 0xd0(%rsp), %rdi
callq 0xab00
movq 0x20(%rsp), %rdi
callq 0xa552
movq %rbx, %rdi
callq 0x3440
nop