// File: /workspace/github_c_makefile_star2_O2/uobikiemukot[P]yaft/yaft.c
// Function: main
int main(int argc, char *const argv[])
{
	extern const char *shell_cmd; /* defined in conf.h */
	const char *cmd;
	uint8_t buf[BUFSIZE];
	ssize_t size;
	fd_set fds;
	struct timeval tv;
	struct framebuffer_t fb;
	struct terminal_t term;
	/* global */
	extern volatile sig_atomic_t need_redraw;
	extern volatile sig_atomic_t child_alive;
	extern struct termios termios_orig;

	/* init */
	if (setlocale(LC_ALL, "") == NULL) /* for wcwidth() */
		logging(WARN, "setlocale falied\n");

	if (!fb_init(&fb)) {
		logging(FATAL, "framebuffer initialize failed\n");
		goto fb_init_failed;
	}

	if (!term_init(&term, fb.info.width, fb.info.height)) {
		logging(FATAL, "terminal initialize failed\n");
		goto term_init_failed;
	}

	if (!tty_init(&termios_orig)) {
		logging(FATAL, "tty initialize failed\n");
		goto tty_init_failed;
	}

	/* fork and exec shell */
	cmd = (argc < 2) ? shell_cmd: argv[1];
	if (!fork_and_exec(&term.fd, cmd, argv + 1, term.lines, term.cols)) {
		logging(FATAL, "forkpty failed\n");
		goto tty_init_failed;
	}
	child_alive = true;

	/* main loop */
	while (child_alive) {
		if (need_redraw) {
			need_redraw = false;
			cmap_update(fb.fd, fb.cmap); /* after VT switching, need to restore cmap (in 8bpp mode) */
			redraw(&term);
			refresh(&fb, &term);
		}

		if (check_fds(&fds, &tv, STDIN_FILENO, term.fd) == -1)
			continue;

		if (FD_ISSET(STDIN_FILENO, &fds)) {
			if ((size = read(STDIN_FILENO, buf, BUFSIZE)) > 0)
				ewrite(term.fd, buf, size);
		}
		if (FD_ISSET(term.fd, &fds)) {
			if ((size = read(term.fd, buf, BUFSIZE)) > 0) {
				if (VERBOSE)
					ewrite(STDOUT_FILENO, buf, size);
				parse(&term, buf, size);
				if (LAZY_DRAW && size == BUFSIZE)
					continue; /* maybe more data arrives soon */
				refresh(&fb, &term);
			}
		}
	}

	/* normal exit */
	tty_die(&termios_orig);
	term_die(&term);
	fb_die(&fb);
	return EXIT_SUCCESS;

	/* error exit */
tty_init_failed:
	term_die(&term);
term_init_failed:
	fb_die(&fb);
fb_init_failed:
	return EXIT_FAILURE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xbc2a8, %rsp          # imm = 0xBC2A8
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x4b2ab(%rip), %rsi     # 0x5186b
pushq $0x6
popq %rdi
callq 0x2230
testq %rax, %rax
jne 0x65de
leaq 0x4b700(%rip), %rsi     # 0x51cd4
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x244c
leaq 0x90(%rsp), %rdi
callq 0x3421
testb %al, %al
je 0x6770
movl 0x4c8(%rsp), %esi
movl 0x4cc(%rsp), %edx
leaq 0x900(%rsp), %rdi
callq 0x411b
testb %al, %al
je 0x6783
leaq 0x4f4cf(%rip), %rdi     # 0x55aec
callq 0x632b
addq $0x8, %rbx
cmpl $0x2, %ebp
leaq 0x4eba8(%rip), %rax     # 0x551d8
cmovgeq %rbx, %rax
movq (%rax), %rsi
leaq 0x900(%rsp), %rdi
movl 0xc(%rdi), %r8d
movl 0x10(%rdi), %ecx
movq %rbx, %rdx
callq 0x64c6
testb %al, %al
je 0x6796
movl $0x1, 0x4f488(%rip)     # 0x55ae8
leaq 0x10(%rsp), %rbx
movq %rsp, %r14
leaq 0x500(%rsp), %r15
leaq 0x900(%rsp), %r12
leaq 0x90(%rsp), %r13
cmpl $0x0, 0x4f461(%rip)     # 0x55ae8
je 0x67d6
cmpl $0x0, 0x4f450(%rip)     # 0x55ae4
je 0x66c7
movl $0x0, 0x4f444(%rip)     # 0x55ae4
movl 0x90(%rsp), %edi
movq 0x4f0(%rsp), %rsi
callq 0x312c
movq %r12, %rdi
callq 0x40aa
movq %r13, %rdi
movq %r12, %rsi
callq 0x3677
movl 0x900(%rsp), %ecx
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x6535
cmpl $-0x1, %eax
je 0x6680
testb $0x1, 0x10(%rsp)
je 0x670d
movl $0x400, %edx            # imm = 0x400
xorl %edi, %edi
movq %r15, %rsi
callq 0x2160
testq %rax, %rax
jle 0x670d
movl 0x900(%rsp), %edi
movq %r15, %rsi
movq %rax, %rdx
callq 0x283d
movl 0x900(%rsp), %edi
movl %edi, %eax
cltd
pushq $0x40
popq %rcx
idivl %ecx
cltq
movq 0x10(%rsp,%rax,8), %rax
btq %rdx, %rax
jae 0x6680
movl $0x400, %edx            # imm = 0x400
movq %r15, %rsi
callq 0x2160
testq %rax, %rax
jle 0x6680
movq %rax, %rbp
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x613c
cmpq $0x400, %rbp            # imm = 0x400
je 0x6680
movq %r13, %rdi
movq %r12, %rsi
callq 0x3677
jmp 0x6680
leaq 0x4b56f(%rip), %rsi     # 0x51ce6
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x244c
jmp 0x67c1
leaq 0x4b57b(%rip), %rsi     # 0x51d05
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x244c
jmp 0x67b4
leaq 0x4b584(%rip), %rsi     # 0x51d21
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x244c
leaq 0x900(%rsp), %rdi
callq 0x40c3
leaq 0x90(%rsp), %rdi
callq 0x361e
pushq $0x1
popq %rax
addq $0xbc2a8, %rsp          # imm = 0xBC2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4f30f(%rip), %rdi     # 0x55aec
callq 0x641f
leaq 0x900(%rsp), %rdi
callq 0x40c3
leaq 0x90(%rsp), %rdi
callq 0x361e
xorl %eax, %eax
jmp 0x67c4