#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 code;
  int value;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, struct input_event *, int);
int main(int argc, char **argv) {
  int fd;
  if (argc < 2) {
    printf("usage: %s <device>\n", argv[0]);
    return 1;
  }
  fd = open(argv[1], O_RDONLY);
  struct input_event ev;
  while (1) {
    read(fd, &ev, sizeof(struct input_event));
    if (ev.type == 1)
      printf("key %i state %i\n", ev.code, ev.value);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1170 <main+0x40>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1040 <read@plt>
cmpl   $0x1,-0x20(%rbp)
jne    11b8 <main+0x88>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1189 <main+0x59>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     115e <main+0x2e>
mov    (%rsi),%rsi
lea    0xeb8(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x8(%rsi),%rdi
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebx
mov    %rsp,%r14
lea    0xe98(%rip),%r15
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1040 <read@plt>
cmpl   $0x1,(%rsp)
jne    1180 <main+0x50>
mov    0x4(%rsp),%esi
mov    0x8(%rsp),%edx
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1180 <main+0x50>

LLVM IR (-O0):
%struct.input_event = type { i32, i32, i32 }
@.str = [20 x i8] c"usage: %s <device>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"key %i state %i\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.input_event
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %13)
ret i32 1
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %6
br label %21
21: ; preds = %15, %33
%22 = load i32, i32* %6
%23 = call i32 @read(i32 %22, %struct.input_event* %7, i32 12)
%24 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 0
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 2
%31 = load i32, i32* %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %31)
br label %33
33: ; preds = %27, %21
br label %21
}

LLVM IR (-O2):
%struct.input_event = type { i32, i32, i32 }
@.str = [20 x i8] c"usage: %s <device>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"key %i state %i\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.input_event
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %6)
ret i32 1
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = bitcast %struct.input_event* %3 to i8*
%14 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 0
%15 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 1
%16 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 2
br label %17
17: ; preds = %25, %8
%18 = call i32 @read(i32 %12, %struct.input_event* %3, i32 12)
%19 = load i32, i32* %14
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* %15
%23 = load i32, i32* %16
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %23)
br label %25
25: ; preds = %21, %17
br label %17
}