// File: 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
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movq %rdi, -0x258(%rbp)
movq %rdi, %rax
movq %rax, -0x250(%rbp)
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movl $0x7fffffff, -0xb4(%rbp) # imm = 0x7FFFFFFF
movq -0x20(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x3ba0
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x3bd0
movq %rax, -0xd0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3c00
testb $0x1, %al
jne 0x2d1c
jmp 0x31b0
leaq -0xc8(%rbp), %rdi
callq 0x3c30
movq %rax, %rsi
leaq -0xf4(%rbp), %rdi
movl $0x24, %edx
callq 0x2130
movq -0x28(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rdi
callq 0x3ba0
movq %rax, -0x108(%rbp)
movq -0x100(%rbp), %rdi
callq 0x3bd0
movq %rax, -0x110(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x3c00
testb $0x1, %al
jne 0x2d89
jmp 0x319d
leaq -0x108(%rbp), %rdi
callq 0x3c30
movq %rax, %rsi
leaq -0x134(%rbp), %rdi
movl $0x24, %edx
callq 0x2130
movq 0x10(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rdi
callq 0x3ba0
movq %rax, -0x148(%rbp)
movq -0x140(%rbp), %rdi
callq 0x3bd0
movq %rax, -0x150(%rbp)
leaq -0x148(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x3c00
testb $0x1, %al
jne 0x2df6
jmp 0x318a
leaq -0x148(%rbp), %rdi
callq 0x3c30
movq %rax, %rsi
leaq -0x174(%rbp), %rdi
movl $0x24, %edx
callq 0x2130
movl $0x0, -0x184(%rbp)
cmpl $0x3, -0x184(%rbp)
jge 0x2ee7
movslq -0x184(%rbp), %rax
movl -0xec(%rbp,%rax,4), %eax
movl %eax, -0x264(%rbp)
leaq -0x174(%rbp), %rdi
callq 0x3c60
movl -0x264(%rbp), %ecx
subl -0x160(%rbp), %eax
imull %eax, %ecx
movslq -0x184(%rbp), %rax
movl -0x12c(%rbp,%rax,4), %eax
imull -0x160(%rbp), %eax
addl %eax, %ecx
movslq -0x184(%rbp), %rax
movl %ecx, -0x180(%rbp,%rax,4)
movslq -0x184(%rbp), %rax
imull $0xff, -0x180(%rbp,%rax,4), %ecx
movl %ecx, -0x180(%rbp,%rax,4)
leaq -0x174(%rbp), %rdi
callq 0x3c60
movl %eax, %ecx
shll $0x6, %ecx
movslq -0x184(%rbp), %rax
movq %rax, -0x260(%rbp)
movl -0x180(%rbp,%rax,4), %eax
cltd
idivl %ecx
movl %eax, %ecx
movq -0x260(%rbp), %rax
movl %ecx, -0x180(%rbp,%rax,4)
movl -0x184(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x184(%rbp)
jmp 0x2e20
movl -0xc(%rbp), %eax
movl -0x180(%rbp), %ecx
subl %ecx, %eax
cltq
movq %rax, %rcx
negq %rcx
cmovnsq %rcx, %rax
movl %eax, -0x188(%rbp)
movl -0x10(%rbp), %eax
movl -0x17c(%rbp), %ecx
subl %ecx, %eax
cltq
movq %rax, %rcx
negq %rcx
cmovnsq %rcx, %rax
movl %eax, -0x18c(%rbp)
movl -0x14(%rbp), %eax
movl -0x178(%rbp), %ecx
subl %ecx, %eax
cltq
movq %rax, %rcx
negq %rcx
cmovnsq %rcx, %rax
movl %eax, -0x190(%rbp)
movslq -0x188(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movslq -0x18c(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movslq -0x190(%rbp), %rax
movq %rax, -0x1c8(%rbp)
leaq -0x1d8(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movq $0x3, -0x1b8(%rbp)
leaq -0x1d9(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0x3c80
movq -0x270(%rbp), %rcx
movq -0x1c0(%rbp), %rsi
movq -0x1b8(%rbp), %rdx
leaq -0x1b0(%rbp), %rdi
callq 0x3ca0
jmp 0x2fb7
leaq -0x1b0(%rbp), %rdi
callq 0x2c10
movq %rax, -0x278(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x3d50
leaq -0x1d9(%rbp), %rdi
callq 0x3d70
movq -0x278(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x198(%rbp), %rax
movslq -0xb4(%rbp), %rcx
cmpq %rcx, %rax
jge 0x3177
movl -0x198(%rbp), %eax
movl %eax, -0xb4(%rbp)
leaq -0xb0(%rbp), %rax
movq %rax, -0x288(%rbp)
movl -0x114(%rbp), %eax
movl %eax, -0x244(%rbp)
movl -0x118(%rbp), %eax
addl $0x1e, %eax
movl %eax, -0x240(%rbp)
movl -0xd8(%rbp), %eax
addl $0x28, %eax
movl %eax, -0x23c(%rbp)
leaq -0x244(%rbp), %rax
movq %rax, -0x238(%rbp)
movq $0x3, -0x230(%rbp)
leaq -0x245(%rbp), %rdi
movq %rdi, -0x280(%rbp)
callq 0x3d90
movq -0x280(%rbp), %rcx
movq -0x238(%rbp), %rsi
movq -0x230(%rbp), %rdx
leaq -0x228(%rbp), %rdi
callq 0x3db0
jmp 0x309a
leaq -0x210(%rbp), %rdi
leaq -0x228(%rbp), %rsi
movl $0x6d, %edx
callq 0x2650
jmp 0x30b4
leaq -0x210(%rbp), %rdi
callq 0x2090
movq -0x288(%rbp), %rdi
movq %rax, %rcx
movl -0x154(%rbp), %r8d
addl -0x158(%rbp), %r8d
movl $0x80, %esi
leaq 0x2ff8(%rip), %rdx      # 0x60dc
movb $0x0, %al
callq 0x22a0
leaq -0x210(%rbp), %rdi
callq 0x2328
leaq -0x228(%rbp), %rdi
callq 0x3e60
leaq -0x245(%rbp), %rdi
callq 0x3e80
jmp 0x3177
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1e8(%rbp)
movl %eax, -0x1ec(%rbp)
leaq -0x1d9(%rbp), %rdi
callq 0x3d70
jmp 0x3219
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1e8(%rbp)
movl %eax, -0x1ec(%rbp)
jmp 0x3166
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1e8(%rbp)
movl %eax, -0x1ec(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x3e60
leaq -0x245(%rbp), %rdi
callq 0x3e80
jmp 0x3219
jmp 0x3179
leaq -0x148(%rbp), %rdi
callq 0x3ea0
jmp 0x2dda
jmp 0x318c
leaq -0x108(%rbp), %rdi
callq 0x3ea0
jmp 0x2d6d
jmp 0x319f
leaq -0xc8(%rbp), %rdi
callq 0x3ea0
jmp 0x2d00
leaq -0x246(%rbp), %rdi
movq %rdi, -0x290(%rbp)
callq 0x22e0
movq -0x258(%rbp), %rdi
movq -0x290(%rbp), %rdx
leaq -0xb0(%rbp), %rsi
callq 0x3920
jmp 0x31df
leaq -0x246(%rbp), %rdi
callq 0x21e0
movq -0x250(%rbp), %rax
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1e8(%rbp)
movl %eax, -0x1ec(%rbp)
leaq -0x246(%rbp), %rdi
callq 0x21e0
movq -0x1e8(%rbp), %rdi
callq 0x22d0
nopw %cs:(%rax,%rax)