// File: /workspace/github_cpp_makefile_star2_O1/nicluo[P]sorting-numbers/merge.cpp
// Function: merge(std::vector<Seq*, std::allocator<Seq*>>&, std::basic_ofstream<char, std::char_traits<char>>&)
void merge(std::vector<Seq*>& sequences, std::ofstream& output) {
    /* Greater<Seq*> comparator for Seq* to create min heap */
    auto comp = []( const Seq* lhs, const Seq* rhs ) { return lhs->value > rhs->value; };
    std::priority_queue<Seq*, std::vector<Seq*>, decltype(comp)> pq(sequences.begin(), sequences.end(), comp);
    
    while(!pq.empty()) {
        /* Get and pop minimum element */
        Seq* top = pq.top();
        pq.pop();
        
        output << top->value << '\n';
        
        /* Since the minimum element is a sequence, if it has a next value,
           we can re-insert into the heap for the next iteration */
        if(next(top)) {
            pq.push(top);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq (%rdi), %rdx
movq 0x8(%rdi), %rcx
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
xorl %esi, %esi
callq 0x1812
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
subq %r14, %r15
sarq $0x3, %r15
cmpq $0x2, %r15
jl 0x1430
leaq -0x2(%r15), %rax
shrq $0x3f, %rax
leaq (%r15,%rax), %r12
addq $-0x2, %r12
sarq %r12
xorl %r13d, %r13d
movq %r12, %rbp
movq (%r14,%r12,8), %rcx
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1760
subq $0x1, %rbp
cmovbq %r13, %rbp
testq %r12, %r12
movq %rbp, %r12
jne 0x140e
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x155e
leaq 0x7(%rsp), %r14
leaq 0x10(%rsp), %r15
leaq 0x8(%rsp), %r12
movq (%rdi), %rcx
movq %rcx, 0x8(%rsp)
movq %rax, %rcx
subq %rdi, %rcx
cmpq $0x9, %rcx
jl 0x1483
leaq -0x8(%rax), %rdx
movq -0x8(%rax), %rcx
movq (%rdi), %rsi
movq %rsi, -0x8(%rax)
subq %rdi, %rdx
sarq $0x3, %rdx
xorl %esi, %esi
callq 0x1760
addq $-0x8, 0x18(%rsp)
movq 0x8(%rsp), %rax
movl (%rax), %esi
movq %rbx, %rdi
callq 0x1180
movb $0xa, 0x7(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x1120
movq 0x8(%rsp), %rsi
movq 0x8(%rsi), %rax
movq -0x18(%rax), %rax
movl 0x28(%rsi,%rax), %ebp
testl %ebp, %ebp
jne 0x154b
leaq 0x8(%rsi), %rdi
callq 0x1090
testl %ebp, %ebp
jne 0x154b
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x14ef
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x14fa
movq %r15, %rdi
movq %r12, %rdx
callq 0x1a66
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rsi
movq -0x8(%rsi), %rcx
subq %rax, %rsi
sarq $0x3, %rsi
leaq -0x1(%rsi), %rdx
cmpq $0x2, %rsi
jl 0x1544
movl (%rcx), %esi
leaq -0x1(%rdx), %rdi
shrq $0x3f, %rdi
addq %rdx, %rdi
decq %rdi
sarq %rdi
movq (%rax,%rdi,8), %r8
cmpl %esi, (%r8)
jle 0x1544
movq %r8, (%rax,%rdx,8)
cmpq $0x2, %rdx
movq %rdi, %rdx
jg 0x151b
jmp 0x1547
movq %rdx, %rdi
movq %rcx, (%rax,%rdi,8)
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
cmpq %rax, %rdi
jne 0x1452
testq %rdi, %rdi
je 0x1570
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x1100
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1581
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x159b
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x1100
movq %rbx, %rdi
callq 0x1190