// File: tomfunkhouser[P]gaps/pkgs/jpeg/jchuff.c
// Function: encode_mcu_huff
METHODDEF(jboolean)
encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  working_state state;
  int blkn, ci;
  jpeg_component_info * compptr;

  /* Load up working state */
  state.next_output_byte = cinfo->dest->next_output_byte;
  state.free_in_buffer = cinfo->dest->free_in_buffer;
  ASSIGN_STATE(state.cur, entropy->saved);
  state.cinfo = cinfo;

  /* Emit restart marker if needed */
  if (cinfo->restart_interval) {
    if (entropy->restarts_to_go == 0)
      if (! emit_restart(&state, entropy->next_restart_num))
	return FALSE;
  }

  /* Encode the MCU data blocks */
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
    ci = cinfo->MCU_membership[blkn];
    compptr = cinfo->cur_comp_info[ci];
    if (! encode_one_block(&state,
			   MCU_data[blkn][0], state.cur.last_dc_val[ci],
			   entropy->dc_derived_tbls[compptr->dc_tbl_no],
			   entropy->ac_derived_tbls[compptr->ac_tbl_no]))
      return FALSE;
    /* Update last_dc_val */
    state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
  }

  /* Completed MCU, so update state */
  cinfo->dest->next_output_byte = state.next_output_byte;
  cinfo->dest->free_in_buffer = state.free_in_buffer;
  ASSIGN_STATE(entropy->saved, state.cur);

  /* 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 $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq 0x58(%rsp), %rax
movq 0x1f0(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x58(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x58(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x20(%rax), %rcx
movq %rcx, 0x28(%rsp)
movq 0x28(%rax), %rcx
movq %rcx, 0x30(%rsp)
movq 0x30(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x58(%rsp), %rax
cmpl $0x0, 0x118(%rax)
je 0xde496
movq 0x48(%rsp), %rax
cmpl $0x0, 0x38(%rax)
jne 0xde494
movq 0x48(%rsp), %rax
movl 0x3c(%rax), %esi
leaq 0x10(%rsp), %rdi
callq 0xde8e0
cmpl $0x0, %eax
jne 0xde492
movl $0x0, 0x64(%rsp)
jmp 0xde609
jmp 0xde494
jmp 0xde496
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl 0x170(%rcx), %eax
jge 0xde55f
movq 0x58(%rsp), %rax
movslq 0xc(%rsp), %rcx
movl 0x174(%rax,%rcx,4), %eax
movl %eax, 0x8(%rsp)
movq 0x58(%rsp), %rax
movslq 0x8(%rsp), %rcx
movq 0x148(%rax,%rcx,8), %rax
movq %rax, (%rsp)
movq 0x50(%rsp), %rax
movslq 0xc(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movslq 0x8(%rsp), %rax
movl 0x2c(%rsp,%rax,4), %edx
movq 0x48(%rsp), %rax
movq (%rsp), %rcx
movslq 0x14(%rcx), %rcx
movq 0x40(%rax,%rcx,8), %rcx
movq 0x48(%rsp), %rax
movq (%rsp), %rdi
movslq 0x18(%rdi), %rdi
movq 0x60(%rax,%rdi,8), %r8
leaq 0x10(%rsp), %rdi
callq 0xde9f0
cmpl $0x0, %eax
jne 0xde535
movl $0x0, 0x64(%rsp)
jmp 0xde609
movq 0x50(%rsp), %rax
movslq 0xc(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movswl (%rax), %ecx
movslq 0x8(%rsp), %rax
movl %ecx, 0x2c(%rsp,%rax,4)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0xde49e
movq 0x10(%rsp), %rcx
movq 0x58(%rsp), %rax
movq 0x28(%rax), %rax
movq %rcx, (%rax)
movq 0x18(%rsp), %rcx
movq 0x58(%rsp), %rax
movq 0x28(%rax), %rax
movq %rcx, 0x8(%rax)
movq 0x48(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x18(%rax)
movq 0x28(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x30(%rsp), %rcx
movq %rcx, 0x28(%rax)
movq 0x38(%rsp), %rcx
movq %rcx, 0x30(%rax)
movq 0x58(%rsp), %rax
cmpl $0x0, 0x118(%rax)
je 0xde601
movq 0x48(%rsp), %rax
cmpl $0x0, 0x38(%rax)
jne 0xde5f3
movq 0x58(%rsp), %rax
movl 0x118(%rax), %ecx
movq 0x48(%rsp), %rax
movl %ecx, 0x38(%rax)
movq 0x48(%rsp), %rax
movl 0x3c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x3c(%rax)
movq 0x48(%rsp), %rax
movl 0x3c(%rax), %ecx
andl $0x7, %ecx
movl %ecx, 0x3c(%rax)
movq 0x48(%rsp), %rax
movl 0x38(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x38(%rax)
movl $0x1, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)