// File: /workspace/github_c_makefile_star2_O2/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 0x3347
cmpl $0x20, %edx
ja 0x33fc
movl %edx, %eax
movabsq $0x101018000, %rcx      # imm = 0x101018000
btq %rax, %rcx
jae 0x33fc
movl (%r15), %eax
movl 0x8(%r15), %r12d
cmpl %r12d, %eax
cmovgl %eax, %r12d
movl 0x10(%r15), %eax
cmpl %eax, %r12d
cmovlel %eax, %r12d
jmp 0x3367
cmpl $0x8, %edx
jne 0x33f3
movq $0x2, 0x10(%r15)
movaps 0x3cb1(%rip), %xmm0     # 0x7010
movups %xmm0, (%r15)
pushq $0x8
popq %r12
pushq $0x1
popq %r13
movl %r12d, %ecx
shll %cl, %r13d
movl %r13d, %edi
callq 0x30bb
movq %rax, (%r14)
movl %r13d, %edi
callq 0x30bb
movq %rax, (%rbx)
movq (%r14), %rdi
testq %rdi, %rdi
sete %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x33e2
movl %ebp, %edi
movq %rax, %rsi
callq 0x3154
testb %al, %al
jne 0x33c5
leaq 0x4e778(%rip), %rsi     # 0x51b27
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x244c
movq (%rbx), %rdi
callq 0x308d
andq $0x0, (%rbx)
movq (%r14), %rdx
movl %ebp, %edi
movq %r15, %rsi
movl %r13d, %ecx
movl %r12d, %r8d
callq 0x3178
movb $0x1, %bpl
testb %al, %al
jne 0x340f
movq (%r14), %rdi
callq 0x308d
movq (%rbx), %rdi
callq 0x308d
xorl %ebp, %ebp
jmp 0x340f
leaq 0x4e70b(%rip), %rsi     # 0x51b05
jmp 0x3403
leaq 0x4e6e0(%rip), %rsi     # 0x51ae3
xorl %ebp, %ebp
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x244c
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq