// File: tomfunkhouser[P]gaps/pkgs/jpeg/jcmaster.c
// Function: per_scan_setup
LOCAL(void)
per_scan_setup (j_compress_ptr cinfo)
/* Do computations that are needed before processing a JPEG scan */
/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
{
  int ci, mcublks, tmp;
  jpeg_component_info *compptr;
  
  if (cinfo->comps_in_scan == 1) {
    
    /* Noninterleaved (single-component) scan */
    compptr = cinfo->cur_comp_info[0];
    
    /* Overall image size in MCUs */
    cinfo->MCUs_per_row = compptr->width_in_blocks;
    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
    
    /* For noninterleaved scan, always one block per MCU */
    compptr->MCU_width = 1;
    compptr->MCU_height = 1;
    compptr->MCU_blocks = 1;
    compptr->MCU_sample_width = DCTSIZE;
    compptr->last_col_width = 1;
    /* For noninterleaved scans, it is convenient to define last_row_height
     * as the number of block rows present in the last iMCU row.
     */
    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
    if (tmp == 0) tmp = compptr->v_samp_factor;
    compptr->last_row_height = tmp;
    
    /* Prepare array describing MCU composition */
    cinfo->blocks_in_MCU = 1;
    cinfo->MCU_membership[0] = 0;
    
  } else {
    
    /* Interleaved (multi-component) scan */
    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
	       MAX_COMPS_IN_SCAN);
    
    /* Overall image size in MCUs */
    cinfo->MCUs_per_row = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_width,
		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
    cinfo->MCU_rows_in_scan = (JDIMENSION)
      jdiv_round_up((long) cinfo->image_height,
		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
    
    cinfo->blocks_in_MCU = 0;
    
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
      compptr = cinfo->cur_comp_info[ci];
      /* Sampling factors give # of blocks of component in each MCU */
      compptr->MCU_width = compptr->h_samp_factor;
      compptr->MCU_height = compptr->v_samp_factor;
      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
      compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
      /* Figure number of non-dummy blocks in last MCU column & row */
      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
      if (tmp == 0) tmp = compptr->MCU_width;
      compptr->last_col_width = tmp;
      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
      if (tmp == 0) tmp = compptr->MCU_height;
      compptr->last_row_height = tmp;
      /* Prepare array describing MCU composition */
      mcublks = compptr->MCU_blocks;
      if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
      while (mcublks-- > 0) {
	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
      }
    }
    
  }

  /* Convert restart specified in rows to actual MCU count. */
  /* Note that count must fit in 16 bits, so we provide limiting. */
  if (cinfo->restart_in_rows > 0) {
    long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
    cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
  }
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq 0x30(%rsp), %rax
cmpl $0x1, 0x144(%rax)
jne 0xd8936
movq 0x30(%rsp), %rax
movq 0x148(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movl 0x1c(%rax), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x168(%rax)
movq 0x18(%rsp), %rax
movl 0x20(%rax), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x16c(%rax)
movq 0x18(%rsp), %rax
movl $0x1, 0x34(%rax)
movq 0x18(%rsp), %rax
movl $0x1, 0x38(%rax)
movq 0x18(%rsp), %rax
movl $0x1, 0x3c(%rax)
movq 0x18(%rsp), %rax
movl $0x8, 0x40(%rax)
movq 0x18(%rsp), %rax
movl $0x1, 0x44(%rax)
movq 0x18(%rsp), %rax
movl 0x20(%rax), %eax
movq 0x18(%rsp), %rcx
xorl %edx, %edx
divl 0xc(%rcx)
movl %edx, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jne 0xd8907
movq 0x18(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x48(%rax)
movq 0x30(%rsp), %rax
movl $0x1, 0x170(%rax)
movq 0x30(%rsp), %rax
movl $0x0, 0x174(%rax)
jmp 0xd8b7f
movq 0x30(%rsp), %rax
cmpl $0x0, 0x144(%rax)
jle 0xd8952
movq 0x30(%rsp), %rax
cmpl $0x4, 0x144(%rax)
jle 0xd8998
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x1a, 0x28(%rax)
movq 0x30(%rsp), %rax
movl 0x144(%rax), %ecx
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x4, 0x30(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x30(%rsp), %rax
movl 0x30(%rax), %eax
movl %eax, %edi
movq 0x30(%rsp), %rax
movl 0x138(%rax), %eax
shll $0x3, %eax
movslq %eax, %rsi
callq 0xf9310
movl %eax, %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x168(%rax)
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, 0x16c(%rax)
movq 0x30(%rsp), %rax
movl $0x0, 0x170(%rax)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x30(%rsp), %rcx
cmpl 0x144(%rcx), %eax
jge 0xd8b7d
movq 0x30(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movq 0x148(%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x34(%rax)
movq 0x18(%rsp), %rax
movl 0xc(%rax), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x38(%rax)
movq 0x18(%rsp), %rax
movl 0x34(%rax), %ecx
movq 0x18(%rsp), %rax
imull 0x38(%rax), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x3c(%rax)
movq 0x18(%rsp), %rax
movl 0x34(%rax), %ecx
shll $0x3, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x40(%rax)
movq 0x18(%rsp), %rax
movl 0x1c(%rax), %eax
movq 0x18(%rsp), %rcx
xorl %edx, %edx
divl 0x34(%rcx)
movl %edx, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jne 0xd8aaa
movq 0x18(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x44(%rax)
movq 0x18(%rsp), %rax
movl 0x20(%rax), %eax
movq 0x18(%rsp), %rcx
xorl %edx, %edx
divl 0x38(%rcx)
movl %edx, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jne 0xd8adf
movq 0x18(%rsp), %rax
movl 0x38(%rax), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x48(%rax)
movq 0x18(%rsp), %rax
movl 0x3c(%rax), %eax
movl %eax, 0x28(%rsp)
movq 0x30(%rsp), %rax
movl 0x170(%rax), %eax
addl 0x28(%rsp), %eax
cmpl $0xa, %eax
jle 0xd8b2c
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0xd, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
jmp 0xd8b2e
movl 0x28(%rsp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, 0x28(%rsp)
cmpl $0x0, %eax
jle 0xd8b6b
movl 0x2c(%rsp), %edx
movq 0x30(%rsp), %rax
movq 0x30(%rsp), %rsi
movl 0x170(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x170(%rsi)
movslq %ecx, %rcx
movl %edx, 0x174(%rax,%rcx,4)
jmp 0xd8b2e
jmp 0xd8b6d
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xd8a09
jmp 0xd8b7f
movq 0x30(%rsp), %rax
cmpl $0x0, 0x11c(%rax)
jle 0xd8be2
movq 0x30(%rsp), %rax
movslq 0x11c(%rax), %rax
movq 0x30(%rsp), %rcx
movl 0x168(%rcx), %ecx
imulq %rcx, %rax
movq %rax, 0x10(%rsp)
cmpq $0xffff, 0x10(%rsp)     # imm = 0xFFFF
jge 0xd8bc4
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0xd8bd0
movl $0xffff, %eax           # imm = 0xFFFF
movq %rax, 0x8(%rsp)
jmp 0xd8bd0
movq 0x8(%rsp), %rax
movl %eax, %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x118(%rax)
addq $0x38, %rsp
retq
nopw (%rax,%rax)