// File: /workspace/github_c_makefile_star2_O1/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 0x3f38(%rip), %rsi      # 0xc3c8
movl $0x40804, %edi          # imm = 0x40804
callq 0x2b85
xorl %r13d, %r13d
testq %rax, %rax
je 0x86a1
movq %rax, %rbx
testq %r14, %r14
je 0x84cf
leaq 0x840(%rsp), %rax
movq $0xff, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq %rax, 0x28(%rsp)
movl $0x1, %r13d
xorl %eax, %eax
movl %r13d, %r15d
movq (%r12,%rax,8), %rcx
testq %rcx, %rcx
je 0x8532
testq %r14, %r14
je 0x84f5
cmpq $0xff, %rax
je 0x852a
cmpl $0x11, 0x404(%r14,%rax,4)
jge 0x852a
movslq %r13d, %r13
leaq (,%r13,2), %rdx
addq %r13, %rdx
leaq 0x840(%rsp,%rdx,8), %rdx
movq %rcx, 0x10(%rdx)
movq %rax, (%rdx)
movq $0x0, 0x8(%rdx)
incl %r13d
movslq %r15d, %rcx
incl %r15d
movq %rdx, 0x28(%rsp,%rcx,8)
jmp 0x8532
addq %rcx, 0x850(%rsp)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x84d4
cmpl $0x2, %r15d
jl 0x856a
movl %r15d, %r12d
shrl %r12d
leaq 0x20(%rsp), %rbp
movl %r12d, %edi
movq %rbp, %rsi
movl %r15d, %edx
callq 0xa05e
leal -0x1(%r12), %eax
cmpl $0x1, %r12d
movl %eax, %r12d
jg 0x854e
cmpl $0x2, %r13d
jl 0x8626
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movl %r13d, %eax
movslq %r15d, %rcx
leaq (%rsp,%rcx,8), %r14
addq $0x20, %r14
decl %r15d
movq %r15, 0x18(%rsp)
leaq (%rax,%rax,2), %rax
leaq (%rsp,%rax,8), %rbx
addq $0x840, %rbx            # imm = 0x840
xorl %r12d, %r12d
movq %r13, %r15
movq 0x28(%rsp), %r13
movq (%r14), %rax
movq %rax, 0x28(%rsp)
movq 0x18(%rsp), %rax
leal (%rax,%r12), %ebp
movl $0x1, %edi
leaq 0x20(%rsp), %rsi
movl %ebp, %edx
callq 0xa05e
movq 0x28(%rsp), %rax
movq %r13, (%rbx)
movq %rax, 0x8(%rbx)
movq 0x10(%rax), %rax
addq 0x10(%r13), %rax
movq %r15, %r13
movq %rax, 0x10(%rbx)
movq %rbx, 0x28(%rsp)
movl $0x1, %edi
leaq 0x20(%rsp), %rsi
movl %ebp, %edx
callq 0xa05e
addq $-0x8, %r14
addq $0x18, %rbx
movl %r12d, %eax
decl %r12d
addl %r15d, %eax
decl %eax
cmpl $0x1, %eax
jne 0x85a6
subl %r12d, %r13d
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
leaq 0x4(%rbx), %r15
movl $0x800, %edx            # imm = 0x800
movq %r15, %rdi
xorl %esi, %esi
callq 0x1130
movslq %r13d, %rax
leaq (%rax,%rax,2), %rax
leaq (%rsp,%rax,8), %rdi
addq $0x828, %rdi            # imm = 0x828
movq %rbx, %r8
addq $0x404, %r8             # imm = 0x404
xorl %esi, %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0xa0d5
testq %r14, %r14
je 0x86a5
movl $0x2, (%rbx)
movl $0x101, %eax            # imm = 0x101
cmpl $0x10, (%r14,%rax,4)
jg 0x867e
cmpl $0x11, (%rbx,%rax,4)
jl 0x8694
movl 0x800(%rbx), %ecx
movl %ecx, (%rbx,%rax,4)
movl 0x400(%rbx), %ecx
movl %ecx, -0x400(%rbx,%rax,4)
incq %rax
cmpq $0x200, %rax            # imm = 0x200
jne 0x8671
jmp 0x86c7
xorl %ebx, %ebx
jmp 0x86c7
movl $0x0, (%rbx)
movl $0x101, %eax            # imm = 0x101
cmpl $0x11, (%rbx,%rax,4)
jl 0x86bc
movl $0x1, (%rbx)
incq %rax
cmpq $0x201, %rax            # imm = 0x201
jne 0x86b0
movq %rbx, %rax
addq $0x3848, %rsp           # imm = 0x3848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq