// File: /workspace/github_c_makefile_star2_O3/termbox[P]termbox/termbox.c
// Function: wait_fill_event
static int wait_fill_event(struct tb_event *event, struct timeval *timeout) {
    // ;-)
#define ENOUGH_DATA_FOR_PARSING 64
    fd_set events;
    memset(event, 0, sizeof(struct tb_event));

    // try to extract event from input buffer, return on success
    event->type = TB_EVENT_KEY;
    if (extract_event(event, &input_buffer, inputmode))
        return event->type;

    // it looks like input buffer is incomplete, let's try the short path,
    // but first make sure there is enough space
    int n = read_up_to(ENOUGH_DATA_FOR_PARSING);
    if (n < 0)
        return -1;
    if (n > 0 && extract_event(event, &input_buffer, inputmode))
        return event->type;

    // n == 0, or not enough data, let's go to select
    while (1) {
        FD_ZERO(&events);
        FD_SET(inout, &events);
        FD_SET(winch_fds[0], &events);
        int maxfd = (winch_fds[0] > inout) ? winch_fds[0] : inout;
        int result = select(maxfd + 1, &events, 0, 0, timeout);
        if (!result)
            return 0;
        if (result < 0)
            return -1;

        if (FD_ISSET(inout, &events)) {
            event->type = TB_EVENT_KEY;
            n = read_up_to(ENOUGH_DATA_FOR_PARSING);
            if (n < 0)
                return -1;

            if (n == 0)
                continue;

            if (extract_event(event, &input_buffer, inputmode))
                return event->type;
        }
        if (FD_ISSET(winch_fds[0], &events)) {
            event->type = TB_EVENT_RESIZE;
            int zzz = 0;
            read(winch_fds[0], &zzz, sizeof(int));
            buffer_size_change_request = 1;
            get_term_size(&event->w, &event->h);
            return TB_EVENT_RESIZE;
        }
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r14
movq %rdi, %rbx
pxor %xmm0, %xmm0
movdqu %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movb $0x1, (%rdi)
movl 0x48ff(%rip), %esi      # 0x9158
callq 0x4eb8
testb %al, %al
je 0x486b
movzbl (%rbx), %r12d
jmp 0x4a3c
callq 0x535c
testl %eax, %eax
js 0x4a36
je 0x488c
movl 0x48d8(%rip), %esi      # 0x9158
movq %rbx, %rdi
callq 0x4eb8
testb %al, %al
jne 0x4862
movl 0x5002(%rip), %edi      # 0x9894
leaq 0x10(%rsp), %r15
pxor %xmm0, %xmm0
movdqa %xmm0, 0x80(%rsp)
movdqa %xmm0, 0x70(%rsp)
movdqa %xmm0, 0x60(%rsp)
movdqa %xmm0, 0x50(%rsp)
movdqa %xmm0, 0x40(%rsp)
movdqa %xmm0, 0x30(%rsp)
movdqa %xmm0, 0x20(%rsp)
movdqa %xmm0, 0x10(%rsp)
movl 0x4fbc(%rip), %eax      # 0x9890
leal 0x3f(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
movl %edx, %esi
andl $-0x40, %esi
movl %eax, %ecx
subl %esi, %ecx
movl $0x1, %esi
shlq %cl, %rsi
sarl $0x6, %edx
movslq %edx, %rcx
orq %rsi, 0x10(%rsp,%rcx,8)
leal 0x3f(%rdi), %edx
testl %edi, %edi
cmovnsl %edi, %edx
movl %edx, %esi
andl $-0x40, %esi
movl %edi, %ecx
subl %esi, %ecx
movl $0x1, %esi
shlq %cl, %rsi
sarl $0x6, %edx
movslq %edx, %rcx
orq %rsi, 0x10(%rsp,%rcx,8)
cmpl %eax, %edi
cmovgl %edi, %eax
incl %eax
xorl %r12d, %r12d
movl %eax, %edi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x3190
testl %eax, %eax
je 0x4a3c
js 0x4a36
movl 0x4f45(%rip), %eax      # 0x9890
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
movq 0x10(%rsp,%rcx,8), %rcx
btq %rax, %rcx
jae 0x4996
movb $0x1, (%rbx)
callq 0x535c
testl %eax, %eax
js 0x4a36
movl 0x4f1a(%rip), %edi      # 0x9894
je 0x4897
movl 0x47d2(%rip), %esi      # 0x9158
movq %rbx, %rdi
callq 0x4eb8
testb %al, %al
jne 0x4862
movl 0x4ef8(%rip), %edi      # 0x9894
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0x10(%rsp,%rax,8), %rax
btq %rdi, %rax
jae 0x4897
movb $0x2, (%rbx)
leaq 0x4(%rsp), %rsi
movl $0x0, (%rsi)
movl $0x4, %edx
callq 0x3140
movl $0x1, 0x4f46(%rip)      # 0x9920
leaq 0x8(%rsp), %r14
movq $0x0, (%r14)
movl 0x4ea4(%rip), %edi      # 0x9890
movl $0x5413, %esi           # imm = 0x5413
movq %r14, %rdx
xorl %eax, %eax
callq 0x3110
movq (%r14), %xmm0
pshuflw $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3,4,5,6,7]
pxor %xmm1, %xmm1
movdqa %xmm0, %xmm2
pcmpeqw %xmm1, %xmm2
movdqa %xmm2, %xmm3
pandn %xmm0, %xmm3
pand 0x164f(%rip), %xmm2     # 0x6070
por %xmm3, %xmm2
punpcklwd %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1],xmm2[2],xmm1[2],xmm2[3],xmm1[3]
movq %xmm2, 0x8(%rbx)
movl $0x2, %r12d
jmp 0x4a3c
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r12d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq