// File: tomfunkhouser[P]gaps/pkgs/jpeg/jccoefct.c
// Function: compress_first_pass
METHODDEF(jboolean)
compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
{
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  JDIMENSION blocks_across, MCUs_across, MCUindex;
  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
  JCOEF lastDC;
  jpeg_component_info *compptr;
  JBLOCKARRAY buffer;
  JBLOCKROW thisblockrow, lastblockrow;

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
    /* Align the virtual buffer for this component. */
    buffer = (*cinfo->mem->access_virt_barray)
      ((j_common_ptr) cinfo, coef->whole_image[ci],
       coef->iMCU_row_num * compptr->v_samp_factor,
       (JDIMENSION) compptr->v_samp_factor, TRUE);
    /* Count non-dummy DCT block rows in this iMCU row. */
    if (coef->iMCU_row_num < last_iMCU_row)
      block_rows = compptr->v_samp_factor;
    else {
      /* NB: can't use last_row_height here, since may not be set! */
      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
    }
    blocks_across = compptr->width_in_blocks;
    h_samp_factor = compptr->h_samp_factor;
    /* Count number of dummy blocks to be added at the right margin. */
    ndummy = (int) (blocks_across % h_samp_factor);
    if (ndummy > 0)
      ndummy = h_samp_factor - ndummy;
    /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
     * on forward_DCT processes a complete horizontal row of DCT blocks.
     */
    for (block_row = 0; block_row < block_rows; block_row++) {
      thisblockrow = buffer[block_row];
      (*cinfo->fdct->forward_DCT) (cinfo, compptr,
				   input_buf[ci], thisblockrow,
				   (JDIMENSION) (block_row * DCTSIZE),
				   (JDIMENSION) 0, blocks_across);
      if (ndummy > 0) {
	/* Create dummy blocks at the right edge of the image. */
	thisblockrow += blocks_across; /* => first dummy block */
	jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
	lastDC = thisblockrow[-1][0];
	for (bi = 0; bi < ndummy; bi++) {
	  thisblockrow[bi][0] = lastDC;
	}
      }
    }
    /* If at end of image, create dummy block rows as needed.
     * The tricky part here is that within each MCU, we want the DC values
     * of the dummy blocks to match the last real block's DC value.
     * This squeezes a few more bytes out of the resulting file...
     */
    if (coef->iMCU_row_num == last_iMCU_row) {
      blocks_across += ndummy;	/* include lower right corner */
      MCUs_across = blocks_across / h_samp_factor;
      for (block_row = block_rows; block_row < compptr->v_samp_factor;
	   block_row++) {
	thisblockrow = buffer[block_row];
	lastblockrow = buffer[block_row-1];
	jzero_far((void FAR *) thisblockrow,
		  (size_t) (blocks_across * SIZEOF(JBLOCK)));
	for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
	  lastDC = lastblockrow[h_samp_factor-1][0];
	  for (bi = 0; bi < h_samp_factor; bi++) {
	    thisblockrow[bi][0] = lastDC;
	  }
	  thisblockrow += h_samp_factor; /* advance to next MCU in row */
	  lastblockrow += h_samp_factor;
	}
      }
    }
  }
  /* NB: compress_output will increment iMCU_row_num if successful.
   * A suspension return will result in redoing all the work above next time.
   */

  /* Emit data to the entropy encoder, sharing code with subsequent passes */
  return compress_output(cinfo, input_buf);
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq 0x70(%rsp), %rax
movq 0x1c8(%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x70(%rsp), %rax
movl 0x140(%rax), %eax
subl $0x1, %eax
movl %eax, 0x5c(%rsp)
movl $0x0, 0x48(%rsp)
movq 0x70(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x28(%rsp)
movl 0x48(%rsp), %eax
movq 0x70(%rsp), %rcx
cmpl 0x4c(%rcx), %eax
jge 0xdb21d
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x40(%rax), %rax
movq 0x70(%rsp), %rdi
movq 0x60(%rsp), %rcx
movslq 0x48(%rsp), %rdx
movq 0x70(%rcx,%rdx,8), %rsi
movq 0x60(%rsp), %rcx
movl 0x10(%rcx), %edx
movq 0x28(%rsp), %rcx
imull 0xc(%rcx), %edx
movq 0x28(%rsp), %rcx
movl 0xc(%rcx), %ecx
movl $0x1, %r8d
callq *%rax
movq %rax, 0x20(%rsp)
movq 0x60(%rsp), %rax
movl 0x10(%rax), %eax
cmpl 0x5c(%rsp), %eax
jae 0xdaf5c
movq 0x28(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x3c(%rsp)
jmp 0xdaf87
movq 0x28(%rsp), %rax
movl 0x20(%rax), %eax
movq 0x28(%rsp), %rcx
xorl %edx, %edx
divl 0xc(%rcx)
movl %edx, 0x3c(%rsp)
cmpl $0x0, 0x3c(%rsp)
jne 0xdaf85
movq 0x28(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x3c(%rsp)
jmp 0xdaf87
movq 0x28(%rsp), %rax
movl 0x1c(%rax), %eax
movl %eax, 0x58(%rsp)
movq 0x28(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x44(%rsp)
movl 0x58(%rsp), %eax
xorl %edx, %edx
divl 0x44(%rsp)
movl %edx, 0x38(%rsp)
cmpl $0x0, 0x38(%rsp)
jle 0xdafc0
movl 0x44(%rsp), %eax
subl 0x38(%rsp), %eax
movl %eax, 0x38(%rsp)
movl $0x0, 0x40(%rsp)
movl 0x40(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jge 0xdb0b8
movq 0x20(%rsp), %rax
movslq 0x40(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
movq 0x70(%rsp), %rax
movq 0x1e8(%rax), %rax
movq 0x8(%rax), %rax
movq 0x70(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x68(%rsp), %rcx
movslq 0x48(%rsp), %rdx
movq (%rcx,%rdx,8), %rdx
movq 0x18(%rsp), %rcx
movl 0x40(%rsp), %r8d
shll $0x3, %r8d
movl 0x58(%rsp), %r10d
xorl %r9d, %r9d
movl %r10d, (%rsp)
callq *%rax
cmpl $0x0, 0x38(%rsp)
jle 0xdb0a6
movl 0x58(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, %ecx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movslq 0x38(%rsp), %rsi
shlq $0x7, %rsi
callq 0xf9470
movq 0x18(%rsp), %rax
movw -0x80(%rax), %ax
movw %ax, 0x36(%rsp)
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x38(%rsp), %eax
jge 0xdb0a4
movw 0x36(%rsp), %cx
movq 0x18(%rsp), %rax
movslq 0x4c(%rsp), %rdx
shlq $0x7, %rdx
addq %rdx, %rax
movw %cx, (%rax)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0xdb074
jmp 0xdb0a6
jmp 0xdb0a8
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0xdafc8
movq 0x60(%rsp), %rax
movl 0x10(%rax), %eax
cmpl 0x5c(%rsp), %eax
jne 0xdb1fd
movl 0x38(%rsp), %eax
addl 0x58(%rsp), %eax
movl %eax, 0x58(%rsp)
movl 0x58(%rsp), %eax
xorl %edx, %edx
divl 0x44(%rsp)
movl %eax, 0x54(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x40(%rsp), %eax
movq 0x28(%rsp), %rcx
cmpl 0xc(%rcx), %eax
jge 0xdb1fb
movq 0x20(%rsp), %rax
movslq 0x40(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movl 0x40(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rdi
movl 0x58(%rsp), %eax
movl %eax, %esi
shlq $0x7, %rsi
callq 0xf9470
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0x54(%rsp), %eax
jae 0xdb1e9
movq 0x10(%rsp), %rax
movl 0x44(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movw (%rax), %ax
movw %ax, 0x36(%rsp)
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x44(%rsp), %eax
jge 0xdb1a9
movw 0x36(%rsp), %cx
movq 0x18(%rsp), %rax
movslq 0x4c(%rsp), %rdx
shlq $0x7, %rdx
addq %rdx, %rax
movw %cx, (%rax)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0xdb179
movl 0x44(%rsp), %ecx
movq 0x18(%rsp), %rax
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movl 0x44(%rsp), %ecx
movq 0x10(%rsp), %rax
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0xdb145
jmp 0xdb1eb
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0xdb0ec
jmp 0xdb1fd
jmp 0xdb1ff
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
movq 0x28(%rsp), %rax
addq $0x60, %rax
movq %rax, 0x28(%rsp)
jmp 0xdaee7
movq 0x70(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0xdb240
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)