// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0x4e711(%rip), %rdi     # 0x51b44
callq 0x2030
testq %rax, %rax
jne 0x3444
movq 0x51d8c(%rip), %rax     # 0x551d0
pushq $0x2
popq %rsi
movq %rax, %rdi
callq 0x2514
movl %eax, (%rbx)
testl %eax, %eax
js 0x353e
leaq 0x420(%rbx), %r14
movl %eax, %edi
movq %r14, %rsi
callq 0x2eee
testb %al, %al
je 0x3537
movq 0x440(%rbx), %rsi
movl (%rbx), %r8d
xorl %r15d, %r15d
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
xorl %r9d, %r9d
callq 0x266f
movq %rax, 0x8(%rbx)
movq 0x440(%rbx), %rsi
pushq $0x1
popq %rdi
callq 0x2732
movq %rax, 0x10(%rbx)
leaq 0x4e6a3(%rip), %rdi     # 0x51b50
callq 0x2030
testq %rax, %rax
je 0x34e3
leaq 0x4e697(%rip), %rsi     # 0x51b55
movq %rax, %rdi
callq 0x2320
testq %rax, %rax
je 0x34e0
movq 0x8(%rbx), %rdi
movq 0x440(%rbx), %rsi
callq 0x3045
movq %rax, %r15
jmp 0x34e3
xorl %r15d, %r15d
movq %r15, 0x18(%rbx)
cmpq $-0x1, 0x8(%rbx)
je 0x350f
cmpq $0x0, 0x10(%rbx)
je 0x350f
cmpl $0x0, 0x454(%rbx)
je 0x354c
leaq 0x4e655(%rip), %rsi     # 0x51b5a
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x244c
movq 0x10(%rbx), %rdi
callq 0x2050
movq 0x18(%rbx), %rdi
callq 0x2050
movq 0x8(%rbx), %rdi
cmpq $-0x1, %rdi
je 0x3537
movq 0x440(%rbx), %rsi
callq 0x26e7
movl (%rbx), %edi
callq 0x257b
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movl 0x458(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x3574
movl (%rbx), %edi
leaq 0x460(%rbx), %rdx
leaq 0x468(%rbx), %rcx
movq %r14, %rsi
callq 0x32ee
jmp 0x358e
testl %eax, %eax
jne 0x35c7
leaq 0x460(%rbx), %rsi
leaq 0x468(%rbx), %rdx
movq %r14, %rdi
callq 0x32ae
testb %al, %al
je 0x350f
xorl %r15d, %r15d
leaq 0x3b40(%rip), %r12      # 0x70e0
cmpq $0x400, %r15            # imm = 0x400
je 0x35c0
movl (%r15,%r12), %esi
movq %r14, %rdi
callq 0x35d3
movl %eax, 0x20(%rbx,%r15)
addq $0x4, %r15
jmp 0x35a0
movb $0x1, %al
jmp 0x3540
leaq 0x4e5a8(%rip), %rsi     # 0x51b76
jmp 0x3505