// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rsi, %r15
movq %rdi, %r14
movq 0x1f0(%rdi), %rbx
movslq 0x1a0(%rdi), %r13
movl 0x1a8(%rdi), %ebp
movq 0x28(%rdi), %rax
movq (%rax), %rcx
movq %rcx, 0x20(%rbx)
movq 0x8(%rax), %rax
movq %rax, 0x28(%rbx)
cmpl $0x0, 0x118(%rdi)
je 0x6d7dd
cmpl $0x0, 0x70(%rbx)
jne 0x6d7dd
movl 0x74(%rbx), %esi
movq %rbx, %rdi
callq 0x6dae4
movq (%r15), %rdi
movslq 0x19c(%r14), %r15
xorl %eax, %eax
leaq 0x2e1f0(%rip), %r8      # 0x9b9e0
movq %r15, %rdx
cmpq %r13, %rdx
jg 0x6d81f
movslq (%r8,%rdx,4), %rcx
movzwl (%rdi,%rcx,2), %ecx
movl %ecx, %esi
negw %si
cmovsw %cx, %si
movzwl %si, %esi
movl %ebp, %ecx
shrl %cl, %esi
movl %esi, 0x30(%rsp,%rdx,4)
cmpl $0x1, %esi
cmovel %edx, %eax
incq %rdx
jmp 0x6d7f3
movq %rdi, 0x20(%rsp)
movq %r14, 0x18(%rsp)
movl 0x60(%rbx), %ecx
addq 0x68(%rbx), %rcx
movq %rcx, 0x8(%rsp)
cltq
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
movq %r13, 0x10(%rsp)
cmpq %r13, %r15
jg 0x6d942
movl 0x30(%rsp,%r15,4), %ecx
testl %ecx, %ecx
je 0x6d8d5
movl %r14d, %r13d
shll $0x4, %r13d
incl %r13d
cmpl $0x10, %r14d
jl 0x6d8bd
cmpq 0x28(%rsp), %r15
jg 0x6d8bd
movq %rbx, %rdi
movq %r15, %rbp
movl %ecx, %r15d
callq 0x6dc57
movl 0x58(%rbx), %esi
movq %rbx, %rdi
movl $0xf0, %edx
callq 0x6db6e
addl $-0x10, %r14d
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl %r12d, %edx
callq 0x6dd21
movl %r15d, %ecx
movq %rbp, %r15
movq 0x68(%rbx), %rax
movq %rax, 0x8(%rsp)
addl $0xffffff00, %r13d      # imm = 0xFFFFFF00
xorl %r12d, %r12d
jmp 0x6d863
cmpl $0x2, %ecx
jl 0x6d8da
andb $0x1, %cl
movl %r12d, %eax
incl %r12d
movq 0x8(%rsp), %rdx
movb %cl, (%rdx,%rax)
jmp 0x6d935
incl %r14d
jmp 0x6d93a
movq %rbx, %rdi
callq 0x6dc57
movl 0x58(%rbx), %esi
movq %rbx, %rdi
movl %r13d, %edx
callq 0x6db6e
leaq 0x2e0e9(%rip), %rax     # 0x9b9e0
movslq (%rax,%r15,4), %rax
xorl %esi, %esi
movq 0x20(%rsp), %rcx
cmpw $0x0, (%rcx,%rax,2)
setns %sil
movq %rbx, %rdi
pushq $0x1
popq %rdx
callq 0x6db99
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl %r12d, %edx
callq 0x6dd21
movq 0x68(%rbx), %rax
movq %rax, 0x8(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
movq 0x10(%rsp), %r13
incq %r15
jmp 0x6d847
testl %r14d, %r14d
setg %al
testl %r12d, %r12d
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x6d97d
movl 0x5c(%rbx), %eax
incl %eax
movl %eax, 0x5c(%rbx)
addl 0x60(%rbx), %r12d
movl %r12d, 0x60(%rbx)
cmpl $0x7fff, %eax           # imm = 0x7FFF
je 0x6d975
cmpl $0x3aa, %r12d           # imm = 0x3AA
jb 0x6d97d
movq %rbx, %rdi
callq 0x6dc57
movq 0x20(%rbx), %rax
movq 0x18(%rsp), %rdx
movq 0x28(%rdx), %rcx
movq %rax, (%rcx)
movq 0x28(%rbx), %rax
movq %rax, 0x8(%rcx)
movl 0x118(%rdx), %eax
testl %eax, %eax
je 0x6d9b8
movl 0x70(%rbx), %ecx
testl %ecx, %ecx
jne 0x6d9b3
movl 0x74(%rbx), %ecx
incl %ecx
andl $0x7, %ecx
movl %ecx, 0x74(%rbx)
movl %eax, %ecx
decl %ecx
movl %ecx, 0x70(%rbx)
pushq $0x1
popq %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq