// File: /workspace/github_c_makefile_star2_O3/adsr[P]phpspy/vendor/termbox2/termbox2.h
// Function: wait_event
static int wait_event(struct tb_event *event, int timeout) {
    int rv;
    char buf[TB_OPT_READ_BUF];

    memset(event, 0, sizeof(*event));
    if_ok_return(rv, extract_event(event));

    fd_set fds;
    struct timeval tv;
    tv.tv_sec = timeout / 1000;
    tv.tv_usec = (timeout - (tv.tv_sec * 1000)) * 1000;

    do {
        FD_ZERO(&fds);
        FD_SET(global.rfd, &fds);
        FD_SET(global.resize_pipefd[0], &fds);

        int maxfd = global.resize_pipefd[0] > global.rfd
                        ? global.resize_pipefd[0]
                        : global.rfd;

        int select_rv =
            select(maxfd + 1, &fds, NULL, NULL, (timeout < 0) ? NULL : &tv);

        if (select_rv < 0) {
            // Let EINTR/EAGAIN bubble up
            global.last_errno = errno;
            return TB_ERR_POLL;
        } else if (select_rv == 0) {
            return TB_ERR_NO_EVENT;
        }

        int tty_has_events = (FD_ISSET(global.rfd, &fds));
        int resize_has_events = (FD_ISSET(global.resize_pipefd[0], &fds));

        if (tty_has_events) {
            ssize_t read_rv = read(global.rfd, buf, sizeof(buf));
            if (read_rv < 0) {
                global.last_errno = errno;
                return TB_ERR_READ;
            } else if (read_rv > 0) {
                bytebuf_nputs(&global.in, buf, read_rv);
            }
        }

        if (resize_has_events) {
            int ignore = 0;
            read(global.resize_pipefd[0], &ignore, sizeof(ignore));
            // TODO Harden against errors encountered mid-resize
            if_err_return(rv, update_term_size());
            if_err_return(rv, resize_cellbufs());
            event->type = TB_EVENT_RESIZE;
            event->w = global.width;
            event->h = global.height;
            return TB_OK;
        }

        memset(event, 0, sizeof(*event));
        if_ok_return(rv, extract_event(event));
    } while (timeout == -1);

    return rv;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movl %esi, %ebp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
callq 0x13479
testl %eax, %eax
je 0x81d7
movslq %ebp, %rax
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x26, %rcx
addl %edx, %ecx
movslq %ecx, %rcx
leaq 0x90(%rsp), %rdx
movq %rcx, (%rdx)
imulq $-0x3e8, %rcx, %rcx     # imm = 0xFC18
addq %rax, %rcx
imulq $0x3e8, %rcx, %rcx      # imm = 0x3E8
xorl %r14d, %r14d
testl %eax, %eax
cmovnsq %rdx, %r14
movq %rcx, 0x8(%rdx)
leaq 0x10(%rsp), %r15
leaq 0xa0(%rsp), %r12
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 0x1d9f7(%rip), %edi     # 0x25a6c
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 0x1d9da(%rip), %eax     # 0x25a78
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 0x6390
testl %eax, %eax
js 0x8181
je 0x8195
movl 0x1d980(%rip), %edi     # 0x25a6c
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0x10(%rsp,%rax,8), %rax
movl 0x1d974(%rip), %ecx     # 0x25a78
leal 0x3f(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
movl %edx, %esi
sarl $0x6, %esi
andl $-0x40, %edx
subl %edx, %ecx
movl $0x1, %r13d
shlq %cl, %r13
movslq %esi, %rcx
andq 0x10(%rsp,%rcx,8), %r13
btq %rdi, %rax
jae 0x8157
movl $0x40, %edx
movq %r12, %rsi
callq 0x62b0
testq %rax, %rax
js 0x81eb
je 0x8157
leaq 0x1daac(%rip), %rdi     # 0x25bf8
movq %r12, %rsi
movq %rax, %rdx
callq 0x8535
testq %r13, %r13
jne 0x819c
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movq %rbx, %rdi
callq 0x13479
testl %eax, %eax
je 0x81d9
cmpl $-0x1, %ebp
je 0x8041
jmp 0x81d9
callq 0x6060
movl (%rax), %eax
movl %eax, 0x1dafa(%rip)     # 0x25c88
movl $0xfffffff2, %eax       # imm = 0xFFFFFFF2
jmp 0x81d9
movl $0xfffffffa, %eax       # imm = 0xFFFFFFFA
jmp 0x81d9
leaq 0xc(%rsp), %rsi
movl $0x0, (%rsi)
movl 0x1d8cb(%rip), %edi     # 0x25a78
movl $0x4, %edx
callq 0x62b0
callq 0x7022
testl %eax, %eax
jne 0x81d9
callq 0x7d12
testl %eax, %eax
jne 0x81d9
movb $0x2, (%rbx)
movq 0x1d8ad(%rip), %rax     # 0x25a80
movq %rax, 0x8(%rbx)
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x6060
movl (%rax), %eax
movl %eax, 0x1da90(%rip)     # 0x25c88
movl $0xfffffff6, %eax       # imm = 0xFFFFFFF6
jmp 0x81d9