// File: /workspace/github_c_makefile_star2_O0/adsr[P]phpspy/vendor/termbox2/termbox2.h
// Function: extract_esc_mouse
static int extract_esc_mouse(struct tb_event *event) {
    struct bytebuf_t *in = &global.in;

    enum type { TYPE_VT200 = 0, TYPE_1006, TYPE_1015, TYPE_MAX };

    char *cmp[TYPE_MAX] = {//
        // X10 mouse encoding, the simplest one
        // \x1b [ M Cb Cx Cy
        [TYPE_VT200] = "\x1b[M",
        // xterm 1006 extended mode or urxvt 1015 extended mode
        // xterm: \x1b [ < Cb ; Cx ; Cy (M or m)
        [TYPE_1006] = "\x1b[<",
        // urxvt: \x1b [ Cb ; Cx ; Cy M
        [TYPE_1015] = "\x1b["};

    enum type type = 0;
    int ret = TB_ERR;

    // Unrolled at compile-time (probably)
    for (; type < TYPE_MAX; type++) {
        size_t size = strlen(cmp[type]);

        if (in->len >= size && (strncmp(cmp[type], in->buf, size)) == 0) {
            break;
        }
    }

    if (type == TYPE_MAX) {
        ret = TB_ERR; // No match
        return ret;
    }

    size_t buf_shift = 0;

    switch (type) {
        case TYPE_VT200:
            if (in->len >= 6) {
                int b = in->buf[3] - 0x20;
                int fail = 0;

                switch (b & 3) {
                    case 0:
                        event->key = ((b & 64) != 0) ? TB_KEY_MOUSE_WHEEL_UP
                                                     : TB_KEY_MOUSE_LEFT;
                        break;
                    case 1:
                        event->key = ((b & 64) != 0) ? TB_KEY_MOUSE_WHEEL_DOWN
                                                     : TB_KEY_MOUSE_MIDDLE;
                        break;
                    case 2:
                        event->key = TB_KEY_MOUSE_RIGHT;
                        break;
                    case 3:
                        event->key = TB_KEY_MOUSE_RELEASE;
                        break;
                    default:
                        ret = TB_ERR;
                        fail = 1;
                        break;
                }

                if (!fail) {
                    if ((b & 32) != 0) {
                        event->mod |= TB_MOD_MOTION;
                    }

                    // the coord is 1,1 for upper left
                    event->x = ((uint8_t)in->buf[4]) - 0x21;
                    event->y = ((uint8_t)in->buf[5]) - 0x21;

                    ret = TB_OK;
                }

                buf_shift = 6;
            }
            break;
        case TYPE_1006:
            // fallthrough
        case TYPE_1015: {
            size_t index_fail = (size_t)-1;

            enum {
                FIRST_M = 0,
                FIRST_SEMICOLON,
                LAST_SEMICOLON,
                FIRST_LAST_MAX
            };

            size_t indices[FIRST_LAST_MAX] = {index_fail, index_fail,
                index_fail};
            int m_is_capital = 0;

            for (size_t i = 0; i < in->len; i++) {
                if (in->buf[i] == ';') {
                    if (indices[FIRST_SEMICOLON] == index_fail) {
                        indices[FIRST_SEMICOLON] = i;
                    } else {
                        indices[LAST_SEMICOLON] = i;
                    }
                } else if (indices[FIRST_M] == index_fail) {
                    if (in->buf[i] == 'm' || in->buf[i] == 'M') {
                        m_is_capital = (in->buf[i] == 'M');
                        indices[FIRST_M] = i;
                    }
                }
            }

            if (indices[FIRST_M] == index_fail ||
                indices[FIRST_SEMICOLON] == index_fail ||
                indices[LAST_SEMICOLON] == index_fail)
            {
                ret = TB_ERR;
            } else {
                int start = (type == TYPE_1015 ? 2 : 3);

                unsigned n1 = strtoul(&in->buf[start], NULL, 10);
                unsigned n2 =
                    strtoul(&in->buf[indices[FIRST_SEMICOLON] + 1], NULL, 10);
                unsigned n3 =
                    strtoul(&in->buf[indices[LAST_SEMICOLON] + 1], NULL, 10);

                if (type == TYPE_1015) {
                    n1 -= 0x20;
                }

                int fail = 0;

                switch (n1 & 3) {
                    case 0:
                        event->key = ((n1 & 64) != 0) ? TB_KEY_MOUSE_WHEEL_UP
                                                      : TB_KEY_MOUSE_LEFT;
                        break;
                    case 1:
                        event->key = ((n1 & 64) != 0) ? TB_KEY_MOUSE_WHEEL_DOWN
                                                      : TB_KEY_MOUSE_MIDDLE;
                        break;
                    case 2:
                        event->key = TB_KEY_MOUSE_RIGHT;
                        break;
                    case 3:
                        event->key = TB_KEY_MOUSE_RELEASE;
                        break;
                    default:
                        ret = TB_ERR;
                        fail = 1;
                        break;
                }

                buf_shift = in->len;

                if (!fail) {
                    if (!m_is_capital) {
                        // on xterm mouse release is signaled by lowercase m
                        event->key = TB_KEY_MOUSE_RELEASE;
                    }

                    if ((n1 & 32) != 0) {
                        event->mod |= TB_MOD_MOTION;
                    }

                    event->x = ((uint8_t)n2) - 1;
                    event->y = ((uint8_t)n3) - 1;

                    ret = TB_OK;
                }
            }
        } break;
        case TYPE_MAX:
            ret = TB_ERR;
    }

    if (buf_shift > 0) {
        bytebuf_shift(in, buf_shift);
    }

    if (ret == TB_OK) {
        event->type = TB_EVENT_MOUSE;
    }

    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
leaq 0x2e6f2(%rip), %rax     # 0x3be98
addq $0x190, %rax            # imm = 0x190
movq %rax, -0x18(%rbp)
movq 0x2c1b9(%rip), %rax     # 0x39970
movq %rax, -0x30(%rbp)
movq 0x2c1b6(%rip), %rax     # 0x39978
movq %rax, -0x28(%rbp)
movq 0x2c1b3(%rip), %rax     # 0x39980
movq %rax, -0x20(%rbp)
movl $0x0, -0x34(%rbp)
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
cmpl $0x3, -0x34(%rbp)
jae 0xd830
movl -0x34(%rbp), %eax
movq -0x30(%rbp,%rax,8), %rdi
callq 0x6160
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
cmpq -0x40(%rbp), %rax
jb 0xd823
movl -0x34(%rbp), %eax
movq -0x30(%rbp,%rax,8), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x40(%rbp), %rdx
callq 0x6080
cmpl $0x0, %eax
jne 0xd823
jmp 0xd830
jmp 0xd825
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xd7df
cmpl $0x3, -0x34(%rbp)
jne 0xd848
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xdc98
movq $0x0, -0x48(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x98(%rbp)
testl %eax, %eax
je 0xd886
jmp 0xd85f
movl -0x98(%rbp), %eax
decl %eax
subl $0x2, %eax
jb 0xd9a1
jmp 0xd872
movl -0x98(%rbp), %eax
subl $0x3, %eax
je 0xdc6a
jmp 0xdc71
movq -0x18(%rbp), %rax
cmpq $0x6, 0x8(%rax)
jb 0xd99c
movq -0x18(%rbp), %rax
movq (%rax), %rax
movsbl 0x3(%rax), %eax
addl $-0x20, %eax
movl %eax, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x4c(%rbp), %eax
andl $0x3, %eax
movl %eax, %ecx
movq %rcx, -0xa0(%rbp)
subl $0x3, %eax
ja 0xd936
movq -0xa0(%rbp), %rax
leaq 0x24a95(%rip), %rcx     # 0x32364
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x4c(%rbp), %edx
andl $0x40, %edx
movl $0xffe8, %eax           # imm = 0xFFE8
movl $0xffe4, %ecx           # imm = 0xFFE4
cmpl $0x0, %edx
cmovnel %ecx, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2(%rax)
jmp 0xd944
movl -0x4c(%rbp), %edx
andl $0x40, %edx
movl $0xffe6, %eax           # imm = 0xFFE6
movl $0xffe3, %ecx           # imm = 0xFFE3
cmpl $0x0, %edx
cmovnel %ecx, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2(%rax)
jmp 0xd944
movq -0x10(%rbp), %rax
movw $0xffe7, 0x2(%rax)      # imm = 0xFFE7
jmp 0xd944
movq -0x10(%rbp), %rax
movw $0xffe5, 0x2(%rax)      # imm = 0xFFE5
jmp 0xd944
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jne 0xd994
movl -0x4c(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xd963
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %ecx
orl $0x8, %ecx
movb %cl, 0x1(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movzbl 0x4(%rax), %ecx
subl $0x21, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movzbl 0x5(%rax), %ecx
subl $0x21, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movl $0x0, -0x38(%rbp)
movq $0x6, -0x48(%rbp)
jmp 0xdc71
movq $-0x1, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x74(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0xda82
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x80(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x3b, %eax
jne 0xda14
movq -0x68(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0xda0a
movq -0x80(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0xda12
movq -0x80(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0xda6f
movq -0x70(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0xda6d
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x80(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x6d, %eax
je 0xda46
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x80(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x4d, %eax
jne 0xda6b
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x80(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x4d, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x74(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0xda6d
jmp 0xda6f
jmp 0xda71
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0xd9d0
movq -0x70(%rbp), %rax
cmpq -0x58(%rbp), %rax
je 0xdaa0
movq -0x68(%rbp), %rax
cmpq -0x58(%rbp), %rax
je 0xdaa0
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0xdaac
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
jmp 0xdc68
movl -0x34(%rbp), %edx
movl $0x3, %eax
movl $0x2, %ecx
cmpl $0x2, %edx
cmovel %ecx, %eax
movl %eax, -0x84(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movslq -0x84(%rbp), %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x6550
movl %eax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x68(%rbp), %rax
addq $0x1, %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x6550
movl %eax, -0x8c(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x60(%rbp), %rax
addq $0x1, %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x6550
movl %eax, -0x90(%rbp)
cmpl $0x2, -0x34(%rbp)
jne 0xdb4b
movl -0x88(%rbp), %eax
subl $0x20, %eax
movl %eax, -0x88(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x88(%rbp), %eax
andl $0x3, %eax
movl %eax, %ecx
movq %rcx, -0xa8(%rbp)
subl $0x3, %eax
ja 0xdbe7
movq -0xa8(%rbp), %rax
leaq 0x247da(%rip), %rcx     # 0x32354
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x88(%rbp), %edx
andl $0x40, %edx
movl $0xffe8, %eax           # imm = 0xFFE8
movl $0xffe4, %ecx           # imm = 0xFFE4
cmpl $0x0, %edx
cmovnel %ecx, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2(%rax)
jmp 0xdbf8
movl -0x88(%rbp), %edx
andl $0x40, %edx
movl $0xffe6, %eax           # imm = 0xFFE6
movl $0xffe3, %ecx           # imm = 0xFFE3
cmpl $0x0, %edx
cmovnel %ecx, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2(%rax)
jmp 0xdbf8
movq -0x10(%rbp), %rax
movw $0xffe7, 0x2(%rax)      # imm = 0xFFE7
jmp 0xdbf8
movq -0x10(%rbp), %rax
movw $0xffe5, 0x2(%rax)      # imm = 0xFFE5
jmp 0xdbf8
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x94(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x94(%rbp)
jne 0xdc66
cmpl $0x0, -0x74(%rbp)
jne 0xdc1d
movq -0x10(%rbp), %rax
movw $0xffe5, 0x2(%rax)      # imm = 0xFFE5
movl -0x88(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xdc39
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %ecx
orl $0x8, %ecx
movb %cl, 0x1(%rax)
movl -0x8c(%rbp), %eax
movzbl %al, %ecx
subl $0x1, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x90(%rbp), %eax
movzbl %al, %ecx
subl $0x1, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movl $0x0, -0x38(%rbp)
jmp 0xdc68
jmp 0xdc71
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
cmpq $0x0, -0x48(%rbp)
jbe 0xdc85
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xd5a0
cmpl $0x0, -0x38(%rbp)
jne 0xdc92
movq -0x10(%rbp), %rax
movb $0x3, (%rax)
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)