// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/jpeg/jcphuff.c
// Function: start_pass_phuff
METHODDEF(void)
start_pass_phuff (j_compress_ptr cinfo, jboolean gather_statistics)
{  
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  jboolean is_DC_band;
  int ci, tbl;
  jpeg_component_info * compptr;

  entropy->cinfo = cinfo;
  entropy->gather_statistics = gather_statistics;

  is_DC_band = (cinfo->Ss == 0);

  /* We assume jcmaster.c already validated the scan parameters. */

  /* Select execution routines */
  if (cinfo->Ah == 0) {
    if (is_DC_band)
      entropy->pub.encode_mcu = encode_mcu_DC_first;
    else
      entropy->pub.encode_mcu = encode_mcu_AC_first;
  } else {
    if (is_DC_band)
      entropy->pub.encode_mcu = encode_mcu_DC_refine;
    else {
      entropy->pub.encode_mcu = encode_mcu_AC_refine;
      /* AC refinement needs a correction bit buffer */
      if (entropy->bit_buffer == NULL)
	entropy->bit_buffer = (char *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				      MAX_CORR_BITS * SIZEOF(char));
    }
  }
  if (gather_statistics)
    entropy->pub.finish_pass = finish_pass_gather_phuff;
  else
    entropy->pub.finish_pass = finish_pass_phuff;

  /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
   * for AC coefficients.
   */
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
    compptr = cinfo->cur_comp_info[ci];
    /* Initialize DC predictions to 0 */
    entropy->last_dc_val[ci] = 0;
    /* Get table index */
    if (is_DC_band) {
      if (cinfo->Ah != 0)	/* DC refinement needs no table */
	continue;
      tbl = compptr->dc_tbl_no;
    } else {
      entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
    }
    if (gather_statistics) {
      /* Check for invalid table index */
      /* (make_c_derived_tbl does this in the other path) */
      if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
      /* Allocate and zero the statistics tables */
      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
      if (entropy->count_ptrs[tbl] == NULL)
	entropy->count_ptrs[tbl] = (long *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				      257 * SIZEOF(long));
      MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
    } else {
      /* Compute derived values for Huffman table */
      /* We may do this more than once for a table, but it's not expensive */
      jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
			      & entropy->derived_tbls[tbl]);
    }
  }

  /* Initialize AC stuff */
  entropy->EOBRUN = 0;
  entropy->BE = 0;

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

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movq 0x1f0(%rdi), %r15
movq %rdi, 0x40(%r15)
movl %esi, 0x18(%r15)
movl 0x19c(%rdi), %r12d
movl $0x0, 0xc(%rsp)
testl %r12d, %r12d
sete %r14b
cmpl $0x0, 0x1a4(%rdi)
je 0x78f04
testl %r12d, %r12d
je 0x78f12
leaq 0x705(%rip), %rax       # 0x795e5
movq %rax, 0x8(%r15)
cmpq $0x0, 0x68(%r15)
jne 0x78f26
movq 0x8(%rbx), %rax
movl $0x3e8, %edx            # imm = 0x3E8
movq %rbx, %rdi
movl $0x1, %esi
callq *(%rax)
movq %rax, 0x68(%r15)
jmp 0x78f26
testl %r12d, %r12d
je 0x78f1b
leaq 0x31d(%rip), %rax       # 0x7922d
jmp 0x78f22
leaq 0x570(%rip), %rax       # 0x79489
jmp 0x78f22
leaq 0x11b(%rip), %rax       # 0x7903d
movq %rax, 0x8(%r15)
testl %ebp, %ebp
leaq 0xa84(%rip), %rax       # 0x799b3
leaq 0x9ae(%rip), %rcx       # 0x798e4
cmoveq %rax, %rcx
movq %rcx, 0x10(%r15)
cmpl $0x0, 0x144(%rbx)
jle 0x79012
movl 0xc(%rsp), %eax
movb %r14b, %al
movl %eax, 0xc(%rsp)
leaq 0x78(%r15), %rax
movq %rax, 0x10(%rsp)
xorl %r14d, %r14d
movq 0x148(%rbx,%r14,8), %rax
movl $0x0, 0x48(%r15,%r14,4)
testl %r12d, %r12d
je 0x78fdc
movl 0x18(%rax), %edx
movl %edx, 0x58(%r15)
movslq %edx, %r13
testl %ebp, %ebp
je 0x78fea
cmpl $0x4, %edx
jb 0x78fa0
movq (%rbx), %rax
movl $0x32, 0x28(%rax)
movl %edx, 0x2c(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
cmpq $0x0, 0x98(%r15,%r13,8)
jne 0x78fc6
movq 0x8(%rbx), %rax
movl $0x808, %edx            # imm = 0x808
movq %rbx, %rdi
movl $0x1, %esi
callq *(%rax)
movq %rax, 0x98(%r15,%r13,8)
movq 0x98(%r15,%r13,8), %rdi
movl $0x808, %edx            # imm = 0x808
xorl %esi, %esi
callq 0xe260
jmp 0x78fff
cmpl $0x0, 0x1a4(%rbx)
jne 0x78fff
movl 0x14(%rax), %edx
jmp 0x78f7f
movq 0x10(%rsp), %rax
leaq (%rax,%r13,8), %rcx
movq %rbx, %rdi
movl 0xc(%rsp), %esi
callq 0x77ab4
incq %r14
movslq 0x144(%rbx), %rax
cmpq %rax, %r14
jl 0x78f62
xorl %eax, %eax
movq %rax, 0x5c(%r15)
movq %rax, 0x30(%r15)
movl %eax, 0x38(%r15)
movl 0x118(%rbx), %ecx
movl %ecx, 0x70(%r15)
movl %eax, 0x74(%r15)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq