// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %rbx
movq 0x270(%rdi), %r14
movl 0x70(%rdi), %ebp
xorl %eax, %eax
cmpl $0x1, %ebp
movl $0x2fe, %ecx            # imm = 0x2FE
movl $0x100, %edx            # imm = 0x100
cmovel %ecx, %edx
sete %al
movl %eax, 0x38(%r14)
movq 0x8(%rdi), %rax
movl 0x90(%rdi), %ecx
pushq $0x1
popq %rsi
callq *0x10(%rax)
movq %rax, 0x30(%r14)
movl 0x28(%r14), %ecx
xorl %r8d, %r8d
movslq 0x90(%rbx), %rax
cmpq %rax, %r8
jge 0x77367
movslq 0x3c(%r14,%r8,4), %r10
movl %ecx, %eax
cltd
idivl %r10d
movq 0x30(%r14), %rdx
movl %eax, %ecx
cmpl $0x1, %ebp
jne 0x772b3
addq $0xff, (%rdx,%r8,8)
movq 0x30(%r14), %rdx
movq (%rdx,%r8,8), %r9
leaq 0xfe(%r10), %rax
leal -0x2(,%r10,2), %edx
movslq %edx, %r11
cqto
idivq %r11
movq %rax, %rdx
addq $0x2fc, %r10            # imm = 0x2FC
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpq $0x100, %r15            # imm = 0x100
je 0x77326
movl %ecx, %eax
mulb %r12b
movl %eax, %esi
movslq %r12d, %r12
imulq $0x1fe, %r12, %rdi      # imm = 0x1FE
addq %r10, %rdi
movslq %edx, %rax
cmpq %rax, %r15
jle 0x7731d
incq %r12
movq %rdi, %rax
cqto
idivq %r11
movq %rax, %rdx
addb %cl, %sil
addq $0x1fe, %rdi            # imm = 0x1FE
jmp 0x772fb
movb %sil, (%r9,%r15)
incq %r15
jmp 0x772de
cmpl $0x1, %ebp
jne 0x7735f
leaq 0x100(%r9), %rax
movl $0xff, %esi
movl $0xff, %edx
subq $0x1, %rdx
jb 0x7735f
movb (%r9), %dil
movb %dil, -0x100(%r9,%rsi)
movb 0xff(%r9), %sil
movb %sil, (%rax)
incq %rax
movq %rdx, %rsi
jmp 0x7733c
incq %r8
jmp 0x77281
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq