// File: /workspace/github_cpp_makefile_star2_O2/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 0x7ebc9
movq %rdi, %r14
cmpl $0x3, %esi
jl 0x7ea6f
leaq 0x1ef19(%rip), %rsi     # 0x9d977
movq %r14, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x84a53
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
pushq $0x1
popq %rcx
cmovael %esi, %ecx
cmovbl %eax, %r13d
movb %r13b, 0x3a1(%r14)
movb %cl, 0x3a0(%r14)
testl %r13d, %r13d
je 0x7eb39
cmpq $0x0, 0x3a8(%r14)
movl %r13d, %r12d
je 0x7ec1f
movq 0x3b0(%r14), %rcx
testq %rcx, %rcx
je 0x7ec4c
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd 0x1ed0e(%rip), %xmm1    # 0x9d7e8
movsd 0x1452e(%rip), %xmm2    # 0x93010
cmpq %rax, %r12
je 0x7eb39
movsd (%r15,%rax,8), %xmm3
ucomisd %xmm3, %xmm0
jbe 0x7eb06
movw $0x100, (%rcx,%rax,2)   # imm = 0x100
movq 0x3b8(%r14), %rsi
movw $0x100, %dx             # imm = 0x100
jmp 0x7eb30
movapd %xmm1, %xmm4
divsd %xmm3, %xmm4
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %edx
movq 0x3b8(%r14), %rsi
movw %dx, (%rsi,%rax,2)
addsd %xmm2, %xmm4
cvttsd2si %xmm4, %edx
movq %rcx, %rsi
movw %dx, (%rsi,%rax,2)
incq %rax
jmp 0x7eae2
movq 0x3c0(%r14), %rcx
testq %rcx, %rcx
je 0x7ebd3
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd 0x144b1(%rip), %xmm1    # 0x93008
movsd 0x14699(%rip), %xmm2    # 0x931f8
movsd 0x144a9(%rip), %xmm3    # 0x93010
cmpq $0x5, %rax
je 0x7ebc9
testq %rbx, %rbx
je 0x7eb7d
movsd (%rbx,%rax,8), %xmm4
ucomisd %xmm4, %xmm0
jbe 0x7eb97
movw $0x8, (%rcx,%rax,2)
movq 0x3c8(%r14), %rsi
movw $0x8, %dx
movw %dx, (%rsi,%rax,2)
incq %rax
jmp 0x7eb67
ucomisd %xmm1, %xmm4
jb 0x7eb92
movapd %xmm2, %xmm5
divsd %xmm4, %xmm5
addsd %xmm3, %xmm5
cvttsd2si %xmm5, %edx
movq 0x3c8(%r14), %rsi
movw %dx, (%rsi,%rax,2)
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
cvttsd2si %xmm4, %edx
movq %rcx, %rsi
jmp 0x7eb8e
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
pushq $0xa
popq %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x848bb
movq %rax, 0x3c0(%r14)
movq %r14, %rdi
movq %r15, %rsi
callq 0x848bb
movq %rax, 0x3c8(%r14)
movq 0x3c0(%r14), %rcx
xorl %edx, %edx
movw $0x8, %si
cmpq $0x5, %rdx
je 0x7eb49
movw %si, (%rcx,%rdx,2)
movw %si, (%rax,%rdx,2)
incq %rdx
jmp 0x7ec08
movq %r14, %rdi
movq %r12, %rsi
callq 0x848bb
movq %rax, 0x3a8(%r14)
xorl %eax, %eax
cmpq %rax, %r12
je 0x7eabc
movq 0x3a8(%r14), %rcx
movb $-0x1, (%rcx,%rax)
incq %rax
jmp 0x7ec33
addl %r13d, %r13d
movq %r14, %rdi
movq %r13, %rsi
callq 0x848bb
movq %rax, 0x3b0(%r14)
movq %r14, %rdi
movq %r13, %rsi
callq 0x848bb
movq %rax, 0x3b8(%r14)
movq 0x3b0(%r14), %rcx
xorl %edx, %edx
movw $0x100, %si             # imm = 0x100
cmpq %rdx, %r12
je 0x7eacc
movw %si, (%rcx,%rdx,2)
movw %si, (%rax,%rdx,2)
incq %rdx
jmp 0x7ec80