// File: /workspace/github_cpp_makefile_star2_O3/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 0x8bf7a
movq %rdi, %r14
cmpl $0x3, %esi
jl 0x8bd7b
leaq 0x20f2d(%rip), %rsi     # 0xacc97
movq %r14, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x9214d
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 0x8bea8
cmpq $0x0, 0x3a8(%r14)
movl %r13d, %r12d
jne 0x8bded
movq %r14, %rdi
movq %r12, %rsi
callq 0x91fa8
movq %rax, 0x3a8(%r14)
xorl %eax, %eax
movq 0x3a8(%r14), %rcx
movb $-0x1, (%rcx,%rax)
incq %rax
cmpq %rax, %r12
jne 0x8bdda
movq 0x3b0(%r14), %rcx
testq %rcx, %rcx
jne 0x8be3d
addl %r13d, %r13d
movq %r14, %rdi
movq %r13, %rsi
callq 0x91fa8
movq %rax, 0x3b0(%r14)
movq %r14, %rdi
movq %r13, %rsi
callq 0x91fa8
movq %rax, 0x3b8(%r14)
movq 0x3b0(%r14), %rcx
xorl %edx, %edx
movw $0x100, %si             # imm = 0x100
movw %si, (%rcx,%rdx,2)
movw %si, (%rax,%rdx,2)
incq %rdx
cmpq %rdx, %r12
jne 0x8be2d
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd 0x20cbd(%rip), %xmm1    # 0xacb08
movsd 0x161dd(%rip), %xmm2    # 0xa2030
movsd (%r15,%rax,8), %xmm3
ucomisd %xmm3, %xmm0
jbe 0x8be72
movw $0x100, (%rcx,%rax,2)   # imm = 0x100
movq 0x3b8(%r14), %rsi
movw $0x100, %dx             # imm = 0x100
jmp 0x8be9c
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
cmpq %rax, %r12
jne 0x8be53
movq 0x3c0(%r14), %rcx
testq %rcx, %rcx
jne 0x8befa
movl $0xa, %esi
movq %r14, %rdi
callq 0x91fa8
movq %rax, 0x3c0(%r14)
movl $0xa, %esi
movq %r14, %rdi
callq 0x91fa8
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 0x8bee9
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd 0x16130(%rip), %xmm1    # 0xa2038
movsd 0x162e8(%rip), %xmm2    # 0xa21f8
movsd 0x16118(%rip), %xmm3    # 0xa2030
testq %rbx, %rbx
je 0x8bf28
movsd (%rbx,%rax,8), %xmm4
ucomisd %xmm4, %xmm0
jbe 0x8bf48
movw $0x8, (%rcx,%rax,2)
movq 0x3c8(%r14), %rsi
movw $0x8, %dx
movw %dx, (%rsi,%rax,2)
incq %rax
cmpq $0x5, %rax
jne 0x8bf18
jmp 0x8bf7a
ucomisd %xmm1, %xmm4
jb 0x8bf3d
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 0x8bf39
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq