// File: tomfunkhouser[P]gaps/pkgs/jpeg/jcmaster.c
// Function: prepare_for_pass
METHODDEF(void)
prepare_for_pass (j_compress_ptr cinfo)
{
  my_master_ptr master = (my_master_ptr) cinfo->master;

  switch (master->pass_type) {
  case main_pass:
    /* Initial pass: will collect input data, and do either Huffman
     * optimization or data output for the first scan.
     */
    select_scan_parameters(cinfo);
    per_scan_setup(cinfo);
    if (! cinfo->raw_data_in) {
      (*cinfo->cconvert->start_pass) (cinfo);
      (*cinfo->downsample->start_pass) (cinfo);
      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
    }
    (*cinfo->fdct->start_pass) (cinfo);
    (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
    (*cinfo->coef->start_pass) (cinfo,
				(master->total_passes > 1 ?
				 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
    if (cinfo->optimize_coding) {
      /* No immediate data output; postpone writing frame/scan headers */
      master->pub.call_pass_startup = FALSE;
    } else {
      /* Will write frame/scan headers at first jpeg_write_scanlines call */
      master->pub.call_pass_startup = TRUE;
    }
    break;
#ifdef ENTROPY_OPT_SUPPORTED
  case huff_opt_pass:
    /* Do Huffman optimization for a scan after the first one. */
    select_scan_parameters(cinfo);
    per_scan_setup(cinfo);
    if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
      (*cinfo->entropy->start_pass) (cinfo, TRUE);
      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
      master->pub.call_pass_startup = FALSE;
      break;
    }
    /* Special case: Huffman DC refinement scans need no Huffman table
     * and therefore we can skip the optimization pass for them.
     */
    master->pass_type = output_pass;
    master->pass_number++;
    /*FALLTHROUGH*/
#endif
  case output_pass:
    /* Do a data-output pass. */
    /* We need not repeat per-scan setup if prior optimization pass did it. */
    if (! cinfo->optimize_coding) {
      select_scan_parameters(cinfo);
      per_scan_setup(cinfo);
    }
    (*cinfo->entropy->start_pass) (cinfo, FALSE);
    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
    /* We emit frame/scan headers now */
    if (master->scan_number == 0)
      (*cinfo->marker->write_frame_header) (cinfo);
    (*cinfo->marker->write_scan_header) (cinfo);
    master->pub.call_pass_startup = FALSE;
    break;
  default:
    ERREXIT(cinfo, JERR_NOT_COMPILED);
  }

  master->pub.is_last_pass = (master->pass_number == master->total_passes-1);

  /* Set up progress monitor's pass info if present */
  if (cinfo->progress != NULL) {
    cinfo->progress->completed_passes = master->pass_number;
    cinfo->progress->total_passes = master->total_passes;
  }
}

// Assembly:
subq $0x18, %rsp
movq %rdi, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0x1b0(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x4(%rsp)
testl %eax, %eax
je 0xd753d
jmp 0xd751c
movl 0x4(%rsp), %eax
subl $0x1, %eax
je 0xd764a
jmp 0xd752b
movl 0x4(%rsp), %eax
subl $0x2, %eax
je 0xd76e9
jmp 0xd7785
movq 0x10(%rsp), %rdi
callq 0xd8660
movq 0x10(%rsp), %rdi
callq 0xd8850
movq 0x10(%rsp), %rax
cmpl $0x0, 0x100(%rax)
jne 0xd75a3
movq 0x10(%rsp), %rax
movq 0x1d8(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x1e0(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x1c0(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x1e8(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x1f0(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
movq 0x10(%rsp), %rcx
movl 0x108(%rcx), %esi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x1c8(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rcx
movl 0x28(%rcx), %edx
xorl %esi, %esi
movl $0x3, %ecx
cmpl $0x1, %edx
cmovgl %ecx, %esi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x1b8(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq *%rax
movq 0x10(%rsp), %rax
cmpl $0x0, 0x108(%rax)
je 0xd7639
movq 0x8(%rsp), %rax
movl $0x0, 0x18(%rax)
jmp 0xd7645
movq 0x8(%rsp), %rax
movl $0x1, 0x18(%rax)
jmp 0xd77a6
movq 0x10(%rsp), %rdi
callq 0xd8660
movq 0x10(%rsp), %rdi
callq 0xd8850
movq 0x10(%rsp), %rax
cmpl $0x0, 0x19c(%rax)
jne 0xd7688
movq 0x10(%rsp), %rax
cmpl $0x0, 0x1a4(%rax)
je 0xd7688
movq 0x10(%rsp), %rax
cmpl $0x0, 0x104(%rax)
je 0xd76cf
movq 0x10(%rsp), %rax
movq 0x1f0(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
movl $0x1, %esi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x1c8(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
movl $0x2, %esi
callq *%rax
movq 0x8(%rsp), %rax
movl $0x0, 0x18(%rax)
jmp 0xd77a6
movq 0x8(%rsp), %rax
movl $0x2, 0x20(%rax)
movq 0x8(%rsp), %rax
movl 0x24(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x24(%rax)
movq 0x10(%rsp), %rax
cmpl $0x0, 0x108(%rax)
jne 0xd770b
movq 0x10(%rsp), %rdi
callq 0xd8660
movq 0x10(%rsp), %rdi
callq 0xd8850
movq 0x10(%rsp), %rax
movq 0x1f0(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x1c8(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
movl $0x2, %esi
callq *%rax
movq 0x8(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
jne 0xd7760
movq 0x10(%rsp), %rax
movq 0x1d0(%rax), %rax
movq 0x8(%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x1d0(%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movq 0x8(%rsp), %rax
movl $0x0, 0x18(%rax)
jmp 0xd77a6
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl $0x30, 0x28(%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movq 0x8(%rsp), %rax
movl 0x24(%rax), %eax
movq 0x8(%rsp), %rcx
movl 0x28(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
sete %al
andb $0x1, %al
movzbl %al, %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x1c(%rax)
movq 0x10(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0xd77ff
movq 0x8(%rsp), %rax
movl 0x24(%rax), %ecx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movl %ecx, 0x18(%rax)
movq 0x8(%rsp), %rax
movl 0x28(%rax), %ecx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movl %ecx, 0x1c(%rax)
addq $0x18, %rsp
retq
nopw %cs:(%rax,%rax)