// File: /workspace/github_c_makefile_star2_O2/kilobyte[P]colormatch/rate.c
// Function: main
int main(int argc, char **argv)
{
    if (argc!=2)
        fprintf(stderr, "Usage: rate file.rgb\n"), exit(1);

    int fd = open(argv[1], O_RDONLY);
    if (read(fd, nt, sizeof(nt))!=sizeof(nt))
        perror("Can't read table file"), exit(1);
    close(fd);

    int maxe=0;
    long long ss=0;
    int worst=0;

    for (int i = 0; i < 16777216; ++i)
    {
        int d = color_diff(i, col256_to_rgb(nt[i]));
        if (d > maxe)
            maxe = d, worst = i;
        ss += ((long long)d) * d / 16777216;
    }

    printf("%-15s maxd=%#-7.6g (\e[38;2;%d;%d;%dm#%06X\e[0m vs \e[38;5;%dm%3d\e[0m:\e[38;2;%d;%d;%dm#%06X\e[0m) Σd²=%#-8.6g\n",
        argv[1],
        maxe/16777216.0,
        (worst>>16)&0xff, (worst>>8)&0xff, worst&0xff, worst,
        nt[worst], nt[worst],
        (col256_to_rgb(nt[worst])>>16)&0xff, (col256_to_rgb(nt[worst])>>8)&0xff, col256_to_rgb(nt[worst])&0xff,
        col256_to_rgb(nt[worst]),
        ss/16777216.0);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
cmpl $0x2, %edi
jne 0x1396
movq %rsi, %r15
movq 0x8(%rsi), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
xorl %eax, %eax
callq 0x10a0
movl %eax, %ebp
leaq 0x2e72(%rip), %r14      # 0x40b0
movl $0x1000000, %edx        # imm = 0x1000000
movl %eax, %edi
movq %r14, %rsi
callq 0x1080
cmpq $0x1000000, %rax        # imm = 0x1000000
jne 0x13b4
movl %ebp, %edi
callq 0x1060
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpq $0x1000000, %r12        # imm = 0x1000000
je 0x12a2
movzbl (%r12,%r14), %edi
callq 0x1c80
movl %r12d, %edi
movl %eax, %esi
callq 0x1c38
cmpl %r13d, %eax
cmovgl %r12d, %ebx
cmovgl %eax, %r13d
cltq
imulq %rax, %rax
shrq $0x18, %rax
addq %rax, %rbp
incq %r12
jmp 0x1268
movq 0x8(%r15), %rax
movq %rax, 0x28(%rsp)
cvtsi2sd %r13d, %xmm0
mulsd 0xd50(%rip), %xmm0      # 0x2008
movsd %xmm0, 0x20(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movl $0xff, %ecx
andl %ecx, %eax
movl %eax, 0xc(%rsp)
movl %ebx, %r12d
shrl $0x8, %r12d
andl %ecx, %r12d
movslq %ebx, %r15
andl %ecx, %ebx
movl $0xff, %r13d
movzbl (%r15,%r14), %edi
movq %rdi, 0x30(%rsp)
callq 0x1c80
shrl $0x10, %eax
andl %r13d, %eax
movq %rax, 0x18(%rsp)
movzbl (%r15,%r14), %edi
callq 0x1c80
movl %eax, %r13d
shrl $0x8, %r13d
movl $0xff, %eax
andl %eax, %r13d
movzbl (%r15,%r14), %edi
callq 0x1c80
movl $0xff, %ecx
andl %ecx, %eax
movq %rax, 0x10(%rsp)
movzbl (%r15,%r14), %edi
callq 0x1c80
movl %eax, %r10d
cvtsi2sd %rbp, %xmm1
mulsd 0xcc2(%rip), %xmm1      # 0x2008
leaq 0xcef(%rip), %rdi       # 0x203c
movq 0x28(%rsp), %rsi
movsd 0x20(%rsp), %xmm0
movl 0xc(%rsp), %edx
movl %r12d, %ecx
movl %ebx, %r8d
movl %r15d, %r9d
movb $0x2, %al
pushq %r10
pushq 0x18(%rsp)
pushq %r13
pushq 0x30(%rsp)
movq 0x50(%rsp), %r10
pushq %r10
pushq %r10
callq 0x1050
addq $0x30, %rsp
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c5b(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc69(%rip), %rdi       # 0x2010
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
callq 0x10e0
jmp 0x13c0
leaq 0xc6b(%rip), %rdi       # 0x2026
callq 0x10b0
pushq $0x1
popq %rdi
callq 0x10d0