// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/extended/compressed_bitsequence.c
// Function: gt_compressed_bitsequence_select_1
GtUword gt_compressed_bitsequence_select_1(GtCompressedBitsequence *cbs,
                                           GtUword num)
{
  unsigned int block_offset_bits,
               class = cbs->blocksize + 1;
  GtUword block_idx,
          blocks_offset_pos,
          containing_s_block,
          end_s_block, middle_s_block, start_s_block,
          position,
          rank_sum = 0,
          start, start_l, start_r;
  uint64_t block;

  gt_assert(num != 0);
  gt_assert(cbs != NULL);
  gt_assert(num < cbs->num_of_bits);

  /* if larger then max rank1 */
  if (num > (GtUword) gt_compressed_bitsequence_get_variable_field(
                      cbs->superblockranks,
                      (cbs->num_of_superblocks - 1) * cbs->superblockranks_bits,
                      cbs->superblockranks_bits))
    return cbs->num_of_bits;
  if (num <= (GtUword)
      gt_compressed_bitsequence_get_variable_field(cbs->superblockranks, 0,
                                                   cbs->superblockranks_bits)) {
    rank_sum = 0;
    containing_s_block = 0;
    blocks_offset_pos = 0;
  }
  else {
    /* binary search for superblock */
    start_s_block = num / (cbs->blocksize * cbs->superblocksize);
    end_s_block = cbs->num_of_superblocks;
    middle_s_block = GT_DIV2(start_s_block + end_s_block);
    while (start_s_block < end_s_block) {
      start = middle_s_block * cbs->superblockranks_bits;
      if ((GtUword) gt_compressed_bitsequence_get_variable_field(
                                                    cbs->superblockranks, start,
                                                    cbs->superblockranks_bits)
          < num) {
        start_r = start + cbs->superblockranks_bits;
        if ((GtUword) gt_compressed_bitsequence_get_variable_field(
                                                  cbs->superblockranks, start_r,
                                                  cbs->superblockranks_bits)
            < num)
          start_s_block = middle_s_block;
        else
          break;
      }
      else {
        start_l = start - cbs->superblockranks_bits;
        if ((GtUword) gt_compressed_bitsequence_get_variable_field(
                                                  cbs->superblockranks, start_l,
                                                  cbs->superblockranks_bits)
            >= num)
          end_s_block = middle_s_block;
        else {
          middle_s_block--;
          break;
        }
      }
      middle_s_block = GT_DIV2(start_s_block + end_s_block);
    }
    blocks_offset_pos = (GtUword)
      gt_compressed_bitsequence_get_variable_field(
                                   cbs->superblockoffsets,
                                   middle_s_block * cbs->superblockoffsets_bits,
                                   cbs->superblockoffsets_bits);
    rank_sum = (GtUword) gt_compressed_bitsequence_get_variable_field(
                                     cbs->superblockranks,
                                     middle_s_block * cbs->superblockranks_bits,
                                     cbs->superblockranks_bits);
    containing_s_block = middle_s_block+1;
  }

  /* search within superblock */
  for (block_idx = containing_s_block * cbs->superblocksize;
       block_idx < cbs->num_of_blocks;
       block_idx++) {
    class = gt_compressed_bitsequence_get_class(cbs, block_idx);
    if (num <= rank_sum + class)
      break;
    blocks_offset_pos += gt_popcount_tab_offset_bits(cbs->popcount_tab, class);
    rank_sum += class;
  }
  gt_assert(class != cbs->blocksize + 1);
  position = block_idx * cbs->blocksize;
  if (class == cbs->blocksize) {
    position += num - rank_sum - 1;
  }
  else {
    block_offset_bits = gt_popcount_tab_offset_bits(cbs->popcount_tab, class);

    /* search within block */
    block = (uint64_t)
      gt_popcount_tab_get(cbs->popcount_tab, class, (GtUword)
                          gt_compressed_bitsequence_get_variable_field(
                                              cbs->c_offsets, blocks_offset_pos,
                                              block_offset_bits));
    if (block_idx != cbs->num_of_blocks - 1)
      block <<= ((sizeof (block) * CHAR_BIT) - cbs->blocksize);
    else
      block <<= ((sizeof (block) * CHAR_BIT) - cbs->last_block_len);

    position +=
      gt_compressed_bitsequence_select_1_word(block,
                                              (unsigned int) (num - rank_sum));
  }

  return position;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rsi, %rsi
je 0xbfa38
movq %rsi, %r13
movq %rdi, %r14
cmpq %rsi, 0xb0(%rdi)
jbe 0xbfa66
movl 0xd8(%r14), %ebp
movq 0x88(%r14), %rdi
movq 0xc0(%r14), %rsi
decq %rsi
movl 0xe8(%r14), %edx
imulq %rdx, %rsi
callq 0xbfacc
cmpq %r13, %rax
jae 0xbf841
movq 0xb0(%r14), %rbx
jmp 0xbfa26
movq 0x88(%r14), %rdi
movl 0xe8(%r14), %edx
xorl %r12d, %r12d
xorl %esi, %esi
callq 0xbfacc
movl $0x0, %r15d
movl $0x0, %ebx
movq %r13, 0x8(%rsp)
cmpq %r13, %rax
jae 0xbf952
movl 0xec(%r14), %ecx
imull 0xd8(%r14), %ecx
movq 0x8(%rsp), %rax
xorl %edx, %edx
divq %rcx
movq %rax, %rbx
movq 0xc0(%r14), %rax
movq %rbx, %r15
addq %rax, %r15
movq 0x8(%rsp), %r13
shrq %r15
cmpq %rax, %rbx
jae 0xbf915
movq %rax, 0x10(%rsp)
movl 0xe8(%r14), %edx
movq %rdx, %r12
imulq %r15, %r12
movq 0x88(%r14), %rdi
movq %r12, %rsi
callq 0xbfacc
movl 0xe8(%r14), %edx
movq 0x88(%r14), %rdi
cmpq %r13, %rax
jae 0xbf8f4
addq %rdx, %r12
movq %r12, %rsi
callq 0xbfacc
cmpq %r13, %rax
movq 0x10(%rsp), %rax
jb 0xbf90a
jmp 0xbf915
subq %rdx, %r12
movq %r12, %rsi
callq 0xbfacc
cmpq %r13, %rax
jb 0xbf912
movq %r15, %rax
movq %rbx, %r15
movq %r15, %rbx
addq %rax, %r15
jmp 0xbf8a0
decq %r15
movq 0x80(%r14), %rdi
movl 0xe4(%r14), %edx
movq %rdx, %rsi
imulq %r15, %rsi
callq 0xbfacc
movq %rax, %r12
movq 0x88(%r14), %rdi
movl 0xe8(%r14), %edx
movq %rdx, %rsi
imulq %r15, %rsi
callq 0xbfacc
movq %rax, %rbx
incq %r15
incl %ebp
movl 0xec(%r14), %r13d
imulq %r15, %r13
cmpq 0xb8(%r14), %r13
jae 0xbf99a
movq %r14, %rdi
movq %r13, %rsi
callq 0xbfb43
movl %eax, %ebp
movl %eax, %r15d
addq %rbx, %r15
cmpq 0x8(%rsp), %r15
jae 0xbf99a
movq 0x68(%r14), %rdi
movl %ebp, %esi
callq 0x1047ab
movl %eax, %eax
addq %rax, %r12
incq %r13
movq %r15, %rbx
jmp 0xbf95f
movl 0xd8(%r14), %r15d
leal 0x1(%r15), %eax
cmpl %eax, %ebp
je 0xbfa94
movl %r15d, %eax
imulq %r13, %r15
cmpl %eax, %ebp
jne 0xbf9c2
notq %rbx
addq 0x8(%rsp), %rbx
jmp 0xbfa23
movq 0x68(%r14), %rdi
movl %ebp, %esi
callq 0x1047ab
movq 0x68(%r14), %rcx
movq %rcx, 0x10(%rsp)
movq 0x70(%r14), %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0xbfacc
movq 0x10(%rsp), %rdi
movl %ebp, %esi
movq %rax, %rdx
callq 0x104561
movq 0xb8(%r14), %rcx
decq %rcx
xorl %edx, %edx
cmpq %r13, %rcx
sete %dl
xorl %ecx, %ecx
subb 0xd8(%r14,%rdx,8), %cl
shlq %cl, %rax
movq 0x8(%rsp), %rsi
subl %ebx, %esi
movq %rax, %rdi
callq 0xbfbcb
movl %eax, %ebx
addq %r15, %rbx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x378529(%rip), %rax    # 0x437f68
movq (%rax), %rdi
leaq 0x2255ca(%rip), %rsi    # 0x2e5013
leaq 0x258edb(%rip), %rdx    # 0x31892b
leaq 0x258edd(%rip), %rcx    # 0x318934
leaq 0x258dd8(%rip), %r8     # 0x318836
movl $0x293, %r9d            # imm = 0x293
jmp 0xbfac0
movq 0x3784fb(%rip), %rax    # 0x437f68
movq (%rax), %rdi
leaq 0x22559c(%rip), %rsi    # 0x2e5013
leaq 0x258ed9(%rip), %rdx    # 0x318957
leaq 0x258eaf(%rip), %rcx    # 0x318934
leaq 0x258daa(%rip), %r8     # 0x318836
movl $0x295, %r9d            # imm = 0x295
jmp 0xbfac0
movq 0x3784cd(%rip), %rax    # 0x437f68
movq (%rax), %rdi
leaq 0x22556e(%rip), %rsi    # 0x2e5013
leaq 0x258ec2(%rip), %rdx    # 0x31896e
leaq 0x258e81(%rip), %rcx    # 0x318934
leaq 0x258d7c(%rip), %r8     # 0x318836
movl $0x2dc, %r9d            # imm = 0x2DC
xorl %eax, %eax
callq 0x17a50
callq 0x17390