// File: tomfunkhouser[P]gaps/pkgs/jpeg/jcphuff.c
// Function: encode_mcu_AC_refine
METHODDEF(jboolean)
encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  register int temp;
  register int r, k;
  int EOB;
  char *BR_buffer;
  unsigned int BR;
  int Se = cinfo->Se;
  int Al = cinfo->Al;
  JBLOCKROW block;
  int absvalues[DCTSIZE2];

  entropy->next_output_byte = cinfo->dest->next_output_byte;
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;

  /* Emit restart marker if needed */
  if (cinfo->restart_interval)
    if (entropy->restarts_to_go == 0)
      emit_restart(entropy, entropy->next_restart_num);

  /* Encode the MCU data block */
  block = MCU_data[0];

  /* It is convenient to make a pre-pass to determine the transformed
   * coefficients' absolute values and the EOB position.
   */
  EOB = 0;
  for (k = cinfo->Ss; k <= Se; k++) {
    temp = (*block)[jpeg_natural_order[k]];
    /* We must apply the point transform by Al.  For AC coefficients this
     * is an integer division with rounding towards 0.  To do this portably
     * in C, we shift after obtaining the absolute value.
     */
    if (temp < 0)
      temp = -temp;		/* temp is abs value of input */
    temp >>= Al;		/* apply the point transform */
    absvalues[k] = temp;	/* save abs value for main pass */
    if (temp == 1)
      EOB = k;			/* EOB = index of last newly-nonzero coef */
  }

  /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
  
  r = 0;			/* r = run length of zeros */
  BR = 0;			/* BR = count of buffered bits added now */
  BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */

  for (k = cinfo->Ss; k <= Se; k++) {
    if ((temp = absvalues[k]) == 0) {
      r++;
      continue;
    }

    /* Emit any required ZRLs, but not if they can be folded into EOB */
    while (r > 15 && k <= EOB) {
      /* emit any pending EOBRUN and the BE correction bits */
      emit_eobrun(entropy);
      /* Emit ZRL */
      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
      r -= 16;
      /* Emit buffered correction bits that must be associated with ZRL */
      emit_buffered_bits(entropy, BR_buffer, BR);
      BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
      BR = 0;
    }

    /* If the coef was previously nonzero, it only needs a correction bit.
     * NOTE: a straight translation of the spec's figure G.7 would suggest
     * that we also need to test r > 15.  But if r > 15, we can only get here
     * if k > EOB, which implies that this coefficient is not 1.
     */
    if (temp > 1) {
      /* The correction bit is the next bit of the absolute value. */
      BR_buffer[BR++] = (char) (temp & 1);
      continue;
    }

    /* Emit any pending EOBRUN and the BE correction bits */
    emit_eobrun(entropy);

    /* Count/emit Huffman symbol for run length / number of bits */
    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);

    /* Emit output bit for newly-nonzero coef */
    temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
    emit_bits(entropy, (unsigned int) temp, 1);

    /* Emit buffered correction bits that must be associated with this code */
    emit_buffered_bits(entropy, BR_buffer, BR);
    BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
    BR = 0;
    r = 0;			/* reset zero run length */
  }

  if (r > 0 || BR > 0) {	/* If there are trailing zeroes, */
    entropy->EOBRUN++;		/* count an EOB */
    entropy->BE += BR;		/* concat my correction bits to older ones */
    /* We force out the EOB if we risk either:
     * 1. overflow of the EOB counter;
     * 2. overflow of the correction bit buffer during the next MCU.
     */
    if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
      emit_eobrun(entropy);
  }

  cinfo->dest->next_output_byte = entropy->next_output_byte;
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;

  /* Update restart-interval state too */
  if (cinfo->restart_interval) {
    if (entropy->restarts_to_go == 0) {
      entropy->restarts_to_go = cinfo->restart_interval;
      entropy->next_restart_num++;
      entropy->next_restart_num &= 7;
    }
    entropy->restarts_to_go--;
  }

  return TRUE;
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x150(%rsp)
movq %rsi, 0x148(%rsp)
movq 0x150(%rsp), %rax
movq 0x1f0(%rax), %rax
movq %rax, 0x140(%rsp)
movq 0x150(%rsp), %rax
movl 0x1a0(%rax), %eax
movl %eax, 0x120(%rsp)
movq 0x150(%rsp), %rax
movl 0x1a8(%rax), %eax
movl %eax, 0x11c(%rsp)
movq 0x150(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rcx
movq 0x140(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x150(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rcx
movq 0x140(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x150(%rsp), %rax
cmpl $0x0, 0x118(%rax)
je 0xdfa88
movq 0x140(%rsp), %rax
cmpl $0x0, 0x70(%rax)
jne 0xdfa86
movq 0x140(%rsp), %rdi
movq 0x140(%rsp), %rax
movl 0x74(%rax), %esi
callq 0xe00e0
jmp 0xdfa88
movq 0x148(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x110(%rsp)
movl $0x0, 0x130(%rsp)
movq 0x150(%rsp), %rax
movl 0x19c(%rax), %eax
movl %eax, 0x134(%rsp)
movl 0x134(%rsp), %eax
cmpl 0x120(%rsp), %eax
jg 0xdfb69
movq 0x110(%rsp), %rax
movslq 0x134(%rsp), %rdx
leaq 0x57a7a(%rip), %rcx     # 0x137560
movslq (%rcx,%rdx,4), %rcx
movswl (%rax,%rcx,2), %eax
movl %eax, 0x13c(%rsp)
cmpl $0x0, 0x13c(%rsp)
jge 0xdfb0f
xorl %eax, %eax
subl 0x13c(%rsp), %eax
movl %eax, 0x13c(%rsp)
movl 0x11c(%rsp), %ecx
movl 0x13c(%rsp), %eax
sarl %cl, %eax
movl %eax, 0x13c(%rsp)
movl 0x13c(%rsp), %ecx
movslq 0x134(%rsp), %rax
movl %ecx, 0x10(%rsp,%rax,4)
cmpl $0x1, 0x13c(%rsp)
jne 0xdfb51
movl 0x134(%rsp), %eax
movl %eax, 0x130(%rsp)
jmp 0xdfb53
movl 0x134(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x134(%rsp)
jmp 0xdfabb
movl $0x0, 0x138(%rsp)
movl $0x0, 0x124(%rsp)
movq 0x140(%rsp), %rax
movq 0x68(%rax), %rax
movq 0x140(%rsp), %rcx
movl 0x60(%rcx), %ecx
addq %rcx, %rax
movq %rax, 0x128(%rsp)
movq 0x150(%rsp), %rax
movl 0x19c(%rax), %eax
movl %eax, 0x134(%rsp)
movl 0x134(%rsp), %eax
cmpl 0x120(%rsp), %eax
jg 0xdfdb9
movslq 0x134(%rsp), %rax
movl 0x10(%rsp,%rax,4), %eax
movl %eax, 0x13c(%rsp)
cmpl $0x0, %eax
jne 0xdfbf8
movl 0x138(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x138(%rsp)
jmp 0xdfda3
jmp 0xdfbfa
xorl %eax, %eax
cmpl $0xf, 0x138(%rsp)
movb %al, 0xf(%rsp)
jle 0xdfc1f
movl 0x134(%rsp), %eax
cmpl 0x130(%rsp), %eax
setle %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0xdfc29
jmp 0xdfca4
movq 0x140(%rsp), %rdi
callq 0xe0420
movq 0x140(%rsp), %rdi
movq 0x140(%rsp), %rax
movl 0x58(%rax), %esi
movl $0xf0, %edx
callq 0xe0200
movl 0x138(%rsp), %eax
subl $0x10, %eax
movl %eax, 0x138(%rsp)
movq 0x140(%rsp), %rdi
movq 0x128(%rsp), %rsi
movl 0x124(%rsp), %edx
callq 0xe05e0
movq 0x140(%rsp), %rax
movq 0x68(%rax), %rax
movq %rax, 0x128(%rsp)
movl $0x0, 0x124(%rsp)
jmp 0xdfbfa
cmpl $0x1, 0x13c(%rsp)
jle 0xdfcdf
movl 0x13c(%rsp), %eax
andl $0x1, %eax
movb %al, %dl
movq 0x128(%rsp), %rax
movl 0x124(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x124(%rsp)
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
jmp 0xdfda3
movq 0x140(%rsp), %rdi
callq 0xe0420
movq 0x140(%rsp), %rdi
movq 0x140(%rsp), %rax
movl 0x58(%rax), %esi
movl 0x138(%rsp), %edx
shll $0x4, %edx
addl $0x1, %edx
callq 0xe0200
movq 0x110(%rsp), %rax
movslq 0x134(%rsp), %rdx
leaq 0x57838(%rip), %rcx     # 0x137560
movslq (%rcx,%rdx,4), %rcx
movswl (%rax,%rcx,2), %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovll %ecx, %eax
movl %eax, 0x13c(%rsp)
movq 0x140(%rsp), %rdi
movl 0x13c(%rsp), %esi
movl $0x1, %edx
callq 0xe0280
movq 0x140(%rsp), %rdi
movq 0x128(%rsp), %rsi
movl 0x124(%rsp), %edx
callq 0xe05e0
movq 0x140(%rsp), %rax
movq 0x68(%rax), %rax
movq %rax, 0x128(%rsp)
movl $0x0, 0x124(%rsp)
movl $0x0, 0x138(%rsp)
movl 0x134(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x134(%rsp)
jmp 0xdfbb6
cmpl $0x0, 0x138(%rsp)
jg 0xdfdcd
cmpl $0x0, 0x124(%rsp)
jbe 0xdfe24
movq 0x140(%rsp), %rax
movl 0x5c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x5c(%rax)
movl 0x124(%rsp), %ecx
movq 0x140(%rsp), %rax
addl 0x60(%rax), %ecx
movl %ecx, 0x60(%rax)
movq 0x140(%rsp), %rax
cmpl $0x7fff, 0x5c(%rax)     # imm = 0x7FFF
je 0xdfe15
movq 0x140(%rsp), %rax
cmpl $0x3a9, 0x60(%rax)      # imm = 0x3A9
jbe 0xdfe22
movq 0x140(%rsp), %rdi
callq 0xe0420
jmp 0xdfe24
movq 0x140(%rsp), %rax
movq 0x20(%rax), %rcx
movq 0x150(%rsp), %rax
movq 0x28(%rax), %rax
movq %rcx, (%rax)
movq 0x140(%rsp), %rax
movq 0x28(%rax), %rcx
movq 0x150(%rsp), %rax
movq 0x28(%rax), %rax
movq %rcx, 0x8(%rax)
movq 0x150(%rsp), %rax
cmpl $0x0, 0x118(%rax)
je 0xdfec6
movq 0x140(%rsp), %rax
cmpl $0x0, 0x70(%rax)
jne 0xdfeb5
movq 0x150(%rsp), %rax
movl 0x118(%rax), %ecx
movq 0x140(%rsp), %rax
movl %ecx, 0x70(%rax)
movq 0x140(%rsp), %rax
movl 0x74(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x74(%rax)
movq 0x140(%rsp), %rax
movl 0x74(%rax), %ecx
andl $0x7, %ecx
movl %ecx, 0x74(%rax)
movq 0x140(%rsp), %rax
movl 0x70(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x70(%rax)
movl $0x1, %eax
addq $0x158, %rsp            # imm = 0x158
retq
nopw %cs:(%rax,%rax)