// File: /workspace/github_c_makefile_star2_O1/uobikiemukot[P]yaft/./fb/common.h
// Function: fb_init
bool fb_init(struct framebuffer_t *fb)
{
	extern const uint32_t color_list[COLORS]; /* defined in color.h */
	extern const char *fb_path;               /* defined in conf.h */
	const char *path;
	char *env;

	/* open framebuffer device: check FRAMEBUFFER env at first */
	path = ((env = getenv("FRAMEBUFFER")) == NULL) ? fb_path: env;
	if ((fb->fd = eopen(path, O_RDWR)) < 0)
		return false;

	/* os dependent initialize */
	if (!set_fbinfo(fb->fd, &fb->info))
		goto set_fbinfo_failed;

	if (VERBOSE)
		fb_print_info(&fb->info);

	/* allocate memory */
	fb->fp   = (uint8_t *) emmap(0, fb->info.screen_size,
				PROT_WRITE | PROT_READ, MAP_SHARED, fb->fd, 0);
	fb->buf  = (uint8_t *) ecalloc(1, fb->info.screen_size);
	fb->wall = ((env = getenv("YAFT")) && strstr(env, "wall")) ?
				load_wallpaper(fb->fp, fb->info.screen_size): NULL;

	/* error check */
	if (fb->fp == MAP_FAILED || !fb->buf)
		goto allocate_failed;

	if (fb->info.type != YAFT_FB_TYPE_PACKED_PIXELS) {
		/* TODO: support planes type */
		logging(ERROR, "unsupport framebuffer type\n");
		goto fb_init_failed;
	}

	if (fb->info.visual == YAFT_FB_VISUAL_TRUECOLOR) {
		if (!init_truecolor(&fb->info, &fb->cmap, &fb->cmap_orig))
			goto fb_init_failed;
	} else if (fb->info.visual == YAFT_FB_VISUAL_DIRECTCOLOR
		|| fb->info.visual == YAFT_FB_VISUAL_PSEUDOCOLOR) {
		if (!init_indexcolor(fb->fd, &fb->info, &fb->cmap, &fb->cmap_orig))
			goto fb_init_failed;
	} else {
		/* TODO: support mono visual */
		logging(ERROR, "unsupport framebuffer visual\n");
		goto fb_init_failed;
	}

	/* init color palette */
	for (int i = 0; i < COLORS; i++)
		fb->real_palette[i] = color2pixel(&fb->info, color_list[i]);

	return true;

fb_init_failed:
allocate_failed:
	free(fb->buf);
	free(fb->wall);
	if (fb->fp != MAP_FAILED)
		emunmap(fb->fp, fb->info.screen_size);
set_fbinfo_failed:
	eclose(fb->fd);
	return false;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rdi, %rbx
leaq 0x4f602(%rip), %rdi     # 0x52b24
callq 0x2030
testq %rax, %rax
jne 0x3533
movq 0x52c9d(%rip), %rax     # 0x561d0
movq %rax, %rdi
movl $0x2, %esi
callq 0x2524
movl %eax, (%rbx)
testl %eax, %eax
js 0x3637
leaq 0x420(%rbx), %r14
movl %eax, %edi
movq %r14, %rsi
callq 0x2f92
testb %al, %al
je 0x3630
movq 0x440(%rbx), %rsi
movl (%rbx), %r8d
xorl %r15d, %r15d
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
xorl %r9d, %r9d
callq 0x2687
movq %rax, 0x8(%rbx)
movq 0x440(%rbx), %rsi
movl $0x1, %edi
callq 0x2754
movq %rax, 0x10(%rbx)
leaq 0x4f58c(%rip), %rdi     # 0x52b30
callq 0x2030
testq %rax, %rax
je 0x35da
leaq 0x4f580(%rip), %rsi     # 0x52b35
movq %rax, %rdi
callq 0x2330
testq %rax, %rax
je 0x35d7
movq 0x8(%rbx), %rdi
movq 0x440(%rbx), %rsi
callq 0x30f9
movq %rax, %r15
jmp 0x35da
xorl %r15d, %r15d
movq %r15, 0x18(%rbx)
cmpq $-0x1, 0x8(%rbx)
je 0x3608
cmpq $0x0, 0x10(%rbx)
je 0x3608
cmpl $0x0, 0x454(%rbx)
je 0x363f
leaq 0x4f53e(%rip), %rsi     # 0x52b3a
movl $0x2, %edi
xorl %eax, %eax
callq 0x245c
movq 0x10(%rbx), %rdi
callq 0x2050
movq 0x18(%rbx), %rdi
callq 0x2050
movq 0x8(%rbx), %rdi
cmpq $-0x1, %rdi
je 0x3630
movq 0x440(%rbx), %rsi
callq 0x2704
movl (%rbx), %edi
callq 0x258a
xorl %eax, %eax
popq %rbx
popq %r14
popq %r15
retq
movl 0x458(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x366b
movl (%rbx), %edi
leaq 0x460(%rbx), %rdx
leaq 0x468(%rbx), %rcx
movq %r14, %rsi
callq 0x33d7
testb %al, %al
je 0x3608
jmp 0x36a1
testl %eax, %eax
jne 0x3727
movl 0x450(%rbx), %edx
cmpq $0x20, %rdx
ja 0x3733
movabsq $0x101018000, %rax      # imm = 0x101018000
btq %rdx, %rax
jae 0x3733
xorps %xmm0, %xmm0
movups %xmm0, 0x460(%rbx)
xorl %edx, %edx
leaq 0x4a16(%rip), %rsi      # 0x80c0
movl $0xff, %edi
movl (%rdx,%rsi), %r8d
movl %r8d, %r10d
shrl $0x10, %r10d
andl %edi, %r10d
movl %r8d, %r9d
shrl $0x8, %r9d
movb $0x8, %cl
subb 0x420(%rbx), %cl
shrl %cl, %r10d
andl %edi, %r9d
movb $0x8, %cl
subb 0x428(%rbx), %cl
shrl %cl, %r9d
andl %edi, %r8d
movb $0x8, %cl
subb 0x430(%rbx), %cl
shrl %cl, %r8d
movb 0x424(%rbx), %cl
movb 0x42c(%rbx), %al
shll %cl, %r10d
movl %eax, %ecx
shll %cl, %r9d
addl %r10d, %r9d
movb 0x434(%rbx), %cl
shll %cl, %r8d
addl %r9d, %r8d
movl %r8d, 0x20(%rbx,%rdx)
addq $0x4, %rdx
movb $0x1, %al
cmpq $0x400, %rdx            # imm = 0x400
jne 0x36af
jmp 0x3639
leaq 0x4f428(%rip), %rsi     # 0x52b56
jmp 0x35fc
leaq 0x4f369(%rip), %rsi     # 0x52aa3
movl $0x2, %edi
xorl %eax, %eax
callq 0x245c
jmp 0x3608