// File: /workspace/github_c_makefile_star2_O3/glitchub[P]evdump/evdump.c
// Function: main
int main(int argc, char*argv[])
{
    int ndevs;
    struct pollfd dev[MAXDEVS];
    char *name[MAXDEVS];
    bool ismouse[MAXDEVS]; // true if the device generates three-octet event codes

    while (1) switch (getopt(argc,argv,":t:c:v:"))
    {
        case 't': onlytype=strtoul(optarg,NULL,0); break;
        case 'c': onlycode=strtoul(optarg,NULL,0); break;
        case 'v': onlyvalue=strtoul(optarg,NULL,0); break;

        case ':':            // missing
        case '?': usage();   // or invalid options
        case -1: goto optx;  // no more options
    } optx:
    argc-=optind-1;
    argv+=optind-1;

    if (argc < 2) usage();

    for (ndevs=0; ndevs < argc-1; ndevs++)
    {
        char *paths[] = {"%s", "/dev/%s", "/dev/input/%s", "/dev/input/event%s", NULL };
        if (ndevs >= MAXDEVS) die("Can't specify more than %d devices\n", MAXDEVS);
        for (char**p=paths;;p++)
        {
            if (!*p) die("Can't find event device '%s'\n", argv[ndevs+1]);
            asprintf(&name[ndevs], *p, argv[ndevs+1]);
            if ((dev[ndevs].fd = open(name[ndevs], O_RDWR))>=0)
            {
                ismouse[ndevs] = (strstr(name[ndevs],"mouse") || strstr(name[ndevs],"mice"));

                if (!ismouse[ndevs])
                {
                    int version;
                    char dn[64];
                    struct input_id id;
                    if (ioctl(dev[ndevs].fd,EVIOCGVERSION,&version) == -1) die("%s EVIOCGVERSION failed: %s\n", name[ndevs], strerror(errno));
                    if (version != EV_VERSION) die("%s unsupported event version %d\n", name[ndevs], version);
                    if (ioctl(dev[ndevs].fd,EVIOCGNAME(sizeof dn),dn) == -1) die("%s: EVIOCGNAME failed: %s\n", name[ndevs], strerror(errno));
                    if (ioctl(dev[ndevs].fd,EVIOCGID,&id) == -1) die("%s EVIOCGID failed: %s\n", name[ndevs], strerror(errno));
                    warn("%s name='%s' bus=%d (%s) vendor=%d product=%d version=%d\n", name[ndevs], dn, id.bustype, ed_BUS(id.bustype)?:"unknown", id.vendor, id.product, id.version);
                }
                else
                {
                    warn("%s name='mouse'\n", name[ndevs]);
                }
                dev[ndevs].events=POLLIN;
                dev[ndevs].revents=0;
                break;
            }
            if (errno==EACCES) die("%s permission denied (are you root?)\n",name[ndevs]);
            free(name[ndevs]);
        }
    }

    while (poll(dev, ndevs, -1) > 0)
    {
        for (int n=0; n < ndevs; n++)
        {
            if (dev[n].revents & POLLIN)
            {
                if (ismouse[n])
                {
                    // Simulate relative mouse:
                    //   EV_KEY, BTN_MOUSE|BTN_RIGHT|BTN_MIDDLE, 1 or 0 (for each key change)
                    //   EV_REL, REL_X, value
                    //   EV_REL, REL_Y, value
                    //   EV_SYN, SYN_REPORT, 0
                    static int8_t pressed=0;
                    struct timeval t;
                    int8_t bxy[3];
                    if (read(dev[n].fd, bxy, 3) != 3) die("%s read failed: %s\n", name[n], strerror(errno));
                    gettimeofday(&t, NULL);
                    int8_t change=(bxy[0] ^ pressed);
                    pressed=bxy[0];
                    if (change & 1) report(name[n], &t, EV_KEY, BTN_MOUSE, (bxy[0] & 1) != 0);
                    if (change & 2) report(name[n], &t, EV_KEY, BTN_RIGHT, (bxy[0] & 2) != 0);
                    if (change & 4) report(name[n], &t, EV_KEY, BTN_MIDDLE, (bxy[0] & 4) != 0);
                    if (bxy[1]) report(name[n], &t, EV_REL, REL_X, bxy[1]);
                    if (bxy[2]) report(name[n], &t, EV_REL, REL_Y, -bxy[2]); // y is negated
                    report(name[n], &t, EV_SYN, SYN_REPORT, 0);
                } else
                {
                    struct input_event e;
                    if (read(dev[n].fd, &e, sizeof e) != sizeof e) die("%s read failed: %s\n", name[n], strerror(errno));
                    report(name[n], &e.time, e.type, e.code, e.value);
                }
            }
            dev[n].revents=0;
        }
    }
    die("Poll failed: %s\n", strerror(errno));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rsi, %r14
movl %edi, %ebx
leaq 0x2d09(%rip), %r15      # 0x86f7
leaq 0x66bb(%rip), %r13      # 0xc0b0
movq 0x65d4(%rip), %rbp      # 0xbfd0
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x50e0
cmpl $0x62, %eax
jle 0x5a29
cmpl $0x76, %eax
je 0x5a3a
movq %r13, %r12
cmpl $0x74, %eax
je 0x5a41
cmpl $0x63, %eax
jne 0x59fc
leaq 0x668d(%rip), %r12      # 0xc0b4
jmp 0x5a41
cmpl $-0x1, %eax
je 0x5a54
cmpl $0x3a, %eax
je 0x5a69
cmpl $0x3f, %eax
jne 0x59fc
jmp 0x5a69
leaq 0x6677(%rip), %r12      # 0xc0b8
movq (%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x50f0
movl %eax, (%r12)
jmp 0x59fc
movq 0x656d(%rip), %rax      # 0xbfc8
movslq (%rax), %rax
leaq -0x1(%rax), %rcx
subl %ecx, %ebx
cmpl $0x2, %ebx
jge 0x5a93
movq 0x6580(%rip), %rax      # 0xbff0
movq (%rax), %rcx
leaq 0x2c85(%rip), %rdi      # 0x86ff
movl $0x221, %esi            # imm = 0x221
movl $0x1, %edx
callq 0x5110
movl $0x1, %edi
callq 0x5100
leaq (%r14,%rax,8), %rax
addq $-0x8, %rax
movq %rax, 0x18(%rsp)
movl $0x80044501, %eax       # imm = 0x80044501
decl %ebx
leaq 0x3c0005(%rax), %rcx
movq %rcx, 0x28(%rsp)
addq $0x40001, %rax          # imm = 0x40001
movq %rax, 0x20(%rsp)
xorl %r14d, %r14d
cmpq $0x10, %r14
je 0x5ec1
leaq (%rsp,%r14,8), %r13
addq $0x80, %r13
leaq 0x1(%r14), %r12
leaq 0x4557(%rip), %r15      # 0xa038
leaq 0x2e4c(%rip), %rsi      # 0x8934
movq 0x18(%rsp), %rax
movq (%rax,%r12,8), %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x50b0
movq 0x80(%rsp,%r14,8), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x50d0
movl %eax, 0x100(%rsp,%r14,8)
testl %eax, %eax
jns 0x5b43
callq 0x5040
cmpl $0xd, (%rax)
je 0x5e80
movq (%r13), %rdi
callq 0x5030
movq (%r15), %rsi
addq $0x8, %r15
testq %rsi, %rsi
jne 0x5ae8
jmp 0x5e97
movl %eax, %ebp
movq (%r13), %r15
movq %r15, %rdi
leaq 0x2e39(%rip), %rsi      # 0x898c
callq 0x5130
testq %rax, %rax
jne 0x5c4e
movq %r15, %rdi
leaq 0x2e27(%rip), %rsi      # 0x8992
callq 0x5130
testq %rax, %rax
setne 0x30(%rsp,%r14)
jne 0x5c54
movl $0x80044501, %esi       # imm = 0x80044501
movl %ebp, %edi
leaq 0x14(%rsp), %rdx
xorl %eax, %eax
callq 0x5070
cmpl $-0x1, %eax
je 0x5ee3
movl 0x14(%rsp), %ecx
cmpl $0x10001, %ecx          # imm = 0x10001
jne 0x5f2d
movl %ebp, %edi
movq 0x28(%rsp), %rsi
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x5070
cmpl $-0x1, %eax
je 0x5f4e
movl %ebp, %edi
movq 0x20(%rsp), %rsi
leaq 0xc(%rsp), %rdx
xorl %eax, %eax
callq 0x5070
movq 0x640f(%rip), %rcx      # 0xbff0
movq (%rcx), %rbp
movq (%r13), %r15
cmpl $-0x1, %eax
je 0x5f77
movzwl 0xc(%rsp), %r13d
movl %r13d, %edi
callq 0x570b
testq %rax, %rax
leaq 0x2ae6(%rip), %rcx      # 0x86ef
cmoveq %rcx, %rax
movzwl 0xe(%rsp), %r10d
movzwl 0x10(%rsp), %r11d
movq %r15, %rdx
movzwl 0x12(%rsp), %r15d
subq $0x8, %rsp
movq %rbp, %rdi
leaq 0x2dd8(%rip), %rsi      # 0x8a08
leaq 0x48(%rsp), %rcx
movl %r13d, %r8d
movq %rax, %r9
xorl %eax, %eax
pushq %r15
pushq %r11
pushq %r10
callq 0x5090
addq $0x20, %rsp
jmp 0x5c6f
movb $0x1, 0x30(%rsp,%r14)
movq 0x6395(%rip), %rax      # 0xbff0
movq (%rax), %rdi
leaq 0x2ddd(%rip), %rsi      # 0x8a42
movq %r15, %rdx
xorl %eax, %eax
callq 0x5090
movl $0x1, 0x104(%rsp,%r14,8)
movq %r12, %r14
cmpq %rbx, %r12
jne 0x5ac1
leaq 0x100(%rsp), %rdi
movq %rbx, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x50c0
testl %eax, %eax
jle 0x5e5b
xorl %r13d, %r13d
leaq 0x100(%rsp), %r14
leaq 0x40(%rsp), %r12
testb $0x1, 0x106(%rsp,%r13,8)
je 0x5e26
cmpb $0x1, 0x30(%rsp,%r13)
jne 0x5de8
movl 0x100(%rsp,%r13,8), %edi
movl $0x3, %edx
leaq 0xc(%rsp), %rsi
callq 0x5080
cmpq $0x3, %rax
jne 0x5f06
movq %r12, %rdi
xorl %esi, %esi
callq 0x5060
movzbl 0xc(%rsp), %ebp
movb 0x63b7(%rip), %r15b     # 0xc0bd
xorb %bpl, %r15b
movb %bpl, 0x63ad(%rip)      # 0xc0bd
testb $0x1, %r15b
je 0x5d37
movq 0x80(%rsp,%r13,8), %rdi
movl %ebp, %r8d
andl $0x1, %r8d
movq %r12, %rsi
movl $0x1, %edx
movl $0x110, %ecx            # imm = 0x110
callq 0x58d7
testb $0x2, %r15b
je 0x5d61
movq 0x80(%rsp,%r13,8), %rdi
movl %ebp, %eax
shrb %al
andb $0x1, %al
movzbl %al, %r8d
movq %r12, %rsi
movl $0x1, %edx
movl $0x111, %ecx            # imm = 0x111
callq 0x58d7
testb $0x4, %r15b
je 0x5d8a
movq 0x80(%rsp,%r13,8), %rdi
shrl $0x2, %ebp
andl $0x1, %ebp
movq %r12, %rsi
movl $0x1, %edx
movl $0x112, %ecx            # imm = 0x112
movl %ebp, %r8d
callq 0x58d7
movsbl 0xd(%rsp), %r8d
testl %r8d, %r8d
je 0x5dac
movq 0x80(%rsp,%r13,8), %rdi
movq %r12, %rsi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x58d7
movsbl 0xe(%rsp), %r8d
testl %r8d, %r8d
je 0x5dd4
movq 0x80(%rsp,%r13,8), %rdi
negl %r8d
movq %r12, %rsi
movl $0x2, %edx
movl $0x1, %ecx
callq 0x58d7
movq 0x80(%rsp,%r13,8), %rdi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
jmp 0x5e21
movl 0x100(%rsp,%r13,8), %edi
movl $0x18, %edx
movq %r12, %rsi
callq 0x5080
cmpq $0x18, %rax
jne 0x5f06
movq 0x80(%rsp,%r13,8), %rdi
movzwl 0x50(%rsp), %edx
movzwl 0x52(%rsp), %ecx
movl 0x54(%rsp), %r8d
movq %r12, %rsi
callq 0x58d7
movw $0x0, 0x106(%rsp,%r13,8)
incq %r13
cmpq %rbx, %r13
jne 0x5cb4
movq %r14, %rdi
movq %rbx, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x50c0
movl $0x0, %r13d
testl %eax, %eax
jg 0x5cb4
movq 0x618e(%rip), %rax      # 0xbff0
movq (%rax), %rbx
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2c15(%rip), %rsi      # 0x8a8d
movq %rbx, %rdi
movq %rax, %rdx
jmp 0x5eb5
movq 0x6169(%rip), %rax      # 0xbff0
movq (%rax), %rdi
movq (%r13), %rdx
leaq 0x2bbe(%rip), %rsi      # 0x8a53
jmp 0x5eb5
movq 0x6152(%rip), %rax      # 0xbff0
movq (%rax), %rdi
movl %r14d, %eax
movq 0x18(%rsp), %rcx
movq 0x8(%rcx,%rax,8), %rdx
leaq 0x2ab9(%rip), %rsi      # 0x896e
xorl %eax, %eax
callq 0x5090
jmp 0x5a89
movq 0x6128(%rip), %rax      # 0xbff0
movq (%rax), %rdi
leaq 0x2a78(%rip), %rsi      # 0x894a
movl $0x10, %edx
xorl %eax, %eax
callq 0x5090
jmp 0x5a89
movq 0x6106(%rip), %rax      # 0xbff0
movq (%rax), %rbx
movq (%r13), %r14
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2a93(%rip), %rsi      # 0x8997
jmp 0x5f6f
movq 0x60e3(%rip), %rax      # 0xbff0
movq (%rax), %rbx
movq 0x80(%rsp,%r13,8), %r14
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2b4e(%rip), %rsi      # 0x8a79
jmp 0x5f6f
movq 0x60bc(%rip), %rax      # 0xbff0
movq (%rax), %rdi
movq (%r13), %rdx
leaq 0x2a72(%rip), %rsi      # 0x89b4
xorl %eax, %eax
callq 0x5090
jmp 0x5a89
movq 0x609b(%rip), %rax      # 0xbff0
movq (%rax), %rbx
movq (%r13), %r14
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2a66(%rip), %rsi      # 0x89d5
movq %rbx, %rdi
movq %r14, %rdx
jmp 0x5f90
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2a66(%rip), %rsi      # 0x89f0
movq %rbp, %rdi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x5090
jmp 0x5a89