// File: /workspace/github_c_makefile_star2_O0/termbox[P]termbox/termbox.c
// Function: parse_mouse_event
static int parse_mouse_event(struct tb_event *event, const char *buf, int len) {
    if (len >= 6 && starts_with(buf, len, "\033[M")) {
        // X10 mouse encoding, the simplest one
        // \033 [ M Cb Cx Cy
        int b = buf[3] - 32;
        switch (b & 3) {
        case 0:
            if ((b & 64) != 0)
                event->key = TB_KEY_MOUSE_WHEEL_UP;
            else
                event->key = TB_KEY_MOUSE_LEFT;
            break;
        case 1:
            if ((b & 64) != 0)
                event->key = TB_KEY_MOUSE_WHEEL_DOWN;
            else
                event->key = TB_KEY_MOUSE_MIDDLE;
            break;
        case 2:
            event->key = TB_KEY_MOUSE_RIGHT;
            break;
        case 3:
            event->key = TB_KEY_MOUSE_RELEASE;
            break;
        default:
            return -6;
        }
        event->type = TB_EVENT_MOUSE; // TB_EVENT_KEY by default
        if ((b & 32) != 0)
            event->mod |= TB_MOD_MOTION;

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

        return 6;
    } else if (starts_with(buf, len, "\033[<") ||
               starts_with(buf, len, "\033[")) {
        // xterm 1006 extended mode or urxvt 1015 extended mode
        // xterm: \033 [ < Cb ; Cx ; Cy (M or m)
        // urxvt: \033 [ Cb ; Cx ; Cy M
        int i, mi = -1, starti = -1;
        int isM, isU, s1 = -1, s2 = -1;
        int n1 = 0, n2 = 0, n3 = 0;

        for (i = 0; i < len; i++) {
            // We search the first (s1) and the last (s2) ';'
            if (buf[i] == ';') {
                if (s1 == -1)
                    s1 = i;
                s2 = i;
            }

            // We search for the first 'm' or 'M'
            if ((buf[i] == 'm' || buf[i] == 'M') && mi == -1) {
                mi = i;
                break;
            }
        }
        if (mi == -1)
            return 0;

        // whether it's a capital M or not
        isM = (buf[mi] == 'M');

        if (buf[2] == '<') {
            isU = 0;
            starti = 3;
        } else {
            isU = 1;
            starti = 2;
        }

        if (s1 == -1 || s2 == -1 || s1 == s2)
            return 0;

        n1 = strtoul(&buf[starti], NULL, 10);
        n2 = strtoul(&buf[s1 + 1], NULL, 10);
        n3 = strtoul(&buf[s2 + 1], NULL, 10);

        if (isU)
            n1 -= 32;

        switch (n1 & 3) {
        case 0:
            if ((n1 & 64) != 0) {
                event->key = TB_KEY_MOUSE_WHEEL_UP;
            } else {
                event->key = TB_KEY_MOUSE_LEFT;
            }
            break;
        case 1:
            if ((n1 & 64) != 0) {
                event->key = TB_KEY_MOUSE_WHEEL_DOWN;
            } else {
                event->key = TB_KEY_MOUSE_MIDDLE;
            }
            break;
        case 2:
            event->key = TB_KEY_MOUSE_RIGHT;
            break;
        case 3:
            event->key = TB_KEY_MOUSE_RELEASE;
            break;
        default:
            return mi + 1;
        }

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

        event->type = TB_EVENT_MOUSE; // TB_EVENT_KEY by default
        if ((n1 & 32) != 0)
            event->mod |= TB_MOD_MOTION;

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

        return mi + 1;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpl $0x6, -0x1c(%rbp)
jl 0x60dc
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x1436(%rip), %rdx      # 0x7411
callq 0x6390
testb $0x1, %al
jne 0x5fe9
jmp 0x60dc
movq -0x18(%rbp), %rax
movsbl 0x3(%rax), %eax
addl $-0x20, %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
andl $0x3, %eax
movl %eax, %ecx
movq %rcx, -0x50(%rbp)
subl $0x3, %eax
ja 0x607a
movq -0x50(%rbp), %rax
leaq 0x107d(%rip), %rcx      # 0x7090
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x20(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x6033
movq -0x10(%rbp), %rax
movw $0xffe5, 0x2(%rax)      # imm = 0xFFE5
jmp 0x603d
movq -0x10(%rbp), %rax
movw $0xffe9, 0x2(%rax)      # imm = 0xFFE9
jmp 0x6086
movl -0x20(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x6056
movq -0x10(%rbp), %rax
movw $0xffe4, 0x2(%rax)      # imm = 0xFFE4
jmp 0x6060
movq -0x10(%rbp), %rax
movw $0xffe7, 0x2(%rax)      # imm = 0xFFE7
jmp 0x6086
movq -0x10(%rbp), %rax
movw $0xffe8, 0x2(%rax)      # imm = 0xFFE8
jmp 0x6086
movq -0x10(%rbp), %rax
movw $0xffe6, 0x2(%rax)      # imm = 0xFFE6
jmp 0x6086
movl $0xfffffffa, -0x4(%rbp) # imm = 0xFFFFFFFA
jmp 0x6383
movq -0x10(%rbp), %rax
movb $0x3, (%rax)
movl -0x20(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x60a6
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %ecx
orl $0x2, %ecx
movb %cl, 0x1(%rax)
movq -0x18(%rbp), %rax
movzbl 0x4(%rax), %ecx
subl $0x1, %ecx
subl $0x20, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x18(%rbp), %rax
movzbl 0x5(%rax), %ecx
subl $0x1, %ecx
subl $0x20, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movl $0x6, -0x4(%rbp)
jmp 0x6383
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x132b(%rip), %rdx      # 0x7415
callq 0x6390
testb $0x1, %al
jne 0x610f
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x1267(%rip), %rdx      # 0x7368
callq 0x6390
testb $0x1, %al
jne 0x610f
jmp 0x637a
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x61af
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x3b, %eax
jne 0x6172
cmpl $-0x1, -0x38(%rbp)
jne 0x616c
movl -0x24(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x6d, %eax
je 0x6194
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x4d, %eax
jne 0x61a2
cmpl $-0x1, -0x28(%rbp)
jne 0x61a2
movl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x61af
jmp 0x61a4
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x6147
cmpl $-0x1, -0x28(%rbp)
jne 0x61c1
movl $0x0, -0x4(%rbp)
jmp 0x6383
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x4d, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x3c, %eax
jne 0x61f8
movl $0x0, -0x34(%rbp)
movl $0x3, -0x2c(%rbp)
jmp 0x6206
movl $0x1, -0x34(%rbp)
movl $0x2, -0x2c(%rbp)
cmpl $-0x1, -0x38(%rbp)
je 0x621a
cmpl $-0x1, -0x3c(%rbp)
je 0x621a
movl -0x38(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jne 0x6226
movl $0x0, -0x4(%rbp)
jmp 0x6383
movq -0x18(%rbp), %rdi
movslq -0x2c(%rbp), %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x3230
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movl -0x38(%rbp), %eax
addl $0x1, %eax
cltq
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x3230
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rdi
movl -0x3c(%rbp), %eax
addl $0x1, %eax
cltq
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x3230
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x6291
movl -0x40(%rbp), %eax
subl $0x20, %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
andl $0x3, %eax
movl %eax, %ecx
movq %rcx, -0x58(%rbp)
subl $0x3, %eax
ja 0x6314
movq -0x58(%rbp), %rax
leaq 0xdd3(%rip), %rcx       # 0x7080
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x40(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x62cd
movq -0x10(%rbp), %rax
movw $0xffe5, 0x2(%rax)      # imm = 0xFFE5
jmp 0x62d7
movq -0x10(%rbp), %rax
movw $0xffe9, 0x2(%rax)      # imm = 0xFFE9
jmp 0x631f
movl -0x40(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x62f0
movq -0x10(%rbp), %rax
movw $0xffe4, 0x2(%rax)      # imm = 0xFFE4
jmp 0x62fa
movq -0x10(%rbp), %rax
movw $0xffe7, 0x2(%rax)      # imm = 0xFFE7
jmp 0x631f
movq -0x10(%rbp), %rax
movw $0xffe8, 0x2(%rax)      # imm = 0xFFE8
jmp 0x631f
movq -0x10(%rbp), %rax
movw $0xffe6, 0x2(%rax)      # imm = 0xFFE6
jmp 0x631f
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x6383
cmpl $0x0, -0x30(%rbp)
jne 0x632f
movq -0x10(%rbp), %rax
movw $0xffe6, 0x2(%rax)      # imm = 0xFFE6
movq -0x10(%rbp), %rax
movb $0x3, (%rax)
movl -0x40(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x634f
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %ecx
orl $0x2, %ecx
movb %cl, 0x1(%rax)
movl -0x44(%rbp), %eax
movzbl %al, %ecx
subl $0x1, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x48(%rbp), %eax
movzbl %al, %ecx
subl $0x1, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x6383
jmp 0x637c
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)