// File: /workspace/github_c_makefile_star2_O3/thegenemyers[P]DASCRUBBER/QV.c
// Function: Huffman
static HScheme *Huffman(uint64 *hist, HScheme *inscheme)
{ HScheme *scheme;
  HTree   *heap[259];
  HTree    node[512];
  int      hsize;
  HTree   *lft, *rgt;
  int      value, range;
  int     i;

  scheme = (HScheme *) Malloc(sizeof(HScheme),"Allocating Huffman scheme record");
  if (scheme == NULL)
    return (NULL);

  hsize = 0;                        //  Load heap
  value = 0;
  if (inscheme != NULL)
    { node[0].count = 0;
      node[0].lft   = (HTree *) (uint64) 255;
      node[0].rgt   = NULL;
      heap[++hsize] = node+(value++);
    }
  for (i = 0; i < 256; i++)
    if (hist[i] > 0)
      { if (inscheme != NULL && (inscheme->codelens[i] > HUFF_CUTOFF || i == 255))
          node[0].count += hist[i];
        else
          { node[value].count = hist[i];
            node[value].lft   = (HTree *) (uint64) i;
            node[value].rgt   = NULL;
            heap[++hsize] = node+(value++);
          }
      }

  for (i = hsize/2; i >= 1; i--)    //  Establish heap property
    Reheap(i,heap,hsize);

  range = value;                    //   Merge pairs with smallest count until have a tree
  for (i = 1; i < value; i++)
    { lft = heap[1];
      heap[1] = heap[hsize--];
      Reheap(1,heap,hsize);
      rgt = heap[1];
      node[range].lft = lft;
      node[range].rgt = rgt;
      node[range].count = lft->count + rgt->count;
      heap[1] = node+(range++);
      Reheap(1,heap,hsize);
    }

  for (i = 0; i < 256; i++)        //  Build the code table
    { scheme->codebits[i] = 0;
      scheme->codelens[i] = 0;
    }

  Build_Table(node+(range-1),0,0,scheme->codebits,scheme->codelens);

  if (inscheme != NULL)            //  Set scheme type and if truncated (2), map truncated codes
    { scheme->type = 2;            //    to code and length for 255
      for (i = 0; i < 255; i++)
        if (inscheme->codelens[i] > HUFF_CUTOFF || scheme->codelens[i] > HUFF_CUTOFF)
          { scheme->codelens[i] = scheme->codelens[255];
            scheme->codebits[i] = scheme->codebits[255];
          }
    }
  else
    { scheme->type = 0;
      for (i = 0; i < 256; i++)
        { if (scheme->codelens[i] > HUFF_CUTOFF)
            scheme->type = 1;
        }
    }

  return (scheme);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3848, %rsp           # imm = 0x3848
movq %rsi, %r14
movq %rdi, %r12
leaq 0x3bde(%rip), %rsi      # 0xc438
movl $0x40804, %edi          # imm = 0x40804
callq 0x2af5
movq %rax, %rbx
testq %rax, %rax
je 0x8a8d
testq %r14, %r14
movq %rax, 0x18(%rsp)
je 0x889d
leaq 0x840(%rsp), %rax
movq $0xff, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq %rax, 0x28(%rsp)
movl $0x1, %r15d
jmp 0x88a0
xorl %r15d, %r15d
xorl %eax, %eax
movl %r15d, %ebp
movq (%r12,%rax,8), %rcx
testq %rcx, %rcx
je 0x88fe
testq %r14, %r14
je 0x88c6
cmpq $0xff, %rax
je 0x88f6
cmpl $0x11, 0x404(%r14,%rax,4)
jge 0x88f6
movslq %r15d, %r15
leaq (%r15,%r15,2), %rdx
leaq (%rsp,%rdx,8), %rdx
addq $0x840, %rdx            # imm = 0x840
movq %rcx, 0x10(%rdx)
movq %rax, (%rdx)
movq $0x0, 0x8(%rdx)
incl %r15d
movslq %ebp, %rcx
incl %ebp
movq %rdx, 0x28(%rsp,%rcx,8)
jmp 0x88fe
addq %rcx, 0x850(%rsp)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x88a5
cmpl $0x2, %ebp
jl 0x8934
movl %ebp, %r12d
shrl %r12d
leaq 0x20(%rsp), %r13
movl %r12d, %edi
movq %r13, %rsi
movl %ebp, %edx
callq 0xa4fc
leal -0x1(%r12), %eax
cmpl $0x1, %r12d
movl %eax, %r12d
ja 0x8919
cmpl $0x2, %r15d
jl 0x89e5
movq %rbx, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movl %r15d, %eax
movslq %ebp, %rcx
leal -0x1(,%r15,2), %edx
movl %edx, 0x4(%rsp)
leaq (%rsp,%rcx,8), %r13
addq $0x20, %r13
decl %ebp
leaq (%rax,%rax,2), %rax
leaq (%rsp,%rax,8), %r14
addq $0x840, %r14            # imm = 0x840
decl %r15d
movq 0x28(%rsp), %r12
movq (%r13), %rax
movq %rax, 0x28(%rsp)
movl $0x1, %edi
leaq 0x20(%rsp), %rbx
movq %rbx, %rsi
movl %ebp, %edx
callq 0xa4fc
movq 0x28(%rsp), %rax
movq %r12, (%r14)
movq %rax, 0x8(%r14)
movq 0x10(%rax), %rax
addq 0x10(%r12), %rax
movq %rax, 0x10(%r14)
movq %r14, 0x28(%rsp)
movl $0x1, %edi
movq %rbx, %rsi
movl %ebp, %edx
callq 0xa4fc
addq $-0x8, %r13
decl %ebp
addq $0x18, %r14
decl %r15d
jne 0x8976
movl 0x4(%rsp), %eax
movl %eax, %r15d
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %rbp
leaq 0x4(%rbp), %r12
addq $0x404, %rbp            # imm = 0x404
movslq %r15d, %rax
leaq (%rax,%rax,2), %rax
leaq (%rsp,%rax,8), %r13
addq $0x828, %r13            # imm = 0x828
movl $0x800, %edx            # imm = 0x800
movq %r12, %rdi
xorl %esi, %esi
callq 0x1130
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r12, %rcx
movq %rbp, %r8
callq 0xa57e
testq %r14, %r14
je 0x8a6b
movl $0x2, (%rbx)
xorl %eax, %eax
cmpl $0x10, 0x404(%r14,%rax,4)
jg 0x8a47
cmpl $0x11, (%rbp,%rax,4)
jl 0x8a5e
movl 0x800(%rbx), %ecx
movl %ecx, 0x404(%rbx,%rax,4)
movl 0x400(%rbx), %ecx
movl %ecx, 0x4(%rbx,%rax,4)
incq %rax
cmpq $0xff, %rax
jne 0x8a35
jmp 0x8a8d
movl $0x0, (%rbx)
movl $0x101, %eax            # imm = 0x101
cmpl $0x11, (%rbx,%rax,4)
jl 0x8a82
movl $0x1, (%rbx)
incq %rax
cmpq $0x201, %rax            # imm = 0x201
jne 0x8a76
movq %rbx, %rax
addq $0x3848, %rsp           # imm = 0x3848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq