// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x6230
movq -0x10(%rbp), %rdi
callq 0xd300
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
jne 0x823e
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x85d5
movl -0x14(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
cltq
movq %rax, -0xf0(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x3e8, -0xf0(%rbp), %rcx # imm = 0x3E8
subq %rcx, %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, -0xe8(%rbp)
jmp 0x8274
leaq -0xe0(%rbp), %rax
movq %rax, -0x100(%rbp)
movl $0x0, -0xf4(%rbp)
movl -0xf4(%rbp), %eax
cmpq $0x10, %rax
jae 0x82be
movq -0x100(%rbp), %rax
movl -0xf4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0x828c
jmp 0x82c0
movl 0x33bd6(%rip), %eax     # 0x3be9c
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl 0x33bb9(%rip), %eax     # 0x3be9c
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xe0(%rbp,%rax,8), %rcx
movq %rcx, -0xe0(%rbp,%rax,8)
movl 0x33ba5(%rip), %eax     # 0x3bea8
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl 0x33b88(%rip), %eax     # 0x3bea8
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xe0(%rbp,%rax,8), %rcx
movq %rcx, -0xe0(%rbp,%rax,8)
movl 0x33b68(%rip), %eax     # 0x3bea8
cmpl 0x33b56(%rip), %eax     # 0x3be9c
jle 0x8356
movl 0x33b5a(%rip), %eax     # 0x3bea8
movl %eax, -0x120(%rbp)
jmp 0x8362
movl 0x33b40(%rip), %eax     # 0x3be9c
movl %eax, -0x120(%rbp)
movl -0x120(%rbp), %eax
movl %eax, -0x104(%rbp)
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x838e
xorl %eax, %eax
movq %rax, -0x130(%rbp)
jmp 0x839e
leaq -0xf0(%rbp), %rax
movq %rax, -0x130(%rbp)
jmp 0x839e
movl -0x124(%rbp), %edi
movq -0x130(%rbp), %r8
leaq -0xe0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x6390
movl %eax, -0x108(%rbp)
cmpl $0x0, -0x108(%rbp)
jge 0x83e6
callq 0x6060
movl (%rax), %eax
movl %eax, 0x33cde(%rip)     # 0x3c0b8
movl $0xfffffff2, -0x4(%rbp) # imm = 0xFFFFFFF2
jmp 0x85d5
cmpl $0x0, -0x108(%rbp)
jne 0x83fb
movl $0xfffffffa, -0x4(%rbp) # imm = 0xFFFFFFFA
jmp 0x85d5
jmp 0x83fd
movl 0x33a99(%rip), %eax     # 0x3be9c
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xe0(%rbp,%rax,8), %rax
movq %rax, -0x140(%rbp)
movl 0x33a7a(%rip), %eax     # 0x3be9c
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x140(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x10c(%rbp)
movl 0x33a4f(%rip), %eax     # 0x3bea8
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xe0(%rbp,%rax,8), %rax
movq %rax, -0x138(%rbp)
movl 0x33a30(%rip), %eax     # 0x3bea8
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x138(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x110(%rbp)
cmpl $0x0, -0x10c(%rbp)
je 0x851c
movl 0x339e4(%rip), %edi     # 0x3be9c
leaq -0x60(%rbp), %rsi
movl $0x40, %edx
callq 0x62a0
movq %rax, -0x118(%rbp)
cmpq $0x0, -0x118(%rbp)
jge 0x84f0
callq 0x6060
movl (%rax), %eax
movl %eax, 0x33bd4(%rip)     # 0x3c0b8
movl $0xfffffff6, -0x4(%rbp) # imm = 0xFFFFFFF6
jmp 0x85d5
cmpq $0x0, -0x118(%rbp)
jle 0x8518
leaq -0x60(%rbp), %rsi
movq -0x118(%rbp), %rdx
leaq 0x3398c(%rip), %rdi     # 0x3be98
addq $0x190, %rdi            # imm = 0x190
callq 0x8ae0
jmp 0x851a
jmp 0x851c
cmpl $0x0, -0x110(%rbp)
je 0x859a
movl $0x0, -0x11c(%rbp)
movl 0x33973(%rip), %edi     # 0x3bea8
leaq -0x11c(%rbp), %rsi
movl $0x4, %edx
callq 0x62a0
callq 0x6db0
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
je 0x855b
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x85d5
callq 0x7cd0
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
je 0x8570
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x85d5
movq -0x10(%rbp), %rax
movb $0x2, (%rax)
movl 0x33933(%rip), %ecx     # 0x3beb0
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movl 0x3392a(%rip), %ecx     # 0x3beb4
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x85d5
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x6230
movq -0x10(%rbp), %rdi
callq 0xd300
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
jne 0x85c3
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x85d5
jmp 0x85c5
cmpl $-0x1, -0x14(%rbp)
je 0x8272
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopw %cs:(%rax,%rax)