// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movl %edx, %ebp
movl %esi, 0x4(%rsp)
movq %rdi, %r14
cmpl $0x4, %edx
jb 0x77aeb
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), %r12
testq %r12, %r12
jne 0x77b20
movq (%r14), %rax
movl $0x32, 0x28(%rax)
movl %ebp, 0x2c(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
cmpq $0x0, (%r15)
jne 0x77b3c
movq 0x8(%r14), %rax
movl $0x500, %edx            # imm = 0x500
movq %r14, %rdi
movl $0x1, %esi
callq *(%rax)
movq %rax, (%r15)
movq (%r15), %rax
movq %rax, 0x8(%rsp)
xorl %ebx, %ebx
movl $0x1, %r15d
movzbl (%r12,%r15), %r13d
leal (%rbx,%r13), %ebp
cmpl $0x101, %ebp            # imm = 0x101
jl 0x77b6c
movq (%r14), %rax
movl $0x8, 0x28(%rax)
movq %r14, %rdi
callq *(%rax)
testl %r13d, %r13d
je 0x77b89
movslq %ebx, %rax
leaq (%rsp,%rax), %rdi
addq $0x10, %rdi
movl %r13d, %edx
movl %r15d, %esi
callq 0xe260
movl %ebp, %ebx
incq %r15
cmpq $0x11, %r15
jne 0x77b4c
movslq %ebx, %rax
movb $0x0, 0x10(%rsp,%rax)
movsbl 0x10(%rsp), %ebp
testl %ebp, %ebp
je 0x77c14
xorl %r15d, %r15d
xorl %r13d, %r13d
movslq %r13d, %rax
movsbl 0x10(%rsp,%rax), %ecx
cmpl %ecx, %ebp
jne 0x77be4
leaq (%rsp,%rax,4), %rcx
addq $0x120, %rcx            # imm = 0x120
leaq 0x11(%rsp), %rdx
addq %rdx, %rax
movl %r15d, %esi
xorl %edx, %edx
leal (%rsi,%rdx), %edi
movl %edi, (%rcx,%rdx,4)
movsbl (%rax,%rdx), %edi
incq %rdx
cmpl %edi, %ebp
je 0x77bcd
addl %edx, %r13d
addl %edx, %r15d
movl %r15d, %eax
movl $0x1, %edx
movl %ebp, %ecx
shlq %cl, %rdx
cmpq %rax, %rdx
jg 0x77c05
movq (%r14), %rax
movl $0x8, 0x28(%rax)
movq %r14, %rdi
callq *(%rax)
addl %r15d, %r15d
incl %ebp
movslq %r13d, %rax
cmpb $0x0, 0x10(%rsp,%rax)
jne 0x77ba9
movq 0x8(%rsp), %rax
leaq 0x400(%rax), %r15
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
xorl %esi, %esi
callq 0xe260
testl %ebx, %ebx
jle 0x77c85
movl %ebx, %ebx
xorl %r13d, %r13d
movb 0x11(%r12,%r13), %al
movzbl %al, %ebp
cmpl $0x0, 0x4(%rsp)
je 0x77c4b
cmpb $0xf, %al
ja 0x77c52
cmpb $0x0, (%r15,%rbp)
je 0x77c61
movq (%r14), %rax
movl $0x8, 0x28(%rax)
movq %r14, %rdi
callq *(%rax)
movl 0x120(%rsp,%r13,4), %eax
movq 0x8(%rsp), %rcx
movl %eax, (%rcx,%rbp,4)
movb 0x10(%rsp,%r13), %al
movb %al, 0x400(%rcx,%rbp)
incq %r13
cmpq %r13, %rbx
jne 0x77c38
addq $0x528, %rsp            # imm = 0x528
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq