// File: /workspace/github_c_makefile_star2_O3/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 0x4b149(%rip), %rsi     # 0x5286b
movl $0x6, %edi
callq 0x2240
testq %rax, %rax
jne 0x7744
leaq 0x4b59c(%rip), %rsi     # 0x52cd4
movl $0x1, %edi
xorl %eax, %eax
callq 0x245c
leaq 0x90(%rsp), %rdi
callq 0x353c
testb %al, %al
je 0x791a
movl 0x4c8(%rsp), %esi
movl 0x4cc(%rsp), %edx
leaq 0x900(%rsp), %rdi
callq 0x44bf
testb %al, %al
je 0x792f
leaq 0x4f369(%rip), %rdi     # 0x56aec
callq 0x7420
addq $0x8, %rbx
cmpl $0x2, %ebp
leaq 0x4ea42(%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 0x760d
testb %al, %al
je 0x7944
movl $0x1, 0x4f322(%rip)     # 0x56ae8
cmpl $0x0, 0x4f31b(%rip)     # 0x56ae8
je 0x78f0
leaq 0x10(%rsp), %rbx
movq %rsp, %r14
leaq 0x500(%rsp), %r15
leaq 0x900(%rsp), %r12
leaq 0x90(%rsp), %r13
cmpl $0x0, 0x4f2ea(%rip)     # 0x56ae4
je 0x7844
movl $0x0, 0x4f2de(%rip)     # 0x56ae4
movl 0x90(%rsp), %edi
movq 0x4f0(%rsp), %rsi
callq 0x3212
movslq 0x910(%rsp), %rdx
testq %rdx, %rdx
jle 0x7839
movq 0x928(%rsp), %rdi
movl $0x1, %esi
callq 0x2130
movq %r13, %rdi
movq %r12, %rsi
callq 0x37d4
movl 0x900(%rsp), %ecx
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x7678
cmpl $-0x1, %eax
je 0x78e3
testb $0x1, 0x10(%rsp)
je 0x788e
movl $0x400, %edx            # imm = 0x400
xorl %edi, %edi
movq %r15, %rsi
callq 0x2170
testq %rax, %rax
jle 0x788e
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 0x78e3
movl $0x400, %edx            # imm = 0x400
movq %r15, %rsi
callq 0x2170
testq %rax, %rax
jle 0x78e3
movq %rax, %rbp
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x720c
cmpq $0x400, %rbp            # imm = 0x400
je 0x78e3
movq %r13, %rdi
movq %r12, %rsi
callq 0x37d4
cmpl $0x0, 0x4f1fe(%rip)     # 0x56ae8
jne 0x77f3
leaq 0x4f1f5(%rip), %rdi     # 0x56aec
callq 0x7559
leaq 0x900(%rsp), %rdi
callq 0x445f
leaq 0x90(%rsp), %rdi
callq 0x3774
xorl %eax, %eax
jmp 0x7976
leaq 0x4b3c5(%rip), %rsi     # 0x52ce6
movl $0x3, %edi
xorl %eax, %eax
callq 0x245c
jmp 0x7971
leaq 0x4b3cf(%rip), %rsi     # 0x52d05
movl $0x3, %edi
xorl %eax, %eax
callq 0x245c
jmp 0x7964
leaq 0x4b3d6(%rip), %rsi     # 0x52d21
movl $0x3, %edi
xorl %eax, %eax
callq 0x245c
leaq 0x900(%rsp), %rdi
callq 0x445f
leaq 0x90(%rsp), %rdi
callq 0x3774
movl $0x1, %eax
addq $0xbc2a8, %rsp          # imm = 0xBC2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq