// File: tomfunkhouser[P]gaps/pkgs/jpeg/jdphuff.c
// Function: start_pass_phuff_decoder
METHODDEF(void)
start_pass_phuff_decoder (j_decompress_ptr cinfo)
{
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  jboolean is_DC_band, bad;
  int ci, coefi, tbl;
  int *coef_bit_ptr;
  jpeg_component_info * compptr;

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

  /* Validate scan parameters */
  bad = FALSE;
  if (is_DC_band) {
    if (cinfo->Se != 0)
      bad = TRUE;
  } else {
    /* need not check Ss/Se < 0 since they came from unsigned bytes */
    if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
      bad = TRUE;
    /* AC scans may have only one component */
    if (cinfo->comps_in_scan != 1)
      bad = TRUE;
  }
  if (cinfo->Ah != 0) {
    /* Successive approximation refinement scan: must have Al = Ah-1. */
    if (cinfo->Al != cinfo->Ah-1)
      bad = TRUE;
  }
  if (cinfo->Al > 13)		/* need not check for < 0 */
    bad = TRUE;
  /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
   * but the spec doesn't say so, and we try to be liberal about what we
   * accept.  Note: large Al values could result in out-of-range DC
   * coefficients during early scans, leading to bizarre displays due to
   * overflows in the IDCT math.  But we won't crash.
   */
  if (bad)
    ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
	     cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
  /* Update progression status, and verify that scan order is legal.
   * Note that inter-scan inconsistencies are treated as warnings
   * not fatal errors ... not clear if this is right way to behave.
   */
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
    int cindex = cinfo->cur_comp_info[ci]->component_index;
    coef_bit_ptr = & cinfo->coef_bits[cindex][0];
    if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
      WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
    for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
      int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
      if (cinfo->Ah != expected)
	WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
      coef_bit_ptr[coefi] = cinfo->Al;
    }
  }

  /* Select MCU decoding routine */
  if (cinfo->Ah == 0) {
    if (is_DC_band)
      entropy->pub.decode_mcu = decode_mcu_DC_first;
    else
      entropy->pub.decode_mcu = decode_mcu_AC_first;
  } else {
    if (is_DC_band)
      entropy->pub.decode_mcu = decode_mcu_DC_refine;
    else
      entropy->pub.decode_mcu = decode_mcu_AC_refine;
  }

  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
    compptr = cinfo->cur_comp_info[ci];
    /* Make sure requested tables are present, and compute derived tables.
     * We may build same derived table more than once, but it's not expensive.
     */
    if (is_DC_band) {
      if (cinfo->Ah == 0) {	/* DC refinement needs no table */
	tbl = compptr->dc_tbl_no;
	jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
				& entropy->derived_tbls[tbl]);
      }
    } else {
      tbl = compptr->ac_tbl_no;
      jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
			      & entropy->derived_tbls[tbl]);
      /* remember the single active table */
      entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
    }
    /* Initialize DC predictions to 0 */
    entropy->saved.last_dc_val[ci] = 0;
  }

  /* Initialize bitread state variables */
  entropy->bitstate.bits_left = 0;
  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
  entropy->pub.insufficient_data = FALSE;

  /* Initialize private state variables */
  entropy->saved.EOBRUN = 0;

  /* Initialize restart counter */
  entropy->restarts_to_go = cinfo->restart_interval;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq 0x250(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rax
cmpl $0x0, 0x20c(%rax)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0xeab09
movq 0x40(%rsp), %rax
cmpl $0x0, 0x210(%rax)
je 0xeab07
movl $0x1, 0x30(%rsp)
jmp 0xeab4f
movq 0x40(%rsp), %rax
movl 0x20c(%rax), %eax
movq 0x40(%rsp), %rcx
cmpl 0x210(%rcx), %eax
jg 0xeab2f
movq 0x40(%rsp), %rax
cmpl $0x40, 0x210(%rax)
jl 0xeab37
movl $0x1, 0x30(%rsp)
movq 0x40(%rsp), %rax
cmpl $0x1, 0x1b0(%rax)
je 0xeab4d
movl $0x1, 0x30(%rsp)
jmp 0xeab4f
movq 0x40(%rsp), %rax
cmpl $0x0, 0x214(%rax)
je 0xeab84
movq 0x40(%rsp), %rax
movl 0x218(%rax), %eax
movq 0x40(%rsp), %rcx
movl 0x214(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0xeab82
movl $0x1, 0x30(%rsp)
jmp 0xeab84
movq 0x40(%rsp), %rax
cmpl $0xd, 0x218(%rax)
jle 0xeab9a
movl $0x1, 0x30(%rsp)
cmpl $0x0, 0x30(%rsp)
je 0xeac1a
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl $0x10, 0x28(%rax)
movq 0x40(%rsp), %rax
movl 0x20c(%rax), %ecx
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movq 0x40(%rsp), %rax
movl 0x210(%rax), %ecx
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x30(%rax)
movq 0x40(%rsp), %rax
movl 0x214(%rax), %ecx
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x34(%rax)
movq 0x40(%rsp), %rax
movl 0x218(%rax), %ecx
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x38(%rax)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x40(%rsp), %rdi
callq *%rax
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x1b0(%rcx), %eax
jge 0xeada8
movq 0x40(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movq 0x1b8(%rax,%rcx,8), %rax
movl 0x4(%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x40(%rsp), %rax
movq 0xc0(%rax), %rax
movslq 0xc(%rsp), %rcx
shlq $0x8, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x34(%rsp)
jne 0xeacc3
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
jge 0xeacc3
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl $0x73, 0x28(%rax)
movl 0xc(%rsp), %ecx
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl $0x0, 0x30(%rax)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq 0x40(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq *%rax
movq 0x40(%rsp), %rax
movl 0x20c(%rax), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x210(%rcx), %eax
jg 0xead96
movq 0x18(%rsp), %rax
movslq 0x28(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jge 0xeacff
xorl %eax, %eax
movl %eax, 0x4(%rsp)
jmp 0xead10
movq 0x18(%rsp), %rax
movslq 0x28(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x40(%rsp), %rax
movl 0x214(%rax), %eax
cmpl 0x8(%rsp), %eax
je 0xead6e
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl $0x73, 0x28(%rax)
movl 0xc(%rsp), %ecx
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movl 0x28(%rsp), %ecx
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x30(%rax)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq 0x40(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq *%rax
movq 0x40(%rsp), %rax
movl 0x218(%rax), %edx
movq 0x18(%rsp), %rax
movslq 0x28(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0xeacd2
jmp 0xead98
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xeac22
movq 0x40(%rsp), %rax
cmpl $0x0, 0x214(%rax)
jne 0xeade1
cmpl $0x0, 0x34(%rsp)
je 0xeadcf
movq 0x38(%rsp), %rax
leaq 0x177(%rip), %rcx       # 0xeaf40
movq %rcx, 0x8(%rax)
jmp 0xeaddf
movq 0x38(%rsp), %rax
leaq 0x5f5(%rip), %rcx       # 0xeb3d0
movq %rcx, 0x8(%rax)
jmp 0xeae0c
cmpl $0x0, 0x34(%rsp)
je 0xeadfa
movq 0x38(%rsp), %rax
leaq 0xa8c(%rip), %rcx       # 0xeb880
movq %rcx, 0x8(%rax)
jmp 0xeae0a
movq 0x38(%rsp), %rax
leaq 0xc3a(%rip), %rcx       # 0xeba40
movq %rcx, 0x8(%rax)
jmp 0xeae0c
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x1b0(%rcx), %eax
jge 0xeaef6
movq 0x40(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movq 0x1b8(%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0xeae8b
movq 0x40(%rsp), %rax
cmpl $0x0, 0x214(%rax)
jne 0xeae89
movq 0x10(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x24(%rsp)
movq 0x40(%rsp), %rdi
movl 0x24(%rsp), %edx
movq 0x38(%rsp), %rcx
addq $0x40, %rcx
movslq 0x24(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rcx
movl $0x1, %esi
callq 0xe9190
jmp 0xeaed4
movq 0x10(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x24(%rsp)
movq 0x40(%rsp), %rdi
movl 0x24(%rsp), %edx
movq 0x38(%rsp), %rcx
addq $0x40, %rcx
movslq 0x24(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rcx
xorl %esi, %esi
callq 0xe9190
movq 0x38(%rsp), %rax
movslq 0x24(%rsp), %rcx
movq 0x40(%rax,%rcx,8), %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0x60(%rax)
movq 0x38(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movl $0x0, 0x2c(%rax,%rcx,4)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xeae14
movq 0x38(%rsp), %rax
movl $0x0, 0x20(%rax)
movq 0x38(%rsp), %rax
movq $0x0, 0x18(%rax)
movq 0x38(%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x38(%rsp), %rax
movl $0x0, 0x28(%rax)
movq 0x40(%rsp), %rax
movl 0x170(%rax), %ecx
movq 0x38(%rsp), %rax
movl %ecx, 0x3c(%rax)
addq $0x48, %rsp
retq
nop