// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/jpeg/jcphuff.c
// Function: encode_mcu_AC_first
METHODDEF(jboolean)
encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  register int temp, temp2;
  register int nbits;
  register int r, k;
  int Se = cinfo->Se;
  int Al = cinfo->Al;
  JBLOCKROW block;

  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];

  /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
  
  r = 0;			/* r = run length of zeros */
   
  for (k = cinfo->Ss; k <= Se; k++) {
    if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
      r++;
      continue;
    }
    /* 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; so the code is
     * interwoven with finding the abs value (temp) and output bits (temp2).
     */
    if (temp < 0) {
      temp = -temp;		/* temp is abs value of input */
      temp >>= Al;		/* apply the point transform */
      /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
      temp2 = ~temp;
    } else {
      temp >>= Al;		/* apply the point transform */
      temp2 = temp;
    }
    /* Watch out for case that nonzero coef is zero after point transform */
    if (temp == 0) {
      r++;
      continue;
    }

    /* Emit any pending EOBRUN */
    if (entropy->EOBRUN > 0)
      emit_eobrun(entropy);
    /* if run length > 15, must emit special run-length-16 codes (0xF0) */
    while (r > 15) {
      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
      r -= 16;
    }

    /* Find the number of bits needed for the magnitude of the coefficient */
    nbits = 1;			/* there must be at least one 1 bit */
    while ((temp >>= 1))
      nbits++;
    /* Check for out-of-range coefficient values */
    if (nbits > MAX_COEF_BITS)
      ERREXIT(cinfo, JERR_BAD_DCT_COEF);

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

    /* Emit that number of bits of the value, if positive, */
    /* or the complement of its magnitude, if negative. */
    emit_bits(entropy, (unsigned int) temp2, nbits);

    r = 0;			/* reset zero run length */
  }

  if (r > 0) {			/* If there are trailing zeroes, */
    entropy->EOBRUN++;		/* count an EOB */
    if (entropy->EOBRUN == 0x7FFF)
      emit_eobrun(entropy);	/* force it out to avoid overflow */
  }

  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 $0x28, %rsp
movq %rsi, %r15
movq 0x1f0(%rdi), %rbx
movslq 0x1a0(%rdi), %r12
movl 0x1a8(%rdi), %eax
movl %eax, 0x14(%rsp)
movq 0x28(%rdi), %rax
movq (%rax), %rcx
movq %rcx, 0x20(%rbx)
movq 0x8(%rax), %rax
movq %rax, 0x28(%rbx)
movq %rdi, 0x8(%rsp)
cmpl $0x0, 0x118(%rdi)
je 0x6d55e
cmpl $0x0, 0x70(%rbx)
jne 0x6d55e
movl 0x74(%rbx), %esi
movq %rbx, %rdi
callq 0x6dae4
movq (%r15), %rsi
movq 0x8(%rsp), %rax
movslq 0x19c(%rax), %r14
xorl %r13d, %r13d
leaq 0x2e469(%rip), %rdi     # 0x9b9e0
movq %r12, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
cmpq %r12, %r14
jg 0x6d65a
movslq (%rdi,%r14,4), %rax
movswl (%rsi,%rax,2), %eax
testl %eax, %eax
je 0x6d5f3
movl %eax, %edx
negl %edx
movl 0x14(%rsp), %ecx
shrl %cl, %edx
movl %eax, %ebp
shrl %cl, %ebp
testw %ax, %ax
movl %edx, %r15d
notl %r15d
cmovnsl %ebp, %r15d
cmovsl %edx, %ebp
testl %ebp, %ebp
je 0x6d5f3
cmpl $0x0, 0x5c(%rbx)
je 0x6d5c6
movq %rbx, %rdi
callq 0x6dc57
movl %r13d, %r12d
shll $0x4, %r12d
incl %r12d
cmpl $0xf, %r13d
jle 0x6d5f8
movl 0x58(%rbx), %esi
movq %rbx, %rdi
movl $0xf0, %edx
callq 0x6db6e
addl $-0x10, %r13d
addl $0xffffff00, %r12d      # imm = 0xFFFFFF00
jmp 0x6d5d0
incl %r13d
jmp 0x6d652
pushq $0x1
popq %r13
cmpl $0x2, %ebp
jb 0x6d60b
shrl %ebp
incl %r13d
incl %r12d
jmp 0x6d5fc
cmpl $0xb, %r13d
jb 0x6d622
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x6, 0x28(%rax)
callq *(%rax)
movl 0x58(%rbx), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x6db6e
movq %rbx, %rdi
movl %r15d, %esi
movl %r13d, %edx
callq 0x6db99
xorl %r13d, %r13d
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %rsi
leaq 0x2e38e(%rip), %rdi     # 0x9b9e0
incq %r14
jmp 0x6d581
testl %r13d, %r13d
jle 0x6d676
movl 0x5c(%rbx), %eax
incl %eax
movl %eax, 0x5c(%rbx)
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x6d676
movq %rbx, %rdi
callq 0x6dc57
movq 0x20(%rbx), %rax
movq 0x8(%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 0x6d6b1
movl 0x70(%rbx), %ecx
testl %ecx, %ecx
jne 0x6d6ac
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 $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq