// File: /workspace/github_c_makefile_star2_O3/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 0x296f7
movq %r15, %rdi
callq 0x1890e
testb %al, %al
jne 0x298d3
callq 0x2962c
movq %rax, %rdi
movq %r14, %rsi
callq 0x28c17
movq %rax, %r14
cmpb $0x0, 0x28(%rax)
je 0x2971b
jne 0x297a5
callq 0xe1b3
movq 0x10(%r14), %rdi
movq %rsp, %r12
movq %r12, %rsi
movq %r15, %rdx
callq 0x28a88
movl %eax, %ebp
testl %eax, %eax
sete %al
movq (%r12), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x2978f
movq %rsp, %r12
callq 0x54f51
testq %rax, %rax
je 0x2975d
movq (%rsp), %rdi
callq 0x21cb4
jmp 0x2976e
movq 0x20(%r14), %rdi
movl $0x8, %edx
movq %r12, %rsi
callq 0x42f18
movq 0x10(%r14), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x28a88
movl %eax, %ebp
testl %eax, %eax
sete %al
jne 0x2978f
movq (%rsp), %rdi
testq %rdi, %rdi
jne 0x29748
testb %al, %al
je 0x298c2
movq 0x20(%r14), %rdi
callq 0x213df
movb $0x1, 0x28(%r14)
movq 0x18(%r14), %r15
movq 0x20(%r14), %rdi
callq 0x42fe8
movq 0x20(%r14), %rdi
cmpq %rax, %r15
jae 0x298b4
movq 0x18(%r14), %rsi
callq 0x42d0f
movq (%rax), %rdi
movq %rdi, (%rbx)
incq 0x18(%r14)
callq 0x2940c
xorl %ebp, %ebp
testq %rax, %rax
je 0x298c2
movq 0x18(%r14), %r15
movq 0x20(%r14), %rdi
callq 0x42fe8
cmpq %rax, %r15
jae 0x298c2
leaq 0x8(%rsp), %r15
leaq 0x18(%rsp), %r12
movq 0x18(%r14), %rsi
movq 0x20(%r14), %rdi
callq 0x42d0f
movq (%rax), %rdi
movq %rdi, (%rsp)
callq 0x2940c
testq %rax, %rax
je 0x298c2
movq (%rbx), %rdi
callq 0x21149
movq %rax, %r13
movq (%rsp), %rdi
callq 0x21149
movq %r13, %rdi
movq %rax, %rsi
callq 0x1f526
testl %eax, %eax
jne 0x298c2
movq (%rbx), %rdi
callq 0x2118d
movq %rax, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq (%rsp), %rdi
callq 0x2118d
movq %rax, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x4e657
movq %rax, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x211ee
movq (%rsp), %rdi
callq 0x21cb4
movq 0x18(%r14), %r13
movq 0x20(%r14), %rdi
incq %r13
movq %r13, 0x18(%r14)
callq 0x42fe8
cmpq %rax, %r13
jb 0x29802
jmp 0x298c2
callq 0x43021
movq $0x0, (%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xe17c