// File: tomfunkhouser[P]gaps/pkgs/jpeg/jcprepct.c
// Function: pre_process_context
METHODDEF(void)
pre_process_context (j_compress_ptr cinfo,
		     JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
		     JDIMENSION in_rows_avail,
		     JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
		     JDIMENSION out_row_groups_avail)
{
  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  int numrows, ci;
  int buf_height = cinfo->max_v_samp_factor * 3;
  JDIMENSION inrows;

  while (*out_row_group_ctr < out_row_groups_avail) {
    if (*in_row_ctr < in_rows_avail) {
      /* Do color conversion to fill the conversion buffer. */
      inrows = in_rows_avail - *in_row_ctr;
      numrows = prep->next_buf_stop - prep->next_buf_row;
      numrows = (int) MIN((JDIMENSION) numrows, inrows);
      (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
					 prep->color_buf,
					 (JDIMENSION) prep->next_buf_row,
					 numrows);
      /* Pad at top of image, if first time through */
      if (prep->rows_to_go == cinfo->image_height) {
	for (ci = 0; ci < cinfo->num_components; ci++) {
	  int row;
	  for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
	    jcopy_sample_rows(prep->color_buf[ci], 0,
			      prep->color_buf[ci], -row,
			      1, cinfo->image_width);
	  }
	}
      }
      *in_row_ctr += numrows;
      prep->next_buf_row += numrows;
      prep->rows_to_go -= numrows;
    } else {
      /* Return for more data, unless we are at the bottom of the image. */
      if (prep->rows_to_go != 0)
	break;
      /* When at bottom of image, pad to fill the conversion buffer. */
      if (prep->next_buf_row < prep->next_buf_stop) {
	for (ci = 0; ci < cinfo->num_components; ci++) {
	  expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
			     prep->next_buf_row, prep->next_buf_stop);
	}
	prep->next_buf_row = prep->next_buf_stop;
      }
    }
    /* If we've gotten enough data, downsample a row group. */
    if (prep->next_buf_row == prep->next_buf_stop) {
      (*cinfo->downsample->downsample) (cinfo,
					prep->color_buf,
					(JDIMENSION) prep->this_row_group,
					output_buf, *out_row_group_ctr);
      (*out_row_group_ctr)++;
      /* Advance pointers with wraparound as necessary. */
      prep->this_row_group += cinfo->max_v_samp_factor;
      if (prep->this_row_group >= buf_height)
	prep->this_row_group = 0;
      if (prep->next_buf_row >= buf_height)
	prep->next_buf_row = 0;
      prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
    }
  }
}

