// File: tomfunkhouser[P]gaps/pkgs/jpeg/jquant2.c
// Function: fill_inverse_cmap
LOCAL(void)
fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
/* Fill the inverse-colormap entries in the update box that contains */
/* histogram cell c0/c1/c2.  (Only that one cell MUST be filled, but */
/* we can fill as many others as we wish.) */
{
  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  hist3d histogram = cquantize->histogram;
  int minc0, minc1, minc2;	/* lower left corner of update box */
  int ic0, ic1, ic2;
  register JSAMPLE * cptr;	/* pointer into bestcolor[] array */
  register histptr cachep;	/* pointer into main cache array */
  /* This array lists the candidate colormap indexes. */
  JSAMPLE colorlist[MAXNUMCOLORS];
  int numcolors;		/* number of candidate colors */
  /* This array holds the actually closest colormap index for each cell. */
  JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];

  /* Convert cell coordinates to update box ID */
  c0 >>= BOX_C0_LOG;
  c1 >>= BOX_C1_LOG;
  c2 >>= BOX_C2_LOG;

  /* Compute true coordinates of update box's origin corner.
   * Actually we compute the coordinates of the center of the corner
   * histogram cell, which are the lower bounds of the volume we care about.
   */
  minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1);
  minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
  minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
  
  /* Determine which colormap entries are close enough to be candidates
   * for the nearest entry to some cell in the update box.
   */
  numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);

  /* Determine the actually nearest colors. */
  find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
		   bestcolor);

  /* Save the best color numbers (plus 1) in the main cache array */
  c0 <<= BOX_C0_LOG;		/* convert ID back to base cell indexes */
  c1 <<= BOX_C1_LOG;
  c2 <<= BOX_C2_LOG;
  cptr = bestcolor;
  for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++) {
    for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) {
      cachep = & histogram[c0+ic0][c1+ic1][c2];
      for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) {
	*cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
      }
    }
  }
}

// Assembly:
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, 0x1f0(%rsp)
movl %esi, 0x1ec(%rsp)
movl %edx, 0x1e8(%rsp)
movl %ecx, 0x1e4(%rsp)
movq 0x1f0(%rsp), %rax
movq 0x270(%rax), %rax
movq %rax, 0x1d8(%rsp)
movq 0x1d8(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x1d0(%rsp)
movl 0x1ec(%rsp), %eax
sarl $0x2, %eax
movl %eax, 0x1ec(%rsp)
movl 0x1e8(%rsp), %eax
sarl $0x3, %eax
movl %eax, 0x1e8(%rsp)
movl 0x1e4(%rsp), %eax
sarl $0x2, %eax
movl %eax, 0x1e4(%rsp)
movl 0x1ec(%rsp), %eax
shll $0x5, %eax
addl $0x4, %eax
movl %eax, 0x1cc(%rsp)
movl 0x1e8(%rsp), %eax
shll $0x5, %eax
addl $0x2, %eax
movl %eax, 0x1c8(%rsp)
movl 0x1e4(%rsp), %eax
shll $0x5, %eax
addl $0x4, %eax
movl %eax, 0x1c4(%rsp)
movq 0x1f0(%rsp), %rdi
movl 0x1cc(%rsp), %esi
movl 0x1c8(%rsp), %edx
movl 0x1c4(%rsp), %ecx
leaq 0xa0(%rsp), %r8
callq 0xf79c0
movl %eax, 0x9c(%rsp)
movq 0x1f0(%rsp), %rdi
movl 0x1cc(%rsp), %esi
movl 0x1c8(%rsp), %edx
movl 0x1c4(%rsp), %ecx
movl 0x9c(%rsp), %r8d
leaq 0xa0(%rsp), %r9
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
callq 0xf80c0
movl 0x1ec(%rsp), %eax
shll $0x2, %eax
movl %eax, 0x1ec(%rsp)
movl 0x1e8(%rsp), %eax
shll $0x3, %eax
movl %eax, 0x1e8(%rsp)
movl 0x1e4(%rsp), %eax
shll $0x2, %eax
movl %eax, 0x1e4(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0x1b0(%rsp)
movl $0x0, 0x1c0(%rsp)
cmpl $0x4, 0x1c0(%rsp)
jge 0xf79a9
movl $0x0, 0x1bc(%rsp)
cmpl $0x8, 0x1bc(%rsp)
jge 0xf7991
movq 0x1d0(%rsp), %rax
movl 0x1ec(%rsp), %ecx
addl 0x1c0(%rsp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl 0x1e8(%rsp), %ecx
addl 0x1bc(%rsp), %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x1e4(%rsp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, 0x1a8(%rsp)
movl $0x0, 0x1b8(%rsp)
cmpl $0x4, 0x1b8(%rsp)
jge 0xf7979
movq 0x1b0(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x1b0(%rsp)
movzbl (%rax), %eax
addl $0x1, %eax
movw %ax, %cx
movq 0x1a8(%rsp), %rax
movq %rax, %rdx
addq $0x2, %rdx
movq %rdx, 0x1a8(%rsp)
movw %cx, (%rax)
movl 0x1b8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1b8(%rsp)
jmp 0xf7922
jmp 0xf797b
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
jmp 0xf78be
jmp 0xf7993
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
jmp 0xf78a5
addq $0x1f8, %rsp            # imm = 0x1F8
retq
nopw %cs:(%rax,%rax)