// File: tomfunkhouser[P]gaps/pkgs/jpeg/jquant2.c
// Function: start_pass_2_quant
METHODDEF(void)
start_pass_2_quant (j_decompress_ptr cinfo, jboolean is_pre_scan)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  hist3d histogram = cquantize->histogram;
  int i;

  /* Only F-S dithering or no dithering is supported. */
  /* If user asks for ordered dither, give him F-S. */
  if (cinfo->dither_mode != JDITHER_NONE)
    cinfo->dither_mode = JDITHER_FS;

  if (is_pre_scan) {
    /* Set up method pointers */
    cquantize->pub.color_quantize = prescan_quantize;
    cquantize->pub.finish_pass = finish_pass1;
    cquantize->needs_zeroed = TRUE; /* Always zero histogram */
  } else {
    /* Set up method pointers */
    if (cinfo->dither_mode == JDITHER_FS)
      cquantize->pub.color_quantize = pass2_fs_dither;
    else
      cquantize->pub.color_quantize = pass2_no_dither;
    cquantize->pub.finish_pass = finish_pass2;

    /* Make sure color count is acceptable */
    i = cinfo->actual_number_of_colors;
    if (i < 1)
      ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1);
    if (i > MAXNUMCOLORS)
      ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);

    if (cinfo->dither_mode == JDITHER_FS) {
      size_t arraysize = (size_t) ((cinfo->output_width + 2) *
				   (3 * SIZEOF(FSERROR)));
      /* Allocate Floyd-Steinberg workspace if we didn't already. */
      if (cquantize->fserrors == NULL)
	cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
	  ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
      /* Initialize the propagated errors to zero. */
      jzero_far((void FAR *) cquantize->fserrors, arraysize);
      /* Make the error-limit table if we didn't already. */
      if (cquantize->error_limiter == NULL)
	init_error_limit(cinfo);
      cquantize->on_odd_row = FALSE;
    }

  }
  /* Zero the histogram or inverse color map, if necessary */
  if (cquantize->needs_zeroed) {
    for (i = 0; i < HIST_C0_ELEMS; i++) {
      jzero_far((void FAR *) histogram[i],
		HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
    }
    cquantize->needs_zeroed = FALSE;
  }
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movl %esi, 0x2c(%rsp)
movq 0x30(%rsp), %rax
movq 0x270(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rax
cmpl $0x0, 0x70(%rax)
je 0xf5c33
movq 0x30(%rsp), %rax
movl $0x2, 0x70(%rax)
cmpl $0x0, 0x2c(%rsp)
je 0xf5c6b
movq 0x20(%rsp), %rax
leaq 0x35a(%rip), %rcx       # 0xf5fa0
movq %rcx, 0x8(%rax)
movq 0x20(%rsp), %rax
leaq 0x46a(%rip), %rcx       # 0xf60c0
movq %rcx, 0x10(%rax)
movq 0x20(%rsp), %rax
movl $0x1, 0x38(%rax)
jmp 0xf5dbd
movq 0x30(%rsp), %rax
cmpl $0x2, 0x70(%rax)
jne 0xf5c88
movq 0x20(%rsp), %rax
leaq 0x49e(%rip), %rcx       # 0xf6120
movq %rcx, 0x8(%rax)
jmp 0xf5c98
movq 0x20(%rsp), %rax
leaq 0xb1c(%rip), %rcx       # 0xf67b0
movq %rcx, 0x8(%rax)
movq 0x20(%rsp), %rax
leaq 0xc8c(%rip), %rcx       # 0xf6930
movq %rcx, 0x10(%rax)
movq 0x30(%rsp), %rax
movl 0x9c(%rax), %eax
movl %eax, 0x14(%rsp)
cmpl $0x1, 0x14(%rsp)
jge 0xf5cee
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x38, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x1, 0x2c(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
cmpl $0x100, 0x14(%rsp)      # imm = 0x100
jle 0xf5d28
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x39, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x100, 0x2c(%rax)      # imm = 0x100
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x30(%rsp), %rax
cmpl $0x2, 0x70(%rax)
jne 0xf5dbb
movq 0x30(%rsp), %rax
movl 0x88(%rax), %eax
addl $0x2, %eax
movl %eax, %eax
imulq $0x6, %rax, %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
cmpq $0x0, 0x40(%rax)
jne 0xf5d86
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rdx
movl $0x1, %esi
callq *%rax
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x20(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x8(%rsp), %rsi
callq 0xf9470
movq 0x20(%rsp), %rax
cmpq $0x0, 0x50(%rax)
jne 0xf5daf
movq 0x30(%rsp), %rdi
callq 0xf5e40
movq 0x20(%rsp), %rax
movl $0x0, 0x48(%rax)
jmp 0xf5dbd
movq 0x20(%rsp), %rax
cmpl $0x0, 0x38(%rax)
je 0xf5e08
movl $0x0, 0x14(%rsp)
cmpl $0x20, 0x14(%rsp)
jge 0xf5dfc
movq 0x18(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0xf9470
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0xf5dd0
movq 0x20(%rsp), %rax
movl $0x0, 0x38(%rax)
addq $0x38, %rsp
retq
nopl (%rax)