// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/jfbuild/src/rg_etc1.cpp
// Function: rg_etc1::etc1_optimizer::evaluate_solution(rg_etc1::etc1_solution_coordinates const&, rg_etc1::etc1_optimizer::potential_solution&, rg_etc1::etc1_optimizer::potential_solution*)
bool etc1_optimizer::evaluate_solution(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution)
   {
      trial_solution.m_valid = false;

      if (m_pParams->m_constrain_against_base_color5)
      {
         const int dr = coords.m_unscaled_color.r - m_pParams->m_base_color5.r;
         const int dg = coords.m_unscaled_color.g - m_pParams->m_base_color5.g;
         const int db = coords.m_unscaled_color.b - m_pParams->m_base_color5.b;

         if ((rg_etc1::minimum(dr, dg, db) < cETC1ColorDeltaMin) || (rg_etc1::maximum(dr, dg, db) > cETC1ColorDeltaMax))
            return false;
      }

      const color_quad_u8 base_color(coords.get_scaled_color());
      
      const uint n = 8;
            
      trial_solution.m_error = cUINT64_MAX;
            
      for (uint inten_table = 0; inten_table < cETC1IntenModifierValues; inten_table++)
      {
         const int* pInten_table = g_etc1_inten_tables[inten_table];

         color_quad_u8 block_colors[4];
         for (uint s = 0; s < 4; s++)
         {
            const int yd = pInten_table[s];
            block_colors[s].set(base_color.r + yd, base_color.g + yd, base_color.b + yd, 0);
         }
         
         uint64 total_error = 0;
         
         const color_quad_u8* pSrc_pixels = m_pParams->m_pSrc_pixels;
         for (uint c = 0; c < n; c++)
         {
            const color_quad_u8& src_pixel = *pSrc_pixels++;
            
            uint best_selector_index = 0;
            uint best_error = rg_etc1::square(src_pixel.r - block_colors[0].r) + rg_etc1::square(src_pixel.g - block_colors[0].g) + rg_etc1::square(src_pixel.b - block_colors[0].b);

            uint trial_error = rg_etc1::square(src_pixel.r - block_colors[1].r) + rg_etc1::square(src_pixel.g - block_colors[1].g) + rg_etc1::square(src_pixel.b - block_colors[1].b);
            if (trial_error < best_error)
            {
               best_error = trial_error;
               best_selector_index = 1;
            }

            trial_error = rg_etc1::square(src_pixel.r - block_colors[2].r) + rg_etc1::square(src_pixel.g - block_colors[2].g) + rg_etc1::square(src_pixel.b - block_colors[2].b);
            if (trial_error < best_error)
            {
               best_error = trial_error;
               best_selector_index = 2;
            }

            trial_error = rg_etc1::square(src_pixel.r - block_colors[3].r) + rg_etc1::square(src_pixel.g - block_colors[3].g) + rg_etc1::square(src_pixel.b - block_colors[3].b);
            if (trial_error < best_error)
            {
               best_error = trial_error;
               best_selector_index = 3;
            }

            m_temp_selectors[c] = static_cast<uint8>(best_selector_index);

            total_error += best_error;
            if (total_error >= trial_solution.m_error)
               break;
         }
         
         if (total_error < trial_solution.m_error)
         {
            trial_solution.m_error = total_error;
            trial_solution.m_coords.m_inten_table = inten_table;
            memcpy(trial_solution.m_selectors, m_temp_selectors, 8);
            trial_solution.m_valid = true;
         }
      }
      trial_solution.m_coords.m_unscaled_color = coords.m_unscaled_color;
      trial_solution.m_coords.m_color4 = m_pParams->m_use_color4;

      bool success = false;
      if (pBest_solution)
      {
         if (trial_solution.m_error < pBest_solution->m_error)
         {
            *pBest_solution = trial_solution;
            success = true;
         }
      }

      return success;
   }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x20(%rbp), %rcx
