#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 {
  scalar_t__ type;
  int /*<<< orphan*/ code;
};

/* Variables and functions */
scalar_t__ EV_KEY;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *keyparser_getname(int /*<<< orphan*/);
int open_input_fd(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, struct input_event *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  struct input_event ev;
  if (argc < 2) {
    fprintf(stderr, "Usage: evf-getkey /dev/input/eventX\n");
    exit(EXIT_FAILURE);
  }
  if ((fd = open_input_fd(argv[1])) < 0) {
    exit(EXIT_FAILURE);
  }
  for (;;) {
    int size = sizeof(struct input_event);
    if (read(fd, &ev, size) < size) {
      fprintf(stderr, "Error reading from fd: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
    if (ev.type == EV_KEY) {
      printf("key %s\n", keyparser_getname(ev.code));
    }
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b5 <main+0x45>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <open_input_fd@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11dc <main+0x6c>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11e1 <main+0x71>
movl   $0x10,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x2c(%rbp),%edx
lea    -0x28(%rbp),%rsi
call   1050 <read@plt>
cmp    -0x2c(%rbp),%eax
jge    123c <main+0xcc>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rax
mov    0x2da1(%rip),%rcx
cmp    (%rcx),%rax
jne    1267 <main+0xf7>
mov    -0x20(%rbp),%edi
call   1030 <keyparser_getname@plt>
mov    %rax,%rsi
lea    0xdde(%rip),%rdi
call   1040 <printf@plt>
jmp    11e1 <main+0x71>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%edi
jg     1199 <main+0x29>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    11e8 <main+0x78>
mov    0x8(%rsi),%rdi
call   1060 <open_input_fd@plt>
test   %eax,%eax
js     11e8 <main+0x78>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1050 <read@plt>
cmp    $0x10,%eax
jge    11f6 <main+0x86>
mov    0x2e13(%rip),%rax
mov    (%rax),%ebx
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe49(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2deb(%rip),%r12
lea    0xe3c(%rip),%r14
lea    0x8(%rsp),%r15
jmp    1224 <main+0xb4>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1050 <read@plt>
cmp    $0x10,%eax
jl     11be <main+0x4e>
mov    0x8(%rsp),%rax
cmp    (%r12),%rax
jne    1210 <main+0xa0>
mov    0x10(%rsp),%edi
call   1030 <keyparser_getname@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
jmp    1210 <main+0xa0>

LLVM IR (-O0):
%struct.input_event = type { i64, i32 }
@stderr = global i32 0
@.str = [37 x i8] c"Usage: evf-getkey /dev/input/eventX\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [27 x i8] c"Error reading from fd: %s\0A\00"
@errno = global i32 0
@EV_KEY = global i64 0
@.str.2 = [8 x i8] c"key %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.input_event
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @open_input_fd(i8* %19)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %16
br label %26
26: ; preds = %49, %25
store i32 16, i32* %8
%27 = load i32, i32* %6
%28 = load i32, i32* %8
%29 = call i32 @read(i32 %27, %struct.input_event* %7, i32 %28)
%30 = load i32, i32* %8
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %39
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %26
%40 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 0
%41 = load i64, i64* %40
%42 = load i64, i64* @EV_KEY
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 1
%46 = load i32, i32* %45
%47 = call i8* @keyparser_getname(i32 %46)
%48 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %44, %39
br label %26
}

LLVM IR (-O2):
%struct.input_event = type { i64, i32 }
@stderr = global i32 0
@.str = [37 x i8] c"Usage: evf-getkey /dev/input/eventX\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [27 x i8] c"Error reading from fd: %s\0A\00"
@errno = global i32 0
@EV_KEY = global i64 0
@.str.2 = [8 x i8] c"key %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.input_event
%4 = bitcast %struct.input_event* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @open_input_fd(i8* %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %11
%17 = call i32 @read(i32 %14, %struct.input_event* %3, i32 16)
%18 = icmp slt i32 %17, 16
br i1 %18, label %25, label %19
19: ; preds = %16
%20 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 0
%21 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 1
br label %32
22: ; preds = %11
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %40, %16
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %19, %40
%33 = load i64, i64* %20
%34 = load i64, i64* @EV_KEY
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i32, i32* %21
%38 = tail call i8* @keyparser_getname(i32 %37)
%39 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %36, %32
%41 = call i32 @read(i32 %14, %struct.input_event* %3, i32 16)
%42 = icmp slt i32 %41, 16
br i1 %42, label %25, label %32
}