// File: 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:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq 0x40(%rsp), %rax
movq 0x1f0(%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x40(%rsp), %rax
movl 0x1a0(%rax), %eax
movl %eax, 0x18(%rsp)
movq 0x40(%rsp), %rax
movl 0x1a8(%rax), %eax
movl %eax, 0x14(%rsp)
movq 0x40(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x40(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x40(%rsp), %rax
cmpl $0x0, 0x118(%rax)
je 0xdf5f5
movq 0x30(%rsp), %rax
cmpl $0x0, 0x70(%rax)
jne 0xdf5f3
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rax
movl 0x74(%rax), %esi
callq 0xe00e0
jmp 0xdf5f5
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movl $0x0, 0x20(%rsp)
movq 0x40(%rsp), %rax
movl 0x19c(%rax), %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x18(%rsp), %eax
jg 0xdf787
movq 0x8(%rsp), %rax
movslq 0x1c(%rsp), %rdx
leaq 0x57f28(%rip), %rcx     # 0x137560
movslq (%rcx,%rdx,4), %rcx
movswl (%rax,%rcx,2), %eax
movl %eax, 0x2c(%rsp)
cmpl $0x0, %eax
jne 0xdf659
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0xdf777
cmpl $0x0, 0x2c(%rsp)
jge 0xdf685
xorl %eax, %eax
subl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x14(%rsp), %ecx
movl 0x2c(%rsp), %eax
sarl %cl, %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
xorl $-0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0xdf69b
movl 0x14(%rsp), %ecx
movl 0x2c(%rsp), %eax
sarl %cl, %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x28(%rsp)
cmpl $0x0, 0x2c(%rsp)
jne 0xdf6b2
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0xdf777
movq 0x30(%rsp), %rax
cmpl $0x0, 0x5c(%rax)
jbe 0xdf6c7
movq 0x30(%rsp), %rdi
callq 0xe0420
jmp 0xdf6c9
cmpl $0xf, 0x20(%rsp)
jle 0xdf6f4
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rax
movl 0x58(%rax), %esi
movl $0xf0, %edx
callq 0xe0200
movl 0x20(%rsp), %eax
subl $0x10, %eax
movl %eax, 0x20(%rsp)
jmp 0xdf6c9
movl $0x1, 0x24(%rsp)
movl 0x2c(%rsp), %eax
sarl %eax
movl %eax, 0x2c(%rsp)
cmpl $0x0, %eax
je 0xdf718
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0xdf6fc
cmpl $0xa, 0x24(%rsp)
jle 0xdf740
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl $0x6, 0x28(%rax)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x40(%rsp), %rdi
callq *%rax
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rax
movl 0x58(%rax), %esi
movl 0x20(%rsp), %edx
shll $0x4, %edx
addl 0x24(%rsp), %edx
callq 0xe0200
movq 0x30(%rsp), %rdi
movl 0x28(%rsp), %esi
movl 0x24(%rsp), %edx
callq 0xe0280
movl $0x0, 0x20(%rsp)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xdf619
cmpl $0x0, 0x20(%rsp)
jle 0xdf7b6
movq 0x30(%rsp), %rax
movl 0x5c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x5c(%rax)
movq 0x30(%rsp), %rax
cmpl $0x7fff, 0x5c(%rax)     # imm = 0x7FFF
jne 0xdf7b4
movq 0x30(%rsp), %rdi
callq 0xe0420
jmp 0xdf7b6
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rcx
movq 0x40(%rsp), %rax
movq 0x28(%rax), %rax
movq %rcx, (%rax)
movq 0x30(%rsp), %rax
movq 0x28(%rax), %rcx
movq 0x40(%rsp), %rax
movq 0x28(%rax), %rax
movq %rcx, 0x8(%rax)
movq 0x40(%rsp), %rax
cmpl $0x0, 0x118(%rax)
je 0xdf837
movq 0x30(%rsp), %rax
cmpl $0x0, 0x70(%rax)
jne 0xdf829
movq 0x40(%rsp), %rax
movl 0x118(%rax), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x70(%rax)
movq 0x30(%rsp), %rax
movl 0x74(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x74(%rax)
movq 0x30(%rsp), %rax
movl 0x74(%rax), %ecx
andl $0x7, %ecx
movl %ecx, 0x74(%rax)
movq 0x30(%rsp), %rax
movl 0x70(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x70(%rax)
movl $0x1, %eax
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)