// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/extended/cds_check_visitor.c
// Function: check_cds_phases
static int check_cds_phases(GtArray *cds_features, GtCDSCheckVisitor *v,
                            bool is_multi, bool second_pass, GtError *err)
{
  GtPhase correct_phase = GT_PHASE_ZERO;
  GtFeatureNode *fn;
  GtStrand strand;
  bool first = true;
  GtUword i,
          total_length = 0;
  int had_err = 0;
  gt_error_check(err);
  gt_assert(cds_features);
  gt_assert(gt_array_size(cds_features));
  fn = *(GtFeatureNode**) gt_array_get_first(cds_features);
  strand = gt_feature_node_get_strand(fn);
  if (strand == GT_STRAND_REVERSE)
    gt_array_reverse(cds_features);
  for (i = 0; !had_err && i < gt_array_size(cds_features); i++) {
    fn = *(GtFeatureNode**) gt_array_get(cds_features, i);
    /* the first phase can be anything (except being undefined), because the
       GFF3 spec says:

       NOTE 4 - CDS features MUST have have a defined phase field. Otherwise it
       is not possible to infer the correct polypeptides corresponding to
       partially annotated genes. */
    if ((!i && gt_feature_node_get_phase(fn) == GT_PHASE_UNDEFINED) ||
        (i && gt_feature_node_get_phase(fn) != correct_phase)) {
      if (gt_hashmap_get(v->cds_features, fn)) {
        if (v->tidy && !is_multi && !gt_feature_node_has_children(fn)) {
          /* we can split the feature */
          gt_warning("%s feature on line %u in file \"%s\" has multiple "
                     "parents which require different phases; split feature",
                     gt_ft_CDS,
                     gt_genome_node_get_line_number((GtGenomeNode*) fn),
                     gt_genome_node_get_filename((GtGenomeNode*) fn));
          gt_hashmap_add(v->cds_features_to_split, fn, fn);
          v->splitting_is_necessary = true; /* split later */
        }
        else {
          gt_error_set(err, "%s feature on line %u in file \"%s\" has multiple "
                       "parents which require different phases",
                       gt_ft_CDS,
                       gt_genome_node_get_line_number((GtGenomeNode*) fn),
                       gt_genome_node_get_filename((GtGenomeNode*) fn));
          had_err = -1;
        }
      }
      else {
        if (v->tidy) {
          if (!second_pass) {
            gt_warning("%s feature on line %u in file \"%s\" has the wrong "
                       "phase %c -> correcting it to %c", gt_ft_CDS,
                       gt_genome_node_get_line_number((GtGenomeNode*) fn),
                       gt_genome_node_get_filename((GtGenomeNode*) fn),
                       GT_PHASE_CHARS[gt_feature_node_get_phase(fn)],
                       GT_PHASE_CHARS[correct_phase]);
          }
          gt_feature_node_set_phase(fn, correct_phase);
        }
        else {
          gt_error_set(err, "%s feature on line %u in file \"%s\" has the "
                       "wrong phase %c (should be %c)", gt_ft_CDS,
                       gt_genome_node_get_line_number((GtGenomeNode*) fn),
                       gt_genome_node_get_filename((GtGenomeNode*) fn),
                       GT_PHASE_CHARS[gt_feature_node_get_phase(fn)],
                       GT_PHASE_CHARS[correct_phase]);
          had_err = -1;
        }
      }
    }
    if (!had_err) {
      GtUword current_length = gt_genome_node_get_length((GtGenomeNode*) fn);
      if (first) {
        /* this handles the case of 5' partial genes, where CDS might not start
           on codon boundaries */
        GtUword current_phase = (GtUword) gt_feature_node_get_phase(fn);
        if (current_length >= current_phase) {
          /* we simply skip the amount of bases defined by phase */
          current_length -= current_phase;
          first = false;
          total_length = current_length;
          correct_phase = (3 - (total_length) % 3) % 3;
        } else {
          /* corner case: first CDS is single base */
          gt_assert(current_length == 1 && current_phase == 2);
          correct_phase = 1;
        }
      } else {
        total_length += current_length;
        correct_phase = (3 - (total_length) % 3) % 3;
      }
      gt_hashmap_add(v->cds_features, fn, fn); /* record CDS feature */
    }
  }
  return had_err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbx
movl %edx, 0x3c(%rsp)
movq %rsi, %rbp
movq %rdi, %r14
testq %rcx, %rcx
je 0x8497a
movq %rbx, %rdi
callq 0x3566a
testb %al, %al
jne 0x84caf
movq %r14, %rdi
callq 0x6ecda
testq %rax, %rax
je 0x84caa
movq %r14, %rdi
callq 0x6ea17
movq (%rax), %rdi
callq 0x3fdd2
cmpl $0x1, %eax
jne 0x849a8
movq %r14, %rdi
callq 0x6eb46
movq %r14, %rdi
callq 0x6ecda
testq %rax, %rax
je 0x84c8a
movq %rbx, 0x40(%rsp)
xorl %r12d, %r12d
movb $0x1, %al
movl %eax, 0x24(%rsp)
movq $0x0, 0x30(%rsp)
xorl %r13d, %r13d
movq %rbp, 0x28(%rsp)
movq %r14, 0x18(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x6e9fa
movq (%rax), %rbx
movq %rbx, %rdi
callq 0x4062b
testq %r12, %r12
je 0x84a0d
cmpl %r13d, %eax
jne 0x84a12
movl $0x0, 0x14(%rsp)
movb $0x1, %r15b
jmp 0x84b33
cmpl $0x3, %eax
jne 0x849fd
movq 0x10(%rbp), %rdi
movq %rbx, %rsi
callq 0x77109
testq %rax, %rax
je 0x84a84
movb 0x20(%rbp), %al
xorb $0x1, %al
orb 0x3c(%rsp), %al
jne 0x84a3e
movq %rbx, %rdi
callq 0x40251
testb %al, %al
je 0x84c39
movq %rbx, %rdi
callq 0x41f30
movl %eax, %ebp
movq %rbx, %rdi
callq 0x41f10
xorl %r15d, %r15d
movq 0x40(%rsp), %rdi
leaq 0x6f684(%rip), %rsi     # 0xf40e3
leaq 0x60789(%rip), %rdx     # 0xe51ef
movl %ebp, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x3559f
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movq 0x28(%rsp), %rbp
jmp 0x84b33
movb 0x20(%rbp), %r15b
movq %rbx, %rdi
callq 0x41f30
movl %eax, %r14d
movq %rbx, %rdi
callq 0x41f10
movq %rax, %rbp
movq %rbx, %rdi
callq 0x4062b
movl %eax, %eax
leaq 0x5e141(%rip), %rdx     # 0xe2bf0
movsbl (%rax,%rdx), %eax
movl %r13d, %ecx
movsbl (%rcx,%rdx), %r9d
cmpb $0x1, %r15b
jne 0x84af7
movl $0x0, 0x14(%rsp)
leaq 0x6f66a(%rip), %rdi     # 0xf413a
leaq 0x60718(%rip), %rsi     # 0xe51ef
movl %r14d, %edx
movq %rbp, %rcx
movl %eax, %r8d
xorl %eax, %eax
callq 0x3da84
movq %rbx, %rdi
movl %r13d, %esi
callq 0x3f918
movb $0x1, %r15b
jmp 0x84b29
movl %r9d, (%rsp)
xorl %r15d, %r15d
movq 0x40(%rsp), %rdi
leaq 0x6f681(%rip), %rsi     # 0xf418b
leaq 0x606de(%rip), %rdx     # 0xe51ef
movl %r14d, %ecx
movq %rbp, %r8
movl %eax, %r9d
xorl %eax, %eax
callq 0x3559f
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movq 0x28(%rsp), %rbp
movq 0x18(%rsp), %r14
testb %r15b, %r15b
je 0x84c1e
movq %rbx, %rdi
callq 0x41fac
movq %rax, %r14
testb $0x1, 0x24(%rsp)
je 0x84b83
movq %rbx, %rdi
callq 0x4062b
movl %eax, %eax
movq %r14, %rcx
subq %rax, %rcx
jae 0x84bc6
cmpq $0x1, %r14
movq 0x18(%rsp), %r14
jne 0x84ca5
movl $0x1, %r13d
cmpl $0x2, %eax
je 0x84c0f
jmp 0x84ca5
movq 0x30(%rsp), %r13
addq %r14, %r13
movq %r13, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq %rdx
leaq (%rdx,%rdx,2), %rax
movq %r13, 0x30(%rsp)
subq %rax, %r13
xorq $0x3, %r13
movq %r13, %rax
movabsq $0x5555555555555556, %rcx # imm = 0x5555555555555556
mulq %rcx
leal (%rdx,%rdx,2), %eax
subl %eax, %r13d
jmp 0x84c0a
movq %rcx, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rdx
shrq %rdx
leaq (%rdx,%rdx,2), %rax
movq %rcx, %r13
subq %rax, %r13
xorq $0x3, %r13
movq %r13, %rax
movabsq $0x5555555555555556, %rdx # imm = 0x5555555555555556
mulq %rdx
leal (%rdx,%rdx,2), %eax
subl %eax, %r13d
movl $0x0, 0x24(%rsp)
movq %rcx, 0x30(%rsp)
movq 0x18(%rsp), %r14
movq 0x10(%rbp), %rdi
movq %rbx, %rsi
movq %rbx, %rdx
callq 0x77144
testb %r15b, %r15b
je 0x84c92
incq %r12
movq %r14, %rdi
callq 0x6ecda
cmpq %rax, %r12
jb 0x849dd
jmp 0x84c92
movq %rbx, %rdi
callq 0x41f30
movl %eax, %ebp
movq %rbx, %rdi
callq 0x41f10
movl $0x0, 0x14(%rsp)
leaq 0x6f423(%rip), %rdi     # 0xf407d
leaq 0x6058e(%rip), %rsi     # 0xe51ef
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3da84
movq 0x28(%rsp), %rbp
movq 0x18(%rbp), %rdi
movq %rbx, %rsi
movq %rbx, %rdx
callq 0x77144
movb $0x1, 0x21(%rbp)
jmp 0x84a05
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1f25e
callq 0x1f295
callq 0x1f227