// File: tomfunkhouser[P]gaps/pkgs/jpeg/jdphuff.c
// Function: decode_mcu_AC_refine
METHODDEF(jboolean)
decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{   
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  int Se = cinfo->Se;
  int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
  int m1 = (-1) << cinfo->Al;	/* -1 in the bit position being coded */
  register int s, k, r;
  unsigned int EOBRUN;
  JBLOCKROW block;
  JCOEFPTR thiscoef;
  BITREAD_STATE_VARS;
  d_derived_tbl * tbl;
  int num_newnz;
  int newnz_pos[DCTSIZE2];

  /* 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, don't modify the MCU.
   */
  if (! entropy->pub.insufficient_data) {

    /* Load up working state */
    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
    EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */

    /* There is always only one block per MCU */
    block = MCU_data[0];
    tbl = entropy->ac_derived_tbl;

    /* If we are forced to suspend, we must undo the assignments to any newly
     * nonzero coefficients in the block, because otherwise we'd get confused
     * next time about which coefficients were already nonzero.
     * But we need not undo addition of bits to already-nonzero coefficients;
     * instead, we can test the current bit to see if we already did it.
     */
    num_newnz = 0;

    /* initialize coefficient loop counter to start of band */
    k = cinfo->Ss;

    if (EOBRUN == 0) {
      for (; k <= Se; k++) {
	HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
	r = s >> 4;
	s &= 15;
	if (s) {
	  if (s != 1)		/* size of new coef should always be 1 */
	    WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
	  CHECK_BIT_BUFFER(br_state, 1, goto undoit);
	  if (GET_BITS(1))
	    s = p1;		/* newly nonzero coef is positive */
	  else
	    s = m1;		/* newly nonzero coef is negative */
	} else {
	  if (r != 15) {
	    EOBRUN = 1 << r;	/* EOBr, run length is 2^r + appended bits */
	    if (r) {
	      CHECK_BIT_BUFFER(br_state, r, goto undoit);
	      r = GET_BITS(r);
	      EOBRUN += r;
	    }
	    break;		/* rest of block is handled by EOB logic */
	  }
	  /* note s = 0 for processing ZRL */
	}
	/* Advance over already-nonzero coefs and r still-zero coefs,
	 * appending correction bits to the nonzeroes.  A correction bit is 1
	 * if the absolute value of the coefficient must be increased.
	 */
	do {
	  thiscoef = *block + jpeg_natural_order[k];
	  if (*thiscoef != 0) {
	    CHECK_BIT_BUFFER(br_state, 1, goto undoit);
	    if (GET_BITS(1)) {
	      if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
		if (*thiscoef >= 0)
		  *thiscoef += p1;
		else
		  *thiscoef += m1;
	      }
	    }
	  } else {
	    if (--r < 0)
	      break;		/* reached target zero coefficient */
	  }
	  k++;
	} while (k <= Se);
	if (s) {
	  int pos = jpeg_natural_order[k];
	  /* Output newly nonzero coefficient */
	  (*block)[pos] = (JCOEF) s;
	  /* Remember its position in case we have to suspend */
	  newnz_pos[num_newnz++] = pos;
	}
      }
    }

    if (EOBRUN > 0) {
      /* Scan any remaining coefficient positions after the end-of-band
       * (the last newly nonzero coefficient, if any).  Append a correction
       * bit to each already-nonzero coefficient.  A correction bit is 1
       * if the absolute value of the coefficient must be increased.
       */
      for (; k <= Se; k++) {
	thiscoef = *block + jpeg_natural_order[k];
	if (*thiscoef != 0) {
	  CHECK_BIT_BUFFER(br_state, 1, goto undoit);
	  if (GET_BITS(1)) {
	    if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
	      if (*thiscoef >= 0)
		*thiscoef += p1;
	      else
		*thiscoef += m1;
	    }
	  }
	}
      }
      /* Count one block completed in EOB run */
      EOBRUN--;
    }

    /* Completed MCU, so update state */
    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
    entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
  }

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

  return TRUE;

undoit:
  /* Re-zero any output coefficients that we made newly nonzero */
  while (num_newnz > 0)
    (*block)[newnz_pos[--num_newnz]] = 0;

  return FALSE;
}

