// File: /workspace/github_cpp_makefile_star2_O3/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), %r14d
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 0x783b6
cmpl $0x0, 0x70(%rbx)
jne 0x783b6
movl 0x74(%rbx), %esi
movq %rbx, %rdi
callq 0x78d3a
movq 0x8(%rsp), %rax
movslq 0x19c(%rax), %r12
cmpl %r14d, %r12d
jg 0x7865c
movq (%r15), %r13
incl %r14d
xorl %r15d, %r15d
movl %r14d, 0x4(%rsp)
movq %r13, 0x20(%rsp)
leaq 0x328db(%rip), %rax     # 0xaacc0
movslq (%rax,%r12,4), %rax
movswl (%r13,%rax,2), %eax
testl %eax, %eax
je 0x7861b
movl %eax, %edx
negl %edx
movl 0x14(%rsp), %ecx
shrl %cl, %edx
movl %eax, %ebp
shrl %cl, %ebp
testw %ax, %ax
movl %edx, %eax
notl %eax
cmovnsl %ebp, %eax
cmovsl %edx, %ebp
testl %ebp, %ebp
je 0x7861b
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x5c(%rbx)
je 0x7842c
movq %rbx, %rdi
callq 0x78ecc
movq %r12, 0x18(%rsp)
cmpl $0x10, %r15d
jl 0x78536
movslq 0x58(%rbx), %rax
cmpl $0x0, 0x18(%rbx)
je 0x78459
movq 0x98(%rbx,%rax,8), %rax
incq 0x780(%rax)
jmp 0x78525
movq 0x78(%rbx,%rax,8), %rax
movl 0x3c0(%rax), %r13d
movsbl 0x4f0(%rax), %r12d
movl 0x38(%rbx), %r14d
testl %r12d, %r12d
je 0x7850b
movq $-0x1, %rax
movl %r12d, %ecx
shlq %cl, %rax
notl %eax
andl %eax, %r13d
addl %r12d, %r14d
movb $0x18, %cl
subb %r14b, %cl
shlq %cl, %r13
orq 0x30(%rbx), %r13
cmpl $0x8, %r14d
jl 0x784ff
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 0x784c3
movq %rbx, %rdi
callq 0x7907c
movl %r13d, %eax
notl %eax
testl $0xff0000, %eax         # imm = 0xFF0000
jne 0x784ec
movq 0x20(%rbx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%rbx)
movb $0x0, (%rax)
decq 0x28(%rbx)
jne 0x784ec
movq %rbx, %rdi
callq 0x7907c
shlq $0x8, %r13
leal -0x8(%r14), %eax
cmpl $0xf, %r14d
movl %eax, %r14d
jg 0x784a1
jmp 0x78502
movl %r14d, %eax
movq %r13, 0x30(%rbx)
movl %eax, 0x38(%rbx)
jmp 0x78525
movq 0x40(%rbx), %rdi
movq (%rdi), %rax
movl $0x28, 0x28(%rax)
callq *(%rax)
cmpl $0x0, 0x18(%rbx)
je 0x7847a
leal -0x10(%r15), %eax
cmpl $0x1f, %r15d
movl %eax, %r15d
jg 0x7843b
movl $0x1, %r12d
cmpl $0x1, %ebp
movq 0x20(%rsp), %r13
je 0x7856f
xorl %eax, %eax
movl %ebp, %ecx
shrl %ecx
incl %eax
cmpl $0x3, %ebp
movl %ecx, %ebp
ja 0x7854a
leal 0x1(%rax), %r12d
cmpl $0xa, %eax
jb 0x7856f
movq 0x8(%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 0x78dd3
xorl %r15d, %r15d
cmpl $0x0, 0x18(%rbx)
jne 0x7862a
movq $-0x1, %r14
movl %r12d, %ecx
shlq %cl, %r14
notl %r14d
andl 0x10(%rsp), %r14d
addl 0x38(%rbx), %r12d
movb $0x18, %cl
subb %r12b, %cl
shlq %cl, %r14
orq 0x30(%rbx), %r14
cmpl $0x8, %r12d
jl 0x78620
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 0x785de
movq %rbx, %rdi
callq 0x7907c
movl %r14d, %eax
notl %eax
testl $0xff0000, %eax         # imm = 0xFF0000
jne 0x78607
movq 0x20(%rbx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%rbx)
movb $0x0, (%rax)
decq 0x28(%rbx)
jne 0x78607
movq %rbx, %rdi
callq 0x7907c
shlq $0x8, %r14
leal -0x8(%r12), %eax
cmpl $0xf, %r12d
movl %eax, %r12d
jg 0x785bc
jmp 0x78623
incl %r15d
jmp 0x78634
movl %r12d, %eax
movq %r14, 0x30(%rbx)
movl %eax, 0x38(%rbx)
movl 0x4(%rsp), %r14d
movq 0x18(%rsp), %r12
incq %r12
cmpl %r12d, %r14d
jne 0x783de
testl %r15d, %r15d
jle 0x7865c
movl 0x5c(%rbx), %eax
incl %eax
movl %eax, 0x5c(%rbx)
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x7865c
movq %rbx, %rdi
callq 0x78ecc
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 0x78697
movl 0x70(%rbx), %ecx
testl %ecx, %ecx
jne 0x78692
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 $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq