// File: tomfunkhouser[P]gaps/pkgs/jpeg/jcsample.c
// Function: int_downsample
METHODDEF(void)
int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
		JSAMPARRAY input_data, JSAMPARRAY output_data)
{
  int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
  JDIMENSION outcol, outcol_h;	/* outcol_h == outcol*h_expand */
  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  JSAMPROW inptr, outptr;
  INT32 outvalue;

  h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
  v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
  numpix = h_expand * v_expand;
  numpix2 = numpix/2;

  /* Expand input data enough to let all the output samples be generated
   * by the standard loop.  Special-casing padded output would be more
   * efficient.
   */
  expand_right_edge(input_data, cinfo->max_v_samp_factor,
		    cinfo->image_width, output_cols * h_expand);

  inrow = 0;
  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
    outptr = output_data[outrow];
    for (outcol = 0, outcol_h = 0; outcol < output_cols;
	 outcol++, outcol_h += h_expand) {
      outvalue = 0;
      for (v = 0; v < v_expand; v++) {
	inptr = input_data[inrow+v] + outcol_h;
	for (h = 0; h < h_expand; h++) {
	  outvalue += (INT32) GETJSAMPLE(*inptr++);
	}
      }
      *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
    }
    inrow += v_expand;
  }
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movq 0x58(%rsp), %rax
movl 0x1c(%rax), %eax
shll $0x3, %eax
movl %eax, 0x1c(%rsp)
movq 0x60(%rsp), %rax
movl 0x138(%rax), %eax
movq 0x58(%rsp), %rcx
cltd
idivl 0x8(%rcx)
movl %eax, 0x3c(%rsp)
movq 0x60(%rsp), %rax
movl 0x13c(%rax), %eax
movq 0x58(%rsp), %rcx
cltd
idivl 0xc(%rcx)
movl %eax, 0x38(%rsp)
movl 0x3c(%rsp), %eax
imull 0x38(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x30(%rsp)
movq 0x50(%rsp), %rdi
movq 0x60(%rsp), %rax
movl 0x13c(%rax), %esi
movq 0x60(%rsp), %rax
movl 0x30(%rax), %edx
movl 0x1c(%rsp), %ecx
imull 0x3c(%rsp), %ecx
callq 0xdd370
movl $0x0, 0x44(%rsp)
movl $0x0, 0x40(%rsp)
movl 0x40(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl 0xc(%rcx), %eax
jge 0xdd366
movq 0x48(%rsp), %rax
movslq 0x40(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x8(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jae 0xdd34a
movq $0x0, (%rsp)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x38(%rsp), %eax
jge 0xdd303
movq 0x50(%rsp), %rax
movl 0x44(%rsp), %ecx
addl 0x28(%rsp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl 0x20(%rsp), %ecx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jge 0xdd2f4
movq 0x10(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rsp)
movzbl (%rax), %eax
cltq
addq (%rsp), %rax
movq %rax, (%rsp)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xdd2bf
jmp 0xdd2f6
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0xdd28d
movq (%rsp), %rax
movslq 0x30(%rsp), %rcx
addq %rcx, %rax
movslq 0x34(%rsp), %rcx
cqto
idivq %rcx
movb %al, %cl
movq 0x8(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x8(%rsp)
movb %cl, (%rax)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
movl 0x3c(%rsp), %eax
addl 0x20(%rsp), %eax
movl %eax, 0x20(%rsp)
jmp 0xdd26f
movl 0x38(%rsp), %eax
addl 0x44(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0xdd23a
addq $0x68, %rsp
retq
nopl (%rax,%rax)