// File: /workspace/github_c_makefile_star2_O2/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 0x100f82
movq %r15, %rdi
callq 0x89eb7
testb %al, %al
jne 0x101151
callq 0x100a94
movq %rax, %rdi
movq %r14, %rsi
callq 0x102c81
movq %rax, %r14
cmpb $0x0, 0x38(%rax)
jne 0x101060
movq %r13, 0x10(%rsp)
xorl %ebx, %ebx
leaq 0x8(%rsp), %r13
xorl %ebp, %ebp
xorl %r12d, %r12d
testl %r12d, %r12d
jne 0x10104a
movq 0x10(%r14), %rdi
callq 0x586a4
cmpq %rax, %rbp
jae 0x101056
andq $0x0, 0x8(%rsp)
movq 0x28(%r14), %rax
movq %rbp, 0x8(%rax,%rbx)
movq 0x10(%r14), %rdi
movq %rbp, %rsi
callq 0x581aa
movq (%rax), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x102a22
movl %eax, %r12d
testl %eax, %eax
sete %al
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x10103e
callq 0x29a8bb
movq 0x8(%rsp), %rdi
testq %rax, %rax
je 0x101024
callq 0xde013
jmp 0x10103e
movq 0x28(%r14), %rax
leaq (%rax,%rbx), %rsi
movq %rdi, (%rax,%rbx)
movq %rbp, 0x8(%rax,%rbx)
movq 0x30(%r14), %rdi
callq 0x105467
incq %rbp
addq $0x10, %rbx
jmp 0x100fb0
movb $0x1, 0x38(%r14)
xorl %ebx, %ebx
jmp 0x10110c
movb $0x1, 0x38(%r14)
movq 0x10(%rsp), %r13
movq 0x30(%r14), %rdi
callq 0x1053da
testb %al, %al
je 0x101077
xorl %r12d, %r12d
xorl %ebx, %ebx
jmp 0x101111
movq %r13, 0x10(%rsp)
movq 0x30(%r14), %rdi
callq 0x105585
andq $0x0, 0x8(%rsp)
testq %rax, %rax
je 0x101127
movq %rax, %r13
movq (%rax), %rbx
testq %rbx, %rbx
je 0x101127
movq 0x10(%r14), %rdi
movq 0x8(%r13), %rsi
callq 0x581aa
movq (%rax), %rdi
leaq 0x8(%rsp), %rbp
movq %rbp, %rsi
movq %r15, %rdx
callq 0x102a22
movl %eax, %r12d
testl %eax, %eax
sete %al
movq (%rbp), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x10110c
callq 0x29a8bb
testq %rax, %rax
je 0x1010f7
andq $0x0, (%r13)
movq 0x8(%rsp), %rdi
callq 0xde013
jmp 0x10110c
movq 0x8(%rsp), %rax
movq %rax, (%r13)
movq 0x30(%r14), %rdi
movq %r13, %rsi
callq 0x105467
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
movq 0x336e3a(%rip), %rax    # 0x437f68
movq (%rax), %rdi
leaq 0x1e3edb(%rip), %rsi    # 0x2e5013
leaq 0x22bcbb(%rip), %rdx    # 0x32cdfa
leaq 0x22bc99(%rip), %rcx    # 0x32cddf
leaq 0x22bbe8(%rip), %r8     # 0x32cd35
pushq $0x5f
jmp 0x101179
movq 0x336e10(%rip), %rax    # 0x437f68
movq (%rax), %rdi
leaq 0x1e3eb1(%rip), %rsi    # 0x2e5013
leaq 0x1e4056(%rip), %rdx    # 0x2e51bf
leaq 0x22bc6f(%rip), %rcx    # 0x32cddf
leaq 0x22bbbe(%rip), %r8     # 0x32cd35
pushq $0x42
popq %r9
xorl %eax, %eax
callq 0x17a50
callq 0x17390
nop