// File: /workspace/github_c_makefile_star2_O2/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)
andq $0x0, 0x10(%rdi)
callq 0x134ab
testl %eax, %eax
je 0x7c30
movl $0x3e8, %ecx            # imm = 0x3E8
movl %ebp, %eax
cltd
idivl %ecx
cltq
leaq 0x10(%rsp), %r14
movq %rax, (%r14)
movslq %ebp, %rdx
imulq $-0x3e8, %rax, %rax     # imm = 0xFC18
addq %rdx, %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, 0x8(%r14)
xorl %ecx, %ecx
testl %edx, %edx
cmovsq %rcx, %r14
pushq $0x40
popq %r13
pushq $0x1
popq %r15
cmpq $0x10, %rcx
jne 0x7bd7
movl 0x1df89(%rip), %edi     # 0x25a6c
movl %edi, %eax
cltd
idivl %r13d
movslq %eax, %rdx
shll $0x6, %eax
movl %edi, %ecx
subl %eax, %ecx
movq %r15, %rax
shlq %cl, %rax
orq %rax, 0x20(%rsp,%rdx,8)
movl 0x1df74(%rip), %esi     # 0x25a78
movl %esi, %eax
cltd
idivl %r13d
movslq %eax, %rdx
shll $0x6, %eax
movl %esi, %ecx
subl %eax, %ecx
movq %r15, %rax
shlq %cl, %rax
orq %rax, 0x20(%rsp,%rdx,8)
cmpl %edi, %esi
cmovgl %esi, %edi
incl %edi
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x6390
testl %eax, %eax
js 0x7be5
je 0x7bf6
movl 0x1df21(%rip), %edi     # 0x25a6c
movl %edi, %eax
cltd
idivl %r13d
movl %edx, %esi
cltq
movq 0x20(%rsp,%rax,8), %r8
movl 0x1df18(%rip), %eax     # 0x25a78
cltd
idivl %r13d
movl %edx, %ecx
movq %r15, %r12
shlq %cl, %r12
cltq
andq 0x20(%rsp,%rax,8), %r12
btq %rsi, %r8
jae 0x7bab
leaq 0xa0(%rsp), %rsi
pushq $0x40
popq %rdx
callq 0x62b0
testq %rax, %rax
js 0x7c34
je 0x7bab
leaq 0x1e05d(%rip), %rdi     # 0x25bf8
leaq 0xa0(%rsp), %rsi
movq %rax, %rdx
callq 0x7f4b
testq %r12, %r12
jne 0x7bfa
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
movq %rbx, %rdi
callq 0x134ab
testl %eax, %eax
je 0x7c44
movl $0x0, %ecx
cmpl $-0x1, %ebp
je 0x7ad3
jmp 0x7c44
andq $0x0, 0x20(%rsp,%rcx,8)
incq %rcx
jmp 0x7ad3
callq 0x6060
movl (%rax), %eax
movl %eax, 0x1e096(%rip)     # 0x25c88
pushq $-0xe
jmp 0x7c43
pushq $-0x6
jmp 0x7c43
leaq 0xc(%rsp), %rsi
andl $0x0, (%rsi)
movl 0x1de70(%rip), %edi     # 0x25a78
pushq $0x4
popq %rdx
callq 0x62b0
callq 0x6dfd
testl %eax, %eax
jne 0x7c44
callq 0x7831
testl %eax, %eax
jne 0x7c44
movb $0x2, (%rbx)
movq 0x1de54(%rip), %rax     # 0x25a80
movq %rax, 0x8(%rbx)
xorl %eax, %eax
jmp 0x7c44
callq 0x6060
movl (%rax), %eax
movl %eax, 0x1e047(%rip)     # 0x25c88
pushq $-0xa
popq %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq