// File: /workspace/github_c_makefile_star2_O0/uobikiemukot[P]yaft/./ctrlseq/csi.h
// Function: rgb2index
uint8_t rgb2index(uint8_t r, uint8_t g, uint8_t b)
{
	/* SGR: Set Graphic Rendition (special case)
	 * 	special color selection (from 256color index or as a 24bit color value)
	 *
	 *	ESC [ 38 ; 5 ; Ps m
	 *	ESC [ 48 ; 5 ; Ps m
	 *		select foreground/background color from 256 color index
	 *
	 *	ESC [ 38 ; 2 ; r ; g ; b m
	 *	ESC [ 48 ; 2 ; r ; g ; b m
	 *		select foreground/background color as a 24bit color value
	 *
	 * 	according to ITU T.416 (https://www.itu.int/rec/T-REC-T.416/en)
	 * 	this format is valid (but most of terminals don't support)
	 *	ESC [ 38 : 5 : Ps m
	 *	ESC [ 48 : 5 : Ps m
	 *
	 *	ESC [ 48 : 2 : r : g : b m
	 *	ESC [ 38 : 2 : r : g : b m
	 */
	int padding;
	uint8_t index;

	/* XXX: this calculation fails if color palette modified by OSC 4 */
	/* 256 color palette is defined in color.h */

	if (r == g && g == b) {
		/*
			search from grayscale
				index: 232 - 255
				value: 0x080808 - 0xEEEEEE
				inc  : 0x0A
		*/
		padding = (r - 0x08) / 0x0A;

		if (padding >= 24)
			/* index 231 (0xFFFFFF) is the last color of 6x6x6 cube */
			index = 231;
		else if (padding <= 0)
			index = 232;
		else
			index = 232 + padding;

	} else {
		/*
			search from 6x6x6 color cube
				index: 16 - 231
				each rgb takes 6 values: {0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF}
		*/
		const uint8_t values[] = {0x0, 0x5F, 0x87, 0xAF, 0xD7, 0xFF};
		const uint8_t rgb[] = {r, g, b};
		uint8_t closest_index[3];
		int small, big;

		for (int c = 0; c < 3; c++) {
			for (int i = 0; i < 5; i++) {
				if (values[i] <= rgb[c] && rgb[c] <= values[i + 1]) {
					small = abs(rgb[c] - values[i]);
					big   = abs(rgb[c] - values[i + 1]);
					if (small < big)
						closest_index[c] = i;
					else
						closest_index[c] = i + 1;
				}
			}
		}
		index = 16 + closest_index[0] * 36 + closest_index[1] * 6 + closest_index[2];
	}

	return index;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movb %dl, %al
movb %sil, %cl
movb %dil, %dl
movb %dl, -0x1(%rbp)
movb %cl, -0x2(%rbp)
movb %al, -0x3(%rbp)
movzbl -0x1(%rbp), %eax
movzbl -0x2(%rbp), %ecx
cmpl %ecx, %eax
jne 0x65a9
movzbl -0x2(%rbp), %eax
movzbl -0x3(%rbp), %ecx
cmpl %ecx, %eax
jne 0x65a9
movzbl -0x1(%rbp), %eax
subl $0x8, %eax
movl $0xa, %ecx
cltd
idivl %ecx
movl %eax, -0x8(%rbp)
cmpl $0x18, -0x8(%rbp)
jl 0x658b
movb $-0x19, -0x9(%rbp)
jmp 0x65a4
cmpl $0x0, -0x8(%rbp)
jg 0x6597
movb $-0x18, -0x9(%rbp)
jmp 0x65a2
movl -0x8(%rbp), %eax
addl $0xe8, %eax
movb %al, -0x9(%rbp)
jmp 0x65a4
jmp 0x66cc
movl 0x4ef5(%rip), %eax      # 0xb4a4
movl %eax, -0xf(%rbp)
movw 0x4eef(%rip), %ax       # 0xb4a8
movw %ax, -0xb(%rbp)
movb -0x1(%rbp), %al
movb %al, -0x12(%rbp)
movb -0x2(%rbp), %al
movb %al, -0x11(%rbp)
movb -0x3(%rbp), %al
movb %al, -0x10(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x3, -0x24(%rbp)
jge 0x66b0
movl $0x0, -0x28(%rbp)
cmpl $0x5, -0x28(%rbp)
jge 0x66a0
movslq -0x28(%rbp), %rax
movzbl -0xf(%rbp,%rax), %eax
movslq -0x24(%rbp), %rcx
movzbl -0x12(%rbp,%rcx), %ecx
cmpl %ecx, %eax
jg 0x6690
movslq -0x24(%rbp), %rax
movzbl -0x12(%rbp,%rax), %eax
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl -0xf(%rbp,%rcx), %ecx
cmpl %ecx, %eax
jg 0x6690
movslq -0x24(%rbp), %rax
movzbl -0x12(%rbp,%rax), %eax
movslq -0x28(%rbp), %rcx
movzbl -0xf(%rbp,%rcx), %ecx
subl %ecx, %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl %eax, -0x1c(%rbp)
movslq -0x24(%rbp), %rax
movzbl -0x12(%rbp,%rax), %eax
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl -0xf(%rbp,%rcx), %ecx
subl %ecx, %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x667e
movl -0x28(%rbp), %eax
movb %al, %cl
movslq -0x24(%rbp), %rax
movb %cl, -0x15(%rbp,%rax)
jmp 0x668e
movl -0x28(%rbp), %eax
addl $0x1, %eax
movb %al, %cl
movslq -0x24(%rbp), %rax
movb %cl, -0x15(%rbp,%rax)
jmp 0x6690
jmp 0x6692
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x65e7
jmp 0x66a2
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x65d6
movzbl -0x15(%rbp), %eax
imull $0x24, %eax, %eax
addl $0x10, %eax
movzbl -0x14(%rbp), %ecx
imull $0x6, %ecx, %ecx
addl %ecx, %eax
movzbl -0x13(%rbp), %ecx
addl %ecx, %eax
movb %al, -0x9(%rbp)
movb -0x9(%rbp), %al
popq %rbp
retq
nopw %cs:(%rax,%rax)