movb $0x0, 0x20(%rcx)
movq (%rax), %rax
testb $0x1, 0x30(%rax)
je 0x9f04a
movq -0xa0(%rbp), %rcx
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
movq (%rcx), %rdx
movzbl 0x2c(%rdx), %edx
subl %edx, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
movq (%rcx), %rdx
movzbl 0x2d(%rdx), %edx
subl %edx, %eax
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x2(%rax), %eax
movq (%rcx), %rcx
movzbl 0x2e(%rcx), %ecx
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x2c(%rbp), %edi
movl -0x30(%rbp), %esi
movl -0x34(%rbp), %edx
callq 0xa2c60
cmpl $-0x4, %eax
jl 0x9f03f
movl -0x2c(%rbp), %edi
movl -0x30(%rbp), %esi
movl -0x34(%rbp), %edx
callq 0xa2c90
cmpl $0x3, %eax
jle 0x9f048
movb $0x0, -0x1(%rbp)
jmp 0x9f47d
jmp 0x9f04a
movq -0x18(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0xa2070
movl $0x8, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movq $-0x1, 0x18(%rax)
movl $0x0, -0x40(%rbp)
cmpl $0x8, -0x40(%rbp)
jae 0x9f418
movl -0x40(%rbp), %eax
movl %eax, %ecx
leaq 0x19bb9(%rip), %rax     # 0xb8c40
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
leaq -0x60(%rbp), %rax
movq %rax, %rcx
addq $0x10, %rcx
movq %rcx, -0xb0(%rbp)
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0xa1c30
movq -0xb8(%rbp), %rax
movq -0xb0(%rbp), %rcx
addq $0x4, %rax
cmpq %rcx, %rax
movq %rax, -0xa8(%rbp)
jne 0x9f0ab
movl $0x0, -0x64(%rbp)
cmpl $0x4, -0x64(%rbp)
jae 0x9f12c
movq -0x48(%rbp), %rax
movl -0x64(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x68(%rbp)
movl -0x64(%rbp), %eax
leaq -0x60(%rbp), %rdi
shlq $0x2, %rax
addq %rax, %rdi
movzbl -0x38(%rbp), %esi
addl -0x68(%rbp), %esi
movzbl -0x37(%rbp), %edx
addl -0x68(%rbp), %edx
movzbl -0x36(%rbp), %ecx
addl -0x68(%rbp), %ecx
xorl %r8d, %r8d
callq 0xa1c40
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x9f0e3
movq -0xa0(%rbp), %rax
movq $0x0, -0x70(%rbp)
movq (%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
cmpl $0x8, -0x7c(%rbp)
jae 0x9f3c6
movq -0x78(%rbp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, -0x78(%rbp)
movq %rax, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movq -0x88(%rbp), %rax
movzbl (%rax), %edi
movzbl -0x60(%rbp), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, -0xc8(%rbp)
movq -0x88(%rbp), %rax
movzbl 0x1(%rax), %edi
movzbl -0x5f(%rbp), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, %ecx
movl -0xc8(%rbp), %eax
addl %ecx, %eax
movl %eax, -0xc4(%rbp)
movq -0x88(%rbp), %rax
movzbl 0x2(%rax), %edi
movzbl -0x5e(%rbp), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, %ecx
movl -0xc4(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x90(%rbp)
movq -0x88(%rbp), %rax
movzbl (%rax), %edi
movzbl -0x5c(%rbp), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, -0xc0(%rbp)
movq -0x88(%rbp), %rax
movzbl 0x1(%rax), %edi
movzbl -0x5b(%rbp), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, %ecx
movl -0xc0(%rbp), %eax
addl %ecx, %eax
movl %eax, -0xbc(%rbp)
movq -0x88(%rbp), %rax
movzbl 0x2(%rax), %edi
movzbl -0x5a(%rbp), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, %ecx
movl -0xbc(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x90(%rbp), %eax
jae 0x9f269
movl -0x94(%rbp), %eax
movl %eax, -0x90(%rbp)
movl $0x1, -0x8c(%rbp)
movq -0x88(%rbp), %rax
movzbl (%rax), %edi
movzbl -0x58(%rbp), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, -0xd0(%rbp)
movq -0x88(%rbp), %rax
movzbl 0x1(%rax), %edi
movzbl -0x57(%rbp), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, %ecx
movl -0xd0(%rbp), %eax
addl %ecx, %eax
movl %eax, -0xcc(%rbp)
movq -0x88(%rbp), %rax
movzbl 0x2(%rax), %edi
movzbl -0x56(%rbp), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, %ecx
movl -0xcc(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x90(%rbp), %eax
jae 0x9f2f4
movl -0x94(%rbp), %eax
movl %eax, -0x90(%rbp)
movl $0x2, -0x8c(%rbp)
movq -0x88(%rbp), %rax
movzbl (%rax), %edi
movzbl -0x54(%rbp), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, -0xd8(%rbp)
movq -0x88(%rbp), %rax
movzbl 0x1(%rax), %edi
movzbl -0x53(%rbp), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, %ecx
movl -0xd8(%rbp), %eax
addl %ecx, %eax
movl %eax, -0xd4(%rbp)
movq -0x88(%rbp), %rax
movzbl 0x2(%rax), %edi
movzbl -0x52(%rbp), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, %ecx
movl -0xd4(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x90(%rbp), %eax
jae 0x9f37f
movl -0x94(%rbp), %eax
movl %eax, -0x90(%rbp)
movl $0x3, -0x8c(%rbp)
movq -0xa0(%rbp), %rax
movl -0x8c(%rbp), %ecx
movb %cl, %dl
movl -0x7c(%rbp), %ecx
movb %dl, 0xf0(%rax,%rcx)
movl -0x90(%rbp), %eax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jb 0x9f3b6
jmp 0x9f3c6
jmp 0x9f3b8
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x9f14d
movq -0x70(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x9f408
movq -0xa0(%rbp), %rcx
movq -0x70(%rbp), %rdx
movq -0x20(%rbp), %rax
movq %rdx, 0x18(%rax)
movl -0x40(%rbp), %edx
movq -0x20(%rbp), %rax
movl %edx, 0x4(%rax)
movq -0x20(%rbp), %rax
movq 0xf0(%rcx), %rcx
movq %rcx, 0xc(%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x20(%rax)
jmp 0x9f40a
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x9f071
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdi
callq 0xa1ff0
movq -0xa0(%rbp), %rax
movq (%rax), %rax
movb 0x18(%rax), %cl
movq -0x20(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x8(%rax)
movb $0x0, -0x95(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x9f472
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x9f470
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdi
callq 0xa2cd0
movb $0x1, -0x95(%rbp)
jmp 0x9f472
movb -0x95(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax,%rax)