// File: /workspace/github_c_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movb $0x1, (%rdi)
movl 0x48b8(%rip), %esi      # 0x9158
callq 0x4f05
testb %al, %al
je 0x48b1
movzbl (%rbx), %ebp
jmp 0x4a88
callq 0x53f8
testl %eax, %eax
js 0x4a83
je 0x48d2
movl 0x4892(%rip), %esi      # 0x9158
movq %rbx, %rdi
callq 0x4f05
testb %al, %al
jne 0x48a9
leaq 0x10(%rsp), %r15
movl $0xffff, %r12d          # imm = 0xFFFF
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movl 0x4f7f(%rip), %edi      # 0x9890
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %edi, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, 0x10(%rsp,%rax,8)
movl 0x4f5a(%rip), %eax      # 0x9894
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)
cmpl %edi, %eax
cmovgl %eax, %edi
incl %edi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x3190
movl $0x1, %r13d
testl %eax, %eax
je 0x49cb
js 0x49d2
movl 0x4f0a(%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 0x49dc
movb $0x1, (%rbx)
callq 0x53f8
testl %eax, %eax
js 0x49d2
je 0x4a71
movl 0x47a1(%rip), %esi      # 0x9158
movq %rbx, %rdi
callq 0x4f05
testb %al, %al
je 0x49dc
movzbl (%rbx), %ebp
jmp 0x4a77
xorl %ebp, %ebp
jmp 0x4a77
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x4a77
movl 0x4eb2(%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 0x4a6c
movb $0x2, (%rbx)
movl $0x0, 0xc(%rsp)
movl $0x4, %edx
leaq 0xc(%rsp), %rsi
callq 0x3140
movl $0x1, 0x4f02(%rip)      # 0x9920
movq $0x0, (%rsp)
movl 0x4e64(%rip), %edi      # 0x9890
movl $0x5413, %esi           # imm = 0x5413
movq %rsp, %rdx
xorl %eax, %eax
callq 0x3110
movzwl 0x2(%rsp), %eax
testw %ax, %ax
movl $0x50, %ecx
cmovel %ecx, %eax
andl %r12d, %eax
movl %eax, 0x8(%rbx)
movl (%rsp), %eax
testw %ax, %ax
movl $0x18, %ecx
cmovel %ecx, %eax
andl %r12d, %eax
movl %eax, 0xc(%rbx)
movl $0x2, %ebp
jmp 0x4a77
xorl %r13d, %r13d
jmp 0x4a77
movl $0x2, %r13d
testb $0x1, %r13b
je 0x48dd
jmp 0x4a88
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq