// File: /workspace/github_c_makefile_star2_O2/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 0x1d70d
movq %r15, %rdi
callq 0x7cff
testb %al, %al
jne 0x1d8d6
callq 0x1d64c
movq %rax, %rdi
movq %r14, %rsi
callq 0x1c5e5
movq %rax, %r14
cmpb $0x0, 0x28(%rax)
je 0x1d759
jne 0x1d7ba
movq 0x8789d(%rip), %rax     # 0xa4fd0
movq (%rax), %rdi
leaq 0x508e7(%rip), %rsi     # 0x6e024
leaq 0x57638(%rip), %rdx     # 0x74d7c
leaq 0x5761d(%rip), %rcx     # 0x74d68
leaq 0x575fb(%rip), %r8      # 0x74d4d
pushq $0x3e
jmp 0x1d8fe
movq %rsp, %r12
pushq $0x8
popq %r13
movq 0x10(%r14), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1c386
movl %eax, %ebp
testl %eax, %eax
jne 0x1d7a4
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1d7a4
callq 0x4786f
testq %rax, %rax
je 0x1d793
movq (%rsp), %rdi
callq 0x1370f
jmp 0x1d760
movq 0x20(%r14), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x32c65
jmp 0x1d760
testl %ebp, %ebp
jne 0x1d8c5
movq 0x20(%r14), %rdi
callq 0x12ce2
movb $0x1, 0x28(%r14)
movq 0x18(%r14), %r15
movq 0x20(%r14), %rdi
callq 0x32dec
movq 0x20(%r14), %rdi
cmpq %rax, %r15
jae 0x1d8ba
movq 0x18(%r14), %rsi
callq 0x328f2
movq (%rax), %rdi
movq %rdi, (%rbx)
incq 0x18(%r14)
callq 0x1d345
xorl %ebp, %ebp
testq %rax, %rax
je 0x1d8c5
movq 0x18(%r14), %r13
leaq 0x8(%rsp), %r15
leaq 0x18(%rsp), %r12
movq 0x20(%r14), %rdi
callq 0x32dec
cmpq %rax, %r13
jae 0x1d8c5
movq 0x18(%r14), %rsi
movq 0x20(%r14), %rdi
callq 0x328f2
movq (%rax), %rdi
movq %rdi, (%rsp)
callq 0x1d345
testq %rax, %rax
je 0x1d8c5
movq (%rbx), %rdi
callq 0x12846
movq %rax, %r13
movq (%rsp), %rdi
callq 0x12846
movq %r13, %rdi
movq %rax, %rsi
callq 0x1016f
testl %eax, %eax
jne 0x1d8c5
movq (%rbx), %rdi
callq 0x128f0
movq %rax, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq (%rsp), %rdi
callq 0x128f0
movq %rax, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x40541
movq %rax, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x129b7
movq (%rsp), %rdi
callq 0x1370f
movq 0x18(%r14), %r13
incq %r13
movq %r13, 0x18(%r14)
jmp 0x1d805
callq 0x32eb4
andq $0x0, (%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x876f3(%rip), %rax     # 0xa4fd0
movq (%rax), %rdi
leaq 0x5073d(%rip), %rsi     # 0x6e024
leaq 0x50d1b(%rip), %rdx     # 0x6e609
leaq 0x57473(%rip), %rcx     # 0x74d68
leaq 0x57451(%rip), %r8      # 0x74d4d
pushq $0x2c
popq %r9
xorl %eax, %eax
callq 0x7710
callq 0x7270