// File: tomfunkhouser[P]gaps/pkgs/jpeg/jdmerge.c
// Function: merged_2v_upsample
METHODDEF(void)
merged_2v_upsample (j_decompress_ptr cinfo,
		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
		    JDIMENSION in_row_groups_avail,
		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
		    JDIMENSION out_rows_avail)
/* 2:1 vertical sampling case: may need a spare row. */
{
  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  JSAMPROW work_ptrs[2];
  JDIMENSION num_rows;		/* number of rows returned to caller */

  if (upsample->spare_full) {
    /* If we have a spare row saved from a previous cycle, just return it. */
    jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
		      1, upsample->out_row_width);
    num_rows = 1;
    upsample->spare_full = FALSE;
  } else {
    /* Figure number of rows to return to caller. */
    num_rows = 2;
    /* Not more than the distance to the end of the image. */
    if (num_rows > upsample->rows_to_go)
      num_rows = upsample->rows_to_go;
    /* And not more than what the client can accept: */
    out_rows_avail -= *out_row_ctr;
    if (num_rows > out_rows_avail)
      num_rows = out_rows_avail;
    /* Create output pointer array for upsampler. */
    work_ptrs[0] = output_buf[*out_row_ctr];
    if (num_rows > 1) {
      work_ptrs[1] = output_buf[*out_row_ctr + 1];
    } else {
      work_ptrs[1] = upsample->spare_row;
      upsample->spare_full = TRUE;
    }
    /* Now do the upsampling. */
    (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
  }

  /* Adjust counts */
  *out_row_ctr += num_rows;
  upsample->rows_to_go -= num_rows;
  /* When the buffer is emptied, declare this input row group consumed */
  if (! upsample->spare_full)
    (*in_row_group_ctr)++;
}

// 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 0x260(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpl $0x0, 0x48(%rax)
je 0xf86ce
movq 0x20(%rsp), %rdi
addq $0x40, %rdi
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rax
movl (%rax), %eax
shlq $0x3, %rax
addq %rax, %rdx
movq 0x20(%rsp), %rax
movl 0x4c(%rax), %r9d
xorl %ecx, %ecx
movl $0x1, %r8d
movl %ecx, %esi
callq 0xf9370
movl $0x1, 0xc(%rsp)
movq 0x20(%rsp), %rax
movl $0x0, 0x48(%rax)
jmp 0xf8786
movl $0x2, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl 0x50(%rcx), %eax
jbe 0xf86f0
movq 0x20(%rsp), %rax
movl 0x50(%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x28(%rsp), %rax
movl (%rax), %ecx
movl 0x60(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x60(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x60(%rsp), %eax
jbe 0xf8713
movl 0x60(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rcx
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
cmpl $0x1, 0xc(%rsp)
jbe 0xf874b
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rcx
movl (%rcx), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
jmp 0xf8765
movq 0x20(%rsp), %rax
movq 0x40(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movl $0x1, 0x48(%rax)
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rcx
movl (%rcx), %edx
leaq 0x10(%rsp), %rcx
callq *%rax
movl 0xc(%rsp), %ecx
movq 0x28(%rsp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl 0xc(%rsp), %edx
movq 0x20(%rsp), %rax
movl 0x50(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x50(%rax)
movq 0x20(%rsp), %rax
cmpl $0x0, 0x48(%rax)
jne 0xf87bb
movq 0x40(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
addq $0x58, %rsp
retq