// File: /workspace/github_cpp_makefile_star2_O3/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 $0x148, %rsp            # imm = 0x148
movq %rsi, %r15
movq %rdi, %r14
movq 0x1f0(%rdi), %rbx
movl 0x1a0(%rdi), %r12d
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 0x78865
cmpl $0x0, 0x70(%rbx)
jne 0x78865
movl 0x74(%rbx), %esi
movq %rbx, %rdi
callq 0x78d3a
movl 0x19c(%r14), %eax
cmpl %r12d, %eax
jle 0x7887b
xorl %r13d, %r13d
xorl %ecx, %ecx
jmp 0x78b99
movq (%r15), %r11
movslq %eax, %rcx
leaq (,%rcx,4), %rdx
addq 0x6c700(%rip), %rdx     # 0xe4f90
leaq (%rsp,%rcx,4), %rsi
addq $0x40, %rsi
movl %r12d, %edi
notl %edi
addq %rax, %rdi
xorl %r9d, %r9d
xorl %r8d, %r8d
movslq (%rdx,%r9,4), %rcx
movzwl (%r11,%rcx,2), %ecx
movl %ecx, %r10d
negw %r10w
cmovsw %cx, %r10w
movzwl %r10w, %r10d
movl %ebp, %ecx
shrl %cl, %r10d
leal (%rax,%r9), %ecx
cmpl $0x1, %r10d
cmovel %ecx, %r8d
movl %r10d, (%rsi,%r9,4)
incq %r9
movl %edi, %ecx
addl %r9d, %ecx
jne 0x788a6
xorl %r13d, %r13d
movl $0x0, %ecx
cmpl %r12d, %eax
jg 0x78b99
movq %r11, 0x30(%rsp)
movq %r14, 0x28(%rsp)
movslq %r8d, %rcx
movq %rcx, 0x38(%rsp)
movl 0x60(%rbx), %ebp
addq 0x68(%rbx), %rbp
movslq %eax, %r15
incl %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
movl %r12d, 0xc(%rsp)
movl 0x40(%rsp,%r15,4), %eax
testl %eax, %eax
je 0x78a64
movl %eax, 0x1c(%rsp)
cmpl $0x10, %r14d
movq %r15, 0x20(%rsp)
jl 0x78a6c
movl %r14d, %r12d
cmpq 0x38(%rsp), %r15
jg 0x78a6f
movl %r13d, 0x10(%rsp)
movq %rbp, %r15
movq %rbx, %rdi
callq 0x78ecc
movslq 0x58(%rbx), %rax
cmpl $0x0, 0x18(%rbx)
je 0x78975
movq 0x98(%rbx,%rax,8), %rax
incq 0x780(%rax)
jmp 0x78a3b
movq 0x78(%rbx,%rax,8), %rax
movl 0x3c0(%rax), %r13d
movsbl 0x4f0(%rax), %r12d
movl 0x38(%rbx), %ebp
testl %r12d, %r12d
je 0x78a21
movq $-0x1, %rax
movl %r12d, %ecx
shlq %cl, %rax
notl %eax
andl %eax, %r13d
addl %r12d, %ebp
movb $0x18, %cl
subb %bpl, %cl
shlq %cl, %r13
orq 0x30(%rbx), %r13
cmpl $0x8, %ebp
jl 0x78a16
movl %r13d, %eax
shrl $0x10, %eax
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
movb %al, (%rcx)
decq 0x28(%rbx)
jne 0x789dd
movq %rbx, %rdi
callq 0x7907c
movl %r13d, %eax
notl %eax
testl $0xff0000, %eax         # imm = 0xFF0000
jne 0x78a06
movq 0x20(%rbx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%rbx)
movb $0x0, (%rax)
decq 0x28(%rbx)
jne 0x78a06
movq %rbx, %rdi
callq 0x7907c
shlq $0x8, %r13
leal -0x8(%rbp), %eax
cmpl $0xf, %ebp
movl %eax, %ebp
jg 0x789bb
jmp 0x78a18
movl %ebp, %eax
movq %r13, 0x30(%rbx)
movl %eax, 0x38(%rbx)
jmp 0x78a3b
movq 0x40(%rbx), %rdi
movq (%rdi), %rax
movl $0x28, 0x28(%rax)
callq *(%rax)
cmpl $0x0, 0x18(%rbx)
je 0x78995
leal -0x10(%r14), %r12d
movq %rbx, %rdi
movq %r15, %rsi
movl 0x10(%rsp), %edx
callq 0x790ba
movq 0x68(%rbx), %rbp
xorl %r13d, %r13d
cmpl $0x1f, %r14d
movl %r12d, %r14d
jg 0x78947
jmp 0x78a6f
incl %r14d
jmp 0x78b82
movl %r14d, %r12d
movl 0x1c(%rsp), %ecx
cmpl $0x2, %ecx
jl 0x78a97
andb $0x1, %cl
movl %r13d, %eax
incl %r13d
movb %cl, (%rbp,%rax)
movl %r12d, %r14d
movl 0xc(%rsp), %r12d
movq 0x20(%rsp), %r15
jmp 0x78b82
movq %rbx, %rdi
callq 0x78ecc
movl 0x58(%rbx), %esi
shll $0x4, %r12d
incl %r12d
movq %rbx, %rdi
movl %r12d, %edx
callq 0x78dd3
cmpl $0x0, 0x18(%rbx)
movl 0xc(%rsp), %r12d
movq 0x20(%rsp), %r15
jne 0x78b6a
movq %rbp, 0x10(%rsp)
leaq 0x321ec(%rip), %rax     # 0xaacc0
movslq (%rax,%r15,4), %rax
xorl %r14d, %r14d
movq 0x30(%rsp), %rcx
cmpw $0x0, (%rcx,%rax,2)
movl 0x38(%rbx), %ebp
setns %r14b
movb $0x17, %cl
subb %bpl, %cl
shlq %cl, %r14
orq 0x30(%rbx), %r14
cmpl $0x7, %ebp
jl 0x78b5c
addl $0x9, %ebp
movl %r14d, %eax
shrl $0x10, %eax
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
movb %al, (%rcx)
decq 0x28(%rbx)
jne 0x78b22
movq %rbx, %rdi
callq 0x7907c
movl %r14d, %eax
notl %eax
testl $0xff0000, %eax         # imm = 0xFF0000
jne 0x78b4b
movq 0x20(%rbx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%rbx)
movb $0x0, (%rax)
decq 0x28(%rbx)
jne 0x78b4b
movq %rbx, %rdi
callq 0x7907c
shlq $0x8, %r14
addl $-0x8, %ebp
cmpl $0xf, %ebp
jg 0x78b00
addl $-0x8, %ebp
jmp 0x78b5e
incl %ebp
movq %r14, 0x30(%rbx)
movl %ebp, 0x38(%rbx)
movq 0x10(%rsp), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
movl %r13d, %edx
callq 0x790ba
movq 0x68(%rbx), %rbp
xorl %r13d, %r13d
xorl %r14d, %r14d
incq %r15
cmpl %r15d, %r12d
jne 0x78919
testl %r14d, %r14d
setg %cl
movq 0x28(%rsp), %r14
testl %r13d, %r13d
setne %al
orb %cl, %al
cmpb $0x1, %al
jne 0x78bcd
movl 0x5c(%rbx), %eax
incl %eax
movl %eax, 0x5c(%rbx)
addl 0x60(%rbx), %r13d
movl %r13d, 0x60(%rbx)
cmpl $0x7fff, %eax           # imm = 0x7FFF
je 0x78bc5
cmpl $0x3aa, %r13d           # imm = 0x3AA
jb 0x78bcd
movq %rbx, %rdi
callq 0x78ecc
movq 0x20(%rbx), %rax
movq 0x28(%r14), %rcx
movq %rax, (%rcx)
movq 0x28(%rbx), %rax
movq %rax, 0x8(%rcx)
movl 0x118(%r14), %eax
testl %eax, %eax
je 0x78c04
movl 0x70(%rbx), %ecx
testl %ecx, %ecx
jne 0x78bff
movl 0x74(%rbx), %ecx
incl %ecx
andl $0x7, %ecx
movl %ecx, 0x74(%rbx)
movl %eax, %ecx
decl %ecx
movl %ecx, 0x70(%rbx)
movl $0x1, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq