// File: /workspace/github_c_makefile_star2_O2/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, %r15
leaq 0x30ee(%rip), %rsi      # 0xa3c8
movl $0x40804, %edi          # imm = 0x40804
callq 0x1bad
movq %rax, %rbx
testq %rax, %rax
je 0x7509
testq %r14, %r14
je 0x7315
pushq $0x1
popq %rbp
leaq 0x840(%rsp), %rax
movq $0xff, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq %rax, 0x28(%rsp)
jmp 0x7317
xorl %ebp, %ebp
xorl %eax, %eax
movl %ebp, %edx
movslq %ebp, %r12
movslq %edx, %r13
cmpq $0x100, %rax            # imm = 0x100
je 0x7383
movq (%r15,%rax,8), %rcx
testq %rcx, %rcx
je 0x737e
testq %r14, %r14
je 0x734a
cmpq $0xff, %rax
je 0x7376
cmpl $0x11, 0x404(%r14,%rax,4)
jge 0x7376
imulq $0x18, %r13, %rdx
addq %rsp, %rdx
addq $0x840, %rdx            # imm = 0x840
movq %rcx, 0x10(%rdx)
movq %rax, (%rdx)
andq $0x0, 0x8(%rdx)
incl %r13d
leal 0x1(%r12), %ebp
movq %rdx, 0x28(%rsp,%r12,8)
movl %r13d, %edx
jmp 0x737e
addq %rcx, 0x850(%rsp)
incq %rax
jmp 0x731b
movl %edx, 0xc(%rsp)
pushq $0x2
popq %rcx
movl %ebp, %eax
cltd
idivl %ecx
movl %eax, %r15d
testl %r15d, %r15d
jle 0x73ab
movl %r15d, %edi
leaq 0x20(%rsp), %rsi
movl %ebp, %edx
callq 0x8cfb
decl %r15d
jmp 0x7392
movq %r14, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
imulq $0x18, %r13, %rax
shlq $0x20, %r13
leaq (%rsp,%rax), %r15
addq $0x840, %r15            # imm = 0x840
leaq (%rsp,%r12,8), %rax
addq $0x20, %rax
decl %ebp
pushq $0x1
popq %rdx
cmpl 0xc(%rsp), %edx
jge 0x7450
movq 0x28(%rsp), %rbx
movq %r13, %r14
movq %rax, %r13
movq (%rax), %rax
movq %rax, 0x28(%rsp)
pushq $0x1
popq %rdi
leaq 0x20(%rsp), %rsi
movl %edx, %r12d
movl %ebp, %edx
callq 0x8cfb
movq 0x28(%rsp), %rax
movq %rbx, (%r15)
movq %rax, 0x8(%r15)
movq 0x10(%rax), %rax
addq 0x10(%rbx), %rax
movq %rax, 0x10(%r15)
movq %r15, 0x28(%rsp)
pushq $0x1
popq %rdi
leaq 0x20(%rsp), %rsi
movl %ebp, %edx
callq 0x8cfb
movl %r12d, %edx
movq %r13, %rax
movq %r14, %r13
incl %edx
movabsq $0x100000000, %rcx      # imm = 0x100000000
addq %rcx, %r13
addq $0x18, %r15
addq $-0x8, %rax
decl %ebp
jmp 0x73d5
movq 0x18(%rsp), %rbx
leaq 0x4(%rbx), %rcx
movq %rbx, %r15
addq $0x404, %r15            # imm = 0x404
movl $0x101, %eax            # imm = 0x101
movq 0x10(%rsp), %r14
cmpq $0x201, %rax            # imm = 0x201
je 0x7486
andl $0x0, -0x400(%rbx,%rax,4)
andl $0x0, (%rbx,%rax,4)
incq %rax
jmp 0x746d
sarq $0x20, %r13
imulq $0x18, %r13, %rax
leaq (%rsp,%rax), %rdi
addq $0x828, %rdi            # imm = 0x828
xorl %esi, %esi
xorl %edx, %edx
movq %r15, %r8
callq 0x8d6e
testq %r14, %r14
je 0x74e8
movl $0x2, (%rbx)
xorl %eax, %eax
cmpq $0xff, %rax
je 0x7509
cmpl $0x10, 0x404(%r14,%rax,4)
jg 0x74cc
cmpl $0x11, (%r15,%rax,4)
jl 0x74e3
movl 0x800(%rbx), %ecx
movl %ecx, 0x404(%rbx,%rax,4)
movl 0x400(%rbx), %ecx
movl %ecx, 0x4(%rbx,%rax,4)
incq %rax
jmp 0x74b2
andl $0x0, (%rbx)
movl $0x101, %eax            # imm = 0x101
cmpq $0x201, %rax            # imm = 0x201
je 0x7509
cmpl $0x11, (%rbx,%rax,4)
jl 0x7504
movl $0x1, (%rbx)
incq %rax
jmp 0x74f0
movq %rbx, %rax
addq $0x3848, %rsp           # imm = 0x3848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq