// File: /workspace/github_c_makefile_star2_O3/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, %r13
movq %rdi, %r15
testq %rdx, %rdx
je 0x128e34
movq %rbx, %rdi
callq 0xfcc32
testb %al, %al
jne 0x1290fb
callq 0x128d60
movq %rax, %rdi
movq %r15, %rsi
callq 0x162ff7
movq %rax, %r15
cmpb $0x0, 0x30(%rax)
jne 0x128ff9
leaq 0x8(%rsp), %r12
movq 0x10(%r15), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x162e68
testl %eax, %eax
jne 0x1290c1
cmpq $0x0, 0x8(%rsp)
je 0x128e95
movq 0x20(%r15), %rdi
movl $0x8, %edx
movq %r12, %rsi
callq 0xd2410
movq 0x8(%rsp), %rdi
callq 0x16e930
testq %rax, %rax
jne 0x128e56
movq %r13, 0x10(%rsp)
movq 0x20(%r15), %rdi
callq 0xd24e0
movq 0x20(%r15), %rdi
testq %rax, %rax
je 0x128f60
xorl %r12d, %r12d
leaq 0x315(%rip), %r13       # 0x1291cf
movq %r12, %rsi
callq 0xd2207
movq (%rax), %rdi
movq %rdi, 0x8(%rsp)
callq 0x144635
testq %rax, %rax
je 0x128f47
movq 0x18(%r15), %r14
movq 0x8(%rsp), %rdi
callq 0x144635
movq %rax, %rdi
callq 0x11b0a1
movq %r14, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x15d32b
testq %rax, %rax
je 0x1290c5
movq %rax, %rbp
movq 0x8(%rsp), %rdi
callq 0x143cab
testq %rax, %rax
je 0x128f32
movq %rax, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movl $0x1, %ecx
movq %rbx, %r8
callq 0x142d9d
testl %eax, %eax
je 0x128f3f
jmp 0x1290f6
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x144709
movq %rbp, %rdi
callq 0x11bdbf
incq %r12
movq 0x20(%r15), %rdi
callq 0xd24e0
movq 0x20(%r15), %rdi
cmpq %rax, %r12
jb 0x128eba
callq 0x144863
movq 0x20(%r15), %rdi
callq 0xd24e0
cmpq $0x3, %rax
jb 0x128fef
xorl %r12d, %r12d
leaq 0x1(%r12), %r13
movq 0x20(%r15), %rdi
movq %r12, %rsi
callq 0xd2207
movq %rax, %rbp
movq 0x20(%r15), %rdi
movq %r13, %rsi
callq 0xd2207
movq %rax, %r14
movq (%rbp), %rdi
movq (%rax), %rsi
callq 0x14499e
testb %al, %al
je 0x128fda
movq (%r14), %rdi
callq 0x16e88a
movq %rax, %r14
movq (%rbp), %rdi
callq 0x16e88a
movq %r14, %rdi
movq %rax, %rsi
callq 0x16e89f
movq (%rbp), %rdi
callq 0x1451a0
movq $0x0, (%rbp)
movq 0x20(%r15), %rdi
callq 0xd24e0
addq $0x3, %r12
cmpq %rax, %r12
movq %r13, %r12
jb 0x128f77
movb $0x1, 0x30(%r15)
movq 0x10(%rsp), %r13
movq 0x28(%r15), %r14
movq 0x20(%r15), %rdi
callq 0xd24e0
cmpq %rax, %r14
jae 0x12903b
movq 0x20(%r15), %rdi
movq 0x28(%r15), %rsi
callq 0xd2207
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x28(%r15), %r14
incq %r14
movq %r14, 0x28(%r15)
testq %rax, %rax
je 0x128ffd
movq %rax, (%r13)
xorl %ebp, %ebp
jmp 0x1290cf
movq 0x10(%r15), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x162e68
movl %eax, %ebp
testl %eax, %eax
jne 0x1290cf
movq (%r13), %rdi
xorl %ebp, %ebp
testq %rdi, %rdi
je 0x1290cf
callq 0x144635
testq %rax, %rax
je 0x1290cf
movq 0x18(%r15), %r14
movq (%r13), %rdi
callq 0x144635
movq %rax, %rdi
callq 0x11b0a1
movq %r14, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x15d32b
testq %rax, %rax
je 0x129100
movq %rax, %r15
movq (%r13), %rdi
callq 0x143cab
testq %rax, %rax
je 0x1290e0
leaq 0x12a(%rip), %rdx       # 0x1291cf
movq %rax, %rdi
movq %r15, %rsi
movl $0x1, %ecx
movq %rbx, %r8
callq 0x142d9d
testl %eax, %eax
je 0x1290ec
callq 0x3bfe2
movl %eax, %ebp
jmp 0x1290ca
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movb $0x1, 0x30(%r15)
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r13), %rdi
movq %r15, %rsi
callq 0x144709
movq %r15, %rdi
callq 0x11bdbf
jmp 0x1290cf
callq 0x3bfab
callq 0x3bf74
callq 0x3c01b