// File: /workspace/github_cpp_makefile_star2_O2/aempirei[P]Chat-Art/palette.cc
// Function: ansi_match(int, int, int, std::__cxx11::list<geometry, std::allocator<geometry>> const&, std::__cxx11::list<geometry, std::allocator<geometry>> const&, std::__cxx11::list<geometry, std::allocator<geometry>> const&)
std::string ansi_match(int R, int G, int B, const geometry_list& bg, const geometry_list& fg, const geometry_list& sym) {
	char best[128];
	int min = INT_MAX;
	for(auto b : bg) {
		for(auto f : fg) {
			for(auto s : sym) {

				rgb_sum_t y;

				for(int i = 0; i < 3; i++) { 
					y[i] = b.color[i] * (s.n() - s.ratio) + f.color[i] * s.ratio;
					y[i] *= 255;
					y[i] /= 64 * s.n();
				}

				int dr = labs(R - y[0]);
				int dg = labs(G - y[1]);
				int db = labs(B - y[2]);

				long test = ms({dr,dg,db});


				if(test < min) {
					min = test;
					snprintf(best, sizeof(best),
							"%s%c",
							ansi::esc({f.base, f.code + 30, b.code + 40}, 'm').c_str(),
							s.base + s.code);
				}
			}
		}
	}
	return std::string(best);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %r9, 0x28(%rsp)
movl %ecx, 0x18(%rsp)
movq %rdi, 0x30(%rsp)
movd %edx, %xmm0
movd %esi, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movdqa %xmm1, 0xe0(%rsp)
movl $0x7fffffff, 0x8(%rsp)  # imm = 0x7FFFFFFF
leaq 0xf0(%rsp), %r12
movq %r8, 0x38(%rsp)
movq (%r8), %r8
cmpq 0x38(%rsp), %r8
je 0x29bb
movdqu 0x10(%r8), %xmm0
movdqu 0x20(%r8), %xmm1
movdqa %xmm1, 0x90(%rsp)
movq %r8, 0x40(%rsp)
movl 0x30(%r8), %eax
movl %eax, 0xa0(%rsp)
movdqa %xmm0, 0x80(%rsp)
movl 0x9c(%rsp), %eax
addl $0x28, %eax
movl %eax, 0xc(%rsp)
movq 0x28(%rsp), %rax
movq (%rax), %rax
cmpq 0x28(%rsp), %rax
je 0x29b1
movdqu 0x10(%rax), %xmm0
movdqu 0x20(%rax), %xmm1
movdqa %xmm1, 0xc0(%rsp)
movq %rax, 0x48(%rsp)
movl 0x30(%rax), %eax
movl %eax, 0x14(%rsp)
movl %eax, 0xd0(%rsp)
movdqa %xmm0, 0xb0(%rsp)
movl 0xcc(%rsp), %eax
addl $0x1e, %eax
movl %eax, 0x10(%rsp)
movq 0x1d0(%rsp), %rbp
movq (%rbp), %rbp
cmpq 0x1d0(%rsp), %rbp
je 0x29a7
movl 0x14(%rbp), %ecx
movl 0x24(%rbp), %eax
movl 0x2c(%rbp), %r13d
movl 0x30(%rbp), %r14d
imull 0x10(%rbp), %ecx
movl %ecx, %edx
subl %eax, %edx
imull $0xff, %edx, %esi
imull $0xff, %eax, %edi
shll $0x6, %ecx
pushq $0x2
popq %r8
cmpq $0x5, %r8
je 0x28a4
movl 0x80(%rsp,%r8,4), %edx
imull %esi, %edx
movl 0xb0(%rsp,%r8,4), %eax
imull %edi, %eax
addl %edx, %eax
cltd
idivl %ecx
movl %eax, 0x48(%rsp,%r8,4)
incq %r8
jmp 0x2879
movl 0x18(%rsp), %eax
subl 0x58(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movq 0x50(%rsp), %xmm0
movdqa 0xe0(%rsp), %xmm1
psubd %xmm0, %xmm1
movdqa %xmm1, %xmm0
psrad $0x1f, %xmm0
pxor %xmm0, %xmm1
psubd %xmm0, %xmm1
pxor %xmm0, %xmm0
pcmpgtd %xmm1, %xmm0
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movdqa %xmm1, 0x60(%rsp)
movslq %ecx, %rax
movq %rax, 0x70(%rsp)
movq %r12, %rdi
leaq 0x60(%rsp), %r15
movq %r15, %rsi
pushq $0x3
popq %rdx
leaq 0x1c(%rsp), %rcx
callq 0x2d98
movq %r12, %rdi
callq 0x2738
movq %rax, %rbx
movq %r12, %rdi
callq 0x2e14
movslq 0x8(%rsp), %rax
cmpq %rax, %rbx
jge 0x283e
movl 0x14(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x24(%rsp)
movq %r15, %rdi
leaq 0x1c(%rsp), %rsi
pushq $0x3
popq %rdx
leaq 0x7(%rsp), %rcx
callq 0x2dd0
movq %r12, %rdi
movq %r15, %rsi
pushq $0x6d
popq %rdx
callq 0x242c
movq 0xf0(%rsp), %rcx
addl %r13d, %r14d
movl $0x80, %esi
leaq 0x110(%rsp), %rdi
leaq 0x1758(%rip), %rdx      # 0x40dc
movl %r14d, %r8d
xorl %eax, %eax
callq 0x21c0
movq %r12, %rdi
callq 0x2208
movq %r15, %rdi
callq 0x2e3e
movl %ebx, 0x8(%rsp)
jmp 0x283e
movq 0x48(%rsp), %rax
jmp 0x27eb
movq 0x40(%rsp), %r8
jmp 0x279c
leaq 0x110(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x2d5c
movq %rbx, %rax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x2e3e
movq %rbx, %rdi
callq 0x21e0