// File: /workspace/github_c_makefile_star2_O2/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 0x2d0a(%rip), %r15      # 0x86a7
leaq 0x670c(%rip), %r13      # 0xc0b0
movq 0x6625(%rip), %rbp      # 0xbfd0
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x50e0
cmpl $0x63, %eax
je 0x59db
movq %r13, %r12
cmpl $0x74, %eax
je 0x59eb
cmpl $0x76, %eax
je 0x59e4
cmpl $-0x1, %eax
je 0x59fe
cmpl $0x3a, %eax
je 0x5a13
cmpl $0x3f, %eax
jne 0x59ab
jmp 0x5a13
leaq 0x66d2(%rip), %r12      # 0xc0b4
jmp 0x59eb
leaq 0x66cd(%rip), %r12      # 0xc0b8
movq (%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x50f0
movl %eax, (%r12)
jmp 0x59ab
movq 0x65c3(%rip), %rax      # 0xbfc8
movslq (%rax), %rax
leaq -0x1(%rax), %rcx
subl %ecx, %ebx
cmpl $0x2, %ebx
jge 0x5a39
movq 0x65d6(%rip), %rax      # 0xbff0
movq (%rax), %rcx
leaq 0x2c8b(%rip), %rdi      # 0x86af
pushq $0x1
popq %rdx
movl $0x221, %esi            # imm = 0x221
callq 0x5110
pushq $0x1
popq %rdi
callq 0x5100
leaq (%r14,%rax,8), %rax
addq $-0x8, %rax
movq %rax, 0x10(%rsp)
movl $0x80044501, %eax       # imm = 0x80044501
decl %ebx
leaq 0x3c0005(%rax), %rcx
movq %rcx, 0x20(%rsp)
addq $0x40001, %rax          # imm = 0x40001
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
cmpq %rbx, %rbp
je 0x5c71
cmpq $0x10, %rbp
je 0x5e0d
leaq (%rsp,%rbp,8), %r12
addq $0x80, %r12
leaq 0x1(%rbp), %r14
leaq 0x45a1(%rip), %r13      # 0xa030
movq (%r13), %rsi
testq %rsi, %rsi
je 0x5c31
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq %r12, %rdi
xorl %eax, %eax
callq 0x50b0
movq 0x80(%rsp,%rbp,8), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x50d0
movl %eax, 0x100(%rsp,%rbp,8)
testl %eax, %eax
jns 0x5ae9
callq 0x5040
cmpl $0xd, (%rax)
je 0x5c50
movq (%r12), %rdi
callq 0x5030
addq $0x8, %r13
jmp 0x5a8f
movl %eax, %r15d
movq (%r12), %r13
movq %r13, %rdi
leaq 0x2e42(%rip), %rsi      # 0x893c
callq 0x5130
testq %rax, %rax
jne 0x5bfe
movq %r13, %rdi
leaq 0x2e30(%rip), %rsi      # 0x8942
callq 0x5130
testq %rax, %rax
setne 0x30(%rsp,%rbp)
jne 0x5c03
movl $0x80044501, %esi       # imm = 0x80044501
movl %r15d, %edi
leaq 0xc(%rsp), %rdx
xorl %eax, %eax
callq 0x5070
cmpl $-0x1, %eax
je 0x5e54
movl 0xc(%rsp), %ecx
cmpl $0x10001, %ecx          # imm = 0x10001
jne 0x5e77
movl %r15d, %edi
movq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x5070
cmpl $-0x1, %eax
je 0x5e98
movl %r15d, %edi
movq 0x18(%rsp), %rsi
leaq 0x4(%rsp), %rdx
xorl %eax, %eax
callq 0x5070
movq 0x6466(%rip), %rcx      # 0xbff0
movq (%rcx), %r15
movq (%r12), %r12
cmpl $-0x1, %eax
je 0x5ece
movq %r12, 0x28(%rsp)
movzwl 0x4(%rsp), %r12d
movl %r12d, %edi
callq 0x570b
testq %rax, %rax
leaq 0x2ae8(%rip), %rcx      # 0x869f
cmoveq %rcx, %rax
movzwl 0x6(%rsp), %r10d
movzwl 0x8(%rsp), %r11d
movzwl 0xa(%rsp), %r13d
subq $0x8, %rsp
movq %r15, %rdi
leaq 0x2ddd(%rip), %rsi      # 0x89b8
movq 0x30(%rsp), %rdx
leaq 0x48(%rsp), %rcx
movl %r12d, %r8d
movq %rax, %r9
xorl %eax, %eax
pushq %r13
pushq %r11
pushq %r10
callq 0x5090
addq $0x20, %rsp
jmp 0x5c1e
movb $0x1, 0x30(%rsp,%rbp)
movq 0x63e6(%rip), %rax      # 0xbff0
movq (%rax), %rdi
leaq 0x2dde(%rip), %rsi      # 0x89f2
movq %r13, %rdx
xorl %eax, %eax
callq 0x5090
movl $0x1, 0x104(%rsp,%rbp,8)
movq %r14, %rbp
jmp 0x5a66
movq 0x63b8(%rip), %rax      # 0xbff0
movq (%rax), %rdi
movl %ebp, %eax
movq 0x10(%rsp), %rcx
movq 0x8(%rcx,%rax,8), %rdx
leaq 0x2cd0(%rip), %rsi      # 0x891e
jmp 0x5c65
movq 0x6399(%rip), %rax      # 0xbff0
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0x2d9e(%rip), %rsi      # 0x8a03
xorl %eax, %eax
callq 0x5090
jmp 0x5a31
leaq 0x40(%rsp), %r13
leaq 0x100(%rsp), %rdi
movq %rbx, %rsi
pushq $-0x1
popq %rdx
callq 0x50c0
testl %eax, %eax
jle 0x5ee9
xorl %r15d, %r15d
cmpq %r15, %rbp
je 0x5c76
testb $0x1, 0x106(%rsp,%r15,8)
je 0x5dfb
cmpb $0x1, 0x30(%rsp,%r15)
jne 0x5dc3
movl 0x100(%rsp,%r15,8), %edi
leaq 0x4(%rsp), %rsi
pushq $0x3
popq %rdx
callq 0x5080
cmpq $0x3, %rax
jne 0x5e2d
movq %r13, %rdi
xorl %esi, %esi
callq 0x5060
movzbl 0x4(%rsp), %r12d
movb 0x63d3(%rip), %r14b     # 0xc0bd
xorb %r12b, %r14b
movb %r12b, 0x63c9(%rip)     # 0xc0bd
testb $0x1, %r14b
je 0x5d19
movq 0x80(%rsp,%r15,8), %rdi
movl %r12d, %r8d
andl $0x1, %r8d
movq %r13, %rsi
pushq $0x1
popq %rdx
movl $0x110, %ecx            # imm = 0x110
callq 0x5886
testb $0x2, %r14b
je 0x5d42
movq 0x80(%rsp,%r15,8), %rdi
movl %r12d, %eax
shrb %al
andb $0x1, %al
movzbl %al, %r8d
movq %r13, %rsi
pushq $0x1
popq %rdx
movl $0x111, %ecx            # imm = 0x111
callq 0x5886
testb $0x4, %r14b
je 0x5d6b
movq 0x80(%rsp,%r15,8), %rdi
shrl $0x2, %r12d
andl $0x1, %r12d
movq %r13, %rsi
pushq $0x1
popq %rdx
movl $0x112, %ecx            # imm = 0x112
movl %r12d, %r8d
callq 0x5886
movsbl 0x5(%rsp), %r8d
testl %r8d, %r8d
je 0x5d8b
movq 0x80(%rsp,%r15,8), %rdi
movq %r13, %rsi
pushq $0x2
popq %rdx
xorl %ecx, %ecx
callq 0x5886
movsbl 0x6(%rsp), %r8d
testl %r8d, %r8d
je 0x5daf
movq 0x80(%rsp,%r15,8), %rdi
negl %r8d
movq %r13, %rsi
pushq $0x2
popq %rdx
pushq $0x1
popq %rcx
callq 0x5886
movq 0x80(%rsp,%r15,8), %rdi
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
jmp 0x5df6
movl 0x100(%rsp,%r15,8), %edi
movq %r13, %rsi
pushq $0x18
popq %rdx
callq 0x5080
cmpq $0x18, %rax
jne 0x5e2d
movq 0x80(%rsp,%r15,8), %rdi
movzwl 0x50(%rsp), %edx
movzwl 0x52(%rsp), %ecx
movl 0x54(%rsp), %r8d
movq %r13, %rsi
callq 0x5886
andw $0x0, 0x106(%rsp,%r15,8)
incq %r15
jmp 0x5c94
movq 0x61dc(%rip), %rax      # 0xbff0
movq (%rax), %rdi
leaq 0x2adc(%rip), %rsi      # 0x88fa
pushq $0x10
popq %rdx
xorl %eax, %eax
callq 0x5090
jmp 0x5a31
movq 0x61bc(%rip), %rax      # 0xbff0
movq (%rax), %rbx
movq 0x80(%rsp,%r15,8), %r14
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2bd7(%rip), %rsi      # 0x8a29
jmp 0x5eb9
movq 0x6195(%rip), %rax      # 0xbff0
movq (%rax), %rbx
movq (%r12), %r14
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2ad2(%rip), %rsi      # 0x8947
jmp 0x5eb9
movq 0x6172(%rip), %rax      # 0xbff0
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0x2ad8(%rip), %rsi      # 0x8964
xorl %eax, %eax
callq 0x5090
jmp 0x5a31
movq 0x6151(%rip), %rax      # 0xbff0
movq (%rax), %rbx
movq (%r12), %r14
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2acc(%rip), %rsi      # 0x8985
movq %rbx, %rdi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x5090
jmp 0x5a31
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2abf(%rip), %rsi      # 0x89a0
movq %r15, %rdi
movq %r12, %rdx
jmp 0x5ebf
movq 0x6100(%rip), %rax      # 0xbff0
movq (%rax), %rbx
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2b37(%rip), %rsi      # 0x8a3d
movq %rbx, %rdi
movq %rax, %rdx
jmp 0x5c65