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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, struct input_event *, int);
int main(int argc, char *argv[]) {
  int fd;
  int ret;
  struct input_event buf;
  fd = open("/dev/input/event0", O_RDONLY);
  if (fd < 0) {
    printf("open %s fail\n", "/dev/input/event0");
    return -1;
  }
  printf("waiting key event...\n");
  while (1) {
    ret = read(fd, &buf, sizeof(struct input_event));
    if (ret <= 0) {
      printf("read key event fail\n");
      break;
    }
    if (buf.code > 0)
      printf("key.code=%d key.value=%d\n", buf.code, buf.value);
  }
  close(fd);
  return 0;
}


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)
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1199 <main+0x59>
lea    0xe93(%rip),%rdi
lea    0xe7a(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <main+0xca>
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     11d8 <main+0x98>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fb <main+0xbb>
cmpl   $0x0,-0x20(%rbp)
jle    11f6 <main+0xb6>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11a7 <main+0x67>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11f2 <main+0xa2>
mov    %eax,%ebx
lea    0xebb(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11db <main+0x8b>
lea    0xe80(%rip),%r14
lea    0x8(%rsp),%r15
jmp    11c3 <main+0x73>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11db <main+0x8b>
mov    0x8(%rsp),%esi
test   %esi,%esi
jle    11b0 <main+0x60>
mov    0xc(%rsp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b0 <main+0x60>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    120c <main+0xbc>
lea    0xe19(%rip),%rdi
lea    0xe00(%rip),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.input_event = type { i32, i32 }
@.str = [18 x i8] c"/dev/input/event0\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"open %s fail\0A\00"
@.str.2 = [22 x i8] c"waiting key event...\0A\00"
@.str.3 = [21 x i8] c"read key event fail\0A\00"
@.str.4 = [26 x i8] c"key.code=%d key.value=%d\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %34
%18 = load i32, i32* %6
%19 = call i32 @read(i32 %18, %struct.input_event* %8, i32 8)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %35
24: ; preds = %17
%25 = getelementptr inbounds %struct.input_event, %struct.input_event* %8, i32 0, i32 0
%26 = load i32, i32* %25
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = getelementptr inbounds %struct.input_event, %struct.input_event* %8, i32 0, i32 0
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.input_event, %struct.input_event* %8, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %30, i32 %32)
br label %34
34: ; preds = %28, %24
br label %17
35: ; preds = %22
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
store i32 0, i32* %3
br label %38
38: ; preds = %35, %13
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.input_event = type { i32, i32 }
@.str = [18 x i8] c"/dev/input/event0\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"open %s fail\0A\00"
@.str.4 = [26 x i8] c"key.code=%d key.value=%d\0A\00"
@str = [21 x i8] c"waiting key event...\00"
@str.5 = [20 x i8] c"read key event fail\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 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %29
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%12 = call i32 @read(i32 %6, %struct.input_event* %3, i32 8)
%13 = icmp slt i32 %12, 1
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 0
%16 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 1
br label %20
17: ; preds = %26, %10
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
%19 = tail call i32 @close(i32 %6)
br label %29
20: ; preds = %14, %26
%21 = load i32, i32* %15
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* %16
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %21, i32 %24)
br label %26
26: ; preds = %23, %20
%27 = call i32 @read(i32 %6, %struct.input_event* %3, i32 8)
%28 = icmp slt i32 %27, 1
br i1 %28, label %17, label %20
29: ; preds = %17, %8
%30 = phi i32 [ -1, %8 ], [ 0, %17 ]
ret i32 %30
}