// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x10(%rdi)
pxor %xmm0, %xmm0
movdqu %xmm0, (%rdi)
movb $0x1, (%rdi)
movl 0x4b65(%rip), %esi      # 0x9158
callq 0x4ba7
testb %al, %al
je 0x4605
movzbl (%rbx), %r12d
jmp 0x479b
callq 0x4f20
pushq $-0x1
popq %rcx
testl %eax, %eax
js 0x471c
je 0x4629
movl 0x4b3b(%rip), %esi      # 0x9158
movq %rbx, %rdi
callq 0x4ba7
testb %al, %al
jne 0x45fc
xorl %eax, %eax
pushq $0x40
popq %r13
pushq $0x1
popq %rbp
leaq 0x18(%rsp), %r15
cmpq $0x10, %rax
jne 0x470e
movl 0x5249(%rip), %edi      # 0x9890
movl %edi, %eax
cltd
idivl %r13d
movslq %eax, %rdx
shll $0x6, %eax
movl %edi, %ecx
subl %eax, %ecx
movq %rbp, %rax
shlq %cl, %rax
orq %rax, 0x18(%rsp,%rdx,8)
movl 0x522c(%rip), %esi      # 0x9894
movl %esi, %eax
cltd
idivl %r13d
movslq %eax, %rdx
shll $0x6, %eax
movl %esi, %ecx
subl %eax, %ecx
movq %rbp, %rax
shlq %cl, %rax
orq %rax, 0x18(%rsp,%rdx,8)
cmpl %edi, %esi
cmovgl %esi, %edi
incl %edi
xorl %r12d, %r12d
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x3190
testl %eax, %eax
je 0x479b
js 0x4721
movl 0x51e4(%rip), %eax      # 0x9890
cltd
idivl %r13d
cltq
movq 0x18(%rsp,%rax,8), %rax
btq %rdx, %rax
jae 0x46ea
movb $0x1, (%rbx)
callq 0x4f20
testl %eax, %eax
js 0x4721
movl $0x0, %eax
je 0x4637
movl 0x4a7e(%rip), %esi      # 0x9158
movq %rbx, %rdi
callq 0x4ba7
testb %al, %al
jne 0x45fc
movl 0x51a4(%rip), %edi      # 0x9894
movl %edi, %eax
cltd
idivl %r13d
cltq
movq 0x18(%rsp,%rax,8), %rax
btq %rdx, %rax
movl $0x0, %eax
jae 0x4637
jmp 0x4729
andq $0x0, 0x18(%rsp,%rax,8)
incq %rax
jmp 0x4637
movl %ecx, %r12d
jmp 0x479b
pushq $-0x1
popq %rax
movl %eax, %r12d
jmp 0x479b
movb $0x2, (%rbx)
leaq 0xc(%rsp), %rsi
andl $0x0, (%rsi)
pushq $0x4
popq %rdx
callq 0x3140
movl $0x1, 0x51da(%rip)      # 0x9920
leaq 0x10(%rsp), %r14
andq $0x0, (%r14)
movl 0x513b(%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 0x18e6(%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)
pushq $0x2
popq %r12
movl %r12d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq