// File: tomfunkhouser[P]gaps/pkgs/jpeg/jcmaster.c
// Function: initial_setup
LOCAL(void)
initial_setup (j_compress_ptr cinfo)
/* Do computations that are needed before master selection phase */
{
  int ci;
  jpeg_component_info *compptr;
  long samplesperrow;
  JDIMENSION jd_samplesperrow;

  /* Sanity check on image dimensions */
  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
      || cinfo->num_components <= 0 || cinfo->input_components <= 0)
    ERREXIT(cinfo, JERR_EMPTY_IMAGE);

  /* Make sure image isn't bigger than I can handle */
  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);

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

  /* For now, precision must match compiled-in value... */
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);

  /* Check that number of components won't exceed internal array sizes */
  if (cinfo->num_components > MAX_COMPONENTS)
    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
	     MAX_COMPONENTS);

  /* Compute maximum sampling factors; check factor validity */
  cinfo->max_h_samp_factor = 1;
  cinfo->max_v_samp_factor = 1;
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
      ERREXIT(cinfo, JERR_BAD_SAMPLING);
    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
				   compptr->h_samp_factor);
    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
				   compptr->v_samp_factor);
  }

  /* Compute dimensions of components */
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
    /* Fill in the correct component_index value; don't rely on application */
    compptr->component_index = ci;
    /* For compression, we never do DCT scaling. */
    compptr->DCT_scaled_size = DCTSIZE;
    /* Size in DCT blocks */
    compptr->width_in_blocks = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
    compptr->height_in_blocks = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
    /* Size in samples */
    compptr->downsampled_width = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
		    (long) cinfo->max_h_samp_factor);
    compptr->downsampled_height = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
		    (long) cinfo->max_v_samp_factor);
    /* Mark component needed (this flag isn't actually used for compression) */
    compptr->component_needed = TRUE;
  }

  /* Compute number of fully interleaved MCU rows (number of times that
   * main controller will call coefficient controller).
   */
  cinfo->total_iMCU_rows = (JDIMENSION)
    jdiv_round_up((long) cinfo->image_height,
		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq 0x30(%rsp), %rax
cmpl $0x0, 0x34(%rax)
jbe 0xd7965
movq 0x30(%rsp), %rax
cmpl $0x0, 0x30(%rax)
jbe 0xd7965
movq 0x30(%rsp), %rax
cmpl $0x0, 0x4c(%rax)
jle 0xd7965
movq 0x30(%rsp), %rax
cmpl $0x0, 0x38(%rax)
jg 0xd7986
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x20, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x30(%rsp), %rax
movl 0x34(%rax), %eax
cmpq $0xffdc, %rax           # imm = 0xFFDC
jg 0xd79a6
movq 0x30(%rsp), %rax
movl 0x30(%rax), %eax
cmpq $0xffdc, %rax           # imm = 0xFFDC
jle 0xd79d6
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x29, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0xffdc, 0x2c(%rax)     # imm = 0xFFDC
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x30(%rsp), %rax
movl 0x30(%rax), %eax
movq 0x30(%rsp), %rcx
movslq 0x38(%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 0xd7a25
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 0x30(%rsp), %rax
cmpl $0x8, 0x48(%rax)
je 0xd7a64
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0xf, 0x28(%rax)
movq 0x30(%rsp), %rax
movl 0x48(%rax), %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 0x30(%rsp), %rax
cmpl $0xa, 0x4c(%rax)
jle 0xd7ab2
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x1a, 0x28(%rax)
movq 0x30(%rsp), %rax
movl 0x4c(%rax), %ecx
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0xa, 0x30(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x30(%rsp), %rax
movl $0x1, 0x138(%rax)
movq 0x30(%rsp), %rax
movl $0x1, 0x13c(%rax)
movl $0x0, 0x2c(%rsp)
movq 0x30(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x20(%rsp)
movl 0x2c(%rsp), %eax
movq 0x30(%rsp), %rcx
cmpl 0x4c(%rcx), %eax
jge 0xd7be5
movq 0x20(%rsp), %rax
cmpl $0x0, 0x8(%rax)
jle 0xd7b24
movq 0x20(%rsp), %rax
cmpl $0x4, 0x8(%rax)
jg 0xd7b24
movq 0x20(%rsp), %rax
cmpl $0x0, 0xc(%rax)
jle 0xd7b24
movq 0x20(%rsp), %rax
cmpl $0x4, 0xc(%rax)
jle 0xd7b45
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x12, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x30(%rsp), %rax
movl 0x138(%rax), %eax
movq 0x20(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jle 0xd7b6b
movq 0x30(%rsp), %rax
movl 0x138(%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0xd7b77
movq 0x20(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x138(%rax)
movq 0x30(%rsp), %rax
movl 0x13c(%rax), %eax
movq 0x20(%rsp), %rcx
cmpl 0xc(%rcx), %eax
jle 0xd7bac
movq 0x30(%rsp), %rax
movl 0x13c(%rax), %eax
movl %eax, 0xc(%rsp)
jmp 0xd7bb8
movq 0x20(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x13c(%rax)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
movq 0x20(%rsp), %rax
addq $0x60, %rax
movq %rax, 0x20(%rsp)
jmp 0xd7ae6
movl $0x0, 0x2c(%rsp)
movq 0x30(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x20(%rsp)
movl 0x2c(%rsp), %eax
movq 0x30(%rsp), %rcx
cmpl 0x4c(%rcx), %eax
jge 0xd7d21
movl 0x2c(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x20(%rsp), %rax
movl $0x8, 0x24(%rax)
movq 0x30(%rsp), %rax
movl 0x30(%rax), %eax
movl %eax, %edi
movq 0x20(%rsp), %rax
movslq 0x8(%rax), %rax
imulq %rax, %rdi
movq 0x30(%rsp), %rax
movl 0x138(%rax), %eax
shll $0x3, %eax
movslq %eax, %rsi
callq 0xf9310
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x1c(%rax)
movq 0x30(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, %edi
movq 0x20(%rsp), %rax
movslq 0xc(%rax), %rax
imulq %rax, %rdi
movq 0x30(%rsp), %rax
movl 0x13c(%rax), %eax
shll $0x3, %eax
movslq %eax, %rsi
callq 0xf9310
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x30(%rsp), %rax
movl 0x30(%rax), %eax
movl %eax, %edi
movq 0x20(%rsp), %rax
movslq 0x8(%rax), %rax
imulq %rax, %rdi
movq 0x30(%rsp), %rax
movslq 0x138(%rax), %rsi
callq 0xf9310
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x28(%rax)
movq 0x30(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, %edi
movq 0x20(%rsp), %rax
movslq 0xc(%rax), %rax
imulq %rax, %rdi
movq 0x30(%rsp), %rax
movslq 0x13c(%rax), %rsi
callq 0xf9310
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x2c(%rax)
movq 0x20(%rsp), %rax
movl $0x1, 0x30(%rax)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
movq 0x20(%rsp), %rax
addq $0x60, %rax
movq %rax, 0x20(%rsp)
jmp 0xd7bfb
movq 0x30(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, %edi
movq 0x30(%rsp), %rax
movl 0x13c(%rax), %eax
shll $0x3, %eax
movslq %eax, %rsi
callq 0xf9310
movl %eax, %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x140(%rax)
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)