// File: /workspace/github_c_makefile_star2_O1/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, 0x10(%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 0x5ec6
movq %r14, %r12
leaq (%rsp,%r14,8), %r13
addq $0x40, %r13
incq %r14
leaq 0x4558(%rip), %r15      # 0xa038
leaq 0x2e4d(%rip), %rsi      # 0x8934
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq %r13, %rdi
xorl %eax, %eax
callq 0x50b0
movq 0x40(%rsp,%r12,8), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x50d0
movl %eax, 0x100(%rsp,%r12,8)
testl %eax, %eax
jns 0x5b3f
callq 0x5040
cmpl $0xd, (%rax)
je 0x5e85
movq (%r13), %rdi
callq 0x5030
movq (%r15), %rsi
addq $0x8, %r15
testq %rsi, %rsi
jne 0x5ae7
jmp 0x5e9c
movl %eax, %ebp
movq 0x40(%rsp,%r12,8), %r15
movq %r15, %rdi
leaq 0x2e3c(%rip), %rsi      # 0x898c
callq 0x5130
testq %rax, %rax
jne 0x5c5e
movq %r15, %rdi
leaq 0x2e2a(%rip), %rsi      # 0x8992
callq 0x5130
testq %rax, %rax
setne 0x30(%rsp,%r12)
jne 0x5c64
movl $0x80044501, %esi       # imm = 0x80044501
movl %ebp, %edi
leaq 0x1c(%rsp), %rdx
xorl %eax, %eax
callq 0x5070
cmpl $-0x1, %eax
je 0x5f0c
movl 0x1c(%rsp), %ecx
cmpl $0x10001, %ecx          # imm = 0x10001
jne 0x5f2f
movl 0x100(%rsp,%r12,8), %edi
movq 0x28(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
xorl %eax, %eax
callq 0x5070
cmpl $-0x1, %eax
je 0x5f50
movl 0x100(%rsp,%r12,8), %edi
movq 0x20(%rsp), %rsi
leaq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x5070
movq 0x6403(%rip), %rcx      # 0xbff0
movq (%rcx), %rbp
movq 0x40(%rsp,%r12,8), %r15
cmpl $-0x1, %eax
je 0x5f79
movzwl 0x8(%rsp), %r13d
movl %r13d, %edi
callq 0x570b
testq %rax, %rax
leaq 0x2ad9(%rip), %rcx      # 0x86ef
cmoveq %rcx, %rax
movzwl 0xa(%rsp), %r10d
movzwl 0xc(%rsp), %r11d
movq %r15, %rdx
movzwl 0xe(%rsp), %r15d
subq $0x8, %rsp
movq %rbp, %rdi
leaq 0x2dcb(%rip), %rsi      # 0x8a08
leaq 0xc8(%rsp), %rcx
movl %r13d, %r8d
movq %rax, %r9
xorl %eax, %eax
pushq %r15
pushq %r11
pushq %r10
callq 0x5090
addq $0x20, %rsp
jmp 0x5c7f
movb $0x1, 0x30(%rsp,%r12)
movq 0x6385(%rip), %rax      # 0xbff0
movq (%rax), %rdi
leaq 0x2dcd(%rip), %rsi      # 0x8a42
movq %r15, %rdx
xorl %eax, %eax
callq 0x5090
movl $0x1, 0x104(%rsp,%r12,8)
cmpq %rbx, %r14
jne 0x5ac1
movl %r14d, %esi
leaq 0x100(%rsp), %rdi
movq %rsi, 0x10(%rsp)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x50c0
testl %eax, %eax
jle 0x5e60
leaq 0x8(%rsp), %r15
leaq 0xc0(%rsp), %r14
movq $-0x1, %r13
testb $0x1, 0x10e(%rsp,%r13,8)
je 0x5e2a
cmpb $0x1, 0x31(%rsp,%r13)
jne 0x5de6
movl 0x108(%rsp,%r13,8), %edi
movl $0x3, %edx
movq %r15, %rsi
callq 0x5080
cmpq $0x3, %rax
jne 0x5ee8
movq %r14, %rdi
xorl %esi, %esi
callq 0x5060
movzbl 0x8(%rsp), %ebp
movb 0x63a4(%rip), %bl       # 0xc0bd
xorb %bpl, %bl
movb %bpl, 0x639a(%rip)      # 0xc0bd
testb $0x1, %bl
je 0x5d46
movq 0x48(%rsp,%r13,8), %rdi
movl %ebp, %r8d
andl $0x1, %r8d
movq %r14, %rsi
movl $0x1, %edx
movl $0x110, %ecx            # imm = 0x110
callq 0x58d7
testb $0x2, %bl
je 0x5d6c
movq 0x48(%rsp,%r13,8), %rdi
movl %ebp, %eax
shrb %al
andb $0x1, %al
movzbl %al, %r8d
movq %r14, %rsi
movl $0x1, %edx
movl $0x111, %ecx            # imm = 0x111
callq 0x58d7
testb $0x4, %bl
je 0x5d91
movq 0x48(%rsp,%r13,8), %rdi
shrl $0x2, %ebp
andl $0x1, %ebp
movq %r14, %rsi
movl $0x1, %edx
movl $0x112, %ecx            # imm = 0x112
movl %ebp, %r8d
callq 0x58d7
movsbl 0x9(%rsp), %r8d
testl %r8d, %r8d
je 0x5db0
movq 0x48(%rsp,%r13,8), %rdi
movq %r14, %rsi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x58d7
movsbl 0xa(%rsp), %r8d
testl %r8d, %r8d
je 0x5dd5
movq 0x48(%rsp,%r13,8), %rdi
negl %r8d
movq %r14, %rsi
movl $0x2, %edx
movl $0x1, %ecx
callq 0x58d7
movq 0x48(%rsp,%r13,8), %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
jmp 0x5e25
movl 0x108(%rsp,%r13,8), %edi
movl $0x18, %edx
movq %r14, %rsi
callq 0x5080
cmpq $0x18, %rax
jne 0x5ee8
movq 0x48(%rsp,%r13,8), %rdi
movzwl 0xd0(%rsp), %edx
movzwl 0xd2(%rsp), %ecx
movl 0xd4(%rsp), %r8d
movq %r14, %rsi
callq 0x58d7
movw $0x0, 0x10e(%rsp,%r13,8)
incq %r13
cmpq %r13, %r12
jne 0x5cca
leaq 0x100(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x50c0
testl %eax, %eax
jg 0x5cc3
movq 0x6189(%rip), %rax      # 0xbff0
movq (%rax), %rbx
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2c10(%rip), %rsi      # 0x8a8d
movq %rbx, %rdi
movq %rax, %rdx
jmp 0x5eba
movq 0x6164(%rip), %rax      # 0xbff0
movq (%rax), %rdi
movq (%r13), %rdx
leaq 0x2bb9(%rip), %rsi      # 0x8a53
jmp 0x5eba
movq 0x614d(%rip), %rax      # 0xbff0
movq (%rax), %rdi
movl %r12d, %eax
movq 0x10(%rsp), %rcx
movq 0x8(%rcx,%rax,8), %rdx
leaq 0x2ab4(%rip), %rsi      # 0x896e
xorl %eax, %eax
callq 0x5090
jmp 0x5a89
movq 0x6123(%rip), %rax      # 0xbff0
movq (%rax), %rdi
leaq 0x2a73(%rip), %rsi      # 0x894a
movl $0x10, %edx
xorl %eax, %eax
callq 0x5090
jmp 0x5a89
movq 0x6101(%rip), %rax      # 0xbff0
movq (%rax), %rbx
movq 0x48(%rsp,%r13,8), %r14
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2b6f(%rip), %rsi      # 0x8a79
jmp 0x5f71
movq 0x60dd(%rip), %rax      # 0xbff0
movq (%rax), %rbx
movq (%r13), %r14
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2a6a(%rip), %rsi      # 0x8997
jmp 0x5f71
movq 0x60ba(%rip), %rax      # 0xbff0
movq (%rax), %rdi
movq (%r13), %rdx
leaq 0x2a70(%rip), %rsi      # 0x89b4
xorl %eax, %eax
callq 0x5090
jmp 0x5a89
movq 0x6099(%rip), %rax      # 0xbff0
movq (%rax), %rbx
movq (%r13), %r14
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2a64(%rip), %rsi      # 0x89d5
movq %rbx, %rdi
movq %r14, %rdx
jmp 0x5f92
callq 0x5040
movl (%rax), %edi
callq 0x5120
leaq 0x2a64(%rip), %rsi      # 0x89f0
movq %rbp, %rdi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x5090
jmp 0x5a89