// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/jpeg/jdhuff.c
// Function: jpeg_make_d_derived_tbl
GLOBAL(void)
jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, jboolean isDC, int tblno,
			 d_derived_tbl ** pdtbl)
{
  JHUFF_TBL *htbl;
  d_derived_tbl *dtbl;
  int p, i, l, si, numsymbols;
  int lookbits, ctr;
  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 = (d_derived_tbl *)
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				  SIZEOF(d_derived_tbl));
  dtbl = *pdtbl;
  dtbl->pub = htbl;		/* fill in back link */
  
  /* 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;
  numsymbols = 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 F.15: generate decoding tables for bit-sequential decoding */

  p = 0;
  for (l = 1; l <= 16; l++) {
    if (htbl->bits[l]) {
      /* valoffset[l] = huffval[] index of 1st symbol of code length l,
       * minus the minimum code of length l
       */
      dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
      p += htbl->bits[l];
      dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
    } else {
      dtbl->maxcode[l] = -1;	/* -1 if no codes of this length */
    }
  }
  dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */

  /* Compute lookahead tables to speed up decoding.
   * First we set all the table entries to 0, indicating "too long";
   * then we iterate through the Huffman codes that are short enough and
   * fill in all the entries that correspond to bit sequences starting
   * with that code.
   */

  MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));

  p = 0;
  for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
    for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
      /* l = current code's length, p = its index in huffcode[] & huffval[]. */
      /* Generate left-justified code followed by all possible bit sequences */
      lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
      for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
	dtbl->look_nbits[lookbits] = l;
	dtbl->look_sym[lookbits] = htbl->huffval[p];
	lookbits++;
      }
    }
  }

  /* Validate symbols as being reasonable.
   * For AC tables, we make no check, but accept all byte values 0..255.
   * For DC tables, we require the symbols to be in range 0..15.
   * (Tighter bounds could be applied depending on the data depth and mode,
   * but this is sufficient to ensure safe decoding.)
   */
  if (isDC) {
    for (i = 0; i < numsymbols; i++) {
      int sym = htbl->huffval[i];
      if (sym < 0 || sym > 15)
	ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x528, %rsp            # imm = 0x528
movq %rcx, %r15
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, %rbx
cmpl $0x4, %edx
jb 0x7dd17
movq (%rbx), %rax
movl $0x32, 0x28(%rax)
movl %r14d, 0x2c(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
xorl %eax, %eax
testl %ebp, %ebp
sete %al
movslq %r14d, %rcx
shll $0x5, %eax
addq %rbx, %rax
movq 0xe8(%rax,%rcx,8), %r12
testq %r12, %r12
jne 0x7dd4a
movq (%rbx), %rax
movl $0x32, 0x28(%rax)
movl %r14d, 0x2c(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
movl %ebp, 0xc(%rsp)
movq (%r15), %r14
testq %r14, %r14
jne 0x7dd6f
movq 0x8(%rbx), %rax
movl $0x620, %edx            # imm = 0x620
movq %rbx, %rdi
movl $0x1, %esi
callq *(%rax)
movq %rax, %r14
movq %rax, (%r15)
movq %rbx, (%rsp)
movq %r12, 0x118(%r14)
xorl %r13d, %r13d
movl $0x1, %r15d
movzbl (%r12,%r15), %ebp
leal (%r13,%rbp), %ebx
cmpl $0x101, %ebx            # imm = 0x101
jl 0x7dda5
movq (%rsp), %rdi
movq (%rdi), %rax
movl $0x8, 0x28(%rax)
callq *(%rax)
testl %ebp, %ebp
je 0x7ddc1
movslq %r13d, %rax
leaq (%rsp,%rax), %rdi
addq $0x10, %rdi
movl %ebp, %edx
movl %r15d, %esi
callq 0xe260
movl %ebx, %r13d
incq %r15
cmpq $0x11, %r15
jne 0x7dd83
movslq %r13d, %rax
movb $0x0, 0x10(%rsp,%rax)
movsbl 0x10(%rsp), %r15d
testl %r15d, %r15d
je 0x7de4e
xorl %ebp, %ebp
movl %r15d, %eax
xorl %ebx, %ebx
movsbl %al, %eax
cmpl %eax, %r15d
jne 0x7de1c
movslq %ebp, %rax
leaq (%rsp,%rax,4), %rcx
addq $0x120, %rcx            # imm = 0x120
leaq 0x11(%rsp), %rdx
addq %rdx, %rax
movl %ebx, %esi
xorl %edx, %edx
leal (%rsi,%rdx), %edi
movl %edi, (%rcx,%rdx,4)
movsbl (%rax,%rdx), %edi
incq %rdx
cmpl %edi, %r15d
je 0x7de06
addl %edx, %ebp
addl %edx, %ebx
movl %ebx, %eax
movl $0x1, %edx
movl %r15d, %ecx
shlq %cl, %rdx
cmpq %rax, %rdx
jg 0x7de3e
movq (%rsp), %rdi
movq (%rdi), %rax
movl $0x8, 0x28(%rax)
callq *(%rax)
addl %ebx, %ebx
incl %r15d
movslq %ebp, %rax
movb 0x10(%rsp,%rax), %al
testb %al, %al
jne 0x7dde4
xorl %eax, %eax
xorl %ecx, %ecx
cmpb $0x0, 0x1(%r12,%rax)
je 0x7de84
movslq %ecx, %rdx
movl 0x120(%rsp,%rdx,4), %ecx
movq %rdx, %rsi
subq %rcx, %rsi
movq %rsi, 0x98(%r14,%rax,8)
movzbl 0x1(%r12,%rax), %ecx
addq %rdx, %rcx
movl 0x11c(%rsp,%rcx,4), %edx
jmp 0x7de8b
movq $-0x1, %rdx
movq %rdx, 0x8(%r14,%rax,8)
incq %rax
cmpq $0x10, %rax
jne 0x7de52
movq $0xfffff, 0x88(%r14) # imm = 0xFFFFF
leaq 0x120(%r14), %rdi
xorl %r15d, %r15d
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0xe260
movl $0x1, %eax
movq (%rsp), %rbp
cmpb $0x0, (%r12,%rax)
je 0x7df26
movl $0x8, %ecx
subl %eax, %ecx
movl $0x1, %edx
shll %cl, %edx
movl $0x1, %esi
movslq %r15d, %r15
incl %edx
movl 0x120(%rsp,%r15,4), %edi
shll %cl, %edi
movslq %edi, %rdi
movl %edx, %r8d
movl %eax, 0x120(%r14,%rdi,4)
movb 0x11(%r12,%r15), %r9b
movb %r9b, 0x520(%r14,%rdi)
incq %rdi
decl %r8d
cmpl $0x1, %r8d
jg 0x7def2
movzbl (%r12,%rax), %edi
leal 0x1(%rsi), %r8d
incq %r15
cmpl %edi, %esi
movl %r8d, %esi
jb 0x7dee2
incq %rax
cmpq $0x9, %rax
jne 0x7dec3
cmpl $0x0, 0xc(%rsp)
je 0x7df61
testl %r13d, %r13d
jle 0x7df61
movl %r13d, %ebx
xorl %r14d, %r14d
cmpb $0x10, 0x11(%r12,%r14)
jb 0x7df59
movq (%rbp), %rax
movl $0x8, 0x28(%rax)
movq %rbp, %rdi
callq *(%rax)
incq %r14
cmpq %r14, %rbx
jne 0x7df41
addq $0x528, %rsp            # imm = 0x528
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq