// File: tomfunkhouser[P]gaps/pkgs/jpeg/jcphuff.c
// Function: encode_mcu_DC_first
METHODDEF(jboolean)
encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  register int temp, temp2;
  register int nbits;
  int blkn, ci;
  int Al = cinfo->Al;
  JBLOCKROW block;
  jpeg_component_info * compptr;
  ISHIFT_TEMPS

  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 blocks */
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
    block = MCU_data[blkn];
    ci = cinfo->MCU_membership[blkn];
    compptr = cinfo->cur_comp_info[ci];

    /* Compute the DC value after the required point transform by Al.
     * This is simply an arithmetic right shift.
     */
    temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);

    /* DC differences are figured on the point-transformed values. */
    temp = temp2 - entropy->last_dc_val[ci];
    entropy->last_dc_val[ci] = temp2;

    /* Encode the DC coefficient difference per section G.1.2.1 */
    temp2 = temp;
    if (temp < 0) {
      temp = -temp;		/* temp is abs value of input */
      /* For a negative input, want temp2 = bitwise complement of abs(input) */
      /* This code assumes we are on a two's complement machine */
      temp2--;
    }
    
    /* Find the number of bits needed for the magnitude of the coefficient */
    nbits = 0;
    while (temp) {
      nbits++;
      temp >>= 1;
    }
    /* Check for out-of-range coefficient values.
     * Since we're encoding a difference, the range limit is twice as much.
     */
    if (nbits > MAX_COEF_BITS+1)
      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
    
    /* Count/emit the Huffman-coded symbol for the number of bits */
    emit_symbol(entropy, compptr->dc_tbl_no, nbits);
    
    /* Emit that number of bits of the value, if positive, */
    /* or the complement of its magnitude, if negative. */
    if (nbits)			/* emit_bits rejects calls with size 0 */
      emit_bits(entropy, (unsigned int) temp2, nbits);
  }

  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 0x1a8(%rax), %eax
movl %eax, 0x18(%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 0xdf386
movq 0x30(%rsp), %rax
cmpl $0x0, 0x70(%rax)
jne 0xdf384
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rax
movl 0x74(%rax), %esi
callq 0xe00e0
jmp 0xdf386
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x170(%rcx), %eax
jge 0xdf4cf
movq 0x38(%rsp), %rax
movslq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
movq 0x40(%rsp), %rax
movslq 0x20(%rsp), %rcx
movl 0x174(%rax,%rcx,4), %eax
movl %eax, 0x1c(%rsp)
movq 0x40(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movq 0x148(%rax,%rcx,8), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rax
movswl (%rax), %eax
movl 0x18(%rsp), %ecx
sarl %cl, %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movq 0x30(%rsp), %rcx
movslq 0x1c(%rsp), %rdx
subl 0x48(%rcx,%rdx,4), %eax
movl %eax, 0x2c(%rsp)
movl 0x28(%rsp), %edx
movq 0x30(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movl %edx, 0x48(%rax,%rcx,4)
movl 0x2c(%rsp), %eax
movl %eax, 0x28(%rsp)
cmpl $0x0, 0x2c(%rsp)
jge 0xdf440
xorl %eax, %eax
subl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x28(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
cmpl $0x0, 0x2c(%rsp)
je 0xdf466
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
movl 0x2c(%rsp), %eax
sarl %eax
movl %eax, 0x2c(%rsp)
jmp 0xdf448
cmpl $0xb, 0x24(%rsp)
jle 0xdf48e
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 0x8(%rsp), %rax
movl 0x14(%rax), %esi
movl 0x24(%rsp), %edx
callq 0xe0200
cmpl $0x0, 0x24(%rsp)
je 0xdf4bd
movq 0x30(%rsp), %rdi
movl 0x28(%rsp), %esi
movl 0x24(%rsp), %edx
callq 0xe0280
jmp 0xdf4bf
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0xdf38e
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 0xdf550
movq 0x30(%rsp), %rax
cmpl $0x0, 0x70(%rax)
jne 0xdf542
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 (%rax,%rax)