// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/jpeg/jchuff.c
// Function: jpeg_make_c_derived_tbl
GLOBAL(void)
jpeg_make_c_derived_tbl (j_compress_ptr cinfo, jboolean isDC, int tblno,
			 c_derived_tbl ** pdtbl)
{
  JHUFF_TBL *htbl;
  c_derived_tbl *dtbl;
  int p, i, l, lastp, si, maxsymbol;
  char huffsize[257];
  unsigned int huffcode[257];
  unsigned int code;

  /* Note that huffsize[] and huffcode[] are filled in code-length order,
   * paralleling the order of the symbols themselves in htbl->huffval[].
   */

  /* Find the input Huffman table */
  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  htbl =
    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
  if (htbl == NULL)
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);

  /* Allocate a workspace if we haven't already done so. */
  if (*pdtbl == NULL)
    *pdtbl = (c_derived_tbl *)
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				  SIZEOF(c_derived_tbl));
  dtbl = *pdtbl;
  
  /* Figure C.1: make table of Huffman code length for each symbol */

  p = 0;
  for (l = 1; l <= 16; l++) {
    i = (int) htbl->bits[l];
    if (i < 0 || p + i > 256)	/* protect against table overrun */
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
    while (i--)
      huffsize[p++] = (char) l;
  }
  huffsize[p] = 0;
  lastp = p;
  
  /* Figure C.2: generate the codes themselves */
  /* We also validate that the counts represent a legal Huffman code tree. */

  code = 0;
  si = huffsize[0];
  p = 0;
  while (huffsize[p]) {
    while (((int) huffsize[p]) == si) {
      huffcode[p++] = code;
      code++;
    }
    /* code is now 1 more than the last code used for codelength si; but
     * it must still fit in si bits, since no code is allowed to be all ones.
     */
    if (((INT32) code) >= (((INT32) 1) << si))
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
    code <<= 1;
    si++;
  }
  
  /* Figure C.3: generate encoding tables */
  /* These are code and size indexed by symbol value */

  /* Set all codeless symbols to have code length 0;
   * this lets us detect duplicate VAL entries here, and later
   * allows emit_bits to detect any attempt to emit such symbols.
   */
  MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));

  /* This is also a convenient place to check for out-of-range
   * and duplicated VAL entries.  We allow 0..255 for AC symbols
   * but only 0..15 for DC.  (We could constrain them further
   * based on data depth and mode, but this seems enough.)
   */
  maxsymbol = isDC ? 15 : 255;

  for (p = 0; p < lastp; p++) {
    i = htbl->huffval[p];
    if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
    dtbl->ehufco[i] = huffcode[p];
    dtbl->ehufsi[i] = huffsize[p];
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x528, %rsp            # imm = 0x528
movq %rcx, %r12
movl %edx, %ebp
movl %esi, 0x4(%rsp)
movq %rdi, %r14
cmpl $0x4, %edx
jb 0x76bab
movq (%r14), %rax
movl $0x32, 0x28(%rax)
movl %ebp, 0x2c(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
xorl %eax, %eax
cmpl $0x0, 0x4(%rsp)
sete %al
movslq %ebp, %rcx
shll $0x5, %eax
addq %r14, %rax
movq 0x80(%rax,%rcx,8), %r13
testq %r13, %r13
jne 0x76be0
movq (%r14), %rax
movl $0x32, 0x28(%rax)
movl %ebp, 0x2c(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
movq (%r12), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x76c0a
movq 0x8(%r14), %rax
movl $0x500, %edx            # imm = 0x500
movq %r14, %rdi
movl $0x1, %esi
callq *(%rax)
movq %rax, 0x8(%rsp)
movq %rax, (%r12)
xorl %ebx, %ebx
movl $0x1, %r12d
movzbl (%r13,%r12), %r15d
leal (%rbx,%r15), %ebp
cmpl $0x101, %ebp            # imm = 0x101
jl 0x76c33
movq (%r14), %rax
movl $0x8, 0x28(%rax)
movq %r14, %rdi
callq *(%rax)
testl %r15d, %r15d
je 0x76c50
movslq %ebx, %rax
leaq (%rsp,%rax), %rdi
addq $0x10, %rdi
movl %r15d, %edx
movl %r12d, %esi
callq 0xe260
movl %ebp, %ebx
incq %r12
cmpq $0x11, %r12
jne 0x76c12
movslq %ebx, %rax
movb $0x0, 0x10(%rsp,%rax)
movsbl 0x10(%rsp), %ebp
testl %ebp, %ebp
je 0x76cdc
xorl %r12d, %r12d
movl %ebp, %eax
xorl %r15d, %r15d
movsbl %al, %eax
cmpl %eax, %ebp
jne 0x76cab
movslq %r15d, %rax
leaq (%rsp,%rax,4), %rcx
addq $0x120, %rcx            # imm = 0x120
leaq 0x11(%rsp), %rdx
addq %rdx, %rax
movl %r12d, %esi
xorl %edx, %edx
leal (%rsi,%rdx), %edi
movl %edi, (%rcx,%rdx,4)
movsbl (%rax,%rdx), %edi
incq %rdx
cmpl %edi, %ebp
je 0x76c94
addl %edx, %r15d
addl %edx, %r12d
movl %r12d, %eax
movl $0x1, %edx
movl %ebp, %ecx
shlq %cl, %rdx
cmpq %rax, %rdx
jg 0x76ccc
movq (%r14), %rax
movl $0x8, 0x28(%rax)
movq %r14, %rdi
callq *(%rax)
addl %r12d, %r12d
incl %ebp
movslq %r15d, %rax
movb 0x10(%rsp,%rax), %al
testb %al, %al
jne 0x76c72
movq 0x8(%rsp), %rax
leaq 0x400(%rax), %r12
movl $0x100, %edx            # imm = 0x100
movq %r12, %rdi
xorl %esi, %esi
callq 0xe260
testl %ebx, %ebx
jle 0x76d4d
movl %ebx, %ebx
xorl %r15d, %r15d
movb 0x11(%r13,%r15), %al
movzbl %al, %ebp
cmpl $0x0, 0x4(%rsp)
je 0x76d13
cmpb $0xf, %al
ja 0x76d1a
cmpb $0x0, (%r12,%rbp)
je 0x76d29
movq (%r14), %rax
movl $0x8, 0x28(%rax)
movq %r14, %rdi
callq *(%rax)
movl 0x120(%rsp,%r15,4), %eax
movq 0x8(%rsp), %rcx
movl %eax, (%rcx,%rbp,4)
movb 0x10(%rsp,%r15), %al
movb %al, 0x400(%rcx,%rbp)
incq %r15
cmpq %r15, %rbx
jne 0x76d00
addq $0x528, %rsp            # imm = 0x528
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq