// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %r13d
movq %rdi, %rbx
leaq 0x4(%rdi), %rax
movq %rax, 0x10(%rsp)
pushq $0x1
popq %rbp
movq %rdi, 0x8(%rsp)
movl 0x4d0d(%rip), %r15d     # 0x98e0
testl %r15d, %r15d
je 0x4d50
movq 0x4cf5(%rip), %r14      # 0x98d8
movb (%r14), %al
cmpb $0x1b, %al
jne 0x4d03
cmpl $0x6, %r15d
jl 0x4c0e
movq %r14, %rdi
movl %r15d, %esi
leaq 0x1810(%rip), %rdx      # 0x6411
callq 0x500f
testb %al, %al
jne 0x4d57
movq %r14, %rdi
movl %r15d, %esi
leaq 0x17fa(%rip), %rdx      # 0x6415
callq 0x500f
testb %al, %al
jne 0x4c3a
movq %r14, %rdi
movl %r15d, %esi
leaq 0x1737(%rip), %rdx      # 0x6368
callq 0x500f
testb %al, %al
je 0x4c8c
testl %r15d, %r15d
movl $0x0, %eax
cmovgl %r15d, %eax
xorl %r12d, %r12d
pushq $-0x1
popq %rbx
movl %ebx, %ebp
cmpq %r12, %rax
je 0x4c8c
movzbl (%r14,%r12), %ecx
movl %ecx, %edx
orl $0x20, %edx
cmpl $0x6d, %edx
je 0x4c7a
cmpl $-0x1, %ebp
movl %ebp, %edx
cmovel %r12d, %edx
cmpb $0x3b, %cl
cmovel %r12d, %ebx
cmovel %edx, %ebp
incq %r12
jmp 0x4c4e
cmpl $-0x1, %ebp
je 0x4c8c
cmpl $-0x1, %ebx
je 0x4c8c
cmpl %ebx, %ebp
jne 0x4d85
movw $0xffff, %bx            # imm = 0xFFFF
movq 0x4c99(%rip), %rbp      # 0x9930
movq (%rbp), %r12
testq %r12, %r12
je 0x4cdb
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x500f
testb %al, %al
jne 0x4cba
decl %ebx
addq $0x8, %rbp
jmp 0x4c97
movq 0x8(%rsp), %rax
andl $0x0, 0x4(%rax)
movw %bx, 0x2(%rax)
movq %r12, %rdi
callq 0x30c0
movq %rax, %r12
testl %r12d, %r12d
jne 0x4eda
testb $0x1, %r13b
jne 0x4d22
testb $0x2, %r13b
movq 0x8(%rsp), %rbx
je 0x4f01
movb $0x1, 0x1(%rbx)
pushq $0x1
popq %rbp
movl %ebp, %edi
callq 0x4fdc
jmp 0x4bcc
cmpb $0x21, %al
setae %cl
cmpb $0x7f, %al
setne %dl
testb %dl, %cl
jne 0x4d3f
andl $0x0, 0x4(%rbx)
movsbl (%r14), %eax
movw %ax, 0x2(%rbx)
jmp 0x4e2d
movq 0x8(%rsp), %rax
andl $0x0, 0x4(%rax)
movw $0x1b, 0x2(%rax)
movb $0x0, 0x1(%rax)
movb $0x1, %bl
pushq $0x1
popq %rbp
jmp 0x4ee9
movsbl %al, %edi
callq 0x5048
cmpl %eax, %r15d
jge 0x4e10
xorl %ebx, %ebx
jmp 0x4ef0
movzbl 0x3(%r14), %ecx
leal -0x20(%rcx), %eax
andl $0x3, %ecx
leaq 0x1327(%rip), %rdx      # 0x6090
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
xorl %ecx, %ecx
testb $0x40, %al
sete %cl
leal -0x1b(,%rcx,4), %ecx
jmp 0x4e50
movb (%r14,%r12), %al
movb %al, 0x10(%rsp)
movb 0x2(%r14), %r15b
xorl %eax, %eax
cmpb $0x3c, %r15b
sete %al
leaq (%r14,%rax), %rdi
addq $0x2, %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x3230
leal -0x20(%rax), %r13d
cmpb $0x3c, %r15b
movslq %ebp, %rcx
leaq 0x1(%r14,%rcx), %rdi
cmovel %eax, %r13d
xorl %esi, %esi
pushq $0xa
popq %rbp
movl %ebp, %edx
callq 0x3230
movq %rax, %r15
movslq %ebx, %rax
leaq (%r14,%rax), %rdi
incq %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x3230
movl %r13d, %esi
movl %r13d, %ecx
andl $0x3, %ecx
leaq 0x128e(%rip), %rdx      # 0x6080
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
xorl %ecx, %ecx
testb $0x40, %sil
sete %cl
leal -0x1b(,%rcx,4), %ecx
jmp 0x4e99
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x5058
andw $0x0, 0x2(%rbx)
movsbl (%r14), %edi
callq 0x5048
movl %eax, %ebp
movb $0x1, %bl
jmp 0x4ee9
movw $0xffe8, %cx            # imm = 0xFFE8
jmp 0x4e50
movw $0xffe6, %cx            # imm = 0xFFE6
jmp 0x4e50
xorl %ecx, %ecx
testb $0x40, %al
sete %cl
leal (%rcx,%rcx,2), %ecx
orl $0xffe4, %ecx           # imm = 0xFFE4
movw %cx, 0x2(%rbx)
movb $0x3, (%rbx)
testb $0x20, %al
je 0x4e5f
orb $0x2, 0x1(%rbx)
movzbl 0x4(%r14), %eax
addl $-0x21, %eax
movl %eax, 0x10(%rbx)
movzbl 0x5(%r14), %eax
addl $-0x21, %eax
movl %eax, 0x14(%rbx)
pushq $0x6
popq %r12
jmp 0x4eda
movw $0xffe8, %cx            # imm = 0xFFE8
jmp 0x4e99
movw $0xffe6, %cx            # imm = 0xFFE6
jmp 0x4e99
xorl %ecx, %ecx
testb $0x40, %sil
sete %cl
leal (%rcx,%rcx,2), %ecx
orl $0xffe4, %ecx           # imm = 0xFFE4
movq 0x8(%rsp), %r13
cmpb $0x4d, 0x10(%rsp)
movl $0xffe6, %edx           # imm = 0xFFE6
cmovel %ecx, %edx
movw %dx, 0x2(%r13)
movb $0x3, (%r13)
testb $0x20, %sil
je 0x4ec0
orb $0x2, 0x1(%r13)
movl $0xff, %ecx
andl %ecx, %r15d
decl %r15d
movl %r15d, 0x10(%r13)
andl %ecx, %eax
decl %eax
movl %eax, 0x14(%r13)
incl %r12d
testl %r12d, %r12d
setns %bl
movl %r12d, %ebp
negl %ebp
cmovsl %r12d, %ebp
movl %ebp, %edi
callq 0x4fdc
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14b6(%rip), %rdi      # 0x63be
leaq 0x1467(%rip), %rsi      # 0x6376
leaq 0x14ba(%rip), %rcx      # 0x63d0
movl $0x299, %edx            # imm = 0x299
callq 0x30e0