// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/bzip2-1.0.6/huffman.c
// Function: BZ2_hbMakeCodeLengths
void BZ2_hbMakeCodeLengths ( UChar *len, 
                             Int32 *freq,
                             Int32 alphaSize,
                             Int32 maxLen )
{
   /*--
      Nodes and heap entries run from 1.  Entry 0
      for both the heap and nodes is a sentinel.
   --*/
   Int32 nNodes, nHeap, n1, n2, i, j, k;
   Bool  tooLong;

   Int32 heap   [ BZ_MAX_ALPHA_SIZE + 2 ];
   Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
   Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ]; 

   for (i = 0; i < alphaSize; i++)
      weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;

   while (True) {

      nNodes = alphaSize;
      nHeap = 0;

      heap[0] = 0;
      weight[0] = 0;
      parent[0] = -2;

      for (i = 1; i <= alphaSize; i++) {
         parent[i] = -1;
         nHeap++;
         heap[nHeap] = i;
         UPHEAP(nHeap);
      }

      AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
   
      while (nHeap > 1) {
         n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
         n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
         nNodes++;
         parent[n1] = parent[n2] = nNodes;
         weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
         parent[nNodes] = -1;
         nHeap++;
         heap[nHeap] = nNodes;
         UPHEAP(nHeap);
      }

      AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );

      tooLong = False;
      for (i = 1; i <= alphaSize; i++) {
         j = 0;
         k = i;
         while (parent[k] >= 0) { k = parent[k]; j++; }
         len[i-1] = j;
         if (j > maxLen) tooLong = True;
      }
      
      if (! tooLong) break;

      /* 17 Oct 04: keep-going condition for the following loop used
         to be 'i < alphaSize', which missed the last element,
         theoretically leading to the possibility of the compressor
         looping.  However, this count-scaling step is only needed if
         one of the generated Huffman code words is longer than
         maxLen, which up to and including version 1.0.2 was 20 bits,
         which is extremely unlikely.  In version 1.0.3 maxLen was
         changed to 17 bits, which has minimal effect on compression
         ratio, but does mean this scaling step is used from time to
         time, enough to verify that it works.

         This means that bzip2-1.0.3 and later will only produce
         Huffman codes with a maximum length of 17 bits.  However, in
         order to preserve backwards compatibility with bitstreams
         produced by versions pre-1.0.3, the decompressor must still
         handle lengths of up to 20. */

      for (i = 1; i <= alphaSize; i++) {
         j = weight[i] >> 8;
         j = 1 + (j / 2);
         weight[i] = j << 8;
      }
   }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1458, %rsp           # imm = 0x1458
movl %ecx, 0x4(%rsp)
movq %rdi, 0x10(%rsp)
xorl %eax, %eax
testl %edx, %edx
movl $0x0, %r15d
movl %edx, (%rsp)
cmovgl %edx, %r15d
movl $0x100, %ecx            # imm = 0x100
cmpq %rax, %r15
je 0xc6736
movl (%rsi,%rax,4), %edx
movl %edx, %edi
shll $0x8, %edi
testl %edx, %edx
cmovel %ecx, %edi
movl %edi, 0x434(%rsp,%rax,4)
incq %rax
jmp 0xc6718
movslq (%rsp), %rax
movq %rax, 0x8(%rsp)
leal 0x1(%r15), %eax
movq %rax, 0x18(%rsp)
pushq $0x1
popq %r8
andl $0x0, 0x20(%rsp)
andl $0x0, 0x430(%rsp)
movl $0xfffffffe, 0xc40(%rsp) # imm = 0xFFFFFFFE
xorl %eax, %eax
movq %r8, %rcx
cmpq %r15, %rax
je 0xc67a9
orl $-0x1, 0xc40(%rsp,%rcx,4)
movl %ecx, 0x24(%rsp,%rax,4)
incq %rax
movl 0x430(%rsp,%rcx,4), %edx
movl %eax, %esi
movl %esi, %edi
shrl %esi
movslq 0x20(%rsp,%rsi,4), %r8
cmpl 0x430(%rsp,%r8,4), %edx
jge 0xc67a0
movl %r8d, 0x20(%rsp,%rdi,4)
jmp 0xc6786
movl %ecx, 0x20(%rsp,%rdi,4)
incq %rcx
jmp 0xc6769
cmpl $0x104, (%rsp)          # imm = 0x104
jl 0xc67bc
movl $0x7d1, %edi            # imm = 0x7D1
callq 0x77bd0
movq 0x8(%rsp), %rdx
movq %r15, %rdi
cmpq $0x2, %rdi
jl 0xc695a
movslq 0x24(%rsp), %rsi
movl 0x20(%rsp,%rdi,4), %r8d
movl %r8d, 0x24(%rsp)
leaq -0x1(%rdi), %rax
movl %eax, %ecx
movslq %r8d, %r9
pushq $0x1
popq %r11
movslq %r11d, %r10
addl %r11d, %r11d
cmpl %edi, %r11d
jge 0xc6849
cmpl %ecx, %r11d
jge 0xc6828
movl %r11d, %r14d
orl $0x1, %r14d
movslq %r14d, %rbx
movslq 0x20(%rsp,%rbx,4), %rbx
movl 0x430(%rsp,%rbx,4), %ebx
movslq %r11d, %r12
movslq 0x20(%rsp,%r12,4), %r12
cmpl 0x430(%rsp,%r12,4), %ebx
jl 0xc6825
movl %r11d, %r14d
movl %r14d, %r11d
movl 0x430(%rsp,%r9,4), %r14d
movslq %r11d, %rbx
movslq 0x20(%rsp,%rbx,4), %rbx
cmpl 0x430(%rsp,%rbx,4), %r14d
jl 0xc6849
movl %ebx, 0x20(%rsp,%r10,4)
jmp 0xc67ea
movl %r8d, 0x20(%rsp,%r10,4)
movslq 0x24(%rsp), %r9
movl %eax, %r8d
movl 0x20(%rsp,%r8,4), %r10d
movl %r10d, 0x24(%rsp)
addl $-0x2, %edi
movslq %r10d, %r11
pushq $0x1
popq %r12
movl %r12d, %ebx
movslq %r12d, %r14
leal (%rbx,%rbx), %r12d
cmpl %edi, %r12d
jg 0xc68c9
jge 0xc68a9
movl %r12d, %ebx
orl $0x1, %ebx
movslq %ebx, %r13
movslq 0x20(%rsp,%r13,4), %r13
movl 0x430(%rsp,%r13,4), %r13d
movslq %r12d, %rbp
movslq 0x20(%rsp,%rbp,4), %rbp
cmpl 0x430(%rsp,%rbp,4), %r13d
jl 0xc68a6
movl %r12d, %ebx
movl %ebx, %r12d
movl 0x430(%rsp,%r11,4), %ebp
movslq %r12d, %rbx
movslq 0x20(%rsp,%rbx,4), %rbx
cmpl 0x430(%rsp,%rbx,4), %ebp
jl 0xc68c9
movl %ebx, 0x20(%rsp,%r14,4)
jmp 0xc686a
movl %r10d, 0x20(%rsp,%r14,4)
leaq 0x1(%rdx), %rdi
movl %edi, 0xc40(%rsp,%r9,4)
movl 0x430(%rsp,%rsi,4), %r10d
movl %r10d, %r11d
movl $0xffffff00, %ebp       # imm = 0xFFFFFF00
andl %ebp, %r11d
movl 0x430(%rsp,%r9,4), %r9d
movl %r9d, %ebx
andl %ebp, %ebx
addl %r11d, %ebx
movl $0xff, %r11d
andl %r11d, %r10d
andl %r11d, %r9d
cmpl %r9d, %r10d
cmoval %r10d, %r9d
movl %edi, 0xc40(%rsp,%rsi,4)
incl %r9d
orl %ebx, %r9d
movl %r9d, 0x434(%rsp,%rdx,4)
orl $-0x1, 0xc44(%rsp,%rdx,4)
movl %edi, 0x20(%rsp,%r8,4)
movl %ecx, %edx
shrl %ecx
movslq 0x20(%rsp,%rcx,4), %rsi
cmpl 0x430(%rsp,%rsi,4), %r9d
jge 0xc694b
movl %esi, 0x20(%rsp,%rdx,4)
jmp 0xc6932
movl %edi, 0x20(%rsp,%rdx,4)
movq %rdi, %rdx
movq %rax, %rdi
jmp 0xc67c4
cmpq $0x204, %rdx            # imm = 0x204
jl 0xc696d
movl $0x7d2, %edi            # imm = 0x7D2
callq 0x77bd0
xorl %eax, %eax
pushq $0x1
popq %r8
movq %r8, %rcx
movl 0x4(%rsp), %edi
cmpq 0x18(%rsp), %rcx
je 0xc69ae
pushq $-0x1
popq %rdx
movl %ecx, %esi
movl %esi, %esi
movl 0xc40(%rsp,%rsi,4), %esi
incl %edx
testl %esi, %esi
jns 0xc6986
movq 0x10(%rsp), %rsi
movb %dl, -0x1(%rsi,%rcx)
cmpl %edi, %edx
movzbl %al, %eax
pushq $0x1
popq %rdx
cmovgl %edx, %eax
incq %rcx
jmp 0xc697a
testb %al, %al
je 0xc69e1
xorl %ecx, %ecx
pushq $0x2
popq %rsi
cmpq %rcx, %r15
je 0xc674c
movl 0x434(%rsp,%rcx,4), %eax
sarl $0x8, %eax
cltd
idivl %esi
shll $0x8, %eax
addl $0x100, %eax            # imm = 0x100
movl %eax, 0x434(%rsp,%rcx,4)
incq %rcx
jmp 0xc69b7
addq $0x1458, %rsp           # imm = 0x1458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq