// File: /workspace/github_c_makefile_star2_O1/uobikiemukot[P]yaft/./fb/common.h
// Function: init_indexcolor
bool init_indexcolor(int fd, struct fb_info_t *info, cmap_t **cmap, cmap_t **cmap_orig)
{
	int colors, max_length;

	if (info->visual == YAFT_FB_VISUAL_DIRECTCOLOR) {
		switch(info->bits_per_pixel) {
		case 15: /* XXX: 15 bpp is not tested */
		case 16:
		case 24: /* XXX: 24 bpp is not tested */
		case 32:
			break;
		default:
			logging(ERROR, "directcolor %d bpp not supported\n", info->bits_per_pixel);
			return false;
		}
		logging(DEBUG, "red.length:%d gree.length:%d blue.length:%d\n",
			info->red.length, info->green.length, info->blue.length);

		max_length = (info->red.length > info->green.length) ? info->red.length: info->green.length;
		max_length = (max_length > info->blue.length) ? max_length: info->blue.length;
	} else { /* YAFT_FB_VISUAL_PSEUDOCOLOR */
		switch(info->bits_per_pixel) {
		case 8:
			break;
		default:
			logging(ERROR, "pseudocolor %d bpp not supported\n", info->bits_per_pixel);
			return false;
		}

		/* in pseudo color, we use fixed palette (red/green: 3bit, blue: 2bit).
			this palette is not compatible with xterm 256 colors,
			but we can convert 24bit color into palette number easily. */
		info->red.length   = 3;
		info->green.length = 3;
		info->blue.length  = 2;

		info->red.offset   = 5;
		info->green.offset = 2;
		info->blue.offset  = 0;

		/* XXX: not 3 but 8, each color has 256 colors palette */
		max_length = 8;
	}

	colors = 1 << max_length;
	logging(DEBUG, "colors:%d max_length:%d\n", colors, max_length);

	*cmap      = cmap_create(colors);
	*cmap_orig = cmap_create(colors);

	if (!(*cmap) || !(*cmap_orig))
		goto cmap_init_err;

	if (!cmap_save(fd, *cmap_orig)) {
		logging(WARN, "couldn't save original cmap\n");
		cmap_die(*cmap_orig);
		*cmap_orig = NULL;
	}

	if (!cmap_init(fd, info, *cmap, colors, max_length))
		goto cmap_init_err;

	return true;

cmap_init_err:
	cmap_die(*cmap);
	cmap_die(*cmap_orig);
	return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movl %edi, %ebp
movl 0x30(%rsi), %edx
cmpl $0x1, 0x38(%rsi)
jne 0x3430
cmpl $0x20, %edx
ja 0x34ec
movl %edx, %eax
movabsq $0x101018000, %rcx      # imm = 0x101018000
btq %rax, %rcx
jae 0x34ec
movl (%r15), %eax
movl 0x8(%r15), %r12d
cmpl %r12d, %eax
cmovgl %eax, %r12d
movl 0x10(%r15), %eax
cmpl %eax, %r12d
cmovlel %eax, %r12d
jmp 0x3458
cmpl $0x8, %edx
jne 0x34e3
movabsq $0x500000003, %rax      # imm = 0x500000003
movq %rax, (%r15)
movaps 0x4bc3(%rip), %xmm0     # 0x8010
movups %xmm0, 0x8(%r15)
movl $0x8, %r12d
movl $0x1, %r13d
movl %r12d, %ecx
shll %cl, %r13d
movl %r13d, %edi
callq 0x3172
movq %rax, (%r14)
movl %r13d, %edi
callq 0x3172
movq %rax, (%rbx)
testq %rax, %rax
je 0x34cf
cmpq $0x0, (%r14)
je 0x34cf
movl %ebp, %edi
movq %rax, %rsi
callq 0x321f
testb %al, %al
jne 0x34b5
leaq 0x4f66d(%rip), %rsi     # 0x52b07
movl $0x1, %edi
xorl %eax, %eax
callq 0x245c
movq (%rbx), %rdi
callq 0x3144
movq $0x0, (%rbx)
movq (%r14), %rdx
movl %ebp, %edi
movq %r15, %rsi
movl %r13d, %ecx
movl %r12d, %r8d
callq 0x324f
movb $0x1, %bpl
testb %al, %al
jne 0x3501
movq (%r14), %rdi
callq 0x3144
movq (%rbx), %rdi
callq 0x3144
xorl %ebp, %ebp
jmp 0x3501
leaq 0x4f5fb(%rip), %rsi     # 0x52ae5
jmp 0x34f3
leaq 0x4f5d0(%rip), %rsi     # 0x52ac3
xorl %ebp, %ebp
movl $0x2, %edi
xorl %eax, %eax
callq 0x245c
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq