// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/jfbuild/src/rg_etc1.cpp
// Function: rg_etc1::pack_etc1_block_solid_color(rg_etc1::etc1_block&, unsigned char const*, rg_etc1::etc1_pack_params&)
static uint64 pack_etc1_block_solid_color(etc1_block& block, const uint8* pColor, etc1_pack_params& pack_params)
   {
      (void)pack_params;    //JonoF
      RG_ETC1_ASSERT(g_etc1_inverse_lookup[0][255]);
            
      static uint s_next_comp[4] = { 1, 2, 0, 1 };
            
      uint best_error = cUINT32_MAX, best_i = 0;
      int best_x = 0, best_packed_c1 = 0, best_packed_c2 = 0;

      // For each possible 8-bit value, there is a precomputed list of diff/inten/selector configurations that allow that 8-bit value to be encoded with no error.
      for (uint i = 0; i < 3; i++)
      {
         const uint c1 = pColor[s_next_comp[i]], c2 = pColor[s_next_comp[i + 1]];

         const int delta_range = 1;
         for (int delta = -delta_range; delta <= delta_range; delta++)
         {
            const int c_plus_delta = rg_etc1::clamp<int>(pColor[i] + delta, 0, 255);

            const uint16* pTable;
            if (!c_plus_delta)
               pTable = g_color8_to_etc_block_config_0_255[0];
            else if (c_plus_delta == 255)
               pTable = g_color8_to_etc_block_config_0_255[1];
            else
               pTable = g_color8_to_etc_block_config_1_to_254[c_plus_delta - 1];

            do
            {
               const uint x = *pTable++;

#ifdef RG_ETC1_BUILD_DEBUG
               const uint diff = x & 1;
               const uint inten = (x >> 1) & 7;
               const uint selector = (x >> 4) & 3;
               const uint p0 = (x >> 8) & 255;
               RG_ETC1_ASSERT(etc1_decode_value(diff, inten, selector, p0) == (uint)c_plus_delta);
#endif

               const uint16* pInverse_table = g_etc1_inverse_lookup[x & 0xFF];
               uint16 p1 = pInverse_table[c1];
               uint16 p2 = pInverse_table[c2];
               const uint trial_error = rg_etc1::square(c_plus_delta - pColor[i]) + rg_etc1::square(p1 >> 8) + rg_etc1::square(p2 >> 8);
               if (trial_error < best_error)
               {
                  best_error = trial_error;
                  best_x = x;
                  best_packed_c1 = p1 & 0xFF;
                  best_packed_c2 = p2 & 0xFF;
                  best_i = i;
                  if (!best_error)
                     goto found_perfect_match;
               }
            } while (*pTable != 0xFFFF);
         }
      }
found_perfect_match:

      const uint diff = best_x & 1;
      const uint inten = (best_x >> 1) & 7;

      block.m_bytes[3] = static_cast<uint8>(((inten | (inten << 3)) << 2) | (diff << 1));
                        
      const uint etc1_selector = g_selector_index_to_etc1[(best_x >> 4) & 3];
      *reinterpret_cast<uint16*>(&block.m_bytes[4]) = (etc1_selector & 2) ? 0xFFFF : 0;
      *reinterpret_cast<uint16*>(&block.m_bytes[6]) = (etc1_selector & 1) ? 0xFFFF : 0;

      const uint best_packed_c0 = (best_x >> 8) & 255;
      if (diff)
      {
         block.m_bytes[best_i] = static_cast<uint8>(best_packed_c0 << 3);
         block.m_bytes[s_next_comp[best_i]] = static_cast<uint8>(best_packed_c1 << 3);
         block.m_bytes[s_next_comp[best_i+1]] = static_cast<uint8>(best_packed_c2 << 3);
      }
      else
      {
         block.m_bytes[best_i] = static_cast<uint8>(best_packed_c0 | (best_packed_c0 << 4));
         block.m_bytes[s_next_comp[best_i]] = static_cast<uint8>(best_packed_c1 | (best_packed_c1 << 4));
         block.m_bytes[s_next_comp[best_i+1]] = static_cast<uint8>(best_packed_c2 | (best_packed_c2 << 4));
      }

      return best_error;
   }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
