// File: /workspace/github_cpp_makefile_star2_O3/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
movl $0x1, %esi
callq *0x10(%rax)
movq %rax, 0x30(%r14)
cmpl $0x0, 0x90(%rbx)
jle 0x83d02
movl 0x28(%r14), %ecx
xorl %edi, %edi
movl $0xff, %r8d
movslq 0x3c(%r14,%rdi,4), %r10
movl %ecx, %eax
cltd
idivl %r10d
movq 0x30(%r14), %rdx
movl %eax, %ecx
cmpl $0x1, %ebp
jne 0x83c54
addq $0xff, (%rdx,%rdi,8)
movq 0x30(%r14), %rdx
movq (%rdx,%rdi,8), %r9
leaq 0xfe(%r10), %rax
leal -0x2(,%r10,2), %edx
movslq %edx, %r11
cqto
idivq %r11
addq $0x2fc, %r10            # imm = 0x2FC
xorl %r15d, %r15d
xorl %r12d, %r12d
movslq %eax, %rdx
cmpq %rdx, %r15
jle 0x83caf
movslq %r12d, %rsi
leaq (%rsi,%rsi), %rax
shlq $0x9, %rsi
subq %rax, %rsi
addq %r10, %rsi
movq %rsi, %rax
cqto
idivq %r11
movslq %eax, %rdx
incl %r12d
addq $0x1fe, %rsi            # imm = 0x1FE
cmpq %rdx, %r15
jg 0x83c95
movl %r12d, %edx
imull %ecx, %edx
movb %dl, (%r9,%r15)
incq %r15
cmpq $0x100, %r15            # imm = 0x100
jne 0x83c7c
cmpl $0x1, %ebp
jne 0x83cef
movb (%r9), %al
leaq -0x1(%r9), %rdx
xorl %esi, %esi
movb %al, (%rdx)
movb 0xff(%r9), %r10b
movb %r10b, 0x100(%r9,%rsi)
decq %rdx
incq %rsi
cmpq %r8, %rsi
jne 0x83cd3
incq %rdi
movslq 0x90(%rbx), %rax
cmpq %rax, %rdi
jl 0x83c32
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq