// File: tomfunkhouser[P]gaps/pkgs/jpeg/jcapimin.c
// Function: jpeg_finish_compress
GLOBAL(void)
jpeg_finish_compress (j_compress_ptr cinfo)
{
  JDIMENSION iMCU_row;

  if (cinfo->global_state == CSTATE_SCANNING ||
      cinfo->global_state == CSTATE_RAW_OK) {
    /* Terminate first pass */
    if (cinfo->next_scanline < cinfo->image_height)
      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
    (*cinfo->master->finish_pass) (cinfo);
  } else if (cinfo->global_state != CSTATE_WRCOEFS)
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  /* Perform any remaining passes */
  while (! cinfo->master->is_last_pass) {
    (*cinfo->master->prepare_for_pass) (cinfo);
    for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
      if (cinfo->progress != NULL) {
	cinfo->progress->pass_counter = (long) iMCU_row;
	cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
      }
      /* We bypass the main controller and invoke coef controller directly;
       * all work is being done from the coefficient buffer.
       */
      if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
	ERREXIT(cinfo, JERR_CANT_SUSPEND);
    }
    (*cinfo->master->finish_pass) (cinfo);
  }
  /* Write EOI, do final cleanup */
  (*cinfo->marker->write_file_trailer) (cinfo);
  (*cinfo->dest->term_destination) (cinfo);
  /* We can use jpeg_abort to release memory and reset global_state */
  jpeg_abort((j_common_ptr) cinfo);
}

// Assembly:
subq $0x18, %rsp
movq %rdi, 0x10(%rsp)
movq 0x10(%rsp), %rax
cmpl $0x65, 0x24(%rax)
je 0xd53ef
movq 0x10(%rsp), %rax
cmpl $0x66, 0x24(%rax)
jne 0xd543e
movq 0x10(%rsp), %rax
movl 0x130(%rax), %eax
movq 0x10(%rsp), %rcx
cmpl 0x34(%rcx), %eax
jae 0xd5425
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl $0x43, 0x28(%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x1b0(%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
jmp 0xd547f
movq 0x10(%rsp), %rax
cmpl $0x67, 0x24(%rax)
je 0xd547d
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl $0x14, 0x28(%rax)
movq 0x10(%rsp), %rax
movl 0x24(%rax), %ecx
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
jmp 0xd547f
jmp 0xd5481
movq 0x10(%rsp), %rax
movq 0x1b0(%rax), %rax
cmpl $0x0, 0x1c(%rax)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xd549f
jmp 0xd558d
movq 0x10(%rsp), %rax
movq 0x1b0(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x10(%rsp), %rcx
cmpl 0x140(%rcx), %eax
jae 0xd5571
movq 0x10(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0xd551e
movl 0xc(%rsp), %eax
movl %eax, %ecx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%rsp), %rax
movl 0x140(%rax), %eax
movl %eax, %ecx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x10(%rax)
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x1c8(%rax), %rax
movq 0x8(%rax), %rax
movq 0x10(%rsp), %rdi
xorl %ecx, %ecx
movl %ecx, %esi
callq *%rax
cmpl $0x0, %eax
jne 0xd555f
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl $0x18, 0x28(%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
jmp 0xd5561
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0xd54bd
movq 0x10(%rsp), %rax
movq 0x1b0(%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
jmp 0xd5481
movq 0x10(%rsp), %rax
movq 0x1d0(%rax), %rax
movq 0x18(%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rax
movq 0x10(%rsp), %rdi
callq *%rax
movq 0x10(%rsp), %rdi
callq 0xf91a0
addq $0x18, %rsp
retq
nopw (%rax,%rax)