// Assembly:
subq $0x58, %rsp
movl 0x60(%rsp), %eax
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movl %ecx, 0x3c(%rsp)
movq %r8, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movq 0x50(%rsp), %rax
movq 0x1c0(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x50(%rsp), %rax
imull $0x3, 0x13c(%rax), %eax
movl %eax, 0x14(%rsp)
movq 0x28(%rsp), %rax
movl (%rax), %eax
cmpl 0x60(%rsp), %eax
jae 0xda2a2
movq 0x40(%rsp), %rax
movl (%rax), %eax
cmpl 0x3c(%rsp), %eax
jae 0xda15e
movl 0x3c(%rsp), %eax
movq 0x40(%rsp), %rcx
subl (%rcx), %eax
movl %eax, 0x10(%rsp)
movq 0x20(%rsp), %rax
movl 0x6c(%rax), %eax
movq 0x20(%rsp), %rcx
subl 0x64(%rcx), %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x10(%rsp), %eax
jae 0xda03f
movl 0x1c(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0xda047
movl 0x10(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x50(%rsp), %rax
movq 0x1d8(%rax), %rax
movq 0x8(%rax), %rax
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rcx
movl (%rcx), %ecx
shlq $0x3, %rcx
addq %rcx, %rsi
movq 0x20(%rsp), %rdx
addq $0x10, %rdx
movq 0x20(%rsp), %rcx
movl 0x64(%rcx), %ecx
movl 0x1c(%rsp), %r8d
callq *%rax
movq 0x20(%rsp), %rax
movl 0x60(%rax), %eax
movq 0x50(%rsp), %rcx
cmpl 0x34(%rcx), %eax
jne 0xda12c
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl 0x4c(%rcx), %eax
jge 0xda12a
movl $0x1, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl 0x13c(%rcx), %eax
jg 0xda11b
movq 0x20(%rsp), %rax
movslq 0x18(%rsp), %rcx
movq 0x10(%rax,%rcx,8), %rdi
movq 0x20(%rsp), %rax
movslq 0x18(%rsp), %rcx
movq 0x10(%rax,%rcx,8), %rdx
xorl %ecx, %ecx
subl 0xc(%rsp), %ecx
movq 0x50(%rsp), %rax
movl 0x30(%rax), %r9d
xorl %esi, %esi
movl $0x1, %r8d
callq 0xf9370
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0xda0c3
jmp 0xda11d
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0xda0ad
jmp 0xda12c
movl 0x1c(%rsp), %ecx
movq 0x40(%rsp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl 0x1c(%rsp), %ecx
movq 0x20(%rsp), %rax
addl 0x64(%rax), %ecx
movl %ecx, 0x64(%rax)
movl 0x1c(%rsp), %edx
movq 0x20(%rsp), %rax
movl 0x60(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x60(%rax)
jmp 0xda1e1
movq 0x20(%rsp), %rax
cmpl $0x0, 0x60(%rax)
je 0xda16e
jmp 0xda2a2
movq 0x20(%rsp), %rax
movl 0x64(%rax), %eax
movq 0x20(%rsp), %rcx
cmpl 0x6c(%rcx), %eax
jge 0xda1df
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl 0x4c(%rcx), %eax
jge 0xda1cf
movq 0x20(%rsp), %rax
movslq 0x18(%rsp), %rcx
movq 0x10(%rax,%rcx,8), %rdi
movq 0x50(%rsp), %rax
movl 0x30(%rax), %esi
movq 0x20(%rsp), %rax
movl 0x64(%rax), %edx
movq 0x20(%rsp), %rax
movl 0x6c(%rax), %ecx
callq 0xda730
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0xda188
movq 0x20(%rsp), %rax
movl 0x6c(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x64(%rax)
jmp 0xda1e1
movq 0x20(%rsp), %rax
movl 0x64(%rax), %eax
movq 0x20(%rsp), %rcx
cmpl 0x6c(%rcx), %eax
jne 0xda29d
movq 0x50(%rsp), %rax
movq 0x1e0(%rax), %rax
movq 0x8(%rax), %rax
movq 0x50(%rsp), %rdi
movq 0x20(%rsp), %rsi
addq $0x10, %rsi
movq 0x20(%rsp), %rcx
movl 0x68(%rcx), %edx
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %r8
movl (%r8), %r8d
callq *%rax
movq 0x28(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq 0x50(%rsp), %rax
movl 0x13c(%rax), %ecx
movq 0x20(%rsp), %rax
addl 0x68(%rax), %ecx
movl %ecx, 0x68(%rax)
movq 0x20(%rsp), %rax
movl 0x68(%rax), %eax
cmpl 0x14(%rsp), %eax
jl 0xda268
movq 0x20(%rsp), %rax
movl $0x0, 0x68(%rax)
movq 0x20(%rsp), %rax
movl 0x64(%rax), %eax
cmpl 0x14(%rsp), %eax
jl 0xda282
movq 0x20(%rsp), %rax
movl $0x0, 0x64(%rax)
movq 0x20(%rsp), %rax
movl 0x64(%rax), %ecx
movq 0x50(%rsp), %rax
addl 0x13c(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x6c(%rax)
jmp 0xd9fe6
addq $0x58, %rsp
retq
nopw (%rax,%rax)