// File: /workspace/github_cpp_makefile_star2_O1/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
movl 0x1a0(%rdi), %r12d
movl 0x1a8(%rdi), %eax
movl %eax, 0x1c(%rsp)
movq 0x28(%rdi), %rax
movq (%rax), %rcx
movq %rcx, 0x20(%rbx)
movq 0x8(%rax), %rax
movq %rax, 0x28(%rbx)
movq %rdi, 0x10(%rsp)
cmpl $0x0, 0x118(%rdi)
je 0x79288
cmpl $0x0, 0x70(%rbx)
jne 0x79288
movl 0x74(%rbx), %esi
movq %rbx, %rdi
callq 0x799ff
movq 0x10(%rsp), %rax
movslq 0x19c(%rax), %r14
cmpl %r12d, %r14d
jg 0x7943c
movq (%r15), %rax
movq %rax, 0x20(%rsp)
incl %r12d
xorl %r15d, %r15d
movl %r12d, 0xc(%rsp)
leaq 0x319c9(%rip), %rax     # 0xaac80
movslq (%rax,%r14,4), %rax
movq 0x20(%rsp), %rcx
movswl (%rcx,%rax,2), %eax
testl %eax, %eax
je 0x79400
movl %eax, %edx
negl %edx
movl 0x1c(%rsp), %ecx
shrl %cl, %edx
movl %eax, %ebp
shrl %cl, %ebp
testw %ax, %ax
movl %edx, %r13d
notl %r13d
cmovnsl %ebp, %r13d
cmovsl %edx, %ebp
testl %ebp, %ebp
je 0x79400
cmpl $0x0, 0x5c(%rbx)
je 0x79300
movq %rbx, %rdi
callq 0x79b8b
cmpl $0x10, %r15d
jl 0x79323
movl 0x58(%rbx), %esi
movq %rbx, %rdi
movl $0xf0, %edx
callq 0x79a98
leal -0x10(%r15), %eax
cmpl $0x1f, %r15d
movl %eax, %r15d
jg 0x79306
movl $0x1, %r12d
cmpl $0x1, %ebp
je 0x79342
movl $0x1, %r12d
movl %ebp, %eax
shrl %eax
incl %r12d
cmpl $0x3, %ebp
movl %eax, %ebp
ja 0x79336
cmpl $0xb, %r12d
jb 0x79359
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movl $0x6, 0x28(%rax)
callq *(%rax)
movl 0x58(%rbx), %esi
shll $0x4, %r15d
addl %r12d, %r15d
movq %rbx, %rdi
movl %r15d, %edx
callq 0x79a98
xorl %r15d, %r15d
cmpl $0x0, 0x18(%rbx)
jne 0x7940f
movq $-0x1, %rbp
movl %r12d, %ecx
shlq %cl, %rbp
notl %ebp
andl %r13d, %ebp
addl 0x38(%rbx), %r12d
movb $0x18, %cl
subb %r12b, %cl
shlq %cl, %rbp
orq 0x30(%rbx), %rbp
cmpl $0x8, %r12d
jl 0x79405
movl %ebp, %eax
shrl $0x10, %eax
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
movb %al, (%rcx)
decq 0x28(%rbx)
jne 0x793c4
movq %rbx, %rdi
callq 0x79d4a
movl %ebp, %eax
notl %eax
testl $0xff0000, %eax         # imm = 0xFF0000
jne 0x793ec
movq 0x20(%rbx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%rbx)
movb $0x0, (%rax)
decq 0x28(%rbx)
jne 0x793ec
movq %rbx, %rdi
callq 0x79d4a
shlq $0x8, %rbp
leal -0x8(%r12), %eax
cmpl $0xf, %r12d
movl %eax, %r12d
jg 0x793a3
jmp 0x79408
incl %r15d
jmp 0x79414
movl %r12d, %eax
movq %rbp, 0x30(%rbx)
movl %eax, 0x38(%rbx)
movl 0xc(%rsp), %r12d
incq %r14
cmpl %r14d, %r12d
jne 0x792b0
testl %r15d, %r15d
jle 0x7943c
movl 0x5c(%rbx), %eax
incl %eax
movl %eax, 0x5c(%rbx)
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x7943c
movq %rbx, %rdi
callq 0x79b8b
movq 0x20(%rbx), %rax
movq 0x10(%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 0x79475
cmpl $0x0, 0x70(%rbx)
jne 0x79472
movl %eax, 0x70(%rbx)
movl 0x74(%rbx), %eax
incl %eax
andl $0x7, %eax
movl %eax, 0x74(%rbx)
decl 0x70(%rbx)
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq