// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/extended/sort_stream.c
// Function: gt_sort_stream_next
static int gt_sort_stream_next(GtNodeStream *ns, GtGenomeNode **gn,
                               GtError *err)
{
  GtSortStream *sort_stream;
  GtEOFNode *eofn;
  GtGenomeNode *node;
  int had_err = 0;
  gt_error_check(err);
  sort_stream = gt_sort_stream_cast(ns);

  if (!sort_stream->sorted) {
    while (!(had_err = gt_node_stream_next(sort_stream->in_stream, &node,
                                           err)) && node) {
      if ((eofn = gt_eof_node_try_cast(node)))
        gt_genome_node_delete(node); /* get rid of EOF nodes */
      else
        gt_array_add(sort_stream->nodes, node);
    }
    if (!had_err) {
      gt_genome_nodes_sort_stable(sort_stream->nodes);
      sort_stream->sorted = true;
    }
  }

  if (!had_err) {
    gt_assert(sort_stream->sorted);
    if (sort_stream->idx < gt_array_size(sort_stream->nodes)) {
      *gn = *(GtGenomeNode**) gt_array_get(sort_stream->nodes,
                                           sort_stream->idx);
      sort_stream->idx++;
      /* join region nodes with the same sequence ID */
      if (gt_region_node_try_cast(*gn)) {
        GtRange range_a, range_b;
        while (sort_stream->idx < gt_array_size(sort_stream->nodes)) {
          node = *(GtGenomeNode**) gt_array_get(sort_stream->nodes,
                                                sort_stream->idx);
          if (!gt_region_node_try_cast(node) ||
              gt_str_cmp(gt_genome_node_get_seqid(*gn),
                         gt_genome_node_get_seqid(node))) {
            /* the next node is not a region node with the same ID */
            break;
          }
          range_a = gt_genome_node_get_range(*gn);
          range_b = gt_genome_node_get_range(node);
          range_a = gt_range_join(&range_a, &range_b);
          gt_genome_node_set_range(*gn, &range_a);
          gt_genome_node_delete(node);
          sort_stream->idx++;
        }
      }
      return 0;
    }
  }

  if (!had_err) {
    gt_array_reset(sort_stream->nodes);
    *gn = NULL;
  }

  return had_err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
testq %rdx, %rdx
je 0x29db7
movq %r15, %rdi
callq 0x18a0e
testb %al, %al
jne 0x29f9d
callq 0x29cec
movq %rax, %rdi
movq %r14, %rsi
callq 0x292d4
movq %rax, %r14
xorl %ebp, %ebp
cmpb $0x0, 0x28(%rax)
jne 0x29e5e
movq 0x10(%r14), %rdi
movq %rsp, %r12
movq %r12, %rsi
movq %r15, %rdx
callq 0x29158
movl %eax, %ebp
testl %eax, %eax
sete %al
movq (%r12), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x29e4a
movq %rsp, %r12
callq 0x54b6f
testq %rax, %rax
je 0x29e18
movq (%rsp), %rdi
callq 0x220ee
jmp 0x29e29
movq 0x20(%r14), %rdi
movl $0x8, %edx
movq %r12, %rsi
callq 0x42222
movq 0x10(%r14), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x29158
movl %eax, %ebp
testl %eax, %eax
sete %al
jne 0x29e4a
movq (%rsp), %rdi
testq %rdi, %rdi
jne 0x29e03
testb %al, %al
je 0x29e5e
movq 0x20(%r14), %rdi
callq 0x217e5
movb $0x1, 0x28(%r14)
xorl %ebp, %ebp
testl %ebp, %ebp
jne 0x29f8c
cmpb $0x0, 0x28(%r14)
je 0x29fa2
movq 0x18(%r14), %r15
movq 0x20(%r14), %rdi
callq 0x422ee
movq 0x20(%r14), %rdi
cmpq %rax, %r15
jae 0x29f80
movq 0x18(%r14), %rsi
callq 0x4200e
movq (%rax), %rdi
movq %rdi, (%rbx)
incq 0x18(%r14)
callq 0x29acc
xorl %ebp, %ebp
testq %rax, %rax
je 0x29f8c
movq 0x18(%r14), %r15
movq 0x20(%r14), %rdi
callq 0x422ee
cmpq %rax, %r15
jae 0x29f8c
leaq 0x8(%rsp), %r15
leaq 0x18(%rsp), %r12
movq 0x18(%r14), %rsi
movq 0x20(%r14), %rdi
callq 0x4200e
movq (%rax), %rdi
movq %rdi, (%rsp)
callq 0x29acc
testq %rax, %rax
je 0x29f8c
movq (%rbx), %rdi
callq 0x2154f
movq %rax, %r13
movq (%rsp), %rdi
callq 0x2154f
movq %r13, %rdi
movq %rax, %rsi
callq 0x1f854
testl %eax, %eax
jne 0x29f8c
movq (%rbx), %rdi
callq 0x21593
movq %rax, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq (%rsp), %rdi
callq 0x21593
movq %rax, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x4dcb7
movq %rax, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x215f4
movq (%rsp), %rdi
callq 0x220ee
movq 0x18(%r14), %r13
movq 0x20(%r14), %rdi
incq %r13
movq %r13, 0x18(%r14)
callq 0x422ee
cmpq %rax, %r13
jb 0x29ece
jmp 0x29f8c
callq 0x42327
movq $0x0, (%rbx)
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xe260
callq 0xe297