// File: /workspace/github_c_makefile_star2_O3/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 0x3457
cmpl $0x20, %edx
ja 0x3515
movl %edx, %eax
movabsq $0x101018000, %rcx      # imm = 0x101018000
btq %rax, %rcx
jae 0x3515
movl (%r15), %eax
movl 0x8(%r15), %r12d
cmpl %r12d, %eax
cmovgl %eax, %r12d
movl 0x10(%r15), %eax
cmpl %eax, %r12d
cmovlel %eax, %r12d
jmp 0x3479
cmpl $0x8, %edx
jne 0x350c
movq $0x2, 0x10(%r15)
movaps 0x4ba1(%rip), %xmm0     # 0x8010
movups %xmm0, (%r15)
movl $0x8, %r12d
movl $0x1, %r13d
movl %r12d, %ecx
shll %cl, %r13d
movl %r13d, %edi
callq 0x3199
movq %rax, (%r14)
movl %r13d, %edi
callq 0x3199
movq %rax, (%rbx)
movq (%r14), %rdi
testq %rdi, %rdi
sete %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x34fb
movl %ebp, %edi
movq %rax, %rsi
callq 0x3246
testb %al, %al
jne 0x34de
leaq 0x4f664(%rip), %rsi     # 0x52b27
movl $0x1, %edi
xorl %eax, %eax
callq 0x245c
movq (%rbx), %rdi
callq 0x316b
movq $0x0, (%rbx)
movq (%r14), %rdx
movl %ebp, %edi
movq %r15, %rsi
movl %r13d, %ecx
movl %r12d, %r8d
callq 0x3276
movb $0x1, %bpl
testb %al, %al
jne 0x352a
movq (%r14), %rdi
callq 0x316b
movq (%rbx), %rdi
callq 0x316b
xorl %ebp, %ebp
jmp 0x352a
leaq 0x4f5f2(%rip), %rsi     # 0x52b05
jmp 0x351c
leaq 0x4f5c7(%rip), %rsi     # 0x52ae3
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