#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct input_event {
  int type;
  int /*<<< orphan*/ value;
  int /*<<< orphan*/ code;
};
typedef int /*<<< orphan*/ name;

/* Variables and functions */
int /*<<< orphan*/ EVIOCGNAME(int);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ getuid();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, struct input_event *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  struct input_event ev;
  int fd;
  char name[256] = "Unknown";
  char *device = NULL;
  if (argv[1] == NULL) {
    usage();
  }
  if ((getuid()) != 0)
    printf("You are not root! This may not work...\n");
  if (argc > 1)
    device = argv[1];
  if ((fd = open(device, O_RDONLY)) == -1) {
    fprintf(stderr, "%s is not a valid device\n", device);
    exit(1);
  }
  ioctl(fd, EVIOCGNAME(sizeof(name)), name);
  printf("Reading From : %s (%s)\n", device, name);
  while (1) {
    if (read(fd, &ev, sizeof(struct input_event)) <= 0) {
      perror("read()");
      exit(1);
    }
    printf("Type: %*d    Code: %*d    Value: %*d\n", 3, ev.type, 3, ev.code, 3,
           ev.value);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x130(%rbp),%rdi
lea    0xe29(%rip),%rsi
mov    $0x100,%edx
call   1080 <memcpy@plt>
movq   $0x0,-0x138(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    1202 <main+0x52>
mov    $0x0,%al
call   1090 <usage@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
je     1221 <main+0x71>
lea    0xee6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jle    123a <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x138(%rbp)
mov    -0x138(%rbp),%rdi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1281 <main+0xd1>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    -0x138(%rbp),%rdx
lea    0xeb6(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x13c(%rbp)
mov    $0x100,%edi
call   10c0 <EVIOCGNAME@plt>
mov    -0x13c(%rbp),%edi
mov    %eax,%esi
lea    -0x130(%rbp),%rdx
call   1050 <ioctl@plt>
mov    -0x138(%rbp),%rsi
lea    -0x130(%rbp),%rdx
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jg     12f5 <main+0x145>
lea    0xe74(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x20(%rbp),%edx
mov    -0x18(%rbp),%r8d
mov    -0x1c(%rbp),%eax
lea    0xe5b(%rip),%rdi
mov    $0x3,%r9d
mov    %r9d,%esi
mov    %r9d,%ecx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c4 <main+0x114>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x120,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe29(%rip),%rsi
lea    0x20(%rsp),%rdi
mov    $0x100,%edx
call   1090 <memcpy@plt>
cmpq   $0x0,0x8(%rbx)
jne    11f4 <main+0x34>
xor    %eax,%eax
call   10a0 <usage@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     120c <main+0x4c>
lea    0xf58(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x2,%ebp
jl     1217 <main+0x57>
mov    0x8(%rbx),%r14
jmp    121a <main+0x5a>
xor    %r14d,%r14d
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
jne    1252 <main+0x92>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xec0(%rip),%rsi
mov    %r14,%rdx
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    %eax,%ebx
mov    $0x100,%edi
call   10d0 <EVIOCGNAME@plt>
lea    0x20(%rsp),%rbp
mov    %ebx,%edi
mov    %eax,%esi
mov    %rbp,%rdx
call   1060 <ioctl@plt>
lea    0xea4(%rip),%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    12ee <main+0x12e>
lea    0xe99(%rip),%r14
lea    0x10(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x18(%rsp),%r8d
mov    0x10(%rsp),%edx
mov    0x14(%rsp),%eax
mov    %eax,(%rsp)
mov    %r14,%rdi
mov    $0x3,%esi
mov    $0x3,%ecx
mov    $0x3,%r9d
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1070 <read@plt>
test   %rax,%rax
jg     12b0 <main+0xf0>
lea    0xe3d(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.input_event = type { i32, i32, i32 }
@__const.main.name = [256 x i8] c"Unknown\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [40 x i8] c"You are not root! This may not work...\0A\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"%s is not a valid device\0A\00"
@.str.2 = [24 x i8] c"Reading From : %s (%s)\0A\00"
@.str.3 = [7 x i8] c"read()\00"
@.str.4 = [38 x i8] c"Type: %*d Code: %*d Value: %*d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.input_event
%7 = alloca i32
%8 = alloca [256 x i8]
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [256 x i8]* %8 to i8*
store i8* null, i8** %9
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (...) @usage()
br label %17
17: ; preds = %15, %2
%18 = call i64 (...) @getuid()
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, 1
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %9
br label %29
29: ; preds = %25, %22
%30 = load i8*, i8** %9
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %7
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %9
%37 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %29
%40 = load i32, i32* %7
%41 = call i32 @EVIOCGNAME(i32 256)
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%43 = call i32 @ioctl(i32 %40, i32 %41, i8* %42)
%44 = load i8*, i8** %9
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %45)
br label %47
47: ; preds = %39, %54
%48 = load i32, i32* %7
%49 = call i64 @read(i32 %48, %struct.input_event* %6, i32 12)
%50 = icmp sle i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %47
%55 = getelementptr inbounds %struct.input_event, %struct.input_event* %6, i32 0, i32 0
%56 = load i32, i32* %55
%57 = getelementptr inbounds %struct.input_event, %struct.input_event* %6, i32 0, i32 2
%58 = load i32, i32* %57
%59 = getelementptr inbounds %struct.input_event, %struct.input_event* %6, i32 0, i32 1
%60 = load i32, i32* %59
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 3, i32 %56, i32 3, i32 %58, i32 3, i32 %60)
br label %47
}

LLVM IR (-O2):
%struct.input_event = type { i32, i32, i32 }
@__const.main.name = [256 x i8] c"Unknown\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"%s is not a valid device\0A\00"
@.str.2 = [24 x i8] c"Reading From : %s (%s)\0A\00"
@.str.3 = [7 x i8] c"read()\00"
@.str.4 = [38 x i8] c"Type: %*d Code: %*d Value: %*d\0A\00"
@str = [39 x i8] c"You are not root! This may not work...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.input_event
%4 = alloca [256 x i8]
%5 = bitcast %struct.input_event* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 (...) @usage()
br label %12
12: ; preds = %10, %2
%13 = tail call i64 (...) @getuid()
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = icmp sgt i32 %0, 1
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = load i8*, i8** %7
br label %21
21: ; preds = %19, %17
%22 = phi i8* [ %20, %19 ], [ null, %17 ]
%23 = load i32, i32* @O_RDONLY
%24 = tail call i32 @open(i8* %22, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = tail call i32 @EVIOCGNAME(i32 256)
%32 = call i32 @ioctl(i32 %24, i32 %31, i8* %6)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %22, i8* %6)
%34 = call i64 @read(i32 %24, %struct.input_event* %3, i32 12)
%35 = icmp slt i64 %34, 1
br i1 %35, label %40, label %36
36: ; preds = %30
%37 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 0
%38 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 2
%39 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 1
br label %43
40: ; preds = %43, %30
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %36, %43
%44 = load i32, i32* %37
%45 = load i32, i32* %38
%46 = load i32, i32* %39
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 3, i32 %44, i32 3, i32 %45, i32 3, i32 %46)
%48 = call i64 @read(i32 %24, %struct.input_event* %3, i32 12)
%49 = icmp slt i64 %48, 1
br i1 %49, label %40, label %43
}