// Assembly:
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, 0x198(%rsp)
movq %rsi, 0x190(%rsp)
movq 0x198(%rsp), %rax
movq 0x250(%rax), %rax
movq %rax, 0x188(%rsp)
movq 0x198(%rsp), %rax
movl 0x210(%rax), %eax
movl %eax, 0x184(%rsp)
movq 0x198(%rsp), %rax
movl 0x218(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x180(%rsp)
movq 0x198(%rsp), %rax
movl 0x218(%rax), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
shll %cl, %eax
movl %eax, 0x17c(%rsp)
movq 0x198(%rsp), %rax
cmpl $0x0, 0x170(%rax)
je 0xebb00
movq 0x188(%rsp), %rax
cmpl $0x0, 0x3c(%rax)
jne 0xebafe
movq 0x198(%rsp), %rdi
callq 0xec320
cmpl $0x0, %eax
jne 0xebafc
movl $0x0, 0x1a4(%rsp)
jmp 0xec302
jmp 0xebafe
jmp 0xebb00
movq 0x188(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xec2a4
movq 0x198(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x198(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq %rax, 0x120(%rsp)
movq 0x198(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, 0x128(%rsp)
movq 0x188(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x150(%rsp)
movq 0x188(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x14c(%rsp)
movq 0x188(%rsp), %rax
movl 0x28(%rax), %eax
movl %eax, 0x16c(%rsp)
movq 0x190(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x160(%rsp)
movq 0x188(%rsp), %rax
movq 0x60(%rax), %rax
movq %rax, 0x118(%rsp)
movl $0x0, 0x114(%rsp)
movq 0x198(%rsp), %rax
movl 0x20c(%rax), %eax
movl %eax, 0x174(%rsp)
cmpl $0x0, 0x16c(%rsp)
jne 0xec0dd
jmp 0xebbe0
movl 0x174(%rsp), %eax
cmpl 0x184(%rsp), %eax
jg 0xec0db
cmpl $0x8, 0x14c(%rsp)
jge 0xebc5a
movq 0x150(%rsp), %rsi
movl 0x14c(%rsp), %edx
leaq 0x120(%rsp), %rdi
xorl %ecx, %ecx
callq 0xe97e0
cmpl $0x0, %eax
jne 0xebc26
jmp 0xec2c2
movq 0x130(%rsp), %rax
movq %rax, 0x150(%rsp)
movl 0x138(%rsp), %eax
movl %eax, 0x14c(%rsp)
cmpl $0x8, 0x14c(%rsp)
jge 0xebc58
movl $0x1, 0xc(%rsp)
jmp 0xebcd1
jmp 0xebc5a
movq 0x150(%rsp), %rax
movl 0x14c(%rsp), %ecx
subl $0x8, %ecx
movl %ecx, %ecx
sarq %cl, %rax
andl $0xff, %eax
movl %eax, 0x8(%rsp)
movq 0x118(%rsp), %rax
movslq 0x8(%rsp), %rcx
movl 0x120(%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
cmpl $0x0, %eax
je 0xebcc9
movl 0xc(%rsp), %ecx
movl 0x14c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x14c(%rsp)
movq 0x118(%rsp), %rax
movslq 0x8(%rsp), %rcx
movzbl 0x520(%rax,%rcx), %eax
movl %eax, 0x178(%rsp)
jmp 0xebd29
movl $0x9, 0xc(%rsp)
movq 0x150(%rsp), %rsi
movl 0x14c(%rsp), %edx
movq 0x118(%rsp), %rcx
movl 0xc(%rsp), %r8d
leaq 0x120(%rsp), %rdi
callq 0xe9a60
movl %eax, 0x178(%rsp)
cmpl $0x0, %eax
jge 0xebd0b
jmp 0xec2c2
movq 0x130(%rsp), %rax
movq %rax, 0x150(%rsp)
movl 0x138(%rsp), %eax
movl %eax, 0x14c(%rsp)
movl 0x178(%rsp), %eax
sarl $0x4, %eax
movl %eax, 0x170(%rsp)
movl 0x178(%rsp), %eax
andl $0xf, %eax
movl %eax, 0x178(%rsp)
cmpl $0x0, 0x178(%rsp)
je 0xebe2f
cmpl $0x1, 0x178(%rsp)
je 0xebd93
movq 0x198(%rsp), %rax
movq (%rax), %rax
movl $0x76, 0x28(%rax)
movq 0x198(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq 0x198(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq *%rax
cmpl $0x1, 0x14c(%rsp)
jge 0xebde6
movq 0x150(%rsp), %rsi
movl 0x14c(%rsp), %edx
leaq 0x120(%rsp), %rdi
movl $0x1, %ecx
callq 0xe97e0
cmpl $0x0, %eax
jne 0xebdc8
jmp 0xec2c2
movq 0x130(%rsp), %rax
movq %rax, 0x150(%rsp)
movl 0x138(%rsp), %eax
movl %eax, 0x14c(%rsp)
movq 0x150(%rsp), %rax
movl 0x14c(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, 0x14c(%rsp)
movl %ecx, %ecx
sarq %cl, %rax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xebe1c
movl 0x180(%rsp), %eax
movl %eax, 0x178(%rsp)
jmp 0xebe2a
movl 0x17c(%rsp), %eax
movl %eax, 0x178(%rsp)
jmp 0xebf17
cmpl $0xf, 0x170(%rsp)
je 0xebf15
movl 0x170(%rsp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x16c(%rsp)
cmpl $0x0, 0x170(%rsp)
je 0xebf10
movl 0x14c(%rsp), %eax
cmpl 0x170(%rsp), %eax
jge 0xebebb
movq 0x150(%rsp), %rsi
movl 0x14c(%rsp), %edx
movl 0x170(%rsp), %ecx
leaq 0x120(%rsp), %rdi
callq 0xe97e0
cmpl $0x0, %eax
jne 0xebe9d
jmp 0xec2c2
movq 0x130(%rsp), %rax
movq %rax, 0x150(%rsp)
movl 0x138(%rsp), %eax
movl %eax, 0x14c(%rsp)
movq 0x150(%rsp), %rax
movl 0x170(%rsp), %edx
movl 0x14c(%rsp), %ecx
subl %edx, %ecx
movl %ecx, 0x14c(%rsp)
movl %ecx, %ecx
sarq %cl, %rax
movl 0x170(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
subl $0x1, %ecx
andl %ecx, %eax
movl %eax, 0x170(%rsp)
movl 0x170(%rsp), %eax
addl 0x16c(%rsp), %eax
movl %eax, 0x16c(%rsp)
jmp 0xec0db
jmp 0xebf17
jmp 0xebf19
movq 0x160(%rsp), %rax
movslq 0x174(%rsp), %rdx
leaq 0x4b630(%rip), %rcx     # 0x137560
movslq (%rcx,%rdx,4), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, 0x158(%rsp)
movq 0x158(%rsp), %rax
movswl (%rax), %eax
cmpl $0x0, %eax
je 0xec02c
cmpl $0x1, 0x14c(%rsp)
jge 0xebfa9
movq 0x150(%rsp), %rsi
movl 0x14c(%rsp), %edx
leaq 0x120(%rsp), %rdi
movl $0x1, %ecx
callq 0xe97e0
cmpl $0x0, %eax
jne 0xebf8b
jmp 0xec2c2
movq 0x130(%rsp), %rax
movq %rax, 0x150(%rsp)
movl 0x138(%rsp), %eax
movl %eax, 0x14c(%rsp)
movq 0x150(%rsp), %rax
movl 0x14c(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, 0x14c(%rsp)
movl %ecx, %ecx
sarq %cl, %rax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xec02a
movq 0x158(%rsp), %rax
movswl (%rax), %eax
andl 0x180(%rsp), %eax
cmpl $0x0, %eax
jne 0xec028
movq 0x158(%rsp), %rax
movswl (%rax), %eax
cmpl $0x0, %eax
jl 0xec00f
movl 0x180(%rsp), %edx
movq 0x158(%rsp), %rax
movswl (%rax), %ecx
addl %edx, %ecx
movw %cx, (%rax)
jmp 0xec026
movl 0x17c(%rsp), %edx
movq 0x158(%rsp), %rax
movswl (%rax), %ecx
addl %edx, %ecx
movw %cx, (%rax)
jmp 0xec028
jmp 0xec02a
jmp 0xec046
movl 0x170(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x170(%rsp)
cmpl $0x0, %eax
jge 0xec044
jmp 0xec06b
jmp 0xec046
movl 0x174(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x174(%rsp)
movl 0x174(%rsp), %eax
cmpl 0x184(%rsp), %eax
jle 0xebf19
cmpl $0x0, 0x178(%rsp)
je 0xec0c3
movslq 0x174(%rsp), %rcx
leaq 0x4b4dc(%rip), %rax     # 0x137560
movl (%rax,%rcx,4), %eax
movl %eax, 0x4(%rsp)
movl 0x178(%rsp), %eax
movw %ax, %dx
movq 0x160(%rsp), %rax
movslq 0x4(%rsp), %rcx
movw %dx, (%rax,%rcx,2)
movl 0x4(%rsp), %ecx
movl 0x114(%rsp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x114(%rsp)
cltq
movl %ecx, 0x10(%rsp,%rax,4)
jmp 0xec0c5
movl 0x174(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x174(%rsp)
jmp 0xebbe0
jmp 0xec0dd
cmpl $0x0, 0x16c(%rsp)
jbe 0xec23d
jmp 0xec0ed
movl 0x174(%rsp), %eax
cmpl 0x184(%rsp), %eax
jg 0xec22c
movq 0x160(%rsp), %rax
movslq 0x174(%rsp), %rdx
leaq 0x4b448(%rip), %rcx     # 0x137560
movslq (%rcx,%rdx,4), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, 0x158(%rsp)
movq 0x158(%rsp), %rax
movswl (%rax), %eax
cmpl $0x0, %eax
je 0xec214
cmpl $0x1, 0x14c(%rsp)
jge 0xec191
movq 0x150(%rsp), %rsi
movl 0x14c(%rsp), %edx
leaq 0x120(%rsp), %rdi
movl $0x1, %ecx
callq 0xe97e0
cmpl $0x0, %eax
jne 0xec173
jmp 0xec2c2
movq 0x130(%rsp), %rax
movq %rax, 0x150(%rsp)
movl 0x138(%rsp), %eax
movl %eax, 0x14c(%rsp)
movq 0x150(%rsp), %rax
movl 0x14c(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, 0x14c(%rsp)
movl %ecx, %ecx
sarq %cl, %rax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xec212
movq 0x158(%rsp), %rax
movswl (%rax), %eax
andl 0x180(%rsp), %eax
cmpl $0x0, %eax
jne 0xec210
movq 0x158(%rsp), %rax
movswl (%rax), %eax
cmpl $0x0, %eax
jl 0xec1f7
movl 0x180(%rsp), %edx
movq 0x158(%rsp), %rax
movswl (%rax), %ecx
addl %edx, %ecx
movw %cx, (%rax)
jmp 0xec20e
movl 0x17c(%rsp), %edx
movq 0x158(%rsp), %rax
movswl (%rax), %ecx
addl %edx, %ecx
movw %cx, (%rax)
jmp 0xec210
jmp 0xec212
jmp 0xec214
jmp 0xec216
movl 0x174(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x174(%rsp)
jmp 0xec0ed
movl 0x16c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x16c(%rsp)
movq 0x120(%rsp), %rcx
movq 0x198(%rsp), %rax
movq 0x28(%rax), %rax
movq %rcx, (%rax)
movq 0x128(%rsp), %rcx
movq 0x198(%rsp), %rax
movq 0x28(%rax), %rax
movq %rcx, 0x8(%rax)
movq 0x150(%rsp), %rcx
movq 0x188(%rsp), %rax
movq %rcx, 0x18(%rax)
movl 0x14c(%rsp), %ecx
movq 0x188(%rsp), %rax
movl %ecx, 0x20(%rax)
movl 0x16c(%rsp), %ecx
movq 0x188(%rsp), %rax
movl %ecx, 0x28(%rax)
movq 0x188(%rsp), %rax
movl 0x3c(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x3c(%rax)
movl $0x1, 0x1a4(%rsp)
jmp 0xec302
jmp 0xec2c4
cmpl $0x0, 0x114(%rsp)
jle 0xec2f7
movq 0x160(%rsp), %rax
movl 0x114(%rsp), %ecx
addl $-0x1, %ecx
movl %ecx, 0x114(%rsp)
movslq %ecx, %rcx
movslq 0x10(%rsp,%rcx,4), %rcx
movw $0x0, (%rax,%rcx,2)
jmp 0xec2c4
movl $0x0, 0x1a4(%rsp)
movl 0x1a4(%rsp), %eax
addq $0x1a8, %rsp            # imm = 0x1A8
retq
nopw %cs:(%rax,%rax)