// File: /workspace/github_c_makefile_star2_O1/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 0x4b02a(%rip), %rsi     # 0x5284b
movl $0x6, %edi
callq 0x2240
testq %rax, %rax
jne 0x7843
leaq 0x4b47d(%rip), %rsi     # 0x52cb4
movl $0x1, %edi
xorl %eax, %eax
callq 0x245c
leaq 0x90(%rsp), %rdi
callq 0x3513
testb %al, %al
je 0x7a19
movl 0x4c8(%rsp), %esi
movl 0x4cc(%rsp), %edx
leaq 0x900(%rsp), %rdi
callq 0x4496
testb %al, %al
je 0x7a2e
leaq 0x4f26a(%rip), %rdi     # 0x56aec
callq 0x7525
addq $0x8, %rbx
cmpl $0x2, %ebp
leaq 0x4e943(%rip), %rax     # 0x561d8
cmovgeq %rbx, %rax
movq (%rax), %rsi
leaq 0x900(%rsp), %rdi
movl 0xc(%rdi), %r8d
movl 0x10(%rdi), %ecx
movq %rbx, %rdx
callq 0x770c
testb %al, %al
je 0x7a43
movl $0x1, 0x4f223(%rip)     # 0x56ae8
cmpl $0x0, 0x4f21c(%rip)     # 0x56ae8
je 0x79ef
leaq 0x10(%rsp), %rbx
movq %rsp, %r14
leaq 0x500(%rsp), %r15
leaq 0x900(%rsp), %r12
leaq 0x90(%rsp), %r13
cmpl $0x0, 0x4f1eb(%rip)     # 0x56ae4
je 0x7943
movl $0x0, 0x4f1df(%rip)     # 0x56ae4
movl 0x90(%rsp), %edi
movq 0x4f0(%rsp), %rsi
callq 0x31eb
movslq 0x910(%rsp), %rdx
testq %rdx, %rdx
jle 0x7938
movq 0x928(%rsp), %rdi
movl $0x1, %esi
callq 0x2130
movq %r13, %rdi
movq %r12, %rsi
callq 0x37ab
movl 0x900(%rsp), %ecx
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x7777
cmpl $-0x1, %eax
je 0x79e2
testb $0x1, 0x10(%rsp)
je 0x798d
movl $0x400, %edx            # imm = 0x400
xorl %edi, %edi
movq %r15, %rsi
callq 0x2170
testq %rax, %rax
jle 0x798d
movl 0x900(%rsp), %edi
movq %r15, %rsi
movq %rax, %rdx
callq 0x286e
movl 0x900(%rsp), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0x10(%rsp,%rax,8), %rax
btq %rdi, %rax
jae 0x79e2
movl $0x400, %edx            # imm = 0x400
movq %r15, %rsi
callq 0x2170
testq %rax, %rax
jle 0x79e2
movq %rax, %rbp
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x7311
cmpq $0x400, %rbp            # imm = 0x400
je 0x79e2
movq %r13, %rdi
movq %r12, %rsi
callq 0x37ab
cmpl $0x0, 0x4f0ff(%rip)     # 0x56ae8
jne 0x78f2
leaq 0x4f0f6(%rip), %rdi     # 0x56aec
callq 0x7658
leaq 0x900(%rsp), %rdi
callq 0x4436
leaq 0x90(%rsp), %rdi
callq 0x374b
xorl %eax, %eax
jmp 0x7a75
leaq 0x4b2a6(%rip), %rsi     # 0x52cc6
movl $0x3, %edi
xorl %eax, %eax
callq 0x245c
jmp 0x7a70
leaq 0x4b2b0(%rip), %rsi     # 0x52ce5
movl $0x3, %edi
xorl %eax, %eax
callq 0x245c
jmp 0x7a63
leaq 0x4b2b7(%rip), %rsi     # 0x52d01
movl $0x3, %edi
xorl %eax, %eax
callq 0x245c
leaq 0x900(%rsp), %rdi
callq 0x4436
leaq 0x90(%rsp), %rdi
callq 0x374b
movl $0x1, %eax
addq $0xbc2a8, %rsp          # imm = 0xBC2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq