// File: tomfunkhouser[P]gaps/pkgs/png/pngrtran.c
// Function: png_set_quantize
void PNGAPI
png_set_quantize(png_structp png_ptr, png_colorp palette,
   int num_palette, int maximum_colors, png_uint_16p histogram,
   int full_quantize)
{
   png_debug(1, "in png_set_quantize");

   if (png_ptr == NULL)
      return;
   png_ptr->transformations |= PNG_QUANTIZE;

   if (!full_quantize)
   {
      int i;

      png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,
         (png_uint_32)(num_palette * png_sizeof(png_byte)));
      for (i = 0; i < num_palette; i++)
         png_ptr->quantize_index[i] = (png_byte)i;
   }

   if (num_palette > maximum_colors)
   {
      if (histogram != NULL)
      {
         /* This is easy enough, just throw out the least used colors.
          * Perhaps not the best solution, but good enough.
          */

         int i;

         /* Initialize an array to sort colors */
         png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
            (png_uint_32)(num_palette * png_sizeof(png_byte)));

         /* Initialize the quantize_sort array */
         for (i = 0; i < num_palette; i++)
            png_ptr->quantize_sort[i] = (png_byte)i;

         /* Find the least used palette entries by starting a
          * bubble sort, and running it until we have sorted
          * out enough colors.  Note that we don't care about
          * sorting all the colors, just finding which are
          * least used.
          */

         for (i = num_palette - 1; i >= maximum_colors; i--)
         {
            int done; /* To stop early if the list is pre-sorted */
            int j;

            done = 1;
            for (j = 0; j < i; j++)
            {
               if (histogram[png_ptr->quantize_sort[j]]
                   < histogram[png_ptr->quantize_sort[j + 1]])
               {
                  png_byte t;

                  t = png_ptr->quantize_sort[j];
                  png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];
                  png_ptr->quantize_sort[j + 1] = t;
                  done = 0;
               }
            }
            if (done)
               break;
         }

         /* Swap the palette around, and set up a table, if necessary */
         if (full_quantize)
         {
            int j = num_palette;

            /* Put all the useful colors within the max, but don't
             * move the others.
             */
            for (i = 0; i < maximum_colors; i++)
            {
               if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
               {
                  do
                     j--;
                  while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
                  palette[i] = palette[j];
               }
            }
         }
         else
         {
            int j = num_palette;

            /* Move all the used colors inside the max limit, and
             * develop a translation table.
             */
            for (i = 0; i < maximum_colors; i++)
            {
               /* Only move the colors we need to */
               if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
               {
                  png_color tmp_color;

                  do
                     j--;
                  while ((int)png_ptr->quantize_sort[j] >= maximum_colors);

                  tmp_color = palette[j];
                  palette[j] = palette[i];
                  palette[i] = tmp_color;
                  /* Indicate where the color went */
                  png_ptr->quantize_index[j] = (png_byte)i;
                  png_ptr->quantize_index[i] = (png_byte)j;
               }
            }

            /* Find closest color for those colors we are not using */
            for (i = 0; i < num_palette; i++)
            {
               if ((int)png_ptr->quantize_index[i] >= maximum_colors)
               {
                  int min_d, k, min_k, d_index;

                  /* Find the closest color to one we threw out */
                  d_index = png_ptr->quantize_index[i];
                  min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
                  for (k = 1, min_k = 0; k < maximum_colors; k++)
                  {
                     int d;

                     d = PNG_COLOR_DIST(palette[d_index], palette[k]);

                     if (d < min_d)
                     {
                        min_d = d;
                        min_k = k;
                     }
                  }
                  /* Point to closest color */
                  png_ptr->quantize_index[i] = (png_byte)min_k;
               }
            }
         }
         png_free(png_ptr, png_ptr->quantize_sort);
         png_ptr->quantize_sort = NULL;
      }
      else
      {
         /* This is much harder to do simply (and quickly).  Perhaps
          * we need to go through a median cut routine, but those
          * don't always behave themselves with only a few colors
          * as input.  So we will just find the closest two colors,
          * and throw out one of them (chosen somewhat randomly).
          * [We don't understand this at all, so if someone wants to
          *  work on improving it, be our guest - AED, GRP]
          */
         int i;
         int max_d;
         int num_new_palette;
         png_dsortp t;
         png_dsortpp hash;

         t = NULL;

         /* Initialize palette index arrays */
         png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
            (png_uint_32)(num_palette * png_sizeof(png_byte)));
         png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
            (png_uint_32)(num_palette * png_sizeof(png_byte)));

         /* Initialize the sort array */
         for (i = 0; i < num_palette; i++)
         {
            png_ptr->index_to_palette[i] = (png_byte)i;
            png_ptr->palette_to_index[i] = (png_byte)i;
         }

         hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
            png_sizeof(png_dsortp)));

         num_new_palette = num_palette;

         /* Initial wild guess at how far apart the farthest pixel
          * pair we will be eliminating will be.  Larger
          * numbers mean more areas will be allocated, Smaller
          * numbers run the risk of not saving enough data, and
          * having to do this all over again.
          *
          * I have not done extensive checking on this number.
          */
         max_d = 96;

         while (num_new_palette > maximum_colors)
         {
            for (i = 0; i < num_new_palette - 1; i++)
            {
               int j;

               for (j = i + 1; j < num_new_palette; j++)
               {
                  int d;

                  d = PNG_COLOR_DIST(palette[i], palette[j]);

                  if (d <= max_d)
                  {

                     t = (png_dsortp)png_malloc_warn(png_ptr,
                         (png_uint_32)(png_sizeof(png_dsort)));
                     if (t == NULL)
                         break;
                     t->next = hash[d];
                     t->left = (png_byte)i;
                     t->right = (png_byte)j;
                     hash[d] = t;
                  }
               }
               if (t == NULL)
                  break;
            }

            if (t != NULL)
            for (i = 0; i <= max_d; i++)
            {
               if (hash[i] != NULL)
               {
                  png_dsortp p;

                  for (p = hash[i]; p; p = p->next)
                  {
                     if ((int)png_ptr->index_to_palette[p->left]
                        < num_new_palette &&
                        (int)png_ptr->index_to_palette[p->right]
                        < num_new_palette)
                     {
                        int j, next_j;

                        if (num_new_palette & 0x01)
                        {
                           j = p->left;
                           next_j = p->right;
                        }
                        else
                        {
                           j = p->right;
                           next_j = p->left;
                        }

                        num_new_palette--;
                        palette[png_ptr->index_to_palette[j]]
                          = palette[num_new_palette];
                        if (!full_quantize)
                        {
                           int k;

                           for (k = 0; k < num_palette; k++)
                           {
                              if (png_ptr->quantize_index[k] ==
                                 png_ptr->index_to_palette[j])
                                 png_ptr->quantize_index[k] =
                                    png_ptr->index_to_palette[next_j];
                              if ((int)png_ptr->quantize_index[k] ==
                                 num_new_palette)
                                 png_ptr->quantize_index[k] =
                                    png_ptr->index_to_palette[j];
                           }
                        }

                        png_ptr->index_to_palette[png_ptr->palette_to_index
                           [num_new_palette]] = png_ptr->index_to_palette[j];
                        png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
                           = png_ptr->palette_to_index[num_new_palette];

                        png_ptr->index_to_palette[j] =
                            (png_byte)num_new_palette;
                        png_ptr->palette_to_index[num_new_palette] =
                            (png_byte)j;
                     }
                     if (num_new_palette <= maximum_colors)
                        break;
                  }
                  if (num_new_palette <= maximum_colors)
                     break;
               }
            }

            for (i = 0; i < 769; i++)
            {
               if (hash[i] != NULL)
               {
                  png_dsortp p = hash[i];
                  while (p)
                  {
                     t = p->next;
                     png_free(png_ptr, p);
                     p = t;
                  }
               }
               hash[i] = 0;
            }
            max_d += 96;
         }
         png_free(png_ptr, hash);
         png_free(png_ptr, png_ptr->palette_to_index);
         png_free(png_ptr, png_ptr->index_to_palette);
         png_ptr->palette_to_index = NULL;
         png_ptr->index_to_palette = NULL;
      }
      num_palette = maximum_colors;
   }
   if (png_ptr->palette == NULL)
   {
      png_ptr->palette = palette;
   }
   png_ptr->num_palette = (png_uint_16)num_palette;

   if (full_quantize)
   {
      int i;
      png_bytep distance;
      int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +
         PNG_QUANTIZE_BLUE_BITS;
      int num_red = (1 << PNG_QUANTIZE_RED_BITS);
      int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);
      int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
      png_size_t num_entries = ((png_size_t)1 << total_bits);

      png_ptr->palette_lookup = (png_bytep )png_calloc(png_ptr,
         (png_uint_32)(num_entries * png_sizeof(png_byte)));

      distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
         png_sizeof(png_byte)));
      png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));

      for (i = 0; i < num_palette; i++)
      {
         int ir, ig, ib;
         int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS));
         int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS));
         int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS));

         for (ir = 0; ir < num_red; ir++)
         {
            /* int dr = abs(ir - r); */
            int dr = ((ir > r) ? ir - r : r - ir);
            int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS +
                PNG_QUANTIZE_GREEN_BITS));

            for (ig = 0; ig < num_green; ig++)
            {
               /* int dg = abs(ig - g); */
               int dg = ((ig > g) ? ig - g : g - ig);
               int dt = dr + dg;
               int dm = ((dr > dg) ? dr : dg);
               int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS);

               for (ib = 0; ib < num_blue; ib++)
               {
                  int d_index = index_g | ib;
                  /* int db = abs(ib - b); */
                  int db = ((ib > b) ? ib - b : b - ib);
                  int dmax = ((dm > db) ? dm : db);
                  int d = dmax + dt + db;

                  if (d < (int)distance[d_index])
                  {
                     distance[d_index] = (png_byte)d;
                     png_ptr->palette_lookup[d_index] = (png_byte)i;
                  }
               }
            }
         }
      }

      png_free(png_ptr, distance);
   }
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
movl %edx, 0x114(%rsp)
movl %ecx, 0x110(%rsp)
movq %r8, 0x108(%rsp)
movl %r9d, 0x104(%rsp)
cmpq $0x0, 0x120(%rsp)
jne 0x106355
jmp 0x1076b1
movq 0x120(%rsp), %rax
movl 0x124(%rax), %ecx
orl $0x40, %ecx
movl %ecx, 0x124(%rax)
cmpl $0x0, 0x104(%rsp)
jne 0x1063fc
movq 0x120(%rsp), %rdi
movslq 0x114(%rsp), %rax
shlq $0x0, %rax
movl %eax, %eax
movl %eax, %esi
callq 0x1135b0
movq %rax, %rcx
movq 0x120(%rsp), %rax
movq %rcx, 0x390(%rax)
movl $0x0, 0x100(%rsp)
movl 0x100(%rsp), %eax
cmpl 0x114(%rsp), %eax
jge 0x1063fa
movl 0x100(%rsp), %eax
movb %al, %dl
movq 0x120(%rsp), %rax
movq 0x390(%rax), %rax
movslq 0x100(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x100(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x100(%rsp)
jmp 0x1063b4
jmp 0x1063fc
movl 0x114(%rsp), %eax
cmpl 0x110(%rsp), %eax
jle 0x10734c
cmpq $0x0, 0x108(%rsp)
je 0x106b20
movq 0x120(%rsp), %rdi
movslq 0x114(%rsp), %rax
shlq $0x0, %rax
movl %eax, %eax
movl %eax, %esi
callq 0x1135b0
movq %rax, %rcx
movq 0x120(%rsp), %rax
movq %rcx, 0x438(%rax)
movl $0x0, 0xfc(%rsp)
movl 0xfc(%rsp), %eax
cmpl 0x114(%rsp), %eax
jge 0x10649f
movl 0xfc(%rsp), %eax
movb %al, %dl
movq 0x120(%rsp), %rax
movq 0x438(%rax), %rax
movslq 0xfc(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x106459
movl 0x114(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xfc(%rsp)
movl 0xfc(%rsp), %eax
cmpl 0x110(%rsp), %eax
jl 0x106610
movl $0x1, 0xf8(%rsp)
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
cmpl 0xfc(%rsp), %eax
jge 0x1065ec
movq 0x108(%rsp), %rax
movq 0x120(%rsp), %rcx
movq 0x438(%rcx), %rcx
movslq 0xf4(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
movzwl (%rax,%rcx,2), %eax
movq 0x108(%rsp), %rcx
movq 0x120(%rsp), %rdx
movq 0x438(%rdx), %rdx
movl 0xf4(%rsp), %esi
addl $0x1, %esi
movslq %esi, %rsi
movzbl (%rdx,%rsi), %edx
movzwl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
jge 0x1065d4
movq 0x120(%rsp), %rax
movq 0x438(%rax), %rax
movslq 0xf4(%rsp), %rcx
movb (%rax,%rcx), %al
movb %al, 0xf3(%rsp)
movq 0x120(%rsp), %rax
movq 0x438(%rax), %rax
movl 0xf4(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq 0x120(%rsp), %rax
movq 0x438(%rax), %rax
movslq 0xf4(%rsp), %rcx
movb %dl, (%rax,%rcx)
movb 0xf3(%rsp), %dl
movq 0x120(%rsp), %rax
movq 0x438(%rax), %rax
movl 0xf4(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl $0x0, 0xf8(%rsp)
jmp 0x1065d6
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x1064da
cmpl $0x0, 0xf8(%rsp)
je 0x1065f8
jmp 0x106610
jmp 0x1065fa
movl 0xfc(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x1064b0
cmpl $0x0, 0x104(%rsp)
je 0x1066fd
movl 0x114(%rsp), %eax
movl %eax, 0xec(%rsp)
movl $0x0, 0xfc(%rsp)
movl 0xfc(%rsp), %eax
cmpl 0x110(%rsp), %eax
jge 0x1066f8
movq 0x120(%rsp), %rax
movq 0x438(%rax), %rax
movslq 0xfc(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl 0x110(%rsp), %eax
jl 0x1066e0
jmp 0x106671
movl 0xec(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xec(%rsp)
movq 0x120(%rsp), %rax
movq 0x438(%rax), %rax
movslq 0xec(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl 0x110(%rsp), %eax
jge 0x106671
movq 0x118(%rsp), %rax
movslq 0xfc(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movq 0x118(%rsp), %rcx
movslq 0xec(%rsp), %rdx
imulq $0x3, %rdx, %rdx
addq %rdx, %rcx
movw (%rcx), %dx
movw %dx, (%rax)
movb 0x2(%rcx), %cl
movb %cl, 0x2(%rax)
jmp 0x1066e2
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x106637
jmp 0x106aec
movl 0x114(%rsp), %eax
movl %eax, 0xe8(%rsp)
movl $0x0, 0xfc(%rsp)
movl 0xfc(%rsp), %eax
cmpl 0x110(%rsp), %eax
jge 0x106879
movq 0x120(%rsp), %rax
movq 0x438(%rax), %rax
movslq 0xfc(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl 0x110(%rsp), %eax
jl 0x106861
jmp 0x106754
movl 0xe8(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xe8(%rsp)
movq 0x120(%rsp), %rax
movq 0x438(%rax), %rax
movslq 0xe8(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl 0x110(%rsp), %eax
jge 0x106754
movq 0x118(%rsp), %rax
movslq 0xe8(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movw (%rax), %cx
movw %cx, 0xe5(%rsp)
movb 0x2(%rax), %al
movb %al, 0xe7(%rsp)
movq 0x118(%rsp), %rax
movslq 0xe8(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movq 0x118(%rsp), %rcx
movslq 0xfc(%rsp), %rdx
imulq $0x3, %rdx, %rdx
addq %rdx, %rcx
movw (%rcx), %dx
movw %dx, (%rax)
movb 0x2(%rcx), %cl
movb %cl, 0x2(%rax)
movq 0x118(%rsp), %rax
movslq 0xfc(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movw 0xe5(%rsp), %cx
movw %cx, (%rax)
movb 0xe7(%rsp), %cl
movb %cl, 0x2(%rax)
movl 0xfc(%rsp), %eax
movb %al, %dl
movq 0x120(%rsp), %rax
movq 0x390(%rax), %rax
movslq 0xe8(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0xe8(%rsp), %eax
movb %al, %dl
movq 0x120(%rsp), %rax
movq 0x390(%rax), %rax
movslq 0xfc(%rsp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x106863
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x106716
movl $0x0, 0xfc(%rsp)
movl 0xfc(%rsp), %eax
cmpl 0x114(%rsp), %eax
jge 0x106aea
movq 0x120(%rsp), %rax
movq 0x390(%rax), %rax
movslq 0xfc(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl 0x110(%rsp), %eax
jl 0x106ad2
movq 0x120(%rsp), %rax
movq 0x390(%rax), %rax
movslq 0xfc(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0xd4(%rsp)
movq 0x118(%rsp), %rax
movslq 0xd4(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movzbl (%rax), %eax
movq 0x118(%rsp), %rcx
movzbl (%rcx), %ecx
subl %ecx, %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movq 0x118(%rsp), %rcx
movslq 0xd4(%rsp), %rdx
imulq $0x3, %rdx, %rdx
addq %rdx, %rcx
movzbl 0x1(%rcx), %ecx
movq 0x118(%rsp), %rdx
movzbl 0x1(%rdx), %edx
subl %edx, %ecx
movl %ecx, %edx
negl %edx
cmovnsl %edx, %ecx
addl %ecx, %eax
movq 0x118(%rsp), %rcx
movslq 0xd4(%rsp), %rdx
imulq $0x3, %rdx, %rdx
addq %rdx, %rcx
movzbl 0x2(%rcx), %ecx
movq 0x118(%rsp), %rdx
movzbl 0x2(%rdx), %edx
subl %edx, %ecx
movl %ecx, %edx
negl %edx
cmovnsl %edx, %ecx
addl %ecx, %eax
movl %eax, 0xe0(%rsp)
movl $0x1, 0xdc(%rsp)
movl $0x0, 0xd8(%rsp)
movl 0xdc(%rsp), %eax
cmpl 0x110(%rsp), %eax
jge 0x106aaf
movq 0x118(%rsp), %rax
movslq 0xd4(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movzbl (%rax), %eax
movq 0x118(%rsp), %rcx
movslq 0xdc(%rsp), %rdx
imulq $0x3, %rdx, %rdx
addq %rdx, %rcx
movzbl (%rcx), %ecx
subl %ecx, %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movq 0x118(%rsp), %rcx
movslq 0xd4(%rsp), %rdx
imulq $0x3, %rdx, %rdx
addq %rdx, %rcx
movzbl 0x1(%rcx), %ecx
movq 0x118(%rsp), %rdx
movslq 0xdc(%rsp), %rsi
imulq $0x3, %rsi, %rsi
addq %rsi, %rdx
movzbl 0x1(%rdx), %edx
subl %edx, %ecx
movl %ecx, %edx
negl %edx
cmovnsl %edx, %ecx
addl %ecx, %eax
movq 0x118(%rsp), %rcx
movslq 0xd4(%rsp), %rdx
imulq $0x3, %rdx, %rdx
addq %rdx, %rcx
movzbl 0x2(%rcx), %ecx
movq 0x118(%rsp), %rdx
movslq 0xdc(%rsp), %rsi
imulq $0x3, %rsi, %rsi
addq %rsi, %rdx
movzbl 0x2(%rdx), %edx
subl %edx, %ecx
movl %ecx, %edx
negl %edx
cmovnsl %edx, %ecx
addl %ecx, %eax
movl %eax, 0xd0(%rsp)
movl 0xd0(%rsp), %eax
cmpl 0xe0(%rsp), %eax
jge 0x106a97
movl 0xd0(%rsp), %eax
movl %eax, 0xe0(%rsp)
movl 0xdc(%rsp), %eax
movl %eax, 0xd8(%rsp)
jmp 0x106a99
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0x106991
movl 0xd8(%rsp), %eax
movb %al, %dl
movq 0x120(%rsp), %rax
movq 0x390(%rax), %rax
movslq 0xfc(%rsp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x106ad4
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x106884
jmp 0x106aec
movq 0x120(%rsp), %rdi
movq 0x120(%rsp), %rax
movq 0x438(%rax), %rsi
callq 0x1136d0
movq 0x120(%rsp), %rax
movq $0x0, 0x438(%rax)
jmp 0x10733e
movq $0x0, 0xb8(%rsp)
movq 0x120(%rsp), %rdi
movslq 0x114(%rsp), %rax
shlq $0x0, %rax
movl %eax, %eax
movl %eax, %esi
callq 0x1135b0
movq %rax, %rcx
movq 0x120(%rsp), %rax
movq %rcx, 0x440(%rax)
movq 0x120(%rsp), %rdi
movslq 0x114(%rsp), %rax
shlq $0x0, %rax
movl %eax, %eax
movl %eax, %esi
callq 0x1135b0
movq %rax, %rcx
movq 0x120(%rsp), %rax
movq %rcx, 0x448(%rax)
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
cmpl 0x114(%rsp), %eax
jge 0x106bfe
movl 0xcc(%rsp), %eax
movb %al, %dl
movq 0x120(%rsp), %rax
movq 0x440(%rax), %rax
movslq 0xcc(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0xcc(%rsp), %eax
movb %al, %dl
movq 0x120(%rsp), %rax
movq 0x448(%rax), %rax
movslq 0xcc(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x106b95
movq 0x120(%rsp), %rdi
movl $0x1808, %esi           # imm = 0x1808
callq 0x113560
movq %rax, 0xb0(%rsp)
movl 0x114(%rsp), %eax
movl %eax, 0xc4(%rsp)
movl $0x60, 0xc8(%rsp)
movl 0xc4(%rsp), %eax
cmpl 0x110(%rsp), %eax
jle 0x1072cb
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
movl 0xc4(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x106e2f
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
movl 0xac(%rsp), %eax
cmpl 0xc4(%rsp), %eax
jge 0x106e0a
movq 0x118(%rsp), %rax
movslq 0xcc(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movzbl (%rax), %eax
movq 0x118(%rsp), %rcx
movslq 0xac(%rsp), %rdx
imulq $0x3, %rdx, %rdx
addq %rdx, %rcx
movzbl (%rcx), %ecx
subl %ecx, %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movq 0x118(%rsp), %rcx
movslq 0xcc(%rsp), %rdx
imulq $0x3, %rdx, %rdx
addq %rdx, %rcx
movzbl 0x1(%rcx), %ecx
movq 0x118(%rsp), %rdx
movslq 0xac(%rsp), %rsi
imulq $0x3, %rsi, %rsi
addq %rsi, %rdx
movzbl 0x1(%rdx), %edx
subl %edx, %ecx
movl %ecx, %edx
negl %edx
cmovnsl %edx, %ecx
addl %ecx, %eax
movq 0x118(%rsp), %rcx
movslq 0xcc(%rsp), %rdx
imulq $0x3, %rdx, %rdx
addq %rdx, %rcx
movzbl 0x2(%rcx), %ecx
movq 0x118(%rsp), %rdx
movslq 0xac(%rsp), %rsi
imulq $0x3, %rsi, %rsi
addq %rsi, %rdx
movzbl 0x2(%rdx), %edx
subl %edx, %ecx
movl %ecx, %edx
negl %edx
cmovnsl %edx, %ecx
addl %ecx, %eax
movl %eax, 0xa8(%rsp)
movl 0xa8(%rsp), %eax
cmpl 0xc8(%rsp), %eax
jg 0x106df2
movq 0x120(%rsp), %rdi
movl $0x10, %esi
callq 0x113760
movq %rax, 0xb8(%rsp)
cmpq $0x0, 0xb8(%rsp)
jne 0x106d8f
jmp 0x106e0a
movq 0xb0(%rsp), %rax
movslq 0xa8(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
movq 0xb8(%rsp), %rax
movq %rcx, (%rax)
movl 0xcc(%rsp), %eax
movb %al, %cl
movq 0xb8(%rsp), %rax
movb %cl, 0x8(%rax)
movl 0xac(%rsp), %eax
movb %al, %cl
movq 0xb8(%rsp), %rax
movb %cl, 0x9(%rax)
movq 0xb8(%rsp), %rdx
movq 0xb0(%rsp), %rax
movslq 0xa8(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x106df4
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x106c7a
cmpq $0x0, 0xb8(%rsp)
jne 0x106e17
jmp 0x106e2f
jmp 0x106e19
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x106c50
cmpq $0x0, 0xb8(%rsp)
je 0x1071f1
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
cmpl 0xc8(%rsp), %eax
jg 0x1071ef
movq 0xb0(%rsp), %rax
movslq 0xcc(%rsp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x1071d7
movq 0xb0(%rsp), %rax
movslq 0xcc(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0xa0(%rsp)
cmpq $0x0, 0xa0(%rsp)
je 0x1071c3
movq 0x120(%rsp), %rax
movq 0x440(%rax), %rax
movq 0xa0(%rsp), %rcx
movzbl 0x8(%rcx), %ecx
movzbl (%rax,%rcx), %eax
cmpl 0xc4(%rsp), %eax
jge 0x107197
movq 0x120(%rsp), %rax
movq 0x440(%rax), %rax
movq 0xa0(%rsp), %rcx
movzbl 0x9(%rcx), %ecx
movzbl (%rax,%rcx), %eax
cmpl 0xc4(%rsp), %eax
jge 0x107197
movl 0xc4(%rsp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x106f32
movq 0xa0(%rsp), %rax
movzbl 0x8(%rax), %eax
movl %eax, 0x9c(%rsp)
movq 0xa0(%rsp), %rax
movzbl 0x9(%rax), %eax
movl %eax, 0x98(%rsp)
jmp 0x106f58
movq 0xa0(%rsp), %rax
movzbl 0x9(%rax), %eax
movl %eax, 0x9c(%rsp)
movq 0xa0(%rsp), %rax
movzbl 0x8(%rax), %eax
movl %eax, 0x98(%rsp)
movl 0xc4(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xc4(%rsp)
movq 0x118(%rsp), %rax
movq 0x120(%rsp), %rcx
movq 0x440(%rcx), %rcx
movslq 0x9c(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movq 0x118(%rsp), %rcx
movslq 0xc4(%rsp), %rdx
imulq $0x3, %rdx, %rdx
addq %rdx, %rcx
movw (%rcx), %dx
movw %dx, (%rax)
movb 0x2(%rcx), %cl
movb %cl, 0x2(%rax)
cmpl $0x0, 0x104(%rsp)
jne 0x1070c3
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
cmpl 0x114(%rsp), %eax
jge 0x1070c1
movq 0x120(%rsp), %rax
movq 0x390(%rax), %rax
movslq 0x94(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movq 0x120(%rsp), %rcx
movq 0x440(%rcx), %rcx
movslq 0x9c(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x107051
movq 0x120(%rsp), %rax
movq 0x440(%rax), %rax
movslq 0x98(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0x120(%rsp), %rax
movq 0x390(%rax), %rax
movslq 0x94(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x120(%rsp), %rax
movq 0x390(%rax), %rax
movslq 0x94(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl 0xc4(%rsp), %eax
jne 0x1070a9
movq 0x120(%rsp), %rax
movq 0x440(%rax), %rax
movslq 0x9c(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0x120(%rsp), %rax
movq 0x390(%rax), %rax
movslq 0x94(%rsp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x1070ab
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x106fcf
jmp 0x1070c3
movq 0x120(%rsp), %rax
movq 0x440(%rax), %rax
movslq 0x9c(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0x120(%rsp), %rax
movq 0x440(%rax), %rax
movq 0x120(%rsp), %rcx
movq 0x448(%rcx), %rcx
movslq 0xc4(%rsp), %rsi
movzbl (%rcx,%rsi), %ecx
movb %dl, (%rax,%rcx)
movq 0x120(%rsp), %rax
movq 0x448(%rax), %rax
movslq 0xc4(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0x120(%rsp), %rax
movq 0x448(%rax), %rax
movq 0x120(%rsp), %rcx
movq 0x440(%rcx), %rcx
movslq 0x9c(%rsp), %rsi
movzbl (%rcx,%rsi), %ecx
movb %dl, (%rax,%rcx)
movl 0xc4(%rsp), %eax
movb %al, %dl
movq 0x120(%rsp), %rax
movq 0x440(%rax), %rax
movslq 0x9c(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x9c(%rsp), %eax
movb %al, %dl
movq 0x120(%rsp), %rax
movq 0x448(%rax), %rax
movslq 0xc4(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0xc4(%rsp), %eax
cmpl 0x110(%rsp), %eax
jg 0x1071a9
jmp 0x1071c3
jmp 0x1071ab
movq 0xa0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xa0(%rsp)
jmp 0x106e94
movl 0xc4(%rsp), %eax
cmpl 0x110(%rsp), %eax
jg 0x1071d5
jmp 0x1071ef
jmp 0x1071d7
jmp 0x1071d9
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x106e49
jmp 0x1071f1
movl $0x0, 0xcc(%rsp)
cmpl $0x301, 0xcc(%rsp) # imm = 0x301
jge 0x1072b5
movq 0xb0(%rsp), %rax
movslq 0xcc(%rsp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x107287
movq 0xb0(%rsp), %rax
movslq 0xcc(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x88(%rsp)
cmpq $0x0, 0x88(%rsp)
je 0x107285
movq 0x88(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0x120(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x1136d0
movq 0xb8(%rsp), %rax
movq %rax, 0x88(%rsp)
jmp 0x107240
jmp 0x107287
movq 0xb0(%rsp), %rax
movslq 0xcc(%rsp), %rcx
movq $0x0, (%rax,%rcx,8)
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x1071fc
movl 0xc8(%rsp), %eax
addl $0x60, %eax
movl %eax, 0xc8(%rsp)
jmp 0x106c31
movq 0x120(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x1136d0
movq 0x120(%rsp), %rdi
movq 0x120(%rsp), %rax
movq 0x448(%rax), %rsi
callq 0x1136d0
movq 0x120(%rsp), %rdi
movq 0x120(%rsp), %rax
movq 0x440(%rax), %rsi
callq 0x1136d0
movq 0x120(%rsp), %rax
movq $0x0, 0x448(%rax)
movq 0x120(%rsp), %rax
movq $0x0, 0x440(%rax)
movl 0x110(%rsp), %eax
movl %eax, 0x114(%rsp)
movq 0x120(%rsp), %rax
cmpq $0x0, 0x238(%rax)
jne 0x107375
movq 0x118(%rsp), %rcx
movq 0x120(%rsp), %rax
movq %rcx, 0x238(%rax)
movl 0x114(%rsp), %eax
movw %ax, %cx
movq 0x120(%rsp), %rax
movw %cx, 0x240(%rax)
cmpl $0x0, 0x104(%rsp)
je 0x1076b1
movl $0xf, 0x74(%rsp)
movl $0x20, 0x70(%rsp)
movl $0x20, 0x6c(%rsp)
movl $0x20, 0x68(%rsp)
movl 0x74(%rsp), %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, 0x60(%rsp)
movq 0x120(%rsp), %rdi
movq 0x60(%rsp), %rax
shlq $0x0, %rax
movl %eax, %eax
movl %eax, %esi
callq 0x113560
movq %rax, %rcx
movq 0x120(%rsp), %rax
movq %rcx, 0x388(%rax)
movq 0x120(%rsp), %rdi
movq 0x60(%rsp), %rax
shlq $0x0, %rax
movl %eax, %eax
movl %eax, %esi
callq 0x1135b0
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rdi
movq 0x60(%rsp), %rdx
shlq $0x0, %rdx
movl $0xff, %esi
callq 0xf280
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
cmpl 0x114(%rsp), %eax
jge 0x10769f
movq 0x118(%rsp), %rax
movslq 0x84(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movzbl (%rax), %eax
sarl $0x3, %eax
movl %eax, 0x50(%rsp)
movq 0x118(%rsp), %rax
movslq 0x84(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movzbl 0x1(%rax), %eax
sarl $0x3, %eax
movl %eax, 0x4c(%rsp)
movq 0x118(%rsp), %rax
movslq 0x84(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movzbl 0x2(%rax), %eax
sarl $0x3, %eax
movl %eax, 0x48(%rsp)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x70(%rsp), %eax
jge 0x107687
movl 0x5c(%rsp), %eax
cmpl 0x50(%rsp), %eax
jle 0x1074e4
movl 0x5c(%rsp), %eax
subl 0x50(%rsp), %eax
movl %eax, 0x1c(%rsp)
jmp 0x1074f0
movl 0x50(%rsp), %eax
subl 0x5c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x5c(%rsp), %eax
shll $0xa, %eax
movl %eax, 0x40(%rsp)
movl $0x0, 0x58(%rsp)
movl 0x58(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jge 0x107675
movl 0x58(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jle 0x107531
movl 0x58(%rsp), %eax
subl 0x4c(%rsp), %eax
movl %eax, 0x18(%rsp)
jmp 0x10753d
movl 0x4c(%rsp), %eax
subl 0x58(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x44(%rsp), %eax
addl 0x3c(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jle 0x107565
movl 0x44(%rsp), %eax
movl %eax, 0x14(%rsp)
jmp 0x10756d
movl 0x3c(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x40(%rsp), %eax
movl 0x58(%rsp), %ecx
shll $0x5, %ecx
orl %ecx, %eax
movl %eax, 0x30(%rsp)
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x68(%rsp), %eax
jge 0x107663
movl 0x30(%rsp), %eax
orl 0x54(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x48(%rsp), %eax
jle 0x1075c0
movl 0x54(%rsp), %eax
subl 0x48(%rsp), %eax
movl %eax, 0x10(%rsp)
jmp 0x1075cc
movl 0x48(%rsp), %eax
subl 0x54(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x28(%rsp), %eax
jle 0x1075e8
movl 0x34(%rsp), %eax
movl %eax, 0xc(%rsp)
jmp 0x1075f0
movl 0x28(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
addl 0x38(%rsp), %eax
addl 0x28(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
movq 0x78(%rsp), %rcx
movslq 0x2c(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jge 0x107651
movl 0x20(%rsp), %eax
movb %al, %dl
movq 0x78(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x84(%rsp), %eax
movb %al, %dl
movq 0x120(%rsp), %rax
movq 0x388(%rax), %rax
movslq 0x2c(%rsp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x107653
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x10758e
jmp 0x107665
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x10750b
jmp 0x107677
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x1074be
jmp 0x107689
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x10743d
movq 0x120(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x1136d0
addq $0x128, %rsp            # imm = 0x128
retq
nopl (%rax)