// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq 0x1f0(%rdi), %rbx
movl 0x1a0(%rdi), %r10d
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 0x79643
cmpl $0x0, 0x70(%rbx)
jne 0x79643
movl 0x74(%rbx), %esi
movq %rbx, %rdi
movl %r10d, %r14d
callq 0x799ff
movl %r14d, %r10d
movq (%r15), %r11
movslq 0x19c(%r12), %r14
xorl %r13d, %r13d
cmpl %r10d, %r14d
jg 0x796b6
leaq (,%r14,4), %rax
addq 0x6b92b(%rip), %rax     # 0xe4f90
leaq (%rsp,%r14,4), %rdx
addq $0x30, %rdx
movl %r14d, %esi
movl %r10d, %edi
notl %edi
addq %rsi, %rdi
xorl %r8d, %r8d
movslq (%rax,%r8,4), %rcx
movzwl (%r11,%rcx,2), %ecx
movl %ecx, %r9d
negw %r9w
cmovsw %cx, %r9w
movzwl %r9w, %r9d
movl %ebp, %ecx
shrl %cl, %r9d
leal (%rsi,%r8), %ecx
cmpl $0x1, %r9d
cmovel %ecx, %r13d
movl %r9d, (%rdx,%r8,4)
incq %r8
movl %edi, %ecx
addl %r8d, %ecx
jne 0x7967b
movslq %r13d, %r13
xorl %ebp, %ebp
movl $0x0, %eax
cmpl %r10d, %r14d
jg 0x79865
movq %r11, 0x28(%rsp)
movq %r12, 0x20(%rsp)
movl 0x60(%rbx), %r15d
addq 0x68(%rbx), %r15
incl %r10d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %r13, 0x10(%rsp)
movl %r10d, 0xc(%rsp)
movl 0x30(%rsp,%r14,4), %eax
testl %eax, %eax
je 0x79741
movl %eax, 0x1c(%rsp)
cmpl $0x10, %r12d
jl 0x7974e
cmpq %r13, %r14
jg 0x7974e
movl %r12d, %r13d
movq %rbx, %rdi
callq 0x79b8b
movl 0x58(%rbx), %esi
movq %rbx, %rdi
movl $0xf0, %edx
callq 0x79a98
leal -0x10(%r13), %r12d
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x79d88
movq 0x68(%rbx), %r15
xorl %ebp, %ebp
cmpl $0x20, %r13d
jl 0x79749
movq 0x10(%rsp), %r13
jmp 0x796fd
incl %r12d
jmp 0x7984e
movq 0x10(%rsp), %r13
movl 0x1c(%rsp), %ecx
cmpl $0x2, %ecx
jl 0x79767
andb $0x1, %cl
movl %ebp, %eax
incl %ebp
movb %cl, (%r15,%rax)
jmp 0x79849
movq %rbx, %rdi
callq 0x79b8b
movl 0x58(%rbx), %esi
shll $0x4, %r12d
incl %r12d
movq %rbx, %rdi
movl %r12d, %edx
callq 0x79a98
cmpl $0x0, 0x18(%rbx)
jne 0x79833
leaq 0x314eb(%rip), %rax     # 0xaac80
movslq (%rax,%r14,4), %rax
xorl %r12d, %r12d
movq 0x28(%rsp), %rcx
cmpw $0x0, (%rcx,%rax,2)
movl 0x38(%rbx), %r13d
setns %r12b
movb $0x17, %cl
subb %r13b, %cl
shlq %cl, %r12
orq 0x30(%rbx), %r12
cmpl $0x7, %r13d
jl 0x79823
addl $0x9, %r13d
movl %r12d, %eax
shrl $0x10, %eax
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
movb %al, (%rcx)
decq 0x28(%rbx)
jne 0x797e6
movq %rbx, %rdi
callq 0x79d4a
movl %r12d, %eax
notl %eax
testl $0xff0000, %eax         # imm = 0xFF0000
jne 0x7980f
movq 0x20(%rbx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%rbx)
movb $0x0, (%rax)
decq 0x28(%rbx)
jne 0x7980f
movq %rbx, %rdi
callq 0x79d4a
shlq $0x8, %r12
addl $-0x8, %r13d
cmpl $0xf, %r13d
jg 0x797c4
addl $-0x8, %r13d
jmp 0x79826
incl %r13d
movq %r12, 0x30(%rbx)
movl %r13d, 0x38(%rbx)
movq 0x10(%rsp), %r13
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x79d88
movq 0x68(%rbx), %r15
xorl %ebp, %ebp
xorl %r12d, %r12d
movl 0xc(%rsp), %r10d
incq %r14
cmpl %r14d, %r10d
jne 0x796ea
testl %r12d, %r12d
setg %al
movq 0x20(%rsp), %r12
testl %ebp, %ebp
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x79896
movl 0x5c(%rbx), %eax
incl %eax
movl %eax, 0x5c(%rbx)
addl 0x60(%rbx), %ebp
movl %ebp, 0x60(%rbx)
cmpl $0x7fff, %eax           # imm = 0x7FFF
je 0x7988e
cmpl $0x3aa, %ebp            # imm = 0x3AA
jb 0x79896
movq %rbx, %rdi
callq 0x79b8b
movq 0x20(%rbx), %rax
movq 0x28(%r12), %rcx
movq %rax, (%rcx)
movq 0x28(%rbx), %rax
movq %rax, 0x8(%rcx)
movl 0x118(%r12), %eax
testl %eax, %eax
je 0x798cd
cmpl $0x0, 0x70(%rbx)
jne 0x798ca
movl %eax, 0x70(%rbx)
movl 0x74(%rbx), %eax
incl %eax
andl $0x7, %eax
movl %eax, 0x74(%rbx)
decl 0x70(%rbx)
movl $0x1, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq