// File: tomfunkhouser[P]gaps/pkgs/jpeg/jdhuff.c
// Function: decode_mcu
METHODDEF(jboolean)
decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  int blkn;
  BITREAD_STATE_VARS;
  savable_state state;

  /* Process restart marker if needed; may have to suspend */
  if (cinfo->restart_interval) {
    if (entropy->restarts_to_go == 0)
      if (! process_restart(cinfo))
	return FALSE;
  }

  /* If we've run out of data, just leave the MCU set to zeroes.
   * This way, we return uniform gray for the remainder of the segment.
   */
  if (! entropy->pub.insufficient_data) {

    /* Load up working state */
    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
    ASSIGN_STATE(state, entropy->saved);

    /* Outer loop handles each block in the MCU */

    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
      JBLOCKROW block = MCU_data[blkn];
      d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
      d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
      register int s, k, r;

      /* Decode a single block's worth of coefficients */

      /* Section F.2.2.1: decode the DC coefficient difference */
      HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
      if (s) {
	CHECK_BIT_BUFFER(br_state, s, return FALSE);
	r = GET_BITS(s);
	s = HUFF_EXTEND(r, s);
      }

      if (entropy->dc_needed[blkn]) {
	/* Convert DC difference to actual value, update last_dc_val */
	int ci = cinfo->MCU_membership[blkn];
	s += state.last_dc_val[ci];
	state.last_dc_val[ci] = s;
	/* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
	(*block)[0] = (JCOEF) s;
      }

      if (entropy->ac_needed[blkn]) {

	/* Section F.2.2.2: decode the AC coefficients */
	/* Since zeroes are skipped, output area must be cleared beforehand */
	for (k = 1; k < DCTSIZE2; k++) {
	  HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
      
	  r = s >> 4;
	  s &= 15;
      
	  if (s) {
	    k += r;
	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
	    r = GET_BITS(s);
	    s = HUFF_EXTEND(r, s);
	    /* Output coefficient in natural (dezigzagged) order.
	     * Note: the extra entries in jpeg_natural_order[] will save us
	     * if k >= DCTSIZE2, which could happen if the data is corrupted.
	     */
	    (*block)[jpeg_natural_order[k]] = (JCOEF) s;
	  } else {
	    if (r != 15)
	      break;
	    k += 15;
	  }
	}

      } else {

	/* Section F.2.2.2: decode the AC coefficients */
	/* In this path we just discard the values */
	for (k = 1; k < DCTSIZE2; k++) {
	  HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
      
	  r = s >> 4;
	  s &= 15;
      
	  if (s) {
	    k += r;
	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
	    DROP_BITS(s);
	  } else {
	    if (r != 15)
	      break;
	    k += 15;
	  }
	}

      }
    }

    /* Completed MCU, so update state */
    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
    ASSIGN_STATE(entropy->saved, state);
  }

  /* Account for restart interval (no-op if not using restarts) */
  entropy->restarts_to_go--;

  return TRUE;
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq 0xa8(%rsp), %rax
movq 0x250(%rax), %rax
movq %rax, 0x98(%rsp)
movq 0xa8(%rsp), %rax
cmpl $0x0, 0x170(%rax)
je 0xe9fe3
movq 0x98(%rsp), %rax
cmpl $0x0, 0x38(%rax)
jne 0xe9fe1
movq 0xa8(%rsp), %rdi
callq 0xea890
cmpl $0x0, %eax
jne 0xe9fdf
movl $0x0, 0xb4(%rsp)
jmp 0xea87d
jmp 0xe9fe1
jmp 0xe9fe3
movq 0x98(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xea861
movq 0xa8(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0xa8(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0xa8(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x98(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x88(%rsp)
movq 0x98(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x84(%rsp)
movq 0x98(%rsp), %rax
movq 0x28(%rax), %rcx
movq %rcx, 0x48(%rsp)
movq 0x30(%rax), %rax
movq %rax, 0x50(%rsp)
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
movq 0xa8(%rsp), %rcx
cmpl 0x1e0(%rcx), %eax
jge 0xea7f8
movq 0xa0(%rsp), %rax
movslq 0x94(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x40(%rsp)
movq 0x98(%rsp), %rax
movslq 0x94(%rsp), %rcx
movq 0x80(%rax,%rcx,8), %rax
movq %rax, 0x38(%rsp)
movq 0x98(%rsp), %rax
movslq 0x94(%rsp), %rcx
movq 0xd0(%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
cmpl $0x8, 0x84(%rsp)
jge 0xea14c
movq 0x88(%rsp), %rsi
movl 0x84(%rsp), %edx
leaq 0x58(%rsp), %rdi
xorl %ecx, %ecx
callq 0xe97e0
cmpl $0x0, %eax
jne 0xea11e
movl $0x0, 0xb4(%rsp)
jmp 0xea87d
movq 0x68(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x84(%rsp)
cmpl $0x8, 0x84(%rsp)
jge 0xea14a
movl $0x1, 0x20(%rsp)
jmp 0xea1ba
jmp 0xea14c
movq 0x88(%rsp), %rax
movl 0x84(%rsp), %ecx
subl $0x8, %ecx
movl %ecx, %ecx
sarq %cl, %rax
andl $0xff, %eax
movl %eax, 0x1c(%rsp)
movq 0x38(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movl 0x120(%rax,%rcx,4), %eax
movl %eax, 0x20(%rsp)
cmpl $0x0, %eax
je 0xea1b2
movl 0x20(%rsp), %ecx
movl 0x84(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x84(%rsp)
movq 0x38(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movzbl 0x520(%rax,%rcx), %eax
movl %eax, 0x2c(%rsp)
jmp 0xea20e
movl $0x9, 0x20(%rsp)
movq 0x88(%rsp), %rsi
movl 0x84(%rsp), %edx
movq 0x38(%rsp), %rcx
movl 0x20(%rsp), %r8d
leaq 0x58(%rsp), %rdi
callq 0xe9a60
movl %eax, 0x2c(%rsp)
cmpl $0x0, %eax
jge 0xea1f6
movl $0x0, 0xb4(%rsp)
jmp 0xea87d
movq 0x68(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x84(%rsp)
cmpl $0x0, 0x2c(%rsp)
je 0xea2e5
movl 0x84(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0xea270
movq 0x88(%rsp), %rsi
movl 0x84(%rsp), %edx
movl 0x2c(%rsp), %ecx
leaq 0x58(%rsp), %rdi
callq 0xe97e0
cmpl $0x0, %eax
jne 0xea258
movl $0x0, 0xb4(%rsp)
jmp 0xea87d
movq 0x68(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x84(%rsp)
movq 0x88(%rsp), %rax
movl 0x2c(%rsp), %edx
movl 0x84(%rsp), %ecx
subl %edx, %ecx
movl %ecx, 0x84(%rsp)
movl %ecx, %ecx
sarq %cl, %rax
movl 0x2c(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
subl $0x1, %ecx
andl %ecx, %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
movslq 0x2c(%rsp), %rdx
leaq 0x4cfa9(%rip), %rcx     # 0x137260
cmpl (%rcx,%rdx,4), %eax
jge 0xea2d5
movl 0x24(%rsp), %eax
movslq 0x2c(%rsp), %rdx
leaq 0x4cfd4(%rip), %rcx     # 0x1372a0
addl (%rcx,%rdx,4), %eax
movl %eax, 0x4(%rsp)
jmp 0xea2dd
movl 0x24(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x98(%rsp), %rax
movslq 0x94(%rsp), %rcx
cmpl $0x0, 0x120(%rax,%rcx,4)
je 0xea347
movq 0xa8(%rsp), %rax
movslq 0x94(%rsp), %rcx
movl 0x1e4(%rax,%rcx,4), %eax
movl %eax, 0x18(%rsp)
movslq 0x18(%rsp), %rax
movl 0x48(%rsp,%rax,4), %eax
addl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %ecx
movslq 0x18(%rsp), %rax
movl %ecx, 0x48(%rsp,%rax,4)
movl 0x2c(%rsp), %eax
movw %ax, %cx
movq 0x40(%rsp), %rax
movw %cx, (%rax)
movq 0x98(%rsp), %rax
movslq 0x94(%rsp), %rcx
cmpl $0x0, 0x148(%rax,%rcx,4)
je 0xea5e5
movl $0x1, 0x28(%rsp)
cmpl $0x40, 0x28(%rsp)
jge 0xea5e0
cmpl $0x8, 0x84(%rsp)
jge 0xea3e0
movq 0x88(%rsp), %rsi
movl 0x84(%rsp), %edx
leaq 0x58(%rsp), %rdi
xorl %ecx, %ecx
callq 0xe97e0
cmpl $0x0, %eax
jne 0xea3b2
movl $0x0, 0xb4(%rsp)
jmp 0xea87d
movq 0x68(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x84(%rsp)
cmpl $0x8, 0x84(%rsp)
jge 0xea3de
movl $0x1, 0x14(%rsp)
jmp 0xea44e
jmp 0xea3e0
movq 0x88(%rsp), %rax
movl 0x84(%rsp), %ecx
subl $0x8, %ecx
movl %ecx, %ecx
sarq %cl, %rax
andl $0xff, %eax
movl %eax, 0x10(%rsp)
movq 0x30(%rsp), %rax
movslq 0x10(%rsp), %rcx
movl 0x120(%rax,%rcx,4), %eax
movl %eax, 0x14(%rsp)
cmpl $0x0, %eax
je 0xea446
movl 0x14(%rsp), %ecx
movl 0x84(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x84(%rsp)
movq 0x30(%rsp), %rax
movslq 0x10(%rsp), %rcx
movzbl 0x520(%rax,%rcx), %eax
movl %eax, 0x2c(%rsp)
jmp 0xea4a2
movl $0x9, 0x14(%rsp)
movq 0x88(%rsp), %rsi
movl 0x84(%rsp), %edx
movq 0x30(%rsp), %rcx
movl 0x14(%rsp), %r8d
leaq 0x58(%rsp), %rdi
callq 0xe9a60
movl %eax, 0x2c(%rsp)
cmpl $0x0, %eax
jge 0xea48a
movl $0x0, 0xb4(%rsp)
jmp 0xea87d
movq 0x68(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0x2c(%rsp), %eax
sarl $0x4, %eax
movl %eax, 0x24(%rsp)
movl 0x2c(%rsp), %eax
andl $0xf, %eax
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
je 0xea5ba
movl 0x24(%rsp), %eax
addl 0x28(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x84(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0xea526
movq 0x88(%rsp), %rsi
movl 0x84(%rsp), %edx
movl 0x2c(%rsp), %ecx
leaq 0x58(%rsp), %rdi
callq 0xe97e0
cmpl $0x0, %eax
jne 0xea50e
movl $0x0, 0xb4(%rsp)
jmp 0xea87d
movq 0x68(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x84(%rsp)
movq 0x88(%rsp), %rax
movl 0x2c(%rsp), %edx
movl 0x84(%rsp), %ecx
subl %edx, %ecx
movl %ecx, 0x84(%rsp)
movl %ecx, %ecx
sarq %cl, %rax
movl 0x2c(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
subl $0x1, %ecx
andl %ecx, %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
movslq 0x2c(%rsp), %rdx
leaq 0x4ccf3(%rip), %rcx     # 0x137260
cmpl (%rcx,%rdx,4), %eax
jge 0xea58a
movl 0x24(%rsp), %eax
movslq 0x2c(%rsp), %rdx
leaq 0x4cd1e(%rip), %rcx     # 0x1372a0
addl (%rcx,%rdx,4), %eax
movl %eax, (%rsp)
jmp 0xea591
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
movl (%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movw %ax, %dx
movq 0x40(%rsp), %rax
movslq 0x28(%rsp), %rsi
leaq 0x4cfb0(%rip), %rcx     # 0x137560
movslq (%rcx,%rsi,4), %rcx
movw %dx, (%rax,%rcx,2)
jmp 0xea5ce
cmpl $0xf, 0x24(%rsp)
je 0xea5c3
jmp 0xea5e0
movl 0x28(%rsp), %eax
addl $0xf, %eax
movl %eax, 0x28(%rsp)
jmp 0xea5d0
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0xea36d
jmp 0xea7e0
movl $0x1, 0x28(%rsp)
cmpl $0x40, 0x28(%rsp)
jge 0xea7de
cmpl $0x8, 0x84(%rsp)
jge 0xea660
movq 0x88(%rsp), %rsi
movl 0x84(%rsp), %edx
leaq 0x58(%rsp), %rdi
xorl %ecx, %ecx
callq 0xe97e0
cmpl $0x0, %eax
jne 0xea632
movl $0x0, 0xb4(%rsp)
jmp 0xea87d
movq 0x68(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x84(%rsp)
cmpl $0x8, 0x84(%rsp)
jge 0xea65e
movl $0x1, 0xc(%rsp)
jmp 0xea6ce
jmp 0xea660
movq 0x88(%rsp), %rax
movl 0x84(%rsp), %ecx
subl $0x8, %ecx
movl %ecx, %ecx
sarq %cl, %rax
andl $0xff, %eax
movl %eax, 0x8(%rsp)
movq 0x30(%rsp), %rax
movslq 0x8(%rsp), %rcx
movl 0x120(%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
cmpl $0x0, %eax
je 0xea6c6
movl 0xc(%rsp), %ecx
movl 0x84(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x84(%rsp)
movq 0x30(%rsp), %rax
movslq 0x8(%rsp), %rcx
movzbl 0x520(%rax,%rcx), %eax
movl %eax, 0x2c(%rsp)
jmp 0xea722
movl $0x9, 0xc(%rsp)
movq 0x88(%rsp), %rsi
movl 0x84(%rsp), %edx
movq 0x30(%rsp), %rcx
movl 0xc(%rsp), %r8d
leaq 0x58(%rsp), %rdi
callq 0xe9a60
movl %eax, 0x2c(%rsp)
cmpl $0x0, %eax
jge 0xea70a
movl $0x0, 0xb4(%rsp)
jmp 0xea87d
movq 0x68(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0x2c(%rsp), %eax
sarl $0x4, %eax
movl %eax, 0x24(%rsp)
movl 0x2c(%rsp), %eax
andl $0xf, %eax
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
je 0xea7b8
movl 0x24(%rsp), %eax
addl 0x28(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x84(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0xea7a2
movq 0x88(%rsp), %rsi
movl 0x84(%rsp), %edx
movl 0x2c(%rsp), %ecx
leaq 0x58(%rsp), %rdi
callq 0xe97e0
cmpl $0x0, %eax
jne 0xea78a
movl $0x0, 0xb4(%rsp)
jmp 0xea87d
movq 0x68(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0x2c(%rsp), %ecx
movl 0x84(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x84(%rsp)
jmp 0xea7cc
cmpl $0xf, 0x24(%rsp)
je 0xea7c1
jmp 0xea7de
movl 0x28(%rsp), %eax
addl $0xf, %eax
movl %eax, 0x28(%rsp)
jmp 0xea7ce
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0xea5ed
jmp 0xea7e0
jmp 0xea7e2
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0xea076
movq 0x58(%rsp), %rcx
movq 0xa8(%rsp), %rax
movq 0x28(%rax), %rax
movq %rcx, (%rax)
movq 0x60(%rsp), %rcx
movq 0xa8(%rsp), %rax
movq 0x28(%rax), %rax
movq %rcx, 0x8(%rax)
movq 0x88(%rsp), %rcx
movq 0x98(%rsp), %rax
movq %rcx, 0x18(%rax)
movl 0x84(%rsp), %ecx
movq 0x98(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x98(%rsp), %rax
movq 0x48(%rsp), %rcx
movq %rcx, 0x28(%rax)
movq 0x50(%rsp), %rcx
movq %rcx, 0x30(%rax)
movq 0x98(%rsp), %rax
movl 0x38(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x38(%rax)
movl $0x1, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nopl (%rax)