// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/extended/wtree_encseq.c
// Function: gt_wtree_encseq_rank_rec
static GtUword gt_wtree_encseq_rank_rec(GtWtreeEncseq *we,
                                        GtUword pos,
                                        GtWtreeSymbol sym,
                                        GtUword node_start,
                                        GtUword node_size,
                                        unsigned int alpha_start,
                                        unsigned int alpha_end)
{
  unsigned int middle = GT_DIV2(alpha_start + alpha_end);
  int bit;
  GtUword zero_rank_prefix = 0,
          one_rank_prefix = 0,
          left_child_size,
          rank;
  gt_log_log("alphabet: %u-%u-%u, sym: " GT_WU,
             alpha_start, middle, alpha_end, (GtUword) sym);
  gt_log_log("pos: "GT_WU"", pos);
  gt_assert(pos < node_size);

  if (alpha_start < alpha_end) {
    bit = middle < (unsigned int) sym ? 1 : 0;
    if (node_start != 0)
      zero_rank_prefix =
        gt_compressed_bitsequence_rank_0(we->c_bits, node_start - 1);
    left_child_size =
      gt_compressed_bitsequence_rank_0(we->c_bits, node_start + node_size - 1) -
      zero_rank_prefix;

    if (bit == 0) {
      rank = gt_compressed_bitsequence_rank_0(we->c_bits, node_start + pos) -
        zero_rank_prefix;
      alpha_end = middle;
      node_start += we->parent_instance.members->length;
      node_size = left_child_size;
    }
    else {
      if (node_start != 0)
        one_rank_prefix =
          gt_compressed_bitsequence_rank_1(we->c_bits, node_start - 1);
      rank = gt_compressed_bitsequence_rank_1(we->c_bits, node_start + pos) -
        one_rank_prefix;
      alpha_start = middle + 1;
      node_size =
        gt_compressed_bitsequence_rank_1(we->c_bits,
                                         node_start + node_size - 1) -
        one_rank_prefix;
      node_start +=
        we->parent_instance.members->length + left_child_size;
    }
    gt_log_log("bit: %d, nodesize: "GT_WU"", bit, node_size);
    if (node_size != 0 && rank != 0) {
      pos = rank - 1;
      return gt_wtree_encseq_rank_rec(we, pos, sym,
                                      node_start, node_size, alpha_start,
                                      alpha_end);
    }
    return 0;
  }
  gt_log_log("found: rank="GT_WU"", pos + 1);
  return pos + 1; /* convert position to count */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %r12d
movq %r8, %rbp
movq %rcx, %r14
movq %rdx, 0x10(%rsp)
movq %rsi, %rbx
movq %rdi, 0x8(%rsp)
movl 0x70(%rsp), %r13d
movq $0x0, 0x18(%rsp)
leal (%r12,%r13), %r15d
shrl %r15d
leaq 0x218c09(%rip), %rdi    # 0x3a5ebb
movl %r12d, %esi
movl %r15d, %edx
movl %r13d, %ecx
movq 0x10(%rsp), %r8
xorl %eax, %eax
callq 0x108e02
leaq 0x2018f5(%rip), %rdi    # 0x38ebc3
movq %rbx, %rsi
xorl %eax, %eax
callq 0x108e02
cmpq %rbp, %rbx
jae 0x18d434
cmpl %r13d, %r12d
jae 0x18d407
movq %r12, 0x20(%rsp)
movq %r13, 0x30(%rsp)
cmpl 0x10(%rsp), %r15d
setb 0x7(%rsp)
testq %r14, %r14
je 0x18d31b
movq 0x8(%rsp), %r12
movq 0x38(%r12), %rdi
leaq -0x1(%r14), %rsi
callq 0x12fe86
movq %rax, %r13
jmp 0x18d323
xorl %r13d, %r13d
movq 0x8(%rsp), %r12
movq 0x38(%r12), %rdi
leaq (%r14,%rbp), %rsi
decq %rsi
movq %rsi, 0x28(%rsp)
callq 0x12fe86
movq %rax, %rbp
subq %r13, %rbp
cmpl 0x10(%rsp), %r15d
jae 0x18d362
testq %r14, %r14
je 0x18d385
movq 0x8(%rsp), %r13
movq 0x38(%r13), %rdi
leaq -0x1(%r14), %rsi
callq 0x12fe07
movq %rax, %r12
jmp 0x18d38d
movq 0x38(%r12), %rdi
addq %r14, %rbx
movq %rbx, %rsi
callq 0x12fe86
movq %rax, %rbx
subq %r13, %rbx
movq 0x8(%r12), %rax
movq (%rax), %r12
movl %r15d, %r13d
jmp 0x18d3d3
xorl %r12d, %r12d
movq 0x8(%rsp), %r13
movq 0x38(%r13), %rdi
addq %r14, %rbx
movq %rbx, %rsi
callq 0x12fe07
movq %rax, %rbx
subq %r12, %rbx
incl %r15d
movq 0x38(%r13), %rdi
movq 0x28(%rsp), %rsi
callq 0x12fe07
movq %r12, %rcx
movq %rbp, %r12
subq %rcx, %rax
movq 0x8(%r13), %rcx
addq (%rcx), %r12
movq %rax, %rbp
movl %r15d, %eax
movq %rax, 0x20(%rsp)
movq 0x30(%rsp), %r13
xorl %esi, %esi
movb 0x7(%rsp), %al
movb %al, %sil
leaq 0x218b0e(%rip), %rdi    # 0x3a5ef1
movq %rbp, %rdx
xorl %eax, %eax
callq 0x108e02
testq %rbp, %rbp
je 0x18d420
testq %rbx, %rbx
je 0x18d420
addq %r12, %r14
decq %rbx
movq 0x20(%rsp), %r12
jmp 0x18d2a4
incq %rbx
leaq 0x218af7(%rip), %rdi    # 0x3a5f08
movq %rbx, %rsi
xorl %eax, %eax
callq 0x108e02
movq %rbx, 0x18(%rsp)
movq 0x18(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5af8c