// File: tomfunkhouser[P]gaps/pkgs/jpeg/jdhuff.c
// Function: jpeg_huff_decode
GLOBAL(int)
jpeg_huff_decode (bitread_working_state * state,
		  register bit_buf_type get_buffer, register int bits_left,
		  d_derived_tbl * htbl, int min_bits)
{
  register int l = min_bits;
  register INT32 code;

  /* HUFF_DECODE has determined that the code is at least min_bits */
  /* bits long, so fetch that many bits in one swoop. */

  CHECK_BIT_BUFFER(*state, l, return -1);
  code = GET_BITS(l);

  /* Collect the rest of the Huffman code one bit at a time. */
  /* This is per Figure F.16 in the JPEG spec. */

  while (code > htbl->maxcode[l]) {
    code <<= 1;
    CHECK_BIT_BUFFER(*state, 1, return -1);
    code |= GET_BITS(1);
    l++;
  }

  /* Unload the local registers */
  state->get_buffer = get_buffer;
  state->bits_left = bits_left;

  /* With garbage input we may reach the sentinel value l = 17. */

  if (l > 16) {
    WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
    return 0;			/* fake a zero as the safest result */
  }

  return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movl %edx, 0x1c(%rsp)
movq %rcx, 0x10(%rsp)
movl %r8d, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x8(%rsp), %eax
jge 0xe9ad1
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl 0x1c(%rsp), %edx
movl 0x8(%rsp), %ecx
callq 0xe97e0
cmpl $0x0, %eax
jne 0xe9ab7
movl $0xffffffff, 0x34(%rsp) # imm = 0xFFFFFFFF
jmp 0xe9c2d
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0x20(%rsp), %rax
movl 0x8(%rsp), %edx
movl 0x1c(%rsp), %ecx
subl %edx, %ecx
movl %ecx, 0x1c(%rsp)
movl %ecx, %ecx
sarq %cl, %rax
movl 0x8(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
subl $0x1, %ecx
andl %ecx, %eax
cltq
movq %rax, (%rsp)
movq (%rsp), %rax
movq 0x10(%rsp), %rcx
movslq 0x8(%rsp), %rdx
cmpq (%rcx,%rdx,8), %rax
jle 0xe9ba1
movq (%rsp), %rax
shlq %rax
movq %rax, (%rsp)
cmpl $0x1, 0x1c(%rsp)
jge 0xe9b6f
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl 0x1c(%rsp), %edx
movl $0x1, %ecx
callq 0xe97e0
cmpl $0x0, %eax
jne 0xe9b55
movl $0xffffffff, 0x34(%rsp) # imm = 0xFFFFFFFF
jmp 0xe9c2d
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0x20(%rsp), %rax
movl 0x1c(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, 0x1c(%rsp)
movl %ecx, %ecx
sarq %cl, %rax
andl $0x1, %eax
cltq
orq (%rsp), %rax
movq %rax, (%rsp)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0xe9b01
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x10(%rax)
movl 0x1c(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x18(%rax)
cmpl $0x10, 0x8(%rsp)
jle 0xe9bff
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rax
movl $0x76, 0x28(%rax)
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq 0x28(%rsp), %rcx
movq 0x20(%rcx), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq *%rax
movl $0x0, 0x34(%rsp)
jmp 0xe9c2d
movq 0x10(%rsp), %rax
movq 0x118(%rax), %rax
movq (%rsp), %rcx
movq 0x10(%rsp), %rdx
movslq 0x8(%rsp), %rsi
addq 0x90(%rdx,%rsi,8), %rcx
movslq %ecx, %rcx
movzbl 0x11(%rax,%rcx), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)