// File: /workspace/github_cpp_makefile_star2_O2/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 $0x538, %rsp            # imm = 0x538
movq %rcx, %r15
movl %edx, %r14d
movl %esi, %ebx
movq %rdi, %r13
cmpl $0x4, %edx
jb 0x71bcd
movq (%r13), %rax
movl $0x32, 0x28(%rax)
movl %r14d, 0x2c(%rax)
movq (%r13), %rax
movq %r13, %rdi
callq *(%rax)
xorl %eax, %eax
testl %ebx, %ebx
sete %al
movslq %r14d, %rcx
shll $0x5, %eax
addq %r13, %rax
movq 0xe8(%rax,%rcx,8), %r12
testq %r12, %r12
jne 0x71c02
movq (%r13), %rax
movl $0x32, 0x28(%rax)
movl %r14d, 0x2c(%rax)
movq (%r13), %rax
movq %r13, %rdi
callq *(%rax)
movl %ebx, 0x10(%rsp)
movq (%r15), %r14
testq %r14, %r14
jne 0x71c25
movq 0x8(%r13), %rax
pushq $0x1
popq %rsi
movl $0x620, %edx            # imm = 0x620
movq %r13, %rdi
callq *(%rax)
movq %rax, %r14
movq %rax, (%r15)
movq %r13, 0x8(%rsp)
movq %r12, 0x118(%r14)
xorl %eax, %eax
pushq $0x1
popq %rbx
movslq %eax, %r13
cmpq $0x11, %rbx
je 0x71c83
movzbl (%r12,%rbx), %r15d
movq %rax, %rbp
addl %r15d, %eax
cmpl $0x101, %eax            # imm = 0x101
jl 0x71c62
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x8, 0x28(%rax)
callq *(%rax)
leaq (%rsp,%r13), %rcx
addq $0x20, %rcx
xorl %eax, %eax
cmpl %eax, %r15d
je 0x71c79
movb %bl, (%rcx,%rax)
incq %rax
jmp 0x71c6c
addl %eax, %ebp
movq %rbp, %rax
incq %rbx
jmp 0x71c36
movq %rax, 0x18(%rsp)
movb $0x0, 0x20(%rsp,%r13)
movb 0x20(%rsp), %dl
movsbl %dl, %ecx
xorl %eax, %eax
movabsq $0x100000000, %rsi      # imm = 0x100000000
pushq $0x1
popq %rbp
xorl %r13d, %r13d
testb %dl, %dl
je 0x71d1a
movq %r13, %rbx
shlq $0x20, %rbx
movslq %r13d, %r13
leal (%rax,%rax), %r15d
movsbl %dl, %edx
cmpl %edx, %ecx
jne 0x71cdb
movl %eax, 0x130(%rsp,%r13,4)
movb 0x21(%rsp,%r13), %dl
incq %r13
incl %eax
addq %rsi, %rbx
addl $0x2, %r15d
jmp 0x71cb9
movl %eax, %eax
movq %rbp, %rdx
shlq %cl, %rdx
cmpq %rax, %rdx
jg 0x71d0b
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x8, 0x28(%rax)
movl %ecx, 0x14(%rsp)
callq *(%rax)
movabsq $0x100000000, %rsi      # imm = 0x100000000
movl 0x14(%rsp), %ecx
incl %ecx
sarq $0x20, %rbx
movb 0x20(%rsp,%rbx), %dl
movl %r15d, %eax
jmp 0x71ca7
xorl %eax, %eax
pushq $-0x1
popq %rcx
xorl %edx, %edx
cmpq $0x10, %rax
je 0x71d66
cmpb $0x0, 0x1(%r12,%rax)
je 0x71d59
movslq %edx, %rsi
movl 0x130(%rsp,%rsi,4), %edx
movq %rsi, %rdi
subq %rdx, %rdi
movq %rdi, 0x98(%r14,%rax,8)
movzbl 0x1(%r12,%rax), %edx
addq %rsi, %rdx
movl 0x12c(%rsp,%rdx,4), %esi
jmp 0x71d5c
movq %rcx, %rsi
movq %rsi, 0x8(%r14,%rax,8)
incq %rax
jmp 0x71d21
movq $0xfffff, 0x88(%r14) # imm = 0xFFFFF
leaq 0x120(%r14), %rdi
xorl %ebx, %ebx
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0xe270
pushq $0x8
popq %rax
pushq $0x1
popq %rdx
cmpq $0x9, %rbp
je 0x71de8
movl %eax, %ecx
subl %ebp, %ecx
movl %edx, %esi
shll %cl, %esi
movslq %ebx, %rbx
movl %edx, %edi
movzbl (%r12,%rbp), %r8d
cmpl %r8d, %edi
ja 0x71de3
movl 0x130(%rsp,%rbx,4), %r8d
shll %cl, %r8d
movslq %r8d, %r8
movl %esi, %r9d
testl %r9d, %r9d
jle 0x71ddc
movl %ebp, 0x120(%r14,%r8,4)
movb 0x11(%r12,%rbx), %r10b
movb %r10b, 0x520(%r14,%r8)
incq %r8
decl %r9d
jmp 0x71dba
incl %edi
incq %rbx
jmp 0x71d9f
incq %rbp
jmp 0x71d8c
cmpl $0x0, 0x10(%rsp)
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %rax
je 0x71e27
xorl %ebx, %ebx
testl %eax, %eax
cmovlel %ebx, %eax
cmpq %rbx, %rax
je 0x71e27
cmpb $0x10, 0x11(%r12,%rbx)
jb 0x71e22
movq %rax, %r15
movq (%r14), %rax
movl $0x8, 0x28(%rax)
movq %r14, %rdi
callq *(%rax)
movq %r15, %rax
incq %rbx
jmp 0x71e00
addq $0x538, %rsp            # imm = 0x538
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq