// File: /workspace/github_c_makefile_star2_O1/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, %r12
movq %rdi, %r14
testq %rdx, %rdx
je 0x16965f
movq %r15, %rdi
callq 0x101a9a
testb %al, %al
jne 0x169812
callq 0x1692c4
movq %rax, %rdi
movq %r14, %rsi
callq 0x16ae64
movq %rax, %r14
xorl %ebp, %ebp
cmpb $0x0, 0x38(%rax)
jne 0x16973f
movq %r12, 0x10(%rsp)
movq 0x10(%r14), %rdi
callq 0xd4a9a
testq %rax, %rax
je 0x169733
xorl %ebx, %ebx
leaq 0x8(%rsp), %r12
xorl %r13d, %r13d
movq $0x0, 0x8(%rsp)
movq 0x28(%r14), %rax
movq %r13, 0x8(%rax,%rbx)
movq 0x10(%r14), %rdi
movq %r13, %rsi
callq 0xd47ba
movq (%rax), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x16ace8
movl %eax, %ebp
testl %eax, %eax
sete %al
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x169714
callq 0x30774f
movq 0x8(%rsp), %rdi
testq %rax, %rax
je 0x1696fa
callq 0x14c43a
jmp 0x169714
movq 0x28(%r14), %rax
leaq (%rax,%rbx), %rsi
movq %rdi, (%rax,%rbx)
movq %r13, 0x8(%rax,%rbx)
movq 0x30(%r14), %rdi
callq 0x16cdd9
testl %ebp, %ebp
jne 0x169735
incq %r13
movq 0x10(%r14), %rdi
callq 0xd4a9a
addq $0x10, %rbx
cmpq %rax, %r13
jb 0x16969f
jmp 0x169735
xorl %ebp, %ebp
movb $0x1, 0x38(%r14)
movq 0x10(%rsp), %r12
xorl %ebx, %ebx
testl %ebp, %ebp
jne 0x1697fd
movq 0x30(%r14), %rdi
callq 0x16cdae
testb %al, %al
je 0x16975d
xorl %ebp, %ebp
jmp 0x1697fd
movq %r12, 0x10(%rsp)
movq 0x30(%r14), %rdi
callq 0x16ceb2
movq $0x0, 0x8(%rsp)
testq %rax, %rax
je 0x169817
movq %rax, %r12
movq (%rax), %rbx
testq %rbx, %rbx
je 0x169817
movq 0x10(%r14), %rdi
movq 0x8(%r12), %rsi
callq 0xd47ba
movq (%rax), %rdi
leaq 0x8(%rsp), %r13
movq %r13, %rsi
movq %r15, %rdx
callq 0x16ace8
movl %eax, %ebp
testl %eax, %eax
sete %al
movq (%r13), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1697f8
callq 0x30774f
testq %rax, %rax
je 0x1697e3
movq $0x0, (%r12)
movq 0x8(%rsp), %rdi
callq 0x14c43a
jmp 0x1697f8
movq 0x8(%rsp), %rax
movq %rax, (%r12)
movq 0x30(%r14), %rdi
movq %r12, %rsi
callq 0x16cdd9
movq 0x10(%rsp), %r12
movq %rbx, (%r12)
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4e3a9
callq 0x4e3e0