// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/png/pngwrite.c
// Function: png_set_filter_heuristics
void PNGAPI
png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
   int num_weights, png_doublep filter_weights,
   png_doublep filter_costs)
{
   int i;

   png_debug(1, "in png_set_filter_heuristics");

   if (png_ptr == NULL)
      return;
   if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
   {
      png_warning(png_ptr, "Unknown filter heuristic method");
      return;
   }

   if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
   {
      heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
   }

   if (num_weights < 0 || filter_weights == NULL ||
      heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
   {
      num_weights = 0;
   }

   png_ptr->num_prev_filters = (png_byte)num_weights;
   png_ptr->heuristic_method = (png_byte)heuristic_method;

   if (num_weights > 0)
   {
      if (png_ptr->prev_filters == NULL)
      {
         png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
            (png_uint_32)(png_sizeof(png_byte) * num_weights));

         /* To make sure that the weighting starts out fairly */
         for (i = 0; i < num_weights; i++)
         {
            png_ptr->prev_filters[i] = 255;
         }
      }

      if (png_ptr->filter_weights == NULL)
      {
         png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
            (png_uint_32)(png_sizeof(png_uint_16) * num_weights));

         png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
            (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
         for (i = 0; i < num_weights; i++)
         {
            png_ptr->inv_filter_weights[i] =
            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
         }
      }

      for (i = 0; i < num_weights; i++)
      {
         if (filter_weights[i] < 0.0)
         {
            png_ptr->inv_filter_weights[i] =
            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
         }
         else
         {
            png_ptr->inv_filter_weights[i] =
               (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
            png_ptr->filter_weights[i] =
               (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
         }
      }
   }

   /* If, in the future, there are other filter methods, this would
    * need to be based on png_ptr->filter.
    */
   if (png_ptr->filter_costs == NULL)
   {
      png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
         (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));

      png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
         (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));

      for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
      {
         png_ptr->inv_filter_costs[i] =
         png_ptr->filter_costs[i] = PNG_COST_FACTOR;
      }
   }

   /* Here is where we set the relative costs of the different filters.  We
    * should take the desired compression level into account when setting
    * the costs, so that Paeth, for instance, has a high relative cost at low
    * compression levels, while it has a lower relative cost at higher
    * compression settings.  The filter types are in order of increasing
    * relative cost, so it would be possible to do this with an algorithm.
    */
   for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
   {
      if (filter_costs == NULL || filter_costs[i] < 0.0)
      {
         png_ptr->inv_filter_costs[i] =
         png_ptr->filter_costs[i] = PNG_COST_FACTOR;
      }
      else if (filter_costs[i] >= 1.0)
      {
         png_ptr->inv_filter_costs[i] =
            (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
         png_ptr->filter_costs[i] =
            (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
      }
   }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
testq %rdi, %rdi
je 0x8cd3d
movq %rdi, %r14
cmpl $0x3, %esi
jl 0x8cb0b
leaq 0x2015d(%rip), %rsi     # 0xacc57
movq %r14, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x92e6e
movq %r8, %rbx
movq %rcx, %r15
xorl %eax, %eax
testq %rcx, %rcx
movl %edx, %r13d
cmovel %eax, %r13d
testl %edx, %edx
cmovsl %eax, %r13d
cmpl $0x2, %esi
movl $0x1, %ecx
cmovael %esi, %ecx
cmovbl %eax, %r13d
movb %r13b, 0x3a1(%r14)
movb %cl, 0x3a0(%r14)
testl %r13d, %r13d
je 0x8cc5d
cmpq $0x0, 0x3a8(%r14)
jne 0x8cb7d
movl %r13d, %r12d
movq %r14, %rdi
movq %r12, %rsi
callq 0x92cb5
movq %rax, 0x3a8(%r14)
xorl %eax, %eax
movq 0x3a8(%r14), %rcx
movb $-0x1, (%rcx,%rax)
incq %rax
cmpq %rax, %r12
jne 0x8cb6a
cmpq $0x0, 0x3b0(%r14)
jne 0x8cbda
leal (,%r13,2), %r12d
movq %r14, %rdi
movq %r12, %rsi
callq 0x92cb5
movq %rax, 0x3b0(%r14)
movq %r14, %rdi
movq %r12, %rsi
callq 0x92cb5
movq %rax, 0x3b8(%r14)
movq 0x3b0(%r14), %rcx
cmpl $0x1, %r13d
movl %r13d, %edx
adcl $0x0, %edx
xorl %esi, %esi
movw $0x100, %di             # imm = 0x100
movw %di, (%rcx,%rsi,2)
movw %di, (%rax,%rsi,2)
incq %rsi
cmpq %rsi, %rdx
jne 0x8cbca
leaq 0x3b0(%r14), %rax
leaq 0x3b8(%r14), %rcx
cmpl $0x1, %r13d
adcl $0x0, %r13d
xorl %edx, %edx
xorpd %xmm0, %xmm0
movsd 0x1feca(%rip), %xmm1    # 0xacac8
movsd 0x1542a(%rip), %xmm2    # 0xa2030
movsd (%r15,%rdx,8), %xmm3
ucomisd %xmm3, %xmm0
jbe 0x8cc24
movq (%rax), %rsi
movw $0x100, (%rsi,%rdx,2)   # imm = 0x100
movw $0x100, %si             # imm = 0x100
movq %rcx, %rdi
jmp 0x8cc4e
movapd %xmm1, %xmm4
divsd %xmm3, %xmm4
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %esi
movq 0x3b8(%r14), %rdi
movw %si, (%rdi,%rdx,2)
addsd %xmm2, %xmm4
cvttsd2si %xmm4, %esi
movq %rax, %rdi
movq (%rdi), %rdi
movw %si, (%rdi,%rdx,2)
incq %rdx
cmpq %rdx, %r13
jne 0x8cc06
cmpq $0x0, 0x3c0(%r14)
jne 0x8ccad
movl $0xa, %esi
movq %r14, %rdi
callq 0x92cb5
movq %rax, 0x3c0(%r14)
movl $0xa, %esi
movq %r14, %rdi
callq 0x92cb5
movq %rax, 0x3c8(%r14)
movq 0x3c0(%r14), %rcx
xorl %edx, %edx
movw $0x8, %si
movw %si, (%rcx,%rdx,2)
movw %si, (%rax,%rdx,2)
incq %rdx
cmpq $0x5, %rdx
jne 0x8cc9c
leaq 0x3c0(%r14), %rax
leaq 0x3c8(%r14), %rcx
xorl %edx, %edx
xorpd %xmm0, %xmm0
movsd 0x1536f(%rip), %xmm1    # 0xa2038
movsd 0x15527(%rip), %xmm2    # 0xa21f8
movsd 0x15357(%rip), %xmm3    # 0xa2030
testq %rbx, %rbx
je 0x8cce9
movsd (%rbx,%rdx,8), %xmm4
ucomisd %xmm4, %xmm0
jbe 0x8cd0b
movq (%rax), %rsi
movw $0x8, (%rsi,%rdx,2)
movw $0x8, %si
movq %rcx, %rdi
movq (%rdi), %rdi
movw %si, (%rdi,%rdx,2)
incq %rdx
cmpq $0x5, %rdx
jne 0x8ccd9
jmp 0x8cd3d
ucomisd %xmm1, %xmm4
jb 0x8cd00
movapd %xmm2, %xmm5
divsd %xmm4, %xmm5
addsd %xmm3, %xmm5
cvttsd2si %xmm5, %esi
movq 0x3c8(%r14), %rdi
movw %si, (%rdi,%rdx,2)
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
cvttsd2si %xmm4, %esi
movq %rax, %rdi
jmp 0x8ccf9
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq