// File: /workspace/github_c_makefile_star2_O0/termbox[P]termbox/termbox.c
// Function: extract_event
static bool extract_event(struct tb_event *event, struct bytebuffer *inbuf,
                          int inputmode) {
    const char *buf = inbuf->buf;
    const int len = inbuf->len;
    if (len == 0)
        return false;

    if (buf[0] == '\033') {
        int n = parse_escape_seq(event, buf, len);
        if (n != 0) {
            bool success = true;
            if (n < 0) {
                success = false;
                n = -n;
            }
            bytebuffer_truncate(inbuf, n);
            return success;
        } else {
            // it's not escape sequence, then it's ALT or ESC,
            // check inputmode
            if (inputmode & TB_INPUT_ESC) {
                // if we're in escape mode, fill ESC event, pop
                // buffer, return success
                event->ch = 0;
                event->key = TB_KEY_ESC;
                event->mod = 0;
                bytebuffer_truncate(inbuf, 1);
                return true;
            } else if (inputmode & TB_INPUT_ALT) {
                // if we're in alt mode, set ALT modifier to
                // event and redo parsing
                event->mod = TB_MOD_ALT;
                bytebuffer_truncate(inbuf, 1);
                return extract_event(event, inbuf, inputmode);
            }
            assert(!"never got here");
        }
    }

    // if we're here, this is not an escape sequence and not an alt sequence
    // so, it's a FUNCTIONAL KEY or a UNICODE character

    // first of all check if it's a functional key
    if ((unsigned char)buf[0] <= TB_KEY_SPACE ||
        (unsigned char)buf[0] == TB_KEY_BACKSPACE2) {
        // fill event, pop buffer, return success */
        event->ch = 0;
        event->key = (uint16_t)buf[0];
        bytebuffer_truncate(inbuf, 1);
        return true;
    }

    // feh... we got utf8 here

    // check if there is all bytes
    if (len >= tb_utf8_char_length(buf[0])) {
        /* everything ok, fill event, pop buffer, return success */
        tb_utf8_char_to_unicode(&event->ch, buf);
        event->key = 0;
        bytebuffer_truncate(inbuf, tb_utf8_char_length(buf[0]));
        return true;
    }

    // event isn't recognized, perhaps there is not enough bytes in utf8
    // sequence
    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x5ae7
movb $0x0, -0x1(%rbp)
jmp 0x5c82
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x1b, %eax
jne 0x5bda
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0x5e70
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x5b3f
movb $0x1, -0x31(%rbp)
cmpl $0x0, -0x30(%rbp)
jge 0x5b26
movb $0x0, -0x31(%rbp)
xorl %eax, %eax
subl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x5f40
movb -0x31(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x5c82
movl -0x1c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x5b7e
movq -0x10(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x10(%rbp), %rax
movw $0x1b, 0x2(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x1(%rax)
movq -0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x5f40
movb $0x1, -0x1(%rbp)
jmp 0x5c82
movl -0x1c(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x5bb9
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
movq -0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x5f40
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x5ab0
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x5c82
jmp 0x5bbb
leaq 0x17fc(%rip), %rdi      # 0x73be
leaq 0x17ad(%rip), %rsi      # 0x7376
movl $0x299, %edx            # imm = 0x299
leaq 0x17fb(%rip), %rcx      # 0x73d0
callq 0x30e0
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x20, %eax
jle 0x5bf2
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x7f, %eax
jne 0x5c23
movq -0x10(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x5f40
movb $0x1, -0x1(%rbp)
jmp 0x5c82
movl -0x2c(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movsbl (%rax), %edi
callq 0x6470
movl %eax, %ecx
movl -0x38(%rbp), %eax
cmpl %ecx, %eax
jl 0x5c7e
movq -0x10(%rbp), %rdi
addq $0x4, %rdi
movq -0x28(%rbp), %rsi
callq 0x6490
movq -0x10(%rbp), %rax
movw $0x0, 0x2(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movsbl (%rax), %edi
callq 0x6470
movq -0x40(%rbp), %rdi
movl %eax, %esi
callq 0x5f40
movb $0x1, -0x1(%rbp)
jmp 0x5c82
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)