// File: /workspace/github_c_makefile_star2_O1/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 %ecx, 0x14(%rsp)
movq %rdi, %r15
movl %edx, %r8d
movq %rdx, 0x8(%rsp)
testl %edx, %edx
jle 0xdd5ab
xorl %eax, %eax
movl $0x100, %ecx            # imm = 0x100
movl (%rsi,%rax,4), %edx
movl %edx, %edi
shll $0x8, %edi
testl %edx, %edx
cmovel %ecx, %edi
movl %edi, 0x444(%rsp,%rax,4)
incq %rax
cmpq %rax, %r8
jne 0xdd58f
leaq (,%r8,4), %rax
movq %rax, 0x20(%rsp)
movq 0x8(%rsp), %rax
movslq %eax, %rcx
movq %rcx, 0x18(%rsp)
leal 0x1(%rax), %r12d
movl $0xffffff00, %ebx       # imm = 0xFFFFFF00
movq %r8, 0x28(%rsp)
xorl %eax, %eax
movl %eax, 0x30(%rsp)
movl %eax, 0x440(%rsp)
movl $0xfffffffe, 0xc50(%rsp) # imm = 0xFFFFFFFE
movl $0x0, %r13d
cmpl $0x0, 0x8(%rsp)
jle 0xdd670
leaq 0xc54(%rsp), %rdi
movl $0xff, %esi
movq 0x20(%rsp), %rdx
callq 0xb2f0
movq 0x28(%rsp), %r9
movl $0x1, %eax
xorl %r13d, %r13d
movl %eax, 0x34(%rsp,%r13,4)
incq %r13
movl 0x440(%rsp,%rax,4), %ecx
movl %r13d, %edx
shrl %edx
movslq 0x30(%rsp,%rdx,4), %rsi
movl %r13d, %edi
cmpl 0x440(%rsp,%rsi,4), %ecx
jge 0xdd662
movl %r13d, %r8d
movl %edx, %edi
movl %r8d, %edx
movl %esi, 0x30(%rsp,%rdx,4)
movl %edi, %edx
shrl %edx
movslq 0x30(%rsp,%rdx,4), %rsi
movl %edi, %r8d
cmpl 0x440(%rsp,%rsi,4), %ecx
jl 0xdd644
movl %edi, %ecx
movl %eax, 0x30(%rsp,%rcx,4)
incq %rax
cmpq %r9, %r13
jne 0xdd61c
cmpl $0x104, %r13d           # imm = 0x104
jb 0xdd683
movl $0x7d1, %edi            # imm = 0x7D1
callq 0x97c40
movq 0x8(%rsp), %rdi
cmpl $0x2, %r13d
jl 0xdd890
movl %r13d, %eax
movq 0x18(%rsp), %rdx
movslq 0x34(%rsp), %rsi
movl 0x30(%rsp,%rax,4), %r8d
movl %r8d, 0x34(%rsp)
leaq -0x1(%rax), %rcx
movl $0x1, %edi
movl $0x1, %r9d
cmpq $0x3, %rax
jl 0xdd736
movslq %r8d, %r9
movl 0x440(%rsp,%r9,4), %r9d
movl $0x1, %r10d
movl $0x2, %r13d
movslq %r13d, %r14
cmpq %r14, %rcx
jle 0xdd702
movl %r14d, %r11d
orl $0x1, %r11d
movslq %r11d, %rbp
movslq 0x30(%rsp,%rbp,4), %rbp
movl 0x440(%rsp,%rbp,4), %ebp
movslq 0x30(%rsp,%r14,4), %r14
cmpl 0x440(%rsp,%r14,4), %ebp
jl 0xdd705
movl %r13d, %r11d
movslq %r11d, %r14
movslq 0x30(%rsp,%r14,4), %r14
cmpl 0x440(%rsp,%r14,4), %r9d
jl 0xdd730
movslq %r10d, %r10
movl %r14d, 0x30(%rsp,%r10,4)
leal (%r11,%r11), %r13d
movslq %r13d, %r14
movl %r11d, %r10d
cmpq %r14, %rax
jg 0xdd6d5
jmp 0xdd733
movl %r10d, %r11d
movslq %r11d, %r9
movl %r8d, 0x30(%rsp,%r9,4)
movslq 0x34(%rsp), %r8
movl 0x30(%rsp,%rcx,4), %r9d
movl %r9d, 0x34(%rsp)
cmpq $0x4, %rax
jl 0xdd7d8
movslq %r9d, %r10
leaq -0x2(%rax), %rdi
movl 0x440(%rsp,%r10,4), %r10d
movl $0x1, %r11d
movl $0x2, %r14d
movslq %r14d, %rbp
cmpq %rbp, %rdi
jle 0xdd79b
movl %ebp, %r13d
orl $0x1, %r13d
movslq %r13d, %rbx
movslq 0x30(%rsp,%rbx,4), %rbx
movl 0x440(%rsp,%rbx,4), %ebx
movslq 0x30(%rsp,%rbp,4), %rbp
cmpl 0x440(%rsp,%rbp,4), %ebx
jl 0xdd79e
movl %r14d, %r13d
movslq %r13d, %rbx
movslq 0x30(%rsp,%rbx,4), %r14
cmpl 0x440(%rsp,%r14,4), %r10d
jl 0xdd7cd
movslq %r11d, %r11
movl %r14d, 0x30(%rsp,%r11,4)
leal (,%r13,2), %r14d
movslq %r14d, %rbx
movl %r13d, %r11d
cmpq %rbx, %rdi
jge 0xdd76f
jmp 0xdd7d0
movl %r11d, %r13d
movslq %r13d, %rdi
movl $0xffffff00, %ebx       # imm = 0xFFFFFF00
movl %r9d, 0x30(%rsp,%rdi,4)
leaq 0x1(%rdx), %rdi
movl %edi, 0xc50(%rsp,%r8,4)
movl 0x440(%rsp,%rsi,4), %r9d
movl %r9d, %r10d
andl %ebx, %r10d
movl 0x440(%rsp,%r8,4), %r8d
movl %r8d, %r11d
andl %ebx, %r11d
addl %r10d, %r11d
movl $0xff, %r10d
andl %r10d, %r9d
andl %r10d, %r8d
cmpl %r8d, %r9d
cmoval %r9d, %r8d
movl %edi, 0xc50(%rsp,%rsi,4)
incl %r8d
orl %r11d, %r8d
movl %r8d, 0x444(%rsp,%rdx,4)
movl $0xffffffff, 0xc54(%rsp,%rdx,4) # imm = 0xFFFFFFFF
movl %edi, 0x30(%rsp,%rcx,4)
movl %ecx, %edx
shrl %edx
movslq 0x30(%rsp,%rdx,4), %rsi
movl %ecx, %r9d
cmpl 0x440(%rsp,%rsi,4), %r8d
jge 0xdd879
movl %ecx, %r10d
movl %edx, %r9d
movl %r10d, %edx
movl %esi, 0x30(%rsp,%rdx,4)
movl %r9d, %edx
shrl %edx
movslq 0x30(%rsp,%rdx,4), %rsi
movl %r9d, %r10d
cmpl 0x440(%rsp,%rsi,4), %r8d
jl 0xdd858
movl %r9d, %edx
movl %edi, 0x30(%rsp,%rdx,4)
movq %rdi, %rdx
cmpq $0x2, %rax
movq %rcx, %rax
jg 0xdd69a
cmpl $0x204, %edi            # imm = 0x204
jl 0xdd8a2
movl $0x7d2, %edi            # imm = 0x7D2
callq 0x97c40
cmpl $0x0, 0x8(%rsp)
movl $0x1, %r8d
jle 0xdd92b
movl $0x1, %ecx
xorl %eax, %eax
movl 0x14(%rsp), %edi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %ecx, %esi
movl %esi, %esi
movl 0xc50(%rsp,%rsi,4), %esi
incl %edx
testl %esi, %esi
jns 0xdd8c1
movb %dl, -0x1(%r15,%rcx)
cmpl %edi, %edx
movzbl %al, %eax
cmovgl %r8d, %eax
incq %rcx
cmpq %r12, %rcx
jne 0xdd8ba
testb %al, %al
je 0xdd92b
cmpl $0x0, 0x8(%rsp)
jle 0xdd5d3
movl $0x1, %eax
movl 0x440(%rsp,%rax,4), %ecx
movl %ecx, %edx
sarl $0x8, %edx
shrl $0x1f, %ecx
addl %edx, %ecx
andl $-0x2, %ecx
shll $0x7, %ecx
addl $0x100, %ecx            # imm = 0x100
movl %ecx, 0x440(%rsp,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0xdd8fa
jmp 0xdd5d3
addq $0x1468, %rsp           # imm = 0x1468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq