// File: /workspace/github_c_makefile_star2_O2/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 0x70e5
xorl %eax, %eax
cmpl $0x1, %r10d
je 0x72a1
testb $0x70, 0x49(%rdi)
setne %cl
cmpl $0x6, %r9d
setb %r8b
testb %r8b, %cl
jne 0x70fd
jmp 0x72a1
cmpl $0x1, %r10d
jne 0x70f1
movb $0x3, %al
pushq $0x6d
jmp 0x7113
xorl %eax, %eax
cmpl $0x5, %r9d
ja 0x72a1
shll $0x3, %r9d
movabsq $0x414002010003, %rax   # imm = 0x414002010003
movl %r9d, %ecx
shrq %cl, %rax
pushq $0x4d
popq %r11
movq %rbx, %r8
shrq $0x20, %r8
testb $0x70, 0x49(%rdi)
je 0x714a
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 0x71ca
addb $0x20, %bpl
leal 0x21(%r8), %r10d
leal 0x21(%rbx), %r9d
cmpl $0x1, %ecx
je 0x71c0
xorl %eax, %eax
testl %ecx, %ecx
jne 0x72a1
movl $0xdf, %ecx
cmpl %ecx, %r8d
setge %dil
cmpl %ecx, %ebx
setge %cl
orb %dil, %cl
jne 0x72a1
movzbl %bpl, %ecx
leaq 0x27aa(%rip), %rax      # 0x994d
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
testb %bpl, %bpl
js 0x71fc
pushq $0x1
popq %rcx
jmp 0x7211
incl %ebx
incl %r8d
movzbl %bpl, %ecx
subq $0x8, %rsp
leaq 0x2789(%rip), %rax      # 0x9967
movq %rsi, %rdi
movq %rdx, %rsi
movq %rax, %rdx
movl %ebx, %r9d
xorl %eax, %eax
pushq %r11
callq 0x3270
addq $0x18, %rsp
jmp 0x72a5
movl %ebp, %eax
shrb $0x3, %al
orb $-0x40, %al
andb $-0x11, %bpl
movb %bpl, 0x5(%rsp)
pushq $0x2
popq %rcx
movl %eax, %ebp
movb %bpl, 0x4(%rsp)
cmpl $0x5f, %r8d
jl 0x7237
movl %r10d, %eax
shrl $0x6, %eax
orb $-0x40, %al
andb $0x3f, %r10b
orb $-0x80, %r10b
movb %r10b, 0x3(%rsp)
pushq $0x2
popq %r11
jmp 0x723e
pushq $0x1
popq %r11
movl %r10d, %eax
movb %al, 0x2(%rsp)
cmpl $0x5f, %ebx
jl 0x7262
pushq $0x2
popq %r10
movl %r9d, %eax
shrl $0x6, %eax
orb $-0x40, %al
andb $0x3f, %r9b
orb $-0x80, %r9b
movb %r9b, 0x7(%rsp)
jmp 0x7269
pushq $0x1
popq %r10
movl %r9d, %eax
leaq 0x6(%rsp), %rbx
movb %al, (%rbx)
subq $0x8, %rsp
leaq 0xa(%rsp), %r14
leaq 0x26d7(%rip), %rax      # 0x9957
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