// File: /workspace/github_cpp_makefile_star2_O2/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 $0x548, %rsp            # imm = 0x548
movq %rcx, %r12
movl %edx, %ebp
movl %esi, %r13d
movq %rdi, %rbx
cmpl $0x4, %edx
jb 0x6c442
movq (%rbx), %rax
movl $0x32, 0x28(%rax)
movl %ebp, 0x2c(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
xorl %eax, %eax
testl %r13d, %r13d
sete %al
movslq %ebp, %rcx
shll $0x5, %eax
movq %rbx, 0x8(%rsp)
addq %rbx, %rax
movq 0x80(%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
jne 0x6c481
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x32, 0x28(%rax)
movl %ebp, 0x2c(%rax)
movq (%rdi), %rax
callq *(%rax)
movq (%r12), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x6c4ab
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rax
pushq $0x1
popq %rsi
movl $0x500, %edx            # imm = 0x500
callq *(%rax)
movq %rax, 0x10(%rsp)
movq %rax, (%r12)
xorl %ebx, %ebx
pushq $0x1
popq %r14
movslq %ebx, %r12
cmpq $0x11, %r14
je 0x6c4ff
movq 0x20(%rsp), %rax
movzbl (%rax,%r14), %r15d
leal (%rbx,%r15), %eax
cmpl $0x101, %eax            # imm = 0x101
jl 0x6c4e0
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x8, 0x28(%rax)
callq *(%rax)
leaq (%rsp,%r12), %rcx
addq $0x30, %rcx
xorl %eax, %eax
cmpl %eax, %r15d
je 0x6c4f8
movb %r14b, (%rcx,%rax)
incq %rax
jmp 0x6c4ea
addl %eax, %ebx
incq %r14
jmp 0x6c4b1
movl %r13d, 0x1c(%rsp)
movb $0x0, 0x30(%rsp,%r12)
movb 0x30(%rsp), %al
movsbl %al, %ebp
xorl %r13d, %r13d
movabsq $0x100000000, %r12      # imm = 0x100000000
pushq $0x1
popq %rsi
xorl %ecx, %ecx
movq %rbx, 0x28(%rsp)
testb %al, %al
je 0x6c597
movq %r13, %r15
shlq $0x20, %r15
movslq %r13d, %r13
leal (%rcx,%rcx), %r14d
movsbl %al, %eax
cmpl %eax, %ebp
jne 0x6c55c
movl %ecx, 0x140(%rsp,%r13,4)
movb 0x31(%rsp,%r13), %al
incq %r13
incl %ecx
addq %r12, %r15
addl $0x2, %r14d
jmp 0x6c53a
movl %ecx, %eax
movq %rsi, %rdx
movl %ebp, %ecx
shlq %cl, %rdx
cmpq %rax, %rdx
jg 0x6c587
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x8, 0x28(%rax)
movq %rsi, %rbx
callq *(%rax)
movq %rbx, %rsi
movq 0x28(%rsp), %rbx
incl %ebp
sarq $0x20, %r15
movb 0x30(%rsp,%r15), %al
movl %r14d, %ecx
jmp 0x6c528
movq 0x10(%rsp), %rax
leaq 0x400(%rax), %r12
xorl %r14d, %r14d
movl $0x100, %edx            # imm = 0x100
movq %r12, %rdi
xorl %esi, %esi
callq 0xe270
testl %ebx, %ebx
cmovlel %r14d, %ebx
movl 0x1c(%rsp), %ebp
movq 0x8(%rsp), %r13
cmpq %r14, %rbx
je 0x6c619
movq 0x20(%rsp), %rax
movb 0x11(%rax,%r14), %al
movzbl %al, %r15d
testl %ebp, %ebp
je 0x6c5df
cmpb $0xf, %al
ja 0x6c5e6
cmpb $0x0, (%r12,%r15)
je 0x6c5f6
movq (%r13), %rax
movl $0x8, 0x28(%rax)
movq %r13, %rdi
callq *(%rax)
movl 0x140(%rsp,%r14,4), %eax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx,%r15,4)
movb 0x30(%rsp,%r14), %al
movb %al, 0x400(%rcx,%r15)
incq %r14
jmp 0x6c5c4
addq $0x548, %rsp            # imm = 0x548
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq