// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rcx, %rbx
movl %edx, 0x2c(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %rbp
testq %rcx, %rcx
je 0x8455d
movq %rbx, %rdi
callq 0x34fea
testb %al, %al
jne 0x84836
movq %rbp, %rdi
callq 0x6f310
testq %rax, %rax
je 0x84831
movq %rbp, %rdi
callq 0x6f054
movq (%rax), %rdi
callq 0x3f462
cmpl $0x1, %eax
jne 0x8458b
movq %rbp, %rdi
callq 0x6f179
movq %rbp, %rdi
callq 0x6f310
testq %rax, %rax
je 0x847b9
movq %rbx, 0x30(%rsp)
movb $0x1, %al
movl %eax, 0x10(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
movq %rbp, 0x18(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x6f037
movq (%rax), %rbx
movq %rbx, %rdi
callq 0x3fcc6
testq %r12, %r12
je 0x845da
cmpl %r13d, %eax
jne 0x845e3
jmp 0x846cf
cmpl $0x3, %eax
jne 0x846cf
movq 0x20(%rsp), %r15
movq 0x10(%r15), %rdi
movq %rbx, %rsi
callq 0x775ed
movb 0x20(%r15), %bpl
testq %rax, %rax
je 0x84668
testb %bpl, %bpl
je 0x847bd
cmpb $0x0, 0x2c(%rsp)
jne 0x847bd
movq %rbx, %rdi
callq 0x3f8ef
testb %al, %al
jne 0x847bd
movq %rbx, %rdi
callq 0x41592
movl %eax, %ebp
movq %rbx, %rdi
callq 0x41572
leaq 0x6f9e3(%rip), %rdi     # 0xf401d
leaq 0x60b8e(%rip), %rsi     # 0xe51cf
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3d110
movq 0x20(%rsp), %r15
movq 0x18(%r15), %rdi
movq %rbx, %rsi
movq %rbx, %rdx
callq 0x77628
movb $0x1, 0x21(%r15)
jmp 0x846cf
movq %rbx, %rdi
callq 0x41592
movl %eax, 0x14(%rsp)
movq %rbx, %rdi
callq 0x41572
movq %rax, %r15
movq %rbx, %rdi
callq 0x3fcc6
movl %eax, %eax
leaq 0x5e560(%rip), %rdx     # 0xe2bf0
movsbl (%rax,%rdx), %eax
movl %r13d, %ecx
movsbl (%rcx,%rdx), %r9d
testb %bpl, %bpl
je 0x847f0
leaq 0x6fa2e(%rip), %rdi     # 0xf40da
leaq 0x60b1c(%rip), %rsi     # 0xe51cf
movl 0x14(%rsp), %edx
movq %r15, %rcx
movl %eax, %r8d
xorl %eax, %eax
callq 0x3d110
movq %rbx, %rdi
movl %r13d, %esi
callq 0x3efa4
movq %rbx, %rdi
callq 0x4160e
movq %rax, %rbp
testb $0x1, 0x10(%rsp)
je 0x84717
movq %rbx, %rdi
callq 0x3fcc6
movl %eax, %eax
movq %rbp, %r15
subq %rax, %r15
jae 0x84754
cmpq $0x1, %rbp
jne 0x8482c
cmpl $0x2, %eax
jne 0x8482c
movl $0x1, %r13d
movb $0x1, %al
movl %eax, 0x10(%rsp)
movq %r14, %r15
jmp 0x84789
addq %r14, %rbp
movq %rbp, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq %rdx
leaq (%rdx,%rdx,2), %rax
movq %rbp, %rcx
subq %rax, %rcx
movl %ecx, %r13d
xorl $0x3, %r13d
xorl %eax, %eax
testq %rcx, %rcx
movl $0x0, 0x10(%rsp)
cmovel %eax, %r13d
movq %rbp, %r15
jmp 0x84789
movq %r15, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq %rdx
leaq (%rdx,%rdx,2), %rax
movq %r15, %rcx
subq %rax, %rcx
movl %ecx, %r13d
xorl $0x3, %r13d
xorl %eax, %eax
testq %rcx, %rcx
movl $0x0, 0x10(%rsp)
cmovel %eax, %r13d
movq 0x18(%rsp), %rbp
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
movq %rbx, %rsi
movq %rbx, %rdx
callq 0x77628
incq %r12
movq %rbp, %rdi
callq 0x6f310
movq %r15, %r14
cmpq %rax, %r12
jb 0x845b5
xorl %eax, %eax
jmp 0x8481d
movq %rbx, %rdi
callq 0x41592
movl %eax, %ebp
movq %rbx, %rdi
callq 0x41572
leaq 0x6f8ad(%rip), %rsi     # 0xf4083
leaq 0x609f2(%rip), %rdx     # 0xe51cf
movq 0x30(%rsp), %rdi
movl %ebp, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x34f1f
jmp 0x84818
movl %r9d, (%rsp)
leaq 0x6f930(%rip), %rsi     # 0xf412b
leaq 0x609cd(%rip), %rdx     # 0xe51cf
movq 0x30(%rsp), %rdi
movl 0x14(%rsp), %ecx
movq %r15, %r8
movl %eax, %r9d
xorl %eax, %eax
callq 0x34f1f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1f1f8
callq 0x1f22f
callq 0x1f1c1