// File: tomfunkhouser[P]gaps/pkgs/jpeg/jdmaster.c
// Function: master_selection
LOCAL(void)
master_selection (j_decompress_ptr cinfo)
{
  my_master_ptr master = (my_master_ptr) cinfo->master;
  jboolean use_c_buffer;
  long samplesperrow;
  JDIMENSION jd_samplesperrow;

  /* Initialize dimensions and other stuff */
  jpeg_calc_output_dimensions(cinfo);
  prepare_range_limit_table(cinfo);

  /* Width of an output scanline must be representable as JDIMENSION. */
  samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
  jd_samplesperrow = (JDIMENSION) samplesperrow;
  if ((long) jd_samplesperrow != samplesperrow)
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);

  /* Initialize my private state */
  master->pass_number = 0;
  master->using_merged_upsample = use_merged_upsample(cinfo);

  /* Color quantizer selection */
  master->quantizer_1pass = NULL;
  master->quantizer_2pass = NULL;
  /* No mode changes if not using buffered-image mode. */
  if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
    cinfo->enable_1pass_quant = FALSE;
    cinfo->enable_external_quant = FALSE;
    cinfo->enable_2pass_quant = FALSE;
  }
  if (cinfo->quantize_colors) {
    if (cinfo->raw_data_out)
      ERREXIT(cinfo, JERR_NOTIMPL);
    /* 2-pass quantizer only works in 3-component color space. */
    if (cinfo->out_color_components != 3) {
      cinfo->enable_1pass_quant = TRUE;
      cinfo->enable_external_quant = FALSE;
      cinfo->enable_2pass_quant = FALSE;
      cinfo->colormap = NULL;
    } else if (cinfo->colormap != NULL) {
      cinfo->enable_external_quant = TRUE;
    } else if (cinfo->two_pass_quantize) {
      cinfo->enable_2pass_quant = TRUE;
    } else {
      cinfo->enable_1pass_quant = TRUE;
    }

    if (cinfo->enable_1pass_quant) {
#ifdef QUANT_1PASS_SUPPORTED
      jinit_1pass_quantizer(cinfo);
      master->quantizer_1pass = cinfo->cquantize;
#else
      ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
    }

    /* We use the 2-pass code to map to external colormaps. */
    if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
#ifdef QUANT_2PASS_SUPPORTED
      jinit_2pass_quantizer(cinfo);
      master->quantizer_2pass = cinfo->cquantize;
#else
      ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
    }
    /* If both quantizers are initialized, the 2-pass one is left active;
     * this is necessary for starting with quantization to an external map.
     */
  }

  /* Post-processing: in particular, color conversion first */
  if (! cinfo->raw_data_out) {
    if (master->using_merged_upsample) {
#ifdef UPSAMPLE_MERGING_SUPPORTED
      jinit_merged_upsampler(cinfo); /* does color conversion too */
#else
      ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
    } else {
      jinit_color_deconverter(cinfo);
      jinit_upsampler(cinfo);
    }
    jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
  }
  /* Inverse DCT */
  jinit_inverse_dct(cinfo);
  /* Entropy decoding: either Huffman or arithmetic coding. */
  if (cinfo->arith_code) {
    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  } else {
    if (cinfo->progressive_mode) {
#ifdef D_PROGRESSIVE_SUPPORTED
      jinit_phuff_decoder(cinfo);
#else
      ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
    } else
      jinit_huff_decoder(cinfo);
  }

  /* Initialize principal buffer controllers. */
  use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
  jinit_d_coef_controller(cinfo, use_c_buffer);

  if (! cinfo->raw_data_out)
    jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);

  /* We can now tell the memory manager to allocate virtual arrays. */
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);

  /* Initialize input side of decompressor to consume first scan. */
  (*cinfo->inputctl->start_input_pass) (cinfo);

#ifdef D_MULTISCAN_FILES_SUPPORTED
  /* If jpeg_start_decompress will read the whole file, initialize
   * progress monitoring appropriately.  The input step is counted
   * as one pass.
   */
  if (cinfo->progress != NULL && ! cinfo->buffered_image &&
      cinfo->inputctl->has_multiple_scans) {
    int nscans;
    /* Estimate number of scans to set pass_limit. */
    if (cinfo->progressive_mode) {
      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
      nscans = 2 + 3 * cinfo->num_components;
    } else {
      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
      nscans = cinfo->num_components;
    }
    cinfo->progress->pass_counter = 0L;
    cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
    cinfo->progress->completed_passes = 0;
    cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
    /* Count the input pass as done */
    master->pass_number++;
  }
#endif /* D_MULTISCAN_FILES_SUPPORTED */
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq 0x220(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rdi
callq 0xe37f0
movq 0x30(%rsp), %rdi
callq 0xe4670
movq 0x30(%rsp), %rax
movl 0x88(%rax), %eax
movq 0x30(%rsp), %rcx
movslq 0x90(%rcx), %rcx
imulq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpq 0x18(%rsp), %rax
je 0xe42a3
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x46, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x28(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x30(%rsp), %rdi
callq 0xe3c50
movl %eax, %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x1c(%rax)
movq 0x28(%rsp), %rax
movq $0x0, 0x20(%rax)
movq 0x28(%rsp), %rax
movq $0x0, 0x28(%rax)
movq 0x30(%rsp), %rax
cmpl $0x0, 0x6c(%rax)
je 0xe42f3
movq 0x30(%rsp), %rax
cmpl $0x0, 0x58(%rax)
jne 0xe431d
movq 0x30(%rsp), %rax
movl $0x0, 0x7c(%rax)
movq 0x30(%rsp), %rax
movl $0x0, 0x80(%rax)
movq 0x30(%rsp), %rax
movl $0x0, 0x84(%rax)
movq 0x30(%rsp), %rax
cmpl $0x0, 0x6c(%rax)
je 0xe4455
movq 0x30(%rsp), %rax
cmpl $0x0, 0x5c(%rax)
je 0xe4358
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x2f, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x30(%rsp), %rax
cmpl $0x3, 0x90(%rax)
je 0xe43a2
movq 0x30(%rsp), %rax
movl $0x1, 0x7c(%rax)
movq 0x30(%rsp), %rax
movl $0x0, 0x80(%rax)
movq 0x30(%rsp), %rax
movl $0x0, 0x84(%rax)
movq 0x30(%rsp), %rax
movq $0x0, 0xa0(%rax)
jmp 0xe43ee
movq 0x30(%rsp), %rax
cmpq $0x0, 0xa0(%rax)
je 0xe43c2
movq 0x30(%rsp), %rax
movl $0x1, 0x80(%rax)
jmp 0xe43ec
movq 0x30(%rsp), %rax
cmpl $0x0, 0x74(%rax)
je 0xe43de
movq 0x30(%rsp), %rax
movl $0x1, 0x84(%rax)
jmp 0xe43ea
movq 0x30(%rsp), %rax
movl $0x1, 0x7c(%rax)
jmp 0xe43ec
jmp 0xe43ee
movq 0x30(%rsp), %rax
cmpl $0x0, 0x7c(%rax)
je 0xe4418
movq 0x30(%rsp), %rdi
callq 0xf42b0
movq 0x30(%rsp), %rax
movq 0x270(%rax), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x30(%rsp), %rax
cmpl $0x0, 0x84(%rax)
jne 0xe4434
movq 0x30(%rsp), %rax
cmpl $0x0, 0x80(%rax)
je 0xe4453
movq 0x30(%rsp), %rdi
callq 0xf5980
movq 0x30(%rsp), %rax
movq 0x270(%rax), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x28(%rax)
jmp 0xe4455
movq 0x30(%rsp), %rax
cmpl $0x0, 0x5c(%rax)
jne 0xe44a0
movq 0x28(%rsp), %rax
cmpl $0x0, 0x1c(%rax)
je 0xe4477
movq 0x30(%rsp), %rdi
callq 0xf84e0
jmp 0xe448b
movq 0x30(%rsp), %rdi
callq 0xf3710
movq 0x30(%rsp), %rdi
callq 0xf2880
movq 0x30(%rsp), %rdi
movq 0x30(%rsp), %rax
movl 0x84(%rax), %esi
callq 0xeec00
movq 0x30(%rsp), %rdi
callq 0xef2e0
movq 0x30(%rsp), %rax
cmpl $0x0, 0x13c(%rax)
je 0xe44db
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x1, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
jmp 0xe4501
movq 0x30(%rsp), %rax
cmpl $0x0, 0x138(%rax)
je 0xe44f5
movq 0x30(%rsp), %rdi
callq 0xea980
jmp 0xe44ff
movq 0x30(%rsp), %rdi
callq 0xe9c40
jmp 0xe4501
movq 0x30(%rsp), %rax
movq 0x240(%rax), %rcx
movb $0x1, %al
cmpl $0x0, 0x20(%rcx)
movb %al, 0xf(%rsp)
jne 0xe4529
movq 0x30(%rsp), %rax
cmpl $0x0, 0x58(%rax)
setne %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x24(%rsp)
movq 0x30(%rsp), %rdi
movl 0x24(%rsp), %esi
callq 0xed100
movq 0x30(%rsp), %rax
cmpl $0x0, 0x5c(%rax)
jne 0xe455b
movq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0xec420
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x30(%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x30(%rsp), %rax
movq 0x240(%rax), %rax
movq 0x10(%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x30(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0xe4660
movq 0x30(%rsp), %rax
cmpl $0x0, 0x58(%rax)
jne 0xe4660
movq 0x30(%rsp), %rax
movq 0x240(%rax), %rax
cmpl $0x0, 0x20(%rax)
je 0xe4660
movq 0x30(%rsp), %rax
cmpl $0x0, 0x138(%rax)
je 0xe45db
movq 0x30(%rsp), %rax
imull $0x3, 0x38(%rax), %eax
addl $0x2, %eax
movl %eax, 0x10(%rsp)
jmp 0xe45e7
movq 0x30(%rsp), %rax
movl 0x38(%rax), %eax
movl %eax, 0x10(%rsp)
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rax
movq $0x0, 0x8(%rax)
movq 0x30(%rsp), %rax
movl 0x1a4(%rax), %eax
movl %eax, %ecx
movslq 0x10(%rsp), %rax
imulq %rax, %rcx
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x10(%rax)
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rax
movl $0x0, 0x18(%rax)
movq 0x30(%rsp), %rax
movl 0x84(%rax), %edx
movl $0x2, %ecx
movl $0x3, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rax
movl %ecx, 0x1c(%rax)
movq 0x28(%rsp), %rax
movl 0x18(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x18(%rax)
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)