// File: tomfunkhouser[P]gaps/pkgs/jpeg/jquant1.c
// Function: create_colorindex
LOCAL(void)
create_colorindex (j_decompress_ptr cinfo)
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  JSAMPROW indexptr;
  int i,j,k, nci, blksize, val, pad;

  /* For ordered dither, we pad the color index tables by MAXJSAMPLE in
   * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE).
   * This is not necessary in the other dithering modes.  However, we
   * flag whether it was done in case user changes dithering mode.
   */
  if (cinfo->dither_mode == JDITHER_ORDERED) {
    pad = MAXJSAMPLE*2;
    cquantize->is_padded = TRUE;
  } else {
    pad = 0;
    cquantize->is_padded = FALSE;
  }

  cquantize->colorindex = (*cinfo->mem->alloc_sarray)
    ((j_common_ptr) cinfo, JPOOL_IMAGE,
     (JDIMENSION) (MAXJSAMPLE+1 + pad),
     (JDIMENSION) cinfo->out_color_components);

  /* blksize is number of adjacent repeated entries for a component */
  blksize = cquantize->sv_actual;

  for (i = 0; i < cinfo->out_color_components; i++) {
    /* fill in colorindex entries for i'th color component */
    nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
    blksize = blksize / nci;

    /* adjust colorindex pointers to provide padding at negative indexes. */
    if (pad)
      cquantize->colorindex[i] += MAXJSAMPLE;

    /* in loop, val = index of current output value, */
    /* and k = largest j that maps to current val */
    indexptr = cquantize->colorindex[i];
    val = 0;
    k = largest_input_value(cinfo, i, 0, nci-1);
    for (j = 0; j <= MAXJSAMPLE; j++) {
      while (j > k)		/* advance val if past boundary */
	k = largest_input_value(cinfo, i, ++val, nci-1);
      /* premultiply so that no multiplication needed in main processing */
      indexptr[j] = (JSAMPLE) (val * blksize);
    }
    /* Pad at both ends if necessary */
    if (pad)
      for (j = 1; j <= MAXJSAMPLE; j++) {
	indexptr[-j] = indexptr[0];
	indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE];
      }
  }
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq 0x270(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rax
cmpl $0x1, 0x70(%rax)
jne 0xf486b
movl $0x1fe, 0x4(%rsp)       # imm = 0x1FE
movq 0x28(%rsp), %rax
movl $0x1, 0x38(%rax)
jmp 0xf487f
movl $0x0, 0x4(%rsp)
movq 0x28(%rsp), %rax
movl $0x0, 0x38(%rax)
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq 0x30(%rsp), %rdi
movl 0x4(%rsp), %edx
addl $0x100, %edx            # imm = 0x100
movq 0x30(%rsp), %rcx
movl 0x90(%rcx), %ecx
movl $0x1, %esi
callq *%rax
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x30(%rax)
movq 0x28(%rsp), %rax
movl 0x28(%rax), %eax
movl %eax, 0xc(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movq 0x30(%rsp), %rcx
cmpl 0x90(%rcx), %eax
jge 0xf4a3a
movq 0x28(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movl 0x3c(%rax,%rcx,4), %eax
movl %eax, 0x10(%rsp)
movl 0xc(%rsp), %eax
cltd
idivl 0x10(%rsp)
movl %eax, 0xc(%rsp)
cmpl $0x0, 0x4(%rsp)
je 0xf4925
movq 0x28(%rsp), %rax
movq 0x30(%rax), %rax
movslq 0x1c(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0xff, %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x28(%rsp), %rax
movq 0x30(%rax), %rax
movslq 0x1c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
movl $0x0, 0x8(%rsp)
movq 0x30(%rsp), %rdi
movl 0x1c(%rsp), %esi
movl 0x10(%rsp), %ecx
subl $0x1, %ecx
xorl %edx, %edx
callq 0xf5940
movl %eax, 0x14(%rsp)
movl $0x0, 0x18(%rsp)
cmpl $0xff, 0x18(%rsp)
jg 0xf49c8
jmp 0xf4973
movl 0x18(%rsp), %eax
cmpl 0x14(%rsp), %eax
jle 0xf49a3
movq 0x30(%rsp), %rdi
movl 0x1c(%rsp), %esi
movl 0x8(%rsp), %edx
addl $0x1, %edx
movl %edx, 0x8(%rsp)
movl 0x10(%rsp), %ecx
subl $0x1, %ecx
callq 0xf5940
movl %eax, 0x14(%rsp)
jmp 0xf4973
movl 0x8(%rsp), %eax
imull 0xc(%rsp), %eax
movb %al, %dl
movq 0x20(%rsp), %rax
movslq 0x18(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0xf4967
cmpl $0x0, 0x4(%rsp)
je 0xf4a28
movl $0x1, 0x18(%rsp)
cmpl $0xff, 0x18(%rsp)
jg 0xf4a26
movq 0x20(%rsp), %rax
movb (%rax), %dl
movq 0x20(%rsp), %rax
xorl %ecx, %ecx
subl 0x18(%rsp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq 0x20(%rsp), %rax
movb 0xff(%rax), %dl
movq 0x20(%rsp), %rax
movl 0x18(%rsp), %ecx
addl $0xff, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0xf49d7
jmp 0xf4a28
jmp 0xf4a2a
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xf48cd
addq $0x38, %rsp
retq
nop