cmpw $0x0, 0x6173ff(%rip)    # 0x6b838e
je 0xa0f93
jmp 0xa0fb2
leaq 0x17a20(%rip), %rdi     # 0xb89ba
leaq 0x17382(%rip), %rsi     # 0xb8323
movl $0x7a2, %edx            # imm = 0x7A2
leaq 0x17a2b(%rip), %rcx     # 0xb89d8
callq 0x61a0
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jae 0xa11a5
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
movl %ecx, %edx
leaq 0x40c1a(%rip), %rcx     # 0xe1c10
movl (%rcx,%rdx,4), %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movl %ecx, %edx
leaq 0x40bfb(%rip), %rcx     # 0xe1c10
movl (%rcx,%rdx,4), %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movl $0xffffffff, -0x40(%rbp) # imm = 0xFFFFFFFF
cmpl $0x1, -0x40(%rbp)
jg 0xa1195
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
movzbl (%rax,%rcx), %edi
addl -0x40(%rbp), %edi
xorl %esi, %esi
movl $0xff, %edx
callq 0xa1aa0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jne 0xa1067
leaq 0x17cef(%rip), %rax     # 0xb8d50
movq %rax, -0x50(%rbp)
jmp 0xa109e
cmpl $0xff, -0x44(%rbp)
jne 0xa1081
leaq 0x17cd9(%rip), %rax     # 0xb8d50
addq $0x42, %rax
movq %rax, -0x50(%rbp)
jmp 0xa109c
movl -0x44(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x17d4f(%rip), %rax     # 0xb8de0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
jmp 0xa109e
jmp 0xa10a0
movq -0x50(%rbp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x50(%rbp)
movzwl (%rax), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
andl $0xff, %eax
movl %eax, %eax
movl %eax, %ecx
leaq 0x6170c8(%rip), %rax    # 0x6b8190
shlq $0x9, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movl -0x34(%rbp), %ecx
movw (%rax,%rcx,2), %ax
movw %ax, -0x62(%rbp)
movq -0x60(%rbp), %rax
movl -0x38(%rbp), %ecx
movw (%rax,%rcx,2), %ax
movw %ax, -0x64(%rbp)
movl -0x44(%rbp), %edi
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
movzbl (%rax,%rcx), %eax
subl %eax, %edi
callq 0xa2cc0
movl %eax, -0x80(%rbp)
movzwl -0x62(%rbp), %edi
sarl $0x8, %edi
callq 0xa2cc0
movl %eax, %ecx
movl -0x80(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x7c(%rbp)
movzwl -0x64(%rbp), %edi
sarl $0x8, %edi
callq 0xa2cc0
movl %eax, %ecx
movl -0x7c(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0xa1171
movl -0x68(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x24(%rbp)
movzwl -0x62(%rbp), %eax
andl $0xff, %eax
movl %eax, -0x28(%rbp)
movzwl -0x64(%rbp), %eax
andl $0xff, %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0xa116f
jmp 0xa11a7
jmp 0xa1171
jmp 0xa1173
movq -0x50(%rbp), %rax
movzwl (%rax), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0xa10a0
jmp 0xa1187
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xa102d
jmp 0xa1197
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xa0fdc
jmp 0xa11a7
movl -0x24(%rbp), %eax
andl $0x1, %eax
movl %eax, -0x6c(%rbp)
movl -0x24(%rbp), %eax
sarl %eax
andl $0x7, %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
movl -0x70(%rbp), %ecx
shll $0x3, %ecx
orl %ecx, %eax
shll $0x2, %eax
movl -0x6c(%rbp), %ecx
shll %ecx
orl %ecx, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x3(%rax)
movl -0x24(%rbp), %eax
sarl $0x4, %eax
andl $0x3, %eax
movslq %eax, %rcx
leaq 0x17b54(%rip), %rax     # 0xb8d40
movzbl (%rax,%rcx), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %edx
andl $0x2, %edx
xorl %eax, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
cmpl $0x0, %edx
cmovnel %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, 0x4(%rax)
movl -0x74(%rbp), %edx
andl $0x1, %edx
xorl %eax, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
cmpl $0x0, %edx
cmovnel %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, 0x6(%rax)
movl -0x24(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movl %eax, -0x78(%rbp)
cmpl $0x0, -0x6c(%rbp)
je 0xa1298
movl -0x78(%rbp), %eax
shll $0x3, %eax
movb %al, %dl
movq -0x8(%rbp), %rax
movl -0x20(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x28(%rbp), %eax
shll $0x3, %eax
movb %al, %dl
movq -0x8(%rbp), %rax
movl -0x20(%rbp), %ecx
movl %ecx, %esi
leaq 0x409a3(%rip), %rcx     # 0xe1c10
movl (%rcx,%rsi,4), %ecx
movb %dl, (%rax,%rcx)
movl -0x2c(%rbp), %eax
shll $0x3, %eax
movb %al, %dl
movq -0x8(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movl %ecx, %esi
leaq 0x40980(%rip), %rcx     # 0xe1c10
movl (%rcx,%rsi,4), %ecx
movb %dl, (%rax,%rcx)
jmp 0xa12fa
movl -0x78(%rbp), %eax
movl -0x78(%rbp), %ecx
shll $0x4, %ecx
orl %ecx, %eax
movb %al, %dl
movq -0x8(%rbp), %rax
movl -0x20(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x28(%rbp), %eax
movl -0x28(%rbp), %ecx
shll $0x4, %ecx
orl %ecx, %eax
movb %al, %dl
movq -0x8(%rbp), %rax
movl -0x20(%rbp), %ecx
movl %ecx, %esi
leaq 0x40944(%rip), %rcx     # 0xe1c10
movl (%rcx,%rsi,4), %ecx
movb %dl, (%rax,%rcx)
movl -0x2c(%rbp), %eax
movl -0x2c(%rbp), %ecx
shll $0x4, %ecx
orl %ecx, %eax
movb %al, %dl
movq -0x8(%rbp), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movl %ecx, %esi
leaq 0x4091c(%rip), %rcx     # 0xe1c10
movl (%rcx,%rsi,4), %ecx
movb %dl, (%rax,%rcx)
movl -0x1c(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)