// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/extended/chseqids_stream.c
// Function: chseqids_stream_next
static int chseqids_stream_next(GtNodeStream *ns, GtGenomeNode **gn,
                                GtError *err)
{
  GtChseqidsStream *cs;
  GtGenomeNode *node, **gn_a, **gn_b;
  GtFeatureNode *feature_node;
  GtStr *changed_seqid;
  GtUword i;
  int GT_UNUSED rval, had_err = 0;
  gt_error_check(err);
  cs = chseqids_stream_cast(ns);

  if (!cs->sequence_regions_processed) {
    while (!had_err) {
      if (!(had_err = gt_node_stream_next(cs->in_stream, &node, err))) {
        if (node)
          gt_array_add(cs->node_buffer, node);
        else
          break;
        if (!gt_region_node_try_cast(node))
          break; /* no more sequence regions */
      }
    }
    /* now the buffer contains only sequence regions (except the last entry)
       -> change sequence ids */
    for (i = 0; !had_err && i < gt_array_size(cs->node_buffer); i++) {
      node = *(GtGenomeNode**) gt_array_get(cs->node_buffer, i);
      if (gt_genome_node_get_seqid(node)) {
        if  ((changed_seqid = gt_mapping_map_string(cs->chseqids_mapping,
                                     gt_str_get(gt_genome_node_get_seqid(node)),
                                                 err))) {
          if ((feature_node = gt_feature_node_try_cast(node))) {
            rval = gt_feature_node_traverse_children(feature_node,
                                                     changed_seqid,
                                                     change_sequence_id, true,
                                                     err);
            gt_assert(!rval); /* change_sequence_id() is sane */
          }
          else
            gt_genome_node_change_seqid(node, changed_seqid);
          gt_str_delete(changed_seqid);
        }
        else
          had_err = -1;
       }
    }
    /* sort them */
    if (!had_err)
      gt_genome_nodes_sort(cs->node_buffer);
    /* consolidate them */
    for (i = 1; !had_err && i + 1 < gt_array_size(cs->node_buffer); i++) {
      gn_a = gt_array_get(cs->node_buffer, i-1);
      gn_b = gt_array_get(cs->node_buffer, i);
      if (gt_genome_nodes_are_equal_region_nodes(*gn_a, *gn_b)) {
        gt_region_node_consolidate(gt_region_node_cast(*gn_b),
                                   gt_region_node_cast(*gn_a));
        gt_genome_node_delete(*gn_a);
        *gn_a = NULL;
      }
    }
    cs->sequence_regions_processed = true;
  }

  /* return non-null nodes from buffer */
  while (!had_err && cs->buffer_index < gt_array_size(cs->node_buffer)) {
    node = *(GtGenomeNode**) gt_array_get(cs->node_buffer, cs->buffer_index);
    cs->buffer_index++;
    if (node) {
      *gn = node;
      return had_err;
    }
  }

  if (!had_err)
    had_err = gt_node_stream_next(cs->in_stream, gn, err);
  if (!had_err && *gn) {
    if (gt_genome_node_get_seqid(*gn)) {
      changed_seqid = gt_mapping_map_string(cs->chseqids_mapping,
                                      gt_str_get(gt_genome_node_get_seqid(*gn)),
                                         err);
      gt_assert(changed_seqid); /* is always defined, because an undefined
                                   mapping would be catched earlier */
      if ((feature_node = gt_feature_node_try_cast(*gn))) {
        rval = gt_feature_node_traverse_children(feature_node, changed_seqid,
                                                 change_sequence_id, true, err);
        gt_assert(!rval); /* change_sequence_id() is sane */
      }
      else
        gt_genome_node_change_seqid(*gn, changed_seqid);
      gt_str_delete(changed_seqid);
    }
  }

  return had_err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r15
testq %rdx, %rdx
je 0x12ed00
movq %rbx, %rdi
callq 0x101a9a
testb %al, %al
jne 0x12efd0
callq 0x12ec2c
movq %rax, %rdi
movq %r15, %rsi
callq 0x16ae64
movq %rax, %r15
xorl %ebp, %ebp
cmpb $0x0, 0x30(%rax)
jne 0x12eee1
movq %r12, 0x8(%rsp)
movq %rsp, %r12
movq 0x10(%r15), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x16ace8
testl %eax, %eax
jne 0x12ed64
xorl %ebp, %ebp
cmpq $0x0, (%rsp)
je 0x12ed66
movq 0x20(%r15), %rdi
movl $0x8, %edx
movq %r12, %rsi
callq 0xd49ce
movq (%rsp), %rdi
callq 0x176cbc
testq %rax, %rax
jne 0x12ed27
jmp 0x12ed66
movl %eax, %ebp
testl %ebp, %ebp
sete %r14b
movq %rbx, 0x10(%rsp)
jne 0x12ee4e
movq 0x20(%r15), %rdi
callq 0xd4a9a
testq %rax, %rax
je 0x12ee45
xorl %r12d, %r12d
leaq 0x316(%rip), %r13       # 0x12f0a9
movq 0x20(%r15), %rdi
movq %r12, %rsi
callq 0xd47ba
movq (%rax), %rdi
movq %rdi, (%rsp)
callq 0x14b89b
movb $0x1, %r14b
testq %rax, %rax
je 0x12ee29
movq 0x18(%r15), %rbp
movq (%rsp), %rdi
callq 0x14b89b
movq %rax, %rdi
callq 0x120d23
movq %rbp, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x16507c
testq %rax, %rax
je 0x12ee0b
movq %rax, %rbp
movq (%rsp), %rdi
callq 0x14aefc
testq %rax, %rax
je 0x12ee15
movq %rax, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movl $0x1, %ecx
movq %rbx, %r8
callq 0x149fca
testl %eax, %eax
je 0x12ee21
jmp 0x12efd5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
jmp 0x12ee2b
movq (%rsp), %rdi
movq %rbp, %rsi
callq 0x14b96f
movq %rbp, %rdi
callq 0x121b03
xorl %ebp, %ebp
testb %r14b, %r14b
je 0x12ee4e
incq %r12
movq 0x20(%r15), %rdi
callq 0xd4a9a
cmpq %rax, %r12
jb 0x12ed93
movq 0x20(%r15), %rdi
callq 0x14bac9
testb %r14b, %r14b
je 0x12eed2
movl $0x2, %ebx
movq 0x20(%r15), %rdi
callq 0xd4a9a
cmpq %rax, %rbx
jae 0x12eed2
leaq -0x1(%rbx), %r13
movq 0x20(%r15), %rdi
leaq -0x2(%rbx), %rsi
callq 0xd47ba
movq %rax, %r12
movq 0x20(%r15), %rdi
movq %r13, %rsi
callq 0xd47ba
movq %rax, %r13
movq (%r12), %rdi
movq (%rax), %rsi
callq 0x14bc04
testb %al, %al
je 0x12eeca
movq (%r13), %rdi
callq 0x176c16
movq %rax, %r13
movq (%r12), %rdi
callq 0x176c16
movq %r13, %rdi
movq %rax, %rsi
callq 0x176c2b
movq (%r12), %rdi
callq 0x14c43a
movq $0x0, (%r12)
incq %rbx
testb %r14b, %r14b
jne 0x12ee58
movb $0x1, 0x30(%r15)
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r12
testl %ebp, %ebp
jne 0x12ef33
movq %rbx, %r14
movq 0x20(%r15), %rdi
movq 0x28(%r15), %rbx
callq 0xd4a9a
cmpq %rax, %rbx
jae 0x12ef1f
movq 0x20(%r15), %rdi
movq 0x28(%r15), %rsi
callq 0xd47ba
movq (%rax), %rax
movq %rax, (%rsp)
incq 0x28(%r15)
testq %rax, %rax
je 0x12eee8
movq %rax, (%r12)
xorl %ebp, %ebp
jmp 0x12ef37
movq 0x10(%r15), %rdi
movq %r12, %rsi
movq %r14, %rbx
movq %r14, %rdx
callq 0x16ace8
movl %eax, %ebp
testl %ebp, %ebp
je 0x12ef48
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r12), %rdi
testq %rdi, %rdi
je 0x12ef37
callq 0x14b89b
testq %rax, %rax
je 0x12ef37
movq 0x18(%r15), %r15
movq (%r12), %rdi
callq 0x14b89b
movq %rax, %rdi
callq 0x120d23
movq %r15, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x16507c
testq %rax, %rax
je 0x12efda
movq %rax, %r15
movq (%r12), %rdi
callq 0x14aefc
testq %rax, %rax
je 0x12efb7
leaq 0x10e(%rip), %rdx       # 0x12f0a9
movq %rax, %rdi
movq %r15, %rsi
movl $0x1, %ecx
movq %rbx, %r8
callq 0x149fca
testl %eax, %eax
je 0x12efc3
callq 0x3ca27
movq (%r12), %rdi
movq %r15, %rsi
callq 0x14b96f
movq %r15, %rdi
callq 0x121b03
jmp 0x12ef37
callq 0x3c9b9
callq 0x3c9f0
callq 0x3ca60