// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/extended/merge_stream.c
// Function: merge_stream_next_in_order
static int merge_stream_next_in_order(GtNodeStream *ns, GtGenomeNode **gn,
                                      GtError *err)
{
  GtMergeStream *ms;
  GtGenomeNode *min_node = NULL;
  GtUword i;
  int had_err = 0;
  gt_error_check(err);
  ms = gt_merge_stream_cast(ns);

  /* initially fill the queue with first nodes from input streams */
  if (!ms->filled) {
    for (i = 0; !had_err && i < gt_array_size(ms->node_streams); i++) {
      GtGenomeNode *firstnode = NULL;
      ms->items[i].input_index = i;
      had_err = gt_node_stream_next(*(GtNodeStream**)
                                      gt_array_get(ms->node_streams, i),
                                    &firstnode, err);
      /* only add start of non-empty input streams */
      if (!had_err && firstnode) {
        if (!gt_eof_node_try_cast(firstnode)) {
          ms->items[i].gn = firstnode;
          ms->items[i].input_index = i;
          gt_priority_queue_add(ms->pq, ms->items+i);
        } else {
          gt_genome_node_delete(firstnode);
        }
      }
    }
    ms->filled = true;
  }

  /* stream the queue contents */
  if (!had_err && !gt_priority_queue_is_empty(ms->pq)) {
    GtMergeStreamItem *min_item = gt_priority_queue_extract_min(ms->pq);
    GtGenomeNode *nextnode = NULL;
    gt_assert(min_item && min_item->gn);
    min_node = min_item->gn;
    /* get next element from the last stream queried */
    had_err = gt_node_stream_next(*(GtNodeStream**)
                                    gt_array_get(ms->node_streams,
                                                 min_item->input_index),
                                  &nextnode, err);
    /* add node to queue if still non-EOF nodes left in that stream */
    if (!had_err && nextnode) {
      if (!gt_eof_node_try_cast(nextnode)) {
        min_item->gn = nextnode;
        gt_priority_queue_add(ms->pq, min_item);
      } else {
        min_item->gn = NULL;
        gt_genome_node_delete(nextnode);
      }
    }
  }

  *gn = min_node;
  return had_err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r14
testq %rdx, %rdx
je 0x161818
movq %r15, %rdi
callq 0xfcc32
testb %al, %al
jne 0x1619c3
callq 0x161494
movq %rax, %rdi
movq %r14, %rsi
callq 0x162ff7
movq %rax, %r14
cmpb $0x0, 0x38(%rax)
jne 0x1618f1
movq %r13, 0x10(%rsp)
xorl %ebx, %ebx
leaq 0x8(%rsp), %r13
xorl %ebp, %ebp
movq 0x10(%r14), %rdi
callq 0xd24e0
cmpq %rax, %rbp
jae 0x1618e7
movq $0x0, 0x8(%rsp)
movq 0x28(%r14), %rax
movq %rbp, 0x8(%rax,%rbx)
movq 0x10(%r14), %rdi
movq %rbp, %rsi
callq 0xd2207
movq (%rax), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x162e68
movl %eax, %r12d
testl %eax, %eax
sete %al
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1618cb
callq 0x2fda49
movq 0x8(%rsp), %rdi
testq %rax, %rax
je 0x1618b1
callq 0x1451a0
jmp 0x1618cb
movq 0x28(%r14), %rax
leaq (%rax,%rbx), %rsi
movq %rdi, (%rax,%rbx)
movq %rbp, 0x8(%rax,%rbx)
movq 0x30(%r14), %rdi
callq 0x164ea9
incq %rbp
addq $0x10, %rbx
testl %r12d, %r12d
je 0x161843
movb $0x1, 0x38(%r14)
xorl %ebx, %ebx
jmp 0x1619a3
movb $0x1, 0x38(%r14)
movq 0x10(%rsp), %r13
movq 0x30(%r14), %rdi
callq 0x164e7e
testb %al, %al
je 0x161908
xorl %r12d, %r12d
xorl %ebx, %ebx
jmp 0x1619a8
movq %r13, 0x10(%rsp)
movq 0x30(%r14), %rdi
callq 0x164f95
movq $0x0, 0x8(%rsp)
testq %rax, %rax
je 0x1619be
movq %rax, %r13
movq (%rax), %rbx
testq %rbx, %rbx
je 0x1619be
movq 0x10(%r14), %rdi
movq 0x8(%r13), %rsi
callq 0xd2207
movq (%rax), %rdi
leaq 0x8(%rsp), %rbp
movq %rbp, %rsi
movq %r15, %rdx
callq 0x162e68
movl %eax, %r12d
testl %eax, %eax
sete %al
movq (%rbp), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1619a3
callq 0x2fda49
testq %rax, %rax
je 0x16198e
movq $0x0, (%r13)
movq 0x8(%rsp), %rdi
callq 0x1451a0
jmp 0x1619a3
movq 0x8(%rsp), %rax
movq %rax, (%r13)
movq 0x30(%r14), %rdi
movq %r13, %rsi
callq 0x164ea9
movq 0x10(%rsp), %r13
movq %rbx, (%r13)
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4d524
callq 0x4d4ed