// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
leaq 0x51d8c(%rip), %rdi     # 0x55c2f
callq 0x2030
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x3ebf
movq 0x55317(%rip), %rax     # 0x591d0
movq %rax, -0x30(%rbp)
jmp 0x3ec7
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movl $0x2, %esi
callq 0x2530
movq -0x10(%rbp), %rcx
movl %eax, (%rcx)
cmpl $0x0, %eax
jge 0x3ef1
movb $0x0, -0x1(%rbp)
jmp 0x414e
movq -0x10(%rbp), %rax
movl (%rax), %edi
movq -0x10(%rbp), %rsi
addq $0x420, %rsi            # imm = 0x420
callq 0x3460
testb $0x1, %al
jne 0x3f10
jmp 0x413f
movq -0x10(%rbp), %rax
movq 0x440(%rax), %rsi
movq -0x10(%rbp), %rax
movl (%rax), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x26f0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x440(%rax), %rsi
movl $0x1, %edi
callq 0x27d0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
leaq 0x51cd0(%rip), %rdi     # 0x55c3b
callq 0x2030
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
je 0x3fae
movq -0x20(%rbp), %rdi
leaq 0x51cbb(%rip), %rsi     # 0x55c40
callq 0x2320
cmpq $0x0, %rax
je 0x3fae
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x440(%rax), %rsi
callq 0x35f0
movq %rax, -0x38(%rbp)
jmp 0x3fb6
xorl %eax, %eax
movq %rax, -0x38(%rbp)
jmp 0x3fb6
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq $-0x1, %rcx
cmpq %rcx, 0x8(%rax)
je 0x3fde
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x3fe3
jmp 0x40fa
movq -0x10(%rbp), %rax
cmpl $0x0, 0x454(%rax)
je 0x4008
movl $0x2, %edi
leaq 0x51c49(%rip), %rsi     # 0x55c45
movb $0x0, %al
callq 0x2450
jmp 0x40f8
movq -0x10(%rbp), %rax
cmpl $0x0, 0x458(%rax)
jne 0x4046
movq -0x10(%rbp), %rdi
addq $0x420, %rdi            # imm = 0x420
movq -0x10(%rbp), %rsi
addq $0x460, %rsi            # imm = 0x460
movq -0x10(%rbp), %rdx
addq $0x468, %rdx            # imm = 0x468
callq 0x3a60
testb $0x1, %al
jne 0x4044
jmp 0x40f8
jmp 0x40ab
movq -0x10(%rbp), %rax
cmpl $0x1, 0x458(%rax)
je 0x4060
movq -0x10(%rbp), %rax
cmpl $0x2, 0x458(%rax)
jne 0x4094
movq -0x10(%rbp), %rax
movl (%rax), %edi
movq -0x10(%rbp), %rsi
addq $0x420, %rsi            # imm = 0x420
movq -0x10(%rbp), %rdx
addq $0x460, %rdx            # imm = 0x460
movq -0x10(%rbp), %rcx
addq $0x468, %rcx            # imm = 0x468
callq 0x3af0
testb $0x1, %al
jne 0x4092
jmp 0x40f8
jmp 0x40a9
movl $0x2, %edi
leaq 0x51bc1(%rip), %rsi     # 0x55c61
movb $0x0, %al
callq 0x2450
jmp 0x40f8
jmp 0x40ab
movl $0x0, -0x24(%rbp)
cmpl $0x100, -0x24(%rbp)     # imm = 0x100
jge 0x40f2
movq -0x10(%rbp), %rdi
addq $0x420, %rdi            # imm = 0x420
movslq -0x24(%rbp), %rcx
leaq 0x6f3f(%rip), %rax      # 0xb010
movl (%rax,%rcx,4), %esi
callq 0x4160
movl %eax, %edx
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, 0x20(%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x40b2
movb $0x1, -0x1(%rbp)
jmp 0x414e
jmp 0x40fa
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x2050
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x2050
movq -0x10(%rbp), %rax
movq $-0x1, %rcx
cmpq %rcx, 0x8(%rax)
je 0x413d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x440(%rax), %rsi
callq 0x2770
jmp 0x413f
movq -0x10(%rbp), %rax
movl (%rax), %edi
callq 0x25b0
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)