// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/extended/huffcode.c
// Function: gt_huffman_decoder_next
int gt_huffman_decoder_next(GtHuffmanDecoder *huff_decoder,
                            GtArray *symbols,
                            GtUword symbols_to_read,
                            GtError *err)
{
  int had_err = 0,
      bits_to_read = GT_INTWORDSIZE;
  GtUword read_symbols = 0;

  gt_assert((symbols_to_read > 0) && huff_decoder &&
            (gt_array_elem_size(symbols) == sizeof (GtUword)));

  if (huff_decoder->cur_bitseq == huff_decoder->length - 1)
    gt_safe_assign(bits_to_read, (GT_INTWORDSIZE - huff_decoder->pad_length));

  /* reached end of data in a previous call, didn't get reset */
  if (huff_decoder->cur_bitseq == huff_decoder->length) {
    if (!huff_decoder->mem_func)
      return 0;  /*EOF*/
    else if (huff_decoder->mem_func_stat == 0)
      return 0;
    else {
      had_err = -1;
      gt_error_set(err, "huff decoder reached EOF");
    }
  }

  while (!had_err && read_symbols < symbols_to_read) {

    /* huffman was initialized with empty dist */
    gt_assert(huff_decoder->cur_node != NULL);

    if (!had_err && huff_decoder->cur_bit == (GtUword) bits_to_read) {
      huff_decoder->cur_bitseq++;

      if (huff_decoder->cur_bitseq == huff_decoder->length - 1)
        gt_safe_assign(bits_to_read,
                       (GT_INTWORDSIZE - huff_decoder->pad_length));

      /* no next bitseq ask for more if possible */
      if (huff_decoder->cur_bitseq == huff_decoder->length ) {
        if (!huff_decoder->mem_func) {
          return 0;  /*EOF*/
        }

        huff_decoder->mem_func_stat =
          huff_decoder->mem_func(&huff_decoder->bitsequence,
                                 &huff_decoder->length,
                                 &huff_decoder->cur_bit,
                                 &huff_decoder->pad_length,
                                 huff_decoder->info);
        if (huff_decoder->mem_func_stat == -1) {
          gt_error_set(err, "error calling mem_func");
          had_err = huff_decoder->mem_func_stat;
        }
        else if (huff_decoder->mem_func_stat == 0) {
          return huff_decoder->mem_func_stat; /*EOF*/
        }

        huff_decoder->cur_bitseq = 0;
        bits_to_read = GT_INTWORDSIZE;
        if (huff_decoder->cur_bitseq == huff_decoder->length - 1)
          bits_to_read -= huff_decoder->pad_length;
      }
      huff_decoder->cur_bit = 0;
    }

    /* read the next bit */
    else if (!had_err) {
      /* nothing to do if huffman tree has only one node, just advance the bits
       */
      if (huff_decoder->cur_node->leftchild == NULL)
        huff_decoder->cur_bit++;
      else {
        if (GT_ISBITSET(huff_decoder->bitsequence[huff_decoder->cur_bitseq],
                        huff_decoder->cur_bit++))
          huff_decoder->cur_node = huff_decoder->cur_node->rightchild;
        else
          huff_decoder->cur_node = huff_decoder->cur_node->leftchild;
      }
    }

    /* symbol found, reset huffman */
    if (!had_err && huff_decoder->cur_node->leftchild == NULL) {
      gt_array_add(symbols, huff_decoder->cur_node->symbol.symbol);
      read_symbols++;
      huff_decoder->cur_node = huff_decoder->huffman->root_huffman_tree;
    }
  }
  if (!had_err) {
    /* all read, but not eof */
    return 1;
  }
  return had_err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
testq %rdi, %rdi
je 0x15c22e
movq %rdx, %r14
testq %rdx, %rdx
je 0x15c22e
movq %rdi, %r12
movq 0x10(%rsp), %rdi
callq 0xd4aa7
cmpq $0x8, %rax
jne 0x15c22e
movq 0x28(%r12), %rbx
movq 0x40(%r12), %rbp
leaq -0x1(%rbp), %rax
movl $0x40, %r13d
cmpq %rax, %rbx
jne 0x15c019
movq 0x38(%r12), %rax
movl $0x40, %ecx
subl %eax, %r13d
leaq 0x7fffffbf(%rax), %rdx
shrq $0x20, %rdx
cmpq $0x40, %rax
sete %sil
testl %r13d, %r13d
setg %al
orb %sil, %al
cmovel %ecx, %r13d
testq %rdx, %rdx
cmovnel %ecx, %r13d
jne 0x15c233
testb %al, %al
je 0x15c233
xorl %r15d, %r15d
cmpq %rbp, %rbx
jne 0x15c054
xorl %eax, %eax
cmpq $0x0, 0x10(%r12)
je 0x15c211
cmpl $0x0, 0x48(%r12)
je 0x15c211
leaq 0x2318d8(%rip), %rsi    # 0x38d91a
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0x1019cf
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpq %rbp, %rbx
je 0x15c206
leaq 0x40(%r12), %rax
movq %rax, 0x20(%rsp)
leaq 0x30(%r12), %rbp
leaq 0x38(%r12), %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
movq 0x18(%r12), %rax
testq %rax, %rax
je 0x15c224
movq (%rbp), %rcx
movslq %r13d, %rdx
cmpq %rdx, %rcx
jne 0x15c0f1
movq 0x28(%r12), %rax
movq 0x40(%r12), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x28(%r12)
leaq -0x2(%rcx), %rsi
cmpq %rsi, %rax
jne 0x15c126
movq 0x38(%r12), %rsi
movl $0x40, %eax
subl %esi, %eax
leaq 0x7fffffbf(%rsi), %rdi
shrq $0x20, %rdi
cmpq $0x40, %rsi
sete %r8b
testl %eax, %eax
setg %sil
orb %r8b, %sil
cmovel %r13d, %eax
testq %rdi, %rdi
cmovnel %r13d, %eax
jne 0x15c229
testb %sil, %sil
jne 0x15c129
jmp 0x15c229
movq 0x20(%rax), %rdx
testq %rdx, %rdx
je 0x15c16c
movq (%r12), %rsi
movq 0x28(%r12), %rdi
movq (%rsi,%rdi,8), %rsi
leaq 0x1(%rcx), %rdi
movq %rdi, 0x30(%r12)
shlq %cl, %rsi
testq %rsi, %rsi
js 0x15c1b7
movq %rdx, 0x18(%r12)
jmp 0x15c1c0
movl %r13d, %eax
xorl %r15d, %r15d
cmpq %rcx, %rdx
jne 0x15c1aa
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x15c220
movq 0x20(%r12), %r8
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq %rbp, %rdx
movq 0x18(%rsp), %rcx
callq *%rax
movl %eax, 0x48(%r12)
cmpl $-0x1, %eax
je 0x15c175
xorl %r15d, %r15d
testl %eax, %eax
jne 0x15c18d
jmp 0x15c211
incq %rcx
movq %rcx, (%rbp)
jmp 0x15c1c0
movq 0x8(%rsp), %rdi
leaq 0x231644(%rip), %rsi    # 0x38d7c5
xorl %eax, %eax
callq 0x1019cf
movl 0x48(%r12), %r15d
movq $0x0, 0x28(%r12)
movl $0x40, %eax
cmpq $0x1, 0x40(%r12)
jne 0x15c1aa
movq 0x18(%rsp), %rcx
subl (%rcx), %eax
movq $0x0, (%rbp)
movl %eax, %r13d
jmp 0x15c1c3
movq 0x28(%rax), %rax
movq %rax, 0x18(%r12)
xorl %r15d, %r15d
testl %r15d, %r15d
jne 0x15c1f8
movq 0x18(%r12), %rsi
cmpq $0x0, 0x20(%rsi)
jne 0x15c1f8
addq $0x18, %rsi
movl $0x8, %edx
movq 0x10(%rsp), %rdi
callq 0xd49ce
incq %rbx
movq 0x8(%r12), %rax
movq 0x10(%rax), %rax
movq %rax, 0x18(%r12)
testl %r15d, %r15d
jne 0x15c206
cmpq %r14, %rbx
jb 0x15c078
cmpl $0x1, %r15d
adcl $0x0, %r15d
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x15c211
callq 0x4a0df
callq 0x4a0b2
callq 0x4a04c
callq 0x4a085