// File: /workspace/github_c_makefile_star2_O3/doy[P]libvt100/src/screen.c
// Function: vt100_screen_format_mouse_reporting_response
int vt100_screen_format_mouse_reporting_response(
    VT100Screen *vt, char *buf, size_t len, struct vt100_loc loc,
    int event_type, int button, int shift, int alt, int ctrl)
{
    unsigned char status;
    int x, y;

    if (button > VT100_BUTTON_RIGHT) {
        /* don't care about button release events for scroll wheel */
        if (event_type == VT100_BUTTONEVENT_RELEASE) {
            return 0;
        }

        /* don't care about scroll wheel events for x10 mode */
        if (!vt100_screen_mouse_reporting_wants_button_release(vt)) {
            return 0;
        }
    }

    if (event_type == VT100_BUTTONEVENT_RELEASE) {
        status = 3;
    }
    else {
        switch (button) {
        case VT100_BUTTON_LEFT:
            status = 0;
            break;
        case VT100_BUTTON_MIDDLE:
            status = 1;
            break;
        case VT100_BUTTON_RIGHT:
            status = 2;
            break;
        case VT100_BUTTON_SCROLL_UP:
            status = 64;
            break;
        case VT100_BUTTON_SCROLL_DOWN:
            status = 65;
            break;
        case VT100_BUTTON_NONE:
            status = 3;
            break;
        default:
            return 0;
        }
    }

    /* don't care about modifier keys in x10 mode */
    if (vt100_screen_mouse_reporting_wants_button_release(vt)) {
        if (shift) {
            status |= 4;
        }
        if (alt) {
            status |= 8;
        }
        if (ctrl) {
            status |= 16;
        }
    }

    if (event_type == VT100_BUTTONEVENT_MOTION) {
        status += 32;
    }

    status += 32;
    x = loc.col + 1 + 32;
    y = loc.row + 1 + 32;

    switch (vt->mouse_reporting_mode) {
    case VT100_MOUSEREPORTING_NORMAL:
        if (x > 255 || y > 255) {
            return 0;
        }

        return snprintf(buf, len, "\033[M%c%c%c", status, x, y);
    case VT100_MOUSEREPORTING_UTF8: {
        char status_bytes[2], x_bytes[2], y_bytes[2];
        int status_len, x_len, y_len;

        if (status > 0x7F) {
            status_bytes[0] = 0xC0 | (status >> 3);
            status_bytes[1] = 0x80 | (status & 0x6F);
            status_len = 2;
        }
        else {
            status_bytes[0] = status;
            status_len = 1;
        }

        if (x > 0x7F) {
            x_bytes[0] = 0xC0 | (x >> 6);
            x_bytes[1] = 0x80 | (x & 0x3F);
            x_len = 2;
        }
        else {
            x_bytes[0] = x;
            x_len = 1;
        }

        if (y > 0x7F) {
            y_bytes[0] = 0xC0 | (y >> 6);
            y_bytes[1] = 0x80 | (y & 0x3F);
            y_len = 2;
        }
        else {
            y_bytes[0] = y;
            y_len = 1;
        }

        return snprintf(
            buf, len, "\033[M%.*s%.*s%.*s", status_len, status_bytes, x_len,
            x_bytes, y_len, y_bytes);
    }
    case VT100_MOUSEREPORTING_SGR:
        return snprintf(
            buf, len, "\033[<%d;%d;%d%c", status - 32, x - 32, y - 32,
            event_type == VT100_BUTTONEVENT_RELEASE ? 'm' : 'M');
    default:
        return 0;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %r8d, %r10d
movq %rcx, %rbx
movl 0x40(%rsp), %ebp
movl 0x38(%rsp), %r14d
movl 0x30(%rsp), %r15d
cmpl $0x4, %r9d
jl 0x7590
xorl %eax, %eax
cmpl $0x1, %r10d
je 0x775d
testb $0x70, 0x49(%rdi)
setne %cl
cmpl $0x6, %r9d
setb %r8b
testb %r8b, %cl
jne 0x75ac
jmp 0x775d
cmpl $0x1, %r10d
jne 0x75a0
movb $0x3, %al
movl $0x6d, %r11d
jmp 0x75c6
xorl %eax, %eax
cmpl $0x5, %r9d
ja 0x775d
shll $0x3, %r9d
movabsq $0x414002010003, %rax   # imm = 0x414002010003
movl %r9d, %ecx
shrq %cl, %rax
movl $0x4d, %r11d
movq %rbx, %r8
shrq $0x20, %r8
testb $0x70, 0x49(%rdi)
je 0x75fb
movzbl %al, %ecx
orb $0x4, %al
testl %r15d, %r15d
movzbl %al, %eax
cmovel %ecx, %eax
movl %eax, %ecx
orb $0x8, %cl
testl %r14d, %r14d
movzbl %cl, %ecx
cmovel %eax, %ecx
movl %ecx, %eax
orb $0x10, %al
testl %ebp, %ebp
movzbl %al, %eax
cmovel %ecx, %eax
leal 0x20(%rax), %ecx
cmpl $0x2, %r10d
movzbl %al, %eax
movzbl %cl, %ebp
cmovnel %eax, %ebp
movzbl 0x48(%rdi), %ecx
cmpl $0x2, %ecx
je 0x76be
addb $0x20, %bpl
leal 0x21(%r8), %r10d
leal 0x21(%rbx), %r9d
cmpl $0x1, %ecx
je 0x7675
xorl %eax, %eax
testl %ecx, %ecx
jne 0x775d
movl $0xdf, %ecx
cmpl %ecx, %r8d
setge %dil
cmpl %ecx, %ebx
setge %cl
orb %dil, %cl
jne 0x775d
movzbl %bpl, %ecx
leaq 0x2315(%rip), %rax      # 0x996d
movq %rsi, %rdi
movq %rdx, %rsi
movq %rax, %rdx
movl %r10d, %r8d
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x3270
movl $0x1, %ecx
testb %bpl, %bpl
jns 0x7696
movl %ebp, %eax
shrb $0x3, %al
orb $-0x40, %al
andb $-0x11, %bpl
movb %bpl, 0x5(%rsp)
movl $0x2, %ecx
movl %eax, %ebp
movb %bpl, 0x4(%rsp)
cmpl $0x5f, %r8d
jl 0x76ed
movl %r10d, %eax
shrl $0x6, %eax
orb $-0x40, %al
andb $0x3f, %r10b
orb $-0x80, %r10b
movb %r10b, 0x3(%rsp)
movl $0x2, %r11d
jmp 0x76f6
incl %ebx
incl %r8d
movzbl %bpl, %ecx
subq $0x8, %rsp
leaq 0x22b5(%rip), %rax      # 0x9987
movq %rsi, %rdi
movq %rdx, %rsi
movq %rax, %rdx
movl %ebx, %r9d
xorl %eax, %eax
pushq %r11
callq 0x3270
addq $0x18, %rsp
jmp 0x7761
movl $0x1, %r11d
movl %r10d, %eax
movb %al, 0x2(%rsp)
cmpl $0x5f, %ebx
jl 0x771c
movl %r9d, %eax
shrl $0x6, %eax
orb $-0x40, %al
andb $0x3f, %r9b
orb $-0x80, %r9b
movb %r9b, 0x7(%rsp)
movl $0x2, %r10d
jmp 0x7725
movl $0x1, %r10d
movl %r9d, %eax
leaq 0x6(%rsp), %rbx
movb %al, (%rbx)
subq $0x8, %rsp
leaq 0xa(%rsp), %r14
leaq 0x223b(%rip), %rax      # 0x9977
leaq 0xc(%rsp), %r8
movq %rsi, %rdi
movq %rdx, %rsi
movq %rax, %rdx
movl %r11d, %r9d
xorl %eax, %eax
pushq %rbx
pushq %r10
pushq %r14
callq 0x3270
addq $0x20, %rsp
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq