// File: /workspace/github_c_makefile_star2_O3/uobikiemukot[P]yaft/./fb/common.h
// Function: cmap_init
bool cmap_init(int fd, struct fb_info_t *info, cmap_t *cmap, int colors, int length)
{
	uint16_t r, g, b, r_index, g_index, b_index;

	for (int i = 0; i < colors; i++) {
		if (info->visual == YAFT_FB_VISUAL_DIRECTCOLOR) {
			r_index = (i >> (length - info->red.length))   & bit_mask[info->red.length];
			g_index = (i >> (length - info->green.length)) & bit_mask[info->green.length];
			b_index = (i >> (length - info->blue.length))  & bit_mask[info->blue.length];

			/* XXX: maybe only upper order byte is used */
			r = r_index << (CMAP_COLOR_LENGTH - info->red.length);
			g = g_index << (CMAP_COLOR_LENGTH - info->green.length);
			b = b_index << (CMAP_COLOR_LENGTH - info->blue.length);

			*(cmap->red   + r_index) = r;
			*(cmap->green + g_index) = g;
			*(cmap->blue  + b_index) = b;
		} else { /* YAFT_FB_VISUAL_PSEUDOCOLOR */
			r_index = (i >> info->red.offset)   & bit_mask[info->red.length];
			g_index = (i >> info->green.offset) & bit_mask[info->green.length];
			b_index = (i >> info->blue.offset)  & bit_mask[info->blue.length];

			r = r_index << (CMAP_COLOR_LENGTH - info->red.length);
			g = g_index << (CMAP_COLOR_LENGTH - info->green.length);
			b = b_index << (CMAP_COLOR_LENGTH - info->blue.length);

			*(cmap->red   + i) = r;
			*(cmap->green + i) = g;
			*(cmap->blue  + i) = b;
		}
	}

	if (!cmap_update(fd, cmap))
		return false;

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %r8d, -0x4(%rsp)
movl %edi, -0x8(%rsp)
testl %ecx, %ecx
jle 0x33a6
movl 0x38(%rsi), %r9d
movl %ecx, %ebx
xorl %r11d, %r11d
leaq 0x524f(%rip), %r8       # 0x84f0
cmpl $0x1, %r9d
jne 0x3335
movslq (%rsi), %r12
movl -0x4(%rsp), %edi
movl %edi, %ecx
subl %r12d, %ecx
movl %r11d, %eax
shrl %cl, %eax
andl (%r8,%r12,4), %eax
movslq 0x8(%rsi), %r13
movl %edi, %ecx
subl %r13d, %ecx
movl %r11d, %r14d
shrl %cl, %r14d
movslq 0x10(%rsi), %rbp
movl %edi, %ecx
subl %ebp, %ecx
movl %r11d, %r15d
shrl %cl, %r15d
andl (%r8,%r13,4), %r14d
andl (%r8,%rbp,4), %r15d
movl %eax, %edi
movb $0x10, %cl
subb %r12b, %cl
shll %cl, %eax
movl %r14d, %r12d
movb $0x10, %cl
subb %r13b, %cl
shll %cl, %r14d
movl %r9d, %r10d
movl $0xffff, %r9d           # imm = 0xFFFF
andl %r9d, %edi
movl %r15d, %r13d
movb $0x10, %cl
subb %bpl, %cl
shll %cl, %r15d
andl %r9d, %r12d
andl %r9d, %r13d
movl %r10d, %r9d
movq 0x8(%rdx), %rcx
movw %ax, (%rcx,%rdi,2)
movq 0x10(%rdx), %rax
movw %r14w, (%rax,%r12,2)
movq 0x18(%rdx), %rax
movw %r15w, (%rax,%r13,2)
jmp 0x339a
movb 0x4(%rsi), %cl
movb 0xc(%rsi), %al
movl %r11d, %r14d
shrl %cl, %r14d
movslq (%rsi), %rdi
andl (%r8,%rdi,4), %r14d
movl %r11d, %r15d
movl %eax, %ecx
shrl %cl, %r15d
movslq 0x8(%rsi), %r12
movb 0x14(%rsi), %cl
movl %r11d, %eax
shrl %cl, %eax
andl (%r8,%r12,4), %r15d
movb $0x10, %cl
subb %dil, %cl
shll %cl, %r14d
movslq 0x10(%rsi), %rdi
movb $0x10, %cl
subb %r12b, %cl
shll %cl, %r15d
andl (%r8,%rdi,4), %eax
movb $0x10, %cl
subb %dil, %cl
shll %cl, %eax
movq 0x8(%rdx), %rcx
movw %r14w, (%rcx,%r11,2)
movq 0x10(%rdx), %rcx
movw %r15w, (%rcx,%r11,2)
movq 0x18(%rdx), %rcx
movw %ax, (%rcx,%r11,2)
incq %r11
cmpq %r11, %rbx
jne 0x32a1
movl -0x8(%rsp), %edi
movq %rdx, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3212