// File: /workspace/github_c_makefile_star2_O3/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 $0x1468, %rsp           # imm = 0x1468
movl %edx, %ebp
testl %edx, %edx
jle 0xddc85
xorl %eax, %eax
movl $0x100, %r8d            # imm = 0x100
movl (%rsi,%rax,4), %r9d
movl %r9d, %r10d
shll $0x8, %r10d
testl %r9d, %r9d
cmovel %r8d, %r10d
movl %r10d, 0x444(%rsp,%rax,4)
incq %rax
cmpq %rax, %rbp
jne 0xddc63
xorl %eax, %eax
movl %eax, 0x30(%rsp)
movl %eax, 0x440(%rsp)
movl $0xfffffffe, 0xc50(%rsp) # imm = 0xFFFFFFFE
testl %edx, %edx
jle 0xde070
leaq (,%rbp,4), %rax
movq %rax, 0x20(%rsp)
leal 0x1(%rdx), %r12d
movslq %edx, %rax
movq %rax, 0x18(%rsp)
movl $0xff, %r15d
movl $0xffffff00, %r13d      # imm = 0xFFFFFF00
movl %ecx, 0x14(%rsp)
movq %rdx, 0x8(%rsp)
movq %rdi, 0x28(%rsp)
leaq 0xc54(%rsp), %rdi
movl $0xff, %esi
movq 0x20(%rsp), %rdx
callq 0xb2f0
movl $0x1, %eax
xorl %ecx, %ecx
movl %eax, 0x34(%rsp,%rcx,4)
incq %rcx
movl 0x440(%rsp,%rax,4), %edx
movl %ecx, %esi
shrl %esi
movslq 0x30(%rsp,%rsi,4), %rdi
cmpl 0x440(%rsp,%rdi,4), %edx
jge 0xddd3b
movl %ecx, %r9d
movl %esi, %r8d
movl %r9d, %esi
movl %edi, 0x30(%rsp,%rsi,4)
movl %r8d, %esi
shrl %esi
movslq 0x30(%rsp,%rsi,4), %rdi
movl %r8d, %r9d
cmpl 0x440(%rsp,%rdi,4), %edx
jl 0xddd19
jmp 0xddd3e
movl %ecx, %r8d
movl %r8d, %edx
movl %eax, 0x30(%rsp,%rdx,4)
incq %rax
cmpq %rbp, %rcx
jne 0xddcf6
movq 0x8(%rsp), %rdx
cmpl $0x104, %edx            # imm = 0x104
jb 0xddd69
movl $0x7d1, %edi            # imm = 0x7D1
callq 0x97178
movq 0x8(%rsp), %rdx
cmpl $0x2, %edx
jl 0xddfab
movq %rbp, %rax
movq 0x18(%rsp), %rdi
movq %rdi, %rcx
movslq 0x34(%rsp), %rsi
movl 0x30(%rsp,%rax,4), %edi
movl %edi, 0x34(%rsp)
leaq -0x1(%rax), %rdx
cmpq $0x2, %rax
jbe 0xdde0b
movslq %edi, %r8
movl 0x440(%rsp,%r8,4), %r8d
movl $0x1, %r9d
movl $0x2, %r11d
movslq %r11d, %r14
cmpq %r14, %rdx
jle 0xddde0
movl %r14d, %r10d
orl $0x1, %r10d
movslq %r10d, %rbx
movslq 0x30(%rsp,%rbx,4), %rbx
movl 0x440(%rsp,%rbx,4), %ebx
movslq 0x30(%rsp,%r14,4), %r14
cmpl 0x440(%rsp,%r14,4), %ebx
jl 0xddddb
movl %r11d, %r10d
movslq %r10d, %r14
jmp 0xddde3
movl %r11d, %r10d
movslq 0x30(%rsp,%r14,4), %r11
cmpl 0x440(%rsp,%r11,4), %r8d
jl 0xdde24
movslq %r9d, %r9
movl %r11d, 0x30(%rsp,%r9,4)
leal (%r10,%r10), %r11d
movslq %r11d, %rbx
movl %r10d, %r9d
cmpq %rbx, %rax
jg 0xdddab
jmp 0xdde27
movl %edi, 0x34(%rsp)
movl 0x30(%rsp,%rdx,4), %r8d
movl %r8d, 0x34(%rsp)
movl $0x1, %r9d
jmp 0xddeda
movl %r9d, %r10d
movslq %r10d, %r8
movl %edi, 0x30(%rsp,%r8,4)
movl 0x34(%rsp), %edi
movl 0x30(%rsp,%rdx,4), %r8d
movl %r8d, 0x34(%rsp)
movl $0x1, %r9d
cmpq $0x4, %rax
jl 0xddeda
movslq %r8d, %r10
leaq -0x2(%rax), %r9
movl 0x440(%rsp,%r10,4), %r10d
movl $0x1, %r11d
movl $0x2, %r14d
movslq %r14d, %r13
cmpq %r13, %r9
jle 0xdde9d
movl %r13d, %ebx
orl $0x1, %ebx
movslq %ebx, %r15
movslq 0x30(%rsp,%r15,4), %r15
movl 0x440(%rsp,%r15,4), %r15d
movslq 0x30(%rsp,%r13,4), %r13
cmpl 0x440(%rsp,%r13,4), %r15d
jl 0xdde98
movl %r14d, %ebx
movslq %ebx, %r13
jmp 0xddea0
movl %r14d, %ebx
movslq 0x30(%rsp,%r13,4), %r14
cmpl 0x440(%rsp,%r14,4), %r10d
jl 0xddec8
movslq %r11d, %r11
movl %r14d, 0x30(%rsp,%r11,4)
leal (%rbx,%rbx), %r14d
movslq %r14d, %r15
movl %ebx, %r11d
cmpq %r15, %r9
jge 0xdde68
jmp 0xddecb
movl %r11d, %ebx
movslq %ebx, %r9
movl $0xff, %r15d
movl $0xffffff00, %r13d      # imm = 0xFFFFFF00
movl %r8d, 0x30(%rsp,%r9,4)
movslq %edi, %r9
movl 0x440(%rsp,%rsi,4), %edi
movl %edi, %r10d
andl %r13d, %r10d
movl 0x440(%rsp,%r9,4), %r8d
movl %r8d, %r11d
andl %r13d, %r11d
addl %r10d, %r11d
andl %r15d, %edi
andl %r15d, %r8d
cmpl %r8d, %edi
cmoval %edi, %r8d
leaq 0x1(%rcx), %rdi
movl %edi, 0xc50(%rsp,%r9,4)
movl %edi, 0xc50(%rsp,%rsi,4)
incl %r8d
orl %r11d, %r8d
movl %r8d, 0x444(%rsp,%rcx,4)
movl $0xffffffff, 0xc54(%rsp,%rcx,4) # imm = 0xFFFFFFFF
movl %edi, 0x30(%rsp,%rdx,4)
movl %edx, %esi
shrl %esi
movslq 0x30(%rsp,%rsi,4), %r9
cmpl 0x440(%rsp,%r9,4), %r8d
jge 0xddf77
movl %edx, %r11d
movl %esi, %r10d
movl %r11d, %esi
movl %r9d, 0x30(%rsp,%rsi,4)
movl %r10d, %esi
shrl %esi
movslq 0x30(%rsp,%rsi,4), %r9
movl %r10d, %r11d
cmpl 0x440(%rsp,%r9,4), %r8d
jl 0xddf53
jmp 0xddf7a
movl %edx, %r10d
movl %r10d, %esi
movl %edi, 0x30(%rsp,%rsi,4)
cmpq $0x2, %rax
movq %rdx, %rax
ja 0xddd7a
cmpq $0x203, %rcx            # imm = 0x203
movq 0x8(%rsp), %rdx
jl 0xddfab
movl $0x7d2, %edi            # imm = 0x7D2
callq 0x97178
movq 0x8(%rsp), %rdx
testl %edx, %edx
movl 0x14(%rsp), %ecx
movq 0x28(%rsp), %rdi
movl $0x1, %r10d
jle 0xde070
movl $0x1, %r9d
xorl %eax, %eax
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl %r9d, %esi
movl %esi, %esi
movl 0xc50(%rsp,%rsi,4), %esi
incl %r8d
testl %esi, %esi
jns 0xddfd3
movb %r8b, -0x1(%rdi,%r9)
cmpl %ecx, %r8d
movzbl %al, %eax
cmovgl %r10d, %eax
incq %r9
cmpq %r12, %r9
jne 0xddfca
testb %al, %al
je 0xde070
testl %edx, %edx
jle 0xde058
movl $0x1, %eax
movl 0x440(%rsp,%rax,4), %esi
movl %esi, %r8d
sarl $0x8, %r8d
shrl $0x1f, %esi
addl %r8d, %esi
andl $-0x2, %esi
shll $0x7, %esi
addl $0x100, %esi            # imm = 0x100
movl %esi, 0x440(%rsp,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0xde007
xorl %eax, %eax
movl %eax, 0x30(%rsp)
movl %eax, 0x440(%rsp)
movl $0xfffffffe, 0xc50(%rsp) # imm = 0xFFFFFFFE
testl %edx, %edx
jg 0xddcd8
jmp 0xde070
xorl %eax, %eax
movl %eax, 0x30(%rsp)
movl %eax, 0x440(%rsp)
movl $0xfffffffe, 0xc50(%rsp) # imm = 0xFFFFFFFE
addq $0x1468, %rsp           # imm = 0x1468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq