// File: tomfunkhouser[P]gaps/pkgs/jpeg/jccoefct.c
// Function: compress_data
METHODDEF(jboolean)
compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
{
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  int blkn, bi, ci, yindex, yoffset, blockcnt;
  JDIMENSION ypos, xpos;
  jpeg_component_info *compptr;

  /* Loop to write as much as one whole iMCU row */
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
       yoffset++) {
    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
	 MCU_col_num++) {
      /* Determine where data comes from in input_buf and do the DCT thing.
       * Each call on forward_DCT processes a horizontal row of DCT blocks
       * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
       * sequentially.  Dummy blocks at the right or bottom edge are filled in
       * specially.  The data in them does not matter for image reconstruction,
       * so we fill them with values that will encode to the smallest amount of
       * data, viz: all zeroes in the AC entries, DC entries equal to previous
       * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
       */
      blkn = 0;
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
	compptr = cinfo->cur_comp_info[ci];
	blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
						: compptr->last_col_width;
	xpos = MCU_col_num * compptr->MCU_sample_width;
	ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
	  if (coef->iMCU_row_num < last_iMCU_row ||
	      yoffset+yindex < compptr->last_row_height) {
	    (*cinfo->fdct->forward_DCT) (cinfo, compptr,
					 input_buf[compptr->component_index],
					 coef->MCU_buffer[blkn],
					 ypos, xpos, (JDIMENSION) blockcnt);
	    if (blockcnt < compptr->MCU_width) {
	      /* Create some dummy blocks at the right edge of the image. */
	      jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
			(compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
	      for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
		coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
	      }
	    }
	  } else {
	    /* Create a row of dummy blocks at the bottom of the image. */
	    jzero_far((void FAR *) coef->MCU_buffer[blkn],
		      compptr->MCU_width * SIZEOF(JBLOCK));
	    for (bi = 0; bi < compptr->MCU_width; bi++) {
	      coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
	    }
	  }
	  blkn += compptr->MCU_width;
	  ypos += DCTSIZE;
	}
      }
      /* Try to write the MCU.  In event of a suspension failure, we will
       * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
       */
      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
	/* Suspension forced; update state counters and exit */
	coef->MCU_vert_offset = yoffset;
	coef->mcu_ctr = MCU_col_num;
	return FALSE;
      }
    }
    /* Completed an MCU row, but perhaps not an iMCU row */
    coef->mcu_ctr = 0;
  }
  /* Completed the iMCU row, advance counters for next one */
  coef->iMCU_row_num++;
  start_iMCU_row(cinfo);
  return TRUE;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq 0x58(%rsp), %rax
movq 0x1c8(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x58(%rsp), %rax
movl 0x168(%rax), %eax
subl $0x1, %eax
movl %eax, 0x40(%rsp)
movq 0x58(%rsp), %rax
movl 0x140(%rax), %eax
subl $0x1, %eax
movl %eax, 0x3c(%rsp)
movq 0x48(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl 0x1c(%rcx), %eax
jge 0xdae6d
movq 0x48(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x40(%rsp), %eax
ja 0xdae51
movl $0x0, 0x38(%rsp)
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl 0x144(%rcx), %eax
jge 0xdadf8
movq 0x58(%rsp), %rax
movslq 0x30(%rsp), %rcx
movq 0x148(%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x40(%rsp), %eax
jae 0xdabef
movq 0x10(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, 0xc(%rsp)
jmp 0xdabfb
movq 0x10(%rsp), %rax
movl 0x44(%rax), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x44(%rsp), %eax
movq 0x10(%rsp), %rcx
imull 0x40(%rcx), %eax
movl %eax, 0x1c(%rsp)
movl 0x28(%rsp), %eax
shll $0x3, %eax
movl %eax, 0x20(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x10(%rsp), %rcx
cmpl 0x38(%rcx), %eax
jge 0xdade6
movq 0x48(%rsp), %rax
movl 0x10(%rax), %eax
cmpl 0x3c(%rsp), %eax
jb 0xdac5d
movl 0x28(%rsp), %eax
addl 0x2c(%rsp), %eax
movq 0x10(%rsp), %rcx
cmpl 0x48(%rcx), %eax
jge 0xdad46
movq 0x58(%rsp), %rax
movq 0x1e8(%rax), %rax
movq 0x8(%rax), %rax
movq 0x58(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x50(%rsp), %rcx
movq 0x10(%rsp), %rdx
movslq 0x4(%rdx), %rdx
movq (%rcx,%rdx,8), %rdx
movq 0x48(%rsp), %rcx
movslq 0x38(%rsp), %r8
movq 0x20(%rcx,%r8,8), %rcx
movl 0x20(%rsp), %r8d
movl 0x1c(%rsp), %r9d
movl 0x24(%rsp), %r10d
movl %r10d, (%rsp)
callq *%rax
movl 0x24(%rsp), %eax
movq 0x10(%rsp), %rcx
cmpl 0x34(%rcx), %eax
jge 0xdad44
movq 0x48(%rsp), %rax
movl 0x38(%rsp), %ecx
addl 0x24(%rsp), %ecx
movslq %ecx, %rcx
movq 0x20(%rax,%rcx,8), %rdi
movq 0x10(%rsp), %rax
movl 0x34(%rax), %eax
subl 0x24(%rsp), %eax
movslq %eax, %rsi
shlq $0x7, %rsi
callq 0xf9470
movl 0x24(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
movq 0x10(%rsp), %rcx
cmpl 0x34(%rcx), %eax
jge 0xdad42
movq 0x48(%rsp), %rax
movl 0x38(%rsp), %ecx
addl 0x34(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq 0x20(%rax,%rcx,8), %rax
movw (%rax), %cx
movq 0x48(%rsp), %rax
movl 0x38(%rsp), %edx
addl 0x34(%rsp), %edx
movslq %edx, %rdx
movq 0x20(%rax,%rdx,8), %rax
movw %cx, (%rax)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0xdacf4
jmp 0xdad44
jmp 0xdadbb
movq 0x48(%rsp), %rax
movslq 0x38(%rsp), %rcx
movq 0x20(%rax,%rcx,8), %rdi
movq 0x10(%rsp), %rax
movslq 0x34(%rax), %rsi
shlq $0x7, %rsi
callq 0xf9470
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
movq 0x10(%rsp), %rcx
cmpl 0x34(%rcx), %eax
jge 0xdadb9
movq 0x48(%rsp), %rax
movl 0x38(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq 0x20(%rax,%rcx,8), %rax
movw (%rax), %cx
movq 0x48(%rsp), %rax
movl 0x38(%rsp), %edx
addl 0x34(%rsp), %edx
movslq %edx, %rdx
movq 0x20(%rax,%rdx,8), %rax
movw %cx, (%rax)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0xdad6f
jmp 0xdadbb
movq 0x10(%rsp), %rax
movl 0x34(%rax), %eax
addl 0x38(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x20(%rsp), %eax
addl $0x8, %eax
movl %eax, 0x20(%rsp)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xdac27
jmp 0xdade8
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0xdabab
movq 0x58(%rsp), %rax
movq 0x1f0(%rax), %rax
movq 0x8(%rax), %rax
movq 0x58(%rsp), %rdi
movq 0x48(%rsp), %rsi
addq $0x20, %rsi
callq *%rax
cmpl $0x0, %eax
jne 0xdae3f
movl 0x28(%rsp), %ecx
movq 0x48(%rsp), %rax
movl %ecx, 0x18(%rax)
movl 0x44(%rsp), %ecx
movq 0x48(%rsp), %rax
movl %ecx, 0x14(%rax)
movl $0x0, 0x64(%rsp)
jmp 0xdae8d
jmp 0xdae41
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0xdab8d
movq 0x48(%rsp), %rax
movl $0x0, 0x14(%rax)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0xdab6f
movq 0x48(%rsp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movq 0x58(%rsp), %rdi
callq 0xdaa80
movl $0x1, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)