// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x5484(%rip), %rdx      # 0x9b3b
callq 0x10f0
movl %eax, %ecx
movl %ecx, -0x1f4(%rbp)
subl $-0x1, %eax
je 0x4791
jmp 0x46cf
movl -0x1f4(%rbp), %eax
subl $0x3a, %eax
je 0x476f
jmp 0x46e0
movl -0x1f4(%rbp), %eax
subl $0x3f, %eax
je 0x476f
jmp 0x46f1
movl -0x1f4(%rbp), %eax
subl $0x63, %eax
je 0x4735
jmp 0x46fe
movl -0x1f4(%rbp), %eax
subl $0x74, %eax
je 0x4718
jmp 0x470b
movl -0x1f4(%rbp), %eax
subl $0x76, %eax
je 0x4752
jmp 0x4793
movq 0x78b9(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1100
movl %eax, 0x797d(%rip)      # 0xc0b0
jmp 0x4793
movq 0x789c(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1100
movl %eax, 0x7964(%rip)      # 0xc0b4
jmp 0x4793
movq 0x787f(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1100
movl %eax, 0x794b(%rip)      # 0xc0b8
jmp 0x4793
movq 0x7882(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x53c3(%rip), %rsi      # 0x9b43
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1110
jmp 0x4798
jmp 0x46a9
movq 0x7831(%rip), %rax      # 0xbfd0
movl (%rax), %ecx
subl $0x1, %ecx
movl -0x8(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x8(%rbp)
movq 0x781d(%rip), %rax      # 0xbfd0
movl (%rax), %ecx
subl $0x1, %ecx
movq -0x10(%rbp), %rax
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x47f2
movq 0x7821(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x5362(%rip), %rsi      # 0x9b43
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1110
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl -0x8(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x4ca6
leaq -0x160(%rbp), %rdi
leaq 0x7598(%rip), %rsi      # 0xbdb0
movl $0x28, %edx
callq 0x10a0
cmpl $0x10, -0x14(%rbp)
jl 0x484f
movq 0x77c9(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x5555(%rip), %rsi      # 0x9d8e
movl $0x10, %edx
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1110
leaq -0x160(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x489d
movq 0x7787(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x5526(%rip), %rsi      # 0x9db2
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1110
movslq -0x14(%rbp), %rax
leaq -0x120(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
movq -0x168(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdx
movb $0x0, %al
callq 0x10c0
movslq -0x14(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10e0
movslq -0x14(%rbp), %rcx
movl %eax, -0xa0(%rbp,%rcx,8)
cmpl $0x0, %eax
jl 0x4c36
movslq -0x14(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdi
leaq 0x54c0(%rip), %rsi      # 0x9dd0
callq 0x1130
movq %rax, %rcx
movb $0x1, %al
cmpq $0x0, %rcx
movb %al, -0x1f5(%rbp)
jne 0x494b
movslq -0x14(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdi
leaq 0x549d(%rip), %rsi      # 0x9dd6
callq 0x1130
cmpq $0x0, %rax
setne %al
movb %al, -0x1f5(%rbp)
movb -0x1f5(%rbp), %cl
movslq -0x14(%rbp), %rax
andb $0x1, %cl
movb %cl, -0x130(%rbp,%rax)
movslq -0x14(%rbp), %rax
testb $0x1, -0x130(%rbp,%rax)
jne 0x4bf4
movslq -0x14(%rbp), %rax
movl -0xa0(%rbp,%rax,8), %edi
movl $0x80044501, %esi       # imm = 0x80044501
leaq -0x16c(%rbp), %rdx
movb $0x0, %al
callq 0x1070
cmpl $-0x1, %eax
jne 0x49ed
movq 0x765d(%rip), %rax      # 0xbff8
movq (%rax), %rax
movq %rax, -0x208(%rbp)
movslq -0x14(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rax
movq %rax, -0x200(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1120
movq -0x208(%rbp), %rdi
movq -0x200(%rbp), %rdx
movq %rax, %rcx
leaq 0x53ff(%rip), %rsi      # 0x9ddb
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1110
cmpl $0x10001, -0x16c(%rbp)  # imm = 0x10001
je 0x4a2d
movq 0x75f8(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movslq -0x14(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdx
movl -0x16c(%rbp), %ecx
leaq 0x53dc(%rip), %rsi      # 0x9df8
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1110
movslq -0x14(%rbp), %rax
movl -0xa0(%rbp,%rax,8), %edi
leaq -0x1b0(%rbp), %rdx
movl $0x80404506, %esi       # imm = 0x80404506
movb $0x0, %al
callq 0x1070
cmpl $-0x1, %eax
jne 0x4aa9
movq 0x75a1(%rip), %rax      # 0xbff8
movq (%rax), %rax
movq %rax, -0x218(%rbp)
movslq -0x14(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rax
movq %rax, -0x210(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1120
movq -0x218(%rbp), %rdi
movq -0x210(%rbp), %rdx
movq %rax, %rcx
leaq 0x5381(%rip), %rsi      # 0x9e19
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1110
movslq -0x14(%rbp), %rax
movl -0xa0(%rbp,%rax,8), %edi
movl $0x80084502, %esi       # imm = 0x80084502
leaq -0x1b8(%rbp), %rdx
movb $0x0, %al
callq 0x1070
cmpl $-0x1, %eax
jne 0x4b25
movq 0x7525(%rip), %rax      # 0xbff8
movq (%rax), %rax
movq %rax, -0x228(%rbp)
movslq -0x14(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rax
movq %rax, -0x220(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1120
movq -0x228(%rbp), %rdi
movq -0x220(%rbp), %rdx
movq %rax, %rcx
leaq 0x5320(%rip), %rsi      # 0x9e34
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1110
movq 0x74cc(%rip), %rax      # 0xbff8
movq (%rax), %rax
movq %rax, -0x250(%rbp)
movslq -0x14(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rax
movq %rax, -0x248(%rbp)
leaq -0x1b0(%rbp), %rax
movq %rax, -0x240(%rbp)
movzwl -0x1b8(%rbp), %eax
movl %eax, -0x234(%rbp)
movzwl -0x1b8(%rbp), %edi
callq 0x40a0
movq %rax, -0x230(%rbp)
cmpq $0x0, %rax
je 0x4b8d
movq -0x230(%rbp), %rax
movq %rax, -0x258(%rbp)
jmp 0x4b9d
leaq 0x4f9f(%rip), %rax      # 0x9b33
movq %rax, -0x258(%rbp)
jmp 0x4b9d
movl -0x234(%rbp), %r8d
movq -0x240(%rbp), %rcx
movq -0x248(%rbp), %rdx
movq -0x250(%rbp), %rdi
movq -0x258(%rbp), %r9
movzwl -0x1b6(%rbp), %r11d
movzwl -0x1b4(%rbp), %r10d
movzwl -0x1b2(%rbp), %eax
leaq 0x526e(%rip), %rsi      # 0x9e4c
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x1090
jmp 0x4c18
movq 0x73fd(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movslq -0x14(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdx
leaq 0x5275(%rip), %rsi      # 0x9e86
movb $0x0, %al
callq 0x1090
movslq -0x14(%rbp), %rax
movw $0x1, -0x9c(%rbp,%rax,8)
movslq -0x14(%rbp), %rax
movw $0x0, -0x9a(%rbp,%rax,8)
jmp 0x4c96
callq 0x1040
cmpl $0xd, (%rax)
jne 0x4c6e
movq 0x73b1(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movslq -0x14(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdx
leaq 0x523a(%rip), %rsi      # 0x9e97
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1110
movslq -0x14(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdi
callq 0x1030
movq -0x168(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x168(%rbp)
jmp 0x485d
jmp 0x4c98
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x47f9
jmp 0x4ca8
leaq -0xa0(%rbp), %rdi
movslq -0x14(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x10d0
cmpl $0x0, %eax
jle 0x5014
movl $0x0, -0x1bc(%rbp)
movl -0x1bc(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x500f
movslq -0x1bc(%rbp), %rax
movswl -0x9a(%rbp,%rax,8), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x4fea
movslq -0x1bc(%rbp), %rax
testb $0x1, -0x130(%rbp,%rax)
je 0x4f37
movslq -0x1bc(%rbp), %rax
movl -0xa0(%rbp,%rax,8), %edi
leaq -0x1d3(%rbp), %rsi
movl $0x3, %edx
callq 0x1080
cmpq $0x3, %rax
je 0x4d90
movq 0x72bd(%rip), %rax      # 0xbff8
movq (%rax), %rax
movq %rax, -0x268(%rbp)
movslq -0x1bc(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rax
movq %rax, -0x260(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1120
movq -0x268(%rbp), %rdi
movq -0x260(%rbp), %rdx
movq %rax, %rcx
leaq 0x513e(%rip), %rsi      # 0x9ebd
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1110
leaq -0x1d0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1060
movsbl -0x1d3(%rbp), %eax
movsbl 0x730f(%rip), %ecx      # 0xc0bd
xorl %ecx, %eax
movb %al, -0x1d4(%rbp)
movb -0x1d3(%rbp), %al
movb %al, 0x72fb(%rip)       # 0xc0bd
movsbl -0x1d4(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x4e0c
movslq -0x1bc(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdi
movsbl -0x1d3(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %r8d
leaq -0x1d0(%rbp), %rsi
movl $0x1, %edx
movl $0x110, %ecx            # imm = 0x110
callq 0x4540
movsbl -0x1d4(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x4e56
movslq -0x1bc(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdi
movsbl -0x1d3(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %r8d
leaq -0x1d0(%rbp), %rsi
movl $0x1, %edx
movl $0x111, %ecx            # imm = 0x111
callq 0x4540
movsbl -0x1d4(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x4ea0
movslq -0x1bc(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdi
movsbl -0x1d3(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %r8d
leaq -0x1d0(%rbp), %rsi
movl $0x1, %edx
movl $0x112, %ecx            # imm = 0x112
callq 0x4540
cmpb $0x0, -0x1d2(%rbp)
je 0x4ed3
movslq -0x1bc(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdi
movsbl -0x1d2(%rbp), %r8d
leaq -0x1d0(%rbp), %rsi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x4540
cmpb $0x0, -0x1d1(%rbp)
je 0x4f0e
movslq -0x1bc(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdi
movsbl -0x1d1(%rbp), %eax
xorl %r8d, %r8d
subl %eax, %r8d
leaq -0x1d0(%rbp), %rsi
movl $0x2, %edx
movl $0x1, %ecx
callq 0x4540
movslq -0x1bc(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdi
leaq -0x1d0(%rbp), %rsi
xorl %r8d, %r8d
movl %r8d, %edx
movl %r8d, %ecx
callq 0x4540
jmp 0x4fe8
movslq -0x1bc(%rbp), %rax
movl -0xa0(%rbp,%rax,8), %edi
leaq -0x1f0(%rbp), %rsi
movl $0x18, %edx
callq 0x1080
cmpq $0x18, %rax
je 0x4fb8
movq 0x7095(%rip), %rax      # 0xbff8
movq (%rax), %rax
movq %rax, -0x278(%rbp)
movslq -0x1bc(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rax
movq %rax, -0x270(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1120
movq -0x278(%rbp), %rdi
movq -0x270(%rbp), %rdx
movq %rax, %rcx
leaq 0x4f16(%rip), %rsi      # 0x9ebd
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1110
movslq -0x1bc(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rdi
leaq -0x1f0(%rbp), %rsi
movzwl -0x1e0(%rbp), %edx
movzwl -0x1de(%rbp), %ecx
movl -0x1dc(%rbp), %r8d
callq 0x4540
jmp 0x4fea
movslq -0x1bc(%rbp), %rax
movw $0x0, -0x9a(%rbp,%rax,8)
movl -0x1bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1bc(%rbp)
jmp 0x4cd0
jmp 0x4ca8
movq 0x6fdd(%rip), %rax      # 0xbff8
movq (%rax), %rax
movq %rax, -0x280(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1120
movq -0x280(%rbp), %rdi
movq %rax, %rdx
leaq 0x4e8f(%rip), %rsi      # 0x9ed1
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1110