// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1a8, %rsp            # imm = 0x1A8
movq %r9, 0x28(%rsp)
movl %ecx, 0x20(%rsp)
movq %rdi, 0x60(%rsp)
movq (%r8), %rcx
movq %r8, 0x68(%rsp)
cmpq %r8, %rcx
je 0x2b23
movd %edx, %xmm0
movd %esi, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movdqa %xmm1, 0x110(%rsp)
movl $0x7fffffff, 0x10(%rsp) # imm = 0x7FFFFFFF
leaq 0x90(%rsp), %r13
leaq 0x40(%rsp), %r14
leaq 0x34(%rsp), %rbp
movl 0x30(%rcx), %eax
movl %eax, 0x100(%rsp)
movdqu 0x10(%rcx), %xmm0
movq %rcx, 0x70(%rsp)
movdqu 0x20(%rcx), %xmm1
movdqa %xmm1, 0xf0(%rsp)
movdqa %xmm0, 0xe0(%rsp)
movq 0x28(%rsp), %rax
movq (%rax), %rcx
cmpq %rax, %rcx
je 0x2b10
movl 0xfc(%rsp), %eax
addl $0x28, %eax
movl %eax, 0x14(%rsp)
movl 0x30(%rcx), %eax
movl %eax, 0xd0(%rsp)
movdqu 0x10(%rcx), %xmm0
movq %rcx, 0x78(%rsp)
movdqu 0x20(%rcx), %xmm1
movdqa %xmm1, 0xc0(%rsp)
movdqa %xmm0, 0xb0(%rsp)
movq 0x1e0(%rsp), %rax
movq (%rax), %r12
cmpq %rax, %r12
je 0x2afd
movl 0xcc(%rsp), %eax
movl 0xd0(%rsp), %ecx
movl %ecx, 0x18(%rsp)
addl $0x1e, %eax
movl %eax, 0x1c(%rsp)
movl 0x14(%r12), %ecx
movl 0x24(%r12), %eax
movl 0x2c(%r12), %edx
movl %edx, 0x24(%rsp)
movl 0x30(%r12), %r15d
imull 0x10(%r12), %ecx
movl %ecx, %edx
subl %eax, %edx
movl %edx, %esi
shll $0x8, %esi
subl %edx, %esi
movl %eax, %edi
shll $0x8, %edi
subl %eax, %edi
shll $0x6, %ecx
movl $0x2, %r8d
movl 0xe0(%rsp,%r8,4), %edx
imull %esi, %edx
movl 0xb0(%rsp,%r8,4), %eax
imull %edi, %eax
addl %edx, %eax
cltd
idivl %ecx
movl %eax, 0x78(%rsp,%r8,4)
incq %r8
cmpq $0x5, %r8
jne 0x294c
movl 0x20(%rsp), %eax
subl 0x88(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movq 0x80(%rsp), %xmm0
movdqa 0x110(%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, 0x40(%rsp)
movslq %ecx, %rax
movq %rax, 0x50(%rsp)
movl $0x3, %edx
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rcx
callq 0x3106
movq 0x90(%rsp), %rdi
cmpq %r13, %rdi
je 0x2a2b
movl %r15d, %r14d
movq %rbp, %r15
movq %rdi, %rax
xorl %ebx, %ebx
movq 0x10(%rax), %rcx
movq (%rax), %rdx
imulq %rcx, %rcx
addq %rcx, %rbx
movq %rdx, %rax
cmpq %r13, %rdx
jne 0x29f3
movq (%rdi), %rbp
movl $0x18, %esi
callq 0x2130
movq %rbp, %rdi
cmpq %r13, %rbp
jne 0x2a09
movq %r15, %rbp
movl %r14d, %r15d
leaq 0x40(%rsp), %r14
jmp 0x2a2d
xorl %ebx, %ebx
movslq 0x10(%rsp), %rax
cmpq %rax, %rbx
jge 0x2aeb
movl 0x18(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl $0x3, %edx
movq %r14, %rdi
movq %rbp, %rsi
leaq 0xf(%rsp), %rcx
callq 0x3154
movq %r13, %rdi
movq %r14, %rsi
movl $0x6d, %edx
callq 0x2474
movq 0x90(%rsp), %rcx
addl 0x24(%rsp), %r15d
movl $0x80, %esi
leaq 0x120(%rsp), %rdi
leaq 0x1640(%rip), %rdx      # 0x40d9
movl %r15d, %r8d
xorl %eax, %eax
callq 0x21b0
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x2ac8
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2130
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x2ae7
movq (%rdi), %r15
movl $0x18, %esi
callq 0x2130
movq %r15, %rdi
cmpq %r14, %r15
jne 0x2ad2
movl %ebx, 0x10(%rsp)
movq (%r12), %r12
cmpq 0x1e0(%rsp), %r12
jne 0x2913
movq 0x78(%rsp), %rcx
movq (%rcx), %rcx
cmpq 0x28(%rsp), %rcx
jne 0x28bb
movq 0x70(%rsp), %rcx
movq (%rcx), %rcx
cmpq 0x68(%rsp), %rcx
jne 0x2871
movq 0x60(%rsp), %r14
leaq 0x10(%r14), %rax
movq %rax, (%r14)
leaq 0x120(%rsp), %rbx
movq %rbx, %rdi
callq 0x2080
leaq (%rsp,%rax), %rdx
addq $0x120, %rdx            # imm = 0x120
movq %r14, %rdi
movq %rbx, %rsi
callq 0x31a2
movq %r14, %rax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x2b8c
movq (%rdi), %r15
movl $0x18, %esi
callq 0x2130
movq %r15, %rdi
cmpq %r14, %r15
jne 0x2b77
movq %rbx, %rdi
callq 0x21e0