// File: /workspace/github_c_makefile_star2_O1/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, %r13d
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq %rdi, 0x8(%rsp)
callq 0x13768
testl %eax, %eax
je 0x83b5
movl %eax, %r12d
movslq %r13d, %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
movq %rcx, 0x8(%rdx)
xorl %r15d, %r15d
testl %eax, %eax
cmovnsq %rdx, %r15
leaq 0x10(%rsp), %rbx
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 0x1d89e(%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 0x1d881(%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 %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x6390
testl %eax, %eax
js 0x82cb
movb $0x1, %r14b
je 0x82e5
movl 0x1d824(%rip), %edi     # 0x25a6c
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0x10(%rsp,%rax,8), %rax
movl 0x1d818(%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, %ebx
shlq %cl, %rbx
movslq %esi, %rcx
andq 0x10(%rsp,%rcx,8), %rbx
btq %rdi, %rax
jae 0x830c
movl %r12d, (%rsp)
movq %r15, %r12
movl %r13d, %r15d
movl $0x40, %edx
leaq 0xa0(%rsp), %rsi
callq 0x62b0
movq %rax, %r13
testq %rax, %rax
js 0x82ef
je 0x8301
leaq 0x1d93f(%rip), %rdi     # 0x25bf8
leaq 0xa0(%rsp), %rsi
movq %r13, %rdx
callq 0x8728
jmp 0x8301
callq 0x6060
movl (%rax), %eax
movl %eax, 0x1d9b0(%rip)     # 0x25c88
movl $0xfffffff2, %ebp       # imm = 0xFFFFFFF2
movb $0x1, %r14b
jmp 0x837c
movl $0xfffffffa, %ebp       # imm = 0xFFFFFFFA
jmp 0x837c
callq 0x6060
movl (%rax), %eax
movl %eax, 0x1d98c(%rip)     # 0x25c88
movl $0xfffffff6, %ebp       # imm = 0xFFFFFFF6
testq %r13, %r13
movl %r15d, %r13d
movq %r12, %r15
js 0x8373
testq %rbx, %rbx
je 0x834d
movl $0x0, 0x4(%rsp)
movl 0x1d759(%rip), %edi     # 0x25a78
movl $0x4, %edx
leaq 0x4(%rsp), %rsi
callq 0x62b0
callq 0x703f
testl %eax, %eax
leaq 0x10(%rsp), %rbx
jne 0x8345
callq 0x7e18
testl %eax, %eax
je 0x838d
movl %eax, %r12d
movl %r12d, %ebp
jmp 0x837c
movq 0x8(%rsp), %rdi
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
callq 0x13768
movl %eax, %r12d
testl %eax, %eax
sete %r14b
cmovel %eax, %ebp
jmp 0x8377
movl (%rsp), %r12d
leaq 0x10(%rsp), %rbx
testb %r14b, %r14b
jne 0x83ac
cmpl $-0x1, %r13d
je 0x819a
jmp 0x83ac
movq 0x8(%rsp), %rcx
movb $0x2, (%rcx)
movl 0x1d6e5(%rip), %eax     # 0x25a80
movl %eax, 0x8(%rcx)
movl 0x1d6e0(%rip), %eax     # 0x25a84
movl %eax, 0xc(%rcx)
xorl %r12d, %r12d
jmp 0x8348
testb %r14b, %r14b
cmovnel %ebp, %r12d
jmp 0x83b8
xorl %r12d, %r12d
movl %r12d, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq