#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 value;
  int type;
  int code;
};
typedef int /*<<< orphan*/ data;

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int open(char const *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, struct input_event *, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  int fd, bytes;
  struct input_event data;
  const char *pDevice = "/dev/input/event4";
  fd = open(pDevice, O_RDONLY | O_NONBLOCK);
  if (fd == -1) {
    printf("ERROR Opening %s\n", pDevice);
    return -1;
  }
  while (1) {
    bytes = read(fd, &data, sizeof(data));
    if (bytes > 0) {
      printf("Keypress value=%x, type=%x, code=%x\n", data.value, data.type,
             data.code);
    } else {
      sleep(1);
    }
  }
  return 0;
}


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)
lea    0xea3(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e73(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a6 <main+0x66>
mov    -0x30(%rbp),%rsi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbp
ret
jmp    11ab <main+0x6b>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11e5 <main+0xa5>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ef <main+0xaf>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11ab <main+0x6b>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e90(%rip),%rax
mov    0x2e91(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe9e(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11b4 <main+0x74>
mov    %eax,%ebx
mov    %rsp,%r14
lea    0xeac(%rip),%r15
jmp    118a <main+0x4a>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1180 <main+0x40>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    118a <main+0x4a>
lea    0xe57(%rip),%rdi
lea    0xe3e(%rip),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.input_event = type { i32, i32, i32 }
@.str = [18 x i8] c"/dev/input/event4\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [18 x i8] c"ERROR Opening %s\0A\00"
@.str.2 = [37 x i8] c"Keypress value=%x, type=%x, code=%x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.input_event
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %9
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %18)
ret i32 -1
20: ; preds = %2
br label %21
21: ; preds = %20, %36
%22 = load i32, i32* %6
%23 = call i32 @read(i32 %22, %struct.input_event* %8, i32 12)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %21
%27 = getelementptr inbounds %struct.input_event, %struct.input_event* %8, i32 0, i32 0
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.input_event, %struct.input_event* %8, i32 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.input_event, %struct.input_event* %8, i32 0, i32 2
%32 = load i32, i32* %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 %30, i32 %32)
br label %36
34: ; preds = %21
%35 = call i32 @sleep(i32 1)
br label %36
36: ; preds = %34, %26
br label %21
}

LLVM IR (-O2):
%struct.input_event = type { i32, i32, i32 }
@.str = [18 x i8] c"/dev/input/event4\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [18 x i8] c"ERROR Opening %s\0A\00"
@.str.2 = [37 x i8] c"Keypress value=%x, type=%x, code=%x\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.input_event
%4 = bitcast %struct.input_event* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 0
%12 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 1
%13 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 2
br label %16
14: ; preds = %2
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
ret i32 -1
16: ; preds = %26, %10
%17 = call i32 @read(i32 %8, %struct.input_event* %3, i32 12)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = load i32, i32* %11
%21 = load i32, i32* %12
%22 = load i32, i32* %13
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 %21, i32 %22)
br label %26
24: ; preds = %16
%25 = tail call i32 @sleep(i32 1)
br label %26
26: ; preds = %24, %19
br label %16
}