// File: tomfunkhouser[P]gaps/pkgs/jpeg/jchuff.c
// Function: start_pass_huff
METHODDEF(void)
start_pass_huff (j_compress_ptr cinfo, jboolean gather_statistics)
{
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  int ci, dctbl, actbl;
  jpeg_component_info * compptr;

  if (gather_statistics) {
#ifdef ENTROPY_OPT_SUPPORTED
    entropy->pub.encode_mcu = encode_mcu_gather;
    entropy->pub.finish_pass = finish_pass_gather;
#else
    ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
  } else {
    entropy->pub.encode_mcu = encode_mcu_huff;
    entropy->pub.finish_pass = finish_pass_huff;
  }

  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
    compptr = cinfo->cur_comp_info[ci];
    dctbl = compptr->dc_tbl_no;
    actbl = compptr->ac_tbl_no;
    if (gather_statistics) {
#ifdef ENTROPY_OPT_SUPPORTED
      /* Check for invalid table indexes */
      /* (make_c_derived_tbl does this in the other path) */
      if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
      if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
      /* Allocate and zero the statistics tables */
      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
      if (entropy->dc_count_ptrs[dctbl] == NULL)
	entropy->dc_count_ptrs[dctbl] = (long *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				      257 * SIZEOF(long));
      MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
      if (entropy->ac_count_ptrs[actbl] == NULL)
	entropy->ac_count_ptrs[actbl] = (long *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				      257 * SIZEOF(long));
      MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
#endif
    } else {
      /* Compute derived values for Huffman tables */
      /* We may do this more than once for a table, but it's not expensive */
      jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
			      & entropy->dc_derived_tbls[dctbl]);
      jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
			      & entropy->ac_derived_tbls[actbl]);
    }
    /* Initialize DC predictions to 0 */
    entropy->saved.last_dc_val[ci] = 0;
  }

  /* Initialize bit buffer to empty */
  entropy->saved.put_buffer = 0;
  entropy->saved.put_bits = 0;

  /* Initialize restart stuff */
  entropy->restarts_to_go = cinfo->restart_interval;
  entropy->next_restart_num = 0;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movl %esi, 0x2c(%rsp)
movq 0x30(%rsp), %rax
movq 0x1f0(%rax), %rax
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x2c(%rsp)
je 0xdde67
movq 0x20(%rsp), %rax
leaq 0x28f(%rip), %rcx       # 0xde0e0
movq %rcx, 0x8(%rax)
movq 0x20(%rsp), %rax
leaq 0x3ef(%rip), %rcx       # 0xde250
movq %rcx, 0x10(%rax)
jmp 0xdde87
movq 0x20(%rsp), %rax
leaq 0x56d(%rip), %rcx       # 0xde3e0
movq %rcx, 0x8(%rax)
movq 0x20(%rsp), %rax
leaq 0x79d(%rip), %rcx       # 0xde620
movq %rcx, 0x10(%rax)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movq 0x30(%rsp), %rcx
cmpl 0x144(%rcx), %eax
jge 0xde095
movq 0x30(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movq 0x148(%rax,%rcx,8), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x18(%rsp)
movq 0x8(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x14(%rsp)
cmpl $0x0, 0x2c(%rsp)
je 0xde026
cmpl $0x0, 0x18(%rsp)
jl 0xddeec
cmpl $0x4, 0x18(%rsp)
jl 0xddf1c
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x32, 0x28(%rax)
movl 0x18(%rsp), %ecx
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
cmpl $0x0, 0x14(%rsp)
jl 0xddf2a
cmpl $0x4, 0x14(%rsp)
jl 0xddf5a
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x32, 0x28(%rax)
movl 0x14(%rsp), %ecx
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x20(%rsp), %rax
movslq 0x18(%rsp), %rcx
cmpq $0x0, 0x80(%rax,%rcx,8)
jne 0xddfa1
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
movl $0x1, %esi
movl $0x808, %edx            # imm = 0x808
callq *%rax
movq %rax, %rdx
movq 0x20(%rsp), %rax
movslq 0x18(%rsp), %rcx
movq %rdx, 0x80(%rax,%rcx,8)
movq 0x20(%rsp), %rax
movslq 0x18(%rsp), %rcx
movq 0x80(%rax,%rcx,8), %rdi
xorl %esi, %esi
movl $0x808, %edx            # imm = 0x808
callq 0xf280
movq 0x20(%rsp), %rax
movslq 0x14(%rsp), %rcx
cmpq $0x0, 0xa0(%rax,%rcx,8)
jne 0xde006
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
movl $0x1, %esi
movl $0x808, %edx            # imm = 0x808
callq *%rax
movq %rax, %rdx
movq 0x20(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq %rdx, 0xa0(%rax,%rcx,8)
movq 0x20(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq 0xa0(%rax,%rcx,8), %rdi
xorl %esi, %esi
movl $0x808, %edx            # imm = 0x808
callq 0xf280
jmp 0xde073
movq 0x30(%rsp), %rdi
movl 0x18(%rsp), %edx
movq 0x20(%rsp), %rcx
addq $0x40, %rcx
movslq 0x18(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rcx
movl $0x1, %esi
callq 0xdd420
movq 0x30(%rsp), %rdi
movl 0x14(%rsp), %edx
movq 0x20(%rsp), %rcx
addq $0x60, %rcx
movslq 0x14(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rcx
xorl %esi, %esi
callq 0xdd420
movq 0x20(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movl $0x0, 0x24(%rax,%rcx,4)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xdde8f
movq 0x20(%rsp), %rax
movq $0x0, 0x18(%rax)
movq 0x20(%rsp), %rax
movl $0x0, 0x20(%rax)
movq 0x30(%rsp), %rax
movl 0x118(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x38(%rax)
movq 0x20(%rsp), %rax
movl $0x0, 0x3c(%rax)
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)