// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movl %edx, %r15d
movl %esi, %ebp
movq %rdi, %rbx
cmpl $0x4, %edx
jb 0x7e987
movq (%rbx), %rax
movl $0x32, 0x28(%rax)
movl %r15d, 0x2c(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
xorl %eax, %eax
testl %ebp, %ebp
sete %al
movslq %r15d, %rcx
shll $0x5, %eax
addq %rbx, %rax
movq 0xe8(%rax,%rcx,8), %r12
testq %r12, %r12
jne 0x7e9ba
movq (%rbx), %rax
movl $0x32, 0x28(%rax)
movl %r15d, 0x2c(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
movl %ebp, 0xc(%rsp)
cmpq $0x0, (%r14)
jne 0x7e9da
movq 0x8(%rbx), %rax
movl $0x620, %edx            # imm = 0x620
movq %rbx, %rdi
movl $0x1, %esi
callq *(%rax)
movq %rax, (%r14)
movq %rbx, (%rsp)
movq (%r14), %r15
movq %r12, 0x118(%r15)
xorl %r13d, %r13d
movl $0x1, %r14d
movzbl (%r12,%r14), %ebp
leal (%r13,%rbp), %ebx
cmpl $0x101, %ebx            # imm = 0x101
jl 0x7ea13
movq (%rsp), %rdi
movq (%rdi), %rax
movl $0x8, 0x28(%rax)
callq *(%rax)
testl %ebp, %ebp
je 0x7ea2f
movslq %r13d, %rax
leaq (%rsp,%rax), %rdi
addq $0x10, %rdi
movl %ebp, %edx
movl %r14d, %esi
callq 0xe260
movl %ebx, %r13d
incq %r14
cmpq $0x11, %r14
jne 0x7e9f1
movslq %r13d, %rax
movb $0x0, 0x10(%rsp,%rax)
movsbl 0x10(%rsp), %r14d
testl %r14d, %r14d
je 0x7eaba
xorl %ebp, %ebp
xorl %ebx, %ebx
movslq %ebp, %rax
movsbl 0x10(%rsp,%rax), %ecx
cmpl %ecx, %r14d
jne 0x7ea89
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, %r14d
je 0x7ea73
addl %edx, %ebp
addl %edx, %ebx
movl %ebx, %eax
movl $0x1, %edx
movl %r14d, %ecx
shlq %cl, %rdx
cmpq %rax, %rdx
jg 0x7eaab
movq (%rsp), %rdi
movq (%rdi), %rax
movl $0x8, 0x28(%rax)
callq *(%rax)
addl %ebx, %ebx
incl %r14d
movslq %ebp, %rax
cmpb $0x0, 0x10(%rsp,%rax)
jne 0x7ea4f
xorl %eax, %eax
xorl %ecx, %ecx
cmpb $0x0, 0x1(%r12,%rax)
je 0x7eaf0
movslq %ecx, %rdx
movl 0x120(%rsp,%rdx,4), %ecx
movq %rdx, %rsi
subq %rcx, %rsi
movq %rsi, 0x98(%r15,%rax,8)
movzbl 0x1(%r12,%rax), %ecx
addq %rdx, %rcx
movl 0x11c(%rsp,%rcx,4), %edx
jmp 0x7eaf7
movq $-0x1, %rdx
movq %rdx, 0x8(%r15,%rax,8)
incq %rax
cmpq $0x10, %rax
jne 0x7eabe
movq $0xfffff, 0x88(%r15) # imm = 0xFFFFF
leaq 0x120(%r15), %rdi
xorl %r14d, %r14d
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0xe260
movl $0x1, %eax
movq (%rsp), %rbp
cmpb $0x0, (%r12,%rax)
je 0x7eb92
movl $0x8, %ecx
subq %rax, %rcx
movl $0x1, %edx
shll %cl, %edx
movl $0x1, %esi
movslq %r14d, %r14
incl %edx
movl 0x120(%rsp,%r14,4), %edi
shll %cl, %edi
movslq %edi, %rdi
movl %edx, %r8d
movl %eax, 0x120(%r15,%rdi,4)
movb 0x11(%r12,%r14), %r9b
movb %r9b, 0x520(%r15,%rdi)
incq %rdi
decl %r8d
cmpl $0x1, %r8d
jg 0x7eb5f
leal 0x1(%rsi), %edi
incq %r14
movzbl (%r12,%rax), %r8d
cmpl %r8d, %esi
movl %edi, %esi
jb 0x7eb4f
incq %rax
cmpq $0x9, %rax
jne 0x7eb2f
cmpl $0x0, 0xc(%rsp)
je 0x7ebcd
testl %r13d, %r13d
jle 0x7ebcd
movl %r13d, %ebx
xorl %r14d, %r14d
cmpb $0x10, 0x11(%r12,%r14)
jb 0x7ebc5
movq (%rbp), %rax
movl $0x8, 0x28(%rax)
movq %rbp, %rdi
callq *(%rax)
incq %r14
cmpq %r14, %rbx
jne 0x7ebad
addq $0x528, %rsp            # imm = 0x528
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq