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

/* Variables and functions */
scalar_t__ EV_KEY;
int KEY_DOWN;
int KEY_UP;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, struct input_event *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  char *input_dev;
  input_dev = "/dev/input/by-path/platform-i8042-serio-0-event-kbd";
  if ((fd = open(input_dev, O_RDONLY)) < 0) {
    fprintf(stderr, "open %s failed: %s\n", input_dev, strerror(errno));
    exit(1);
  } else {
    fprintf(stdout, "open %s successful\n", input_dev);
  }
  struct input_event ev[64];
  int running;
  ssize_t rb;
  int i;
  running = 1;
  while (running) {
    rb = read(fd, ev, sizeof(ev));
    if (rb < (int)sizeof(struct input_event)) {
      perror("read error");
      exit(1);
    } else {
      fprintf(stdout, "receive char\n");
    }
    for (i = 0; i < (int)(rb / sizeof(struct input_event)); i++) {
      if (EV_KEY == ev[i].type) {
        fprintf(stdout, "%d\n", ev[i].code);
        if (ev[i].value == 1 || ev[i].value == 2) {
          if (ev[i].code == KEY_UP) {
            fprintf(stdout, "UP\n");
          } else if (ev[i].code == KEY_DOWN) {
            fprintf(stdout, "DOWN\n");
          }
        }
      }
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe70(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1202 <main+0x92>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x43c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rdx
mov    %rax,%rcx
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe32(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
je     13ee <main+0x27e>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0x10,-0x428(%rbp)
jge    1271 <main+0x101>
lea    0xdfa(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
movslq -0x428(%rbp),%rcx
shr    $0x4,%rcx
cmp    %ecx,%eax
jge    13e9 <main+0x279>
mov    0x2d2e(%rip),%rax
mov    (%rax),%rax
movslq -0x42c(%rbp),%rdx
lea    -0x420(%rbp),%rcx
shl    $0x4,%rdx
add    %rdx,%rcx
cmp    (%rcx),%rax
jne    13d0 <main+0x260>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
movslq -0x42c(%rbp),%rcx
lea    -0x420(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%edx
lea    0xd7a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movslq -0x42c(%rbp),%rcx
lea    -0x420(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
cmpl   $0x1,0xc(%rax)
je     1340 <main+0x1d0>
movslq -0x42c(%rbp),%rcx
lea    -0x420(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
cmpl   $0x2,0xc(%rax)
jne    13cb <main+0x25b>
movslq -0x42c(%rbp),%rcx
lea    -0x420(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%eax
mov    0x2c71(%rip),%rcx
cmp    (%rcx),%eax
jne    1383 <main+0x213>
mov    0x2c42(%rip),%rax
mov    (%rax),%edi
lea    0xd02(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    13c6 <main+0x256>
movslq -0x42c(%rbp),%rcx
lea    -0x420(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%eax
mov    0x2c36(%rip),%rcx
cmp    (%rcx),%eax
jne    13c1 <main+0x251>
mov    0x2bff(%rip),%rax
mov    (%rax),%edi
lea    0xcc3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    13c6 <main+0x256>
jmp    13cb <main+0x25b>
jmp    13d0 <main+0x260>
jmp    13d5 <main+0x265>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    1292 <main+0x122>
jmp    1227 <main+0xb7>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
jns    11c5 <main+0x65>
mov    0x2e2f(%rip),%rax
mov    (%rax),%ebx
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe8c(%rip),%rsi
lea    0xe51(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %eax,%ebp
mov    0x2de2(%rip),%r14
mov    (%r14),%edi
lea    0xe70(%rip),%rsi
lea    0xe21(%rip),%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1030 <read@plt>
cmp    $0x10,%eax
jge    1212 <main+0xb2>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %eax,%ebx
mov    0x2dc5(%rip),%r12
lea    0xe53(%rip),%r13
mov    %ebp,0xc(%rsp)
jmp    124a <main+0xea>
nopl   0x0(%rax,%rax,1)
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,%ebx
cmp    $0xf,%eax
jle    11fc <main+0x9c>
mov    (%r14),%edi
lea    0xe13(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
shr    $0x4,%ebx
lea    0x1c(%rsp),%r15
jmp    127a <main+0x11a>
mov    (%r14),%edi
xor    %eax,%eax
call   1040 <fprintf@plt>
nop
add    $0x10,%r15
add    $0xffffffffffffffff,%rbx
je     1230 <main+0xd0>
mov    (%r12),%rax
cmp    -0xc(%r15),%rax
jne    1270 <main+0x110>
mov    (%r14),%edi
mov    -0x4(%r15),%ebp
mov    %r13,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r15),%eax
add    $0xffffffff,%eax
cmp    $0x1,%eax
ja     1270 <main+0x110>
lea    0xdd0(%rip),%rsi
mov    0x2d20(%rip),%rax
cmp    (%rax),%ebp
je     1265 <main+0x105>
lea    0xdc2(%rip),%rsi
mov    0x2d16(%rip),%rax
cmp    (%rax),%ebp
je     1265 <main+0x105>
jmp    1270 <main+0x110>

LLVM IR (-O0):
%struct.input_event = type { i64, i32, i32 }
@.str = [52 x i8] c"/dev/input/by-path/platform-i8042-serio-0-event-kbd\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [20 x i8] c"open %s failed: %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.2 = [20 x i8] c"open %s successful\0A\00"
@.str.3 = [11 x i8] c"read error\00"
@.str.4 = [14 x i8] c"receive char\0A\00"
@EV_KEY = global i64 0
@.str.5 = [4 x i8] c"%d\0A\00"
@KEY_UP = global i32 0
@.str.6 = [4 x i8] c"UP\0A\00"
@KEY_DOWN = global i32 0
@.str.7 = [6 x i8] c"DOWN\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [64 x %struct.input_event]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8** %7
%12 = load i8*, i8** %7
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %7
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i32, i32* @stdout
%25 = load i8*, i8** %7
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %23
store i32 1, i32* %9
br label %28
28: ; preds = %109, %27
%29 = load i32, i32* %9
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %110
31: ; preds = %28
%32 = load i32, i32* %6
%33 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %8, i64 0, i64 0
%34 = call i32 @read(i32 %32, %struct.input_event* %33, i32 1024)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp slt i32 %35, 16
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %31
%41 = load i32, i32* @stdout
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %40
store i32 0, i32* %11
br label %44
44: ; preds = %106, %43
%45 = load i32, i32* %11
%46 = load i32, i32* %10
%47 = sext i32 %46 to i64
%48 = udiv i64 %47, 16
%49 = trunc i64 %48 to i32
%50 = icmp slt i32 %45, %49
br i1 %50, label %51, label %109
51: ; preds = %44
%52 = load i64, i64* @EV_KEY
%53 = load i32, i32* %11
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %8, i64 0, i64 %54
%56 = getelementptr inbounds %struct.input_event, %struct.input_event* %55, i32 0, i32 0
%57 = load i64, i64* %56
%58 = icmp eq i64 %52, %57
br i1 %58, label %59, label %105
59: ; preds = %51
%60 = load i32, i32* @stdout
%61 = load i32, i32* %11
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %8, i64 0, i64 %62
%64 = getelementptr inbounds %struct.input_event, %struct.input_event* %63, i32 0, i32 1
%65 = load i32, i32* %64
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %65)
%67 = load i32, i32* %11
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %8, i64 0, i64 %68
%70 = getelementptr inbounds %struct.input_event, %struct.input_event* %69, i32 0, i32 2
%71 = load i32, i32* %70
%72 = icmp eq i32 %71, 1
br i1 %72, label %80, label %73
73: ; preds = %59
%74 = load i32, i32* %11
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %8, i64 0, i64 %75
%77 = getelementptr inbounds %struct.input_event, %struct.input_event* %76, i32 0, i32 2
%78 = load i32, i32* %77
%79 = icmp eq i32 %78, 2
br i1 %79, label %80, label %104
80: ; preds = %73, %59
%81 = load i32, i32* %11
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %8, i64 0, i64 %82
%84 = getelementptr inbounds %struct.input_event, %struct.input_event* %83, i32 0, i32 1
%85 = load i32, i32* %84
%86 = load i32, i32* @KEY_UP
%87 = icmp eq i32 %85, %86
br i1 %87, label %88, label %91
88: ; preds = %80
%89 = load i32, i32* @stdout
%90 = call i32 (i32, i8*, ...) @fprintf(i32 %89, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
br label %103
91: ; preds = %80
%92 = load i32, i32* %11
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %8, i64 0, i64 %93
%95 = getelementptr inbounds %struct.input_event, %struct.input_event* %94, i32 0, i32 1
%96 = load i32, i32* %95
%97 = load i32, i32* @KEY_DOWN
%98 = icmp eq i32 %96, %97
br i1 %98, label %99, label %102
99: ; preds = %91
%100 = load i32, i32* @stdout
%101 = call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %102
102: ; preds = %99, %91
br label %103
103: ; preds = %102, %88
br label %104
104: ; preds = %103, %73
br label %105
105: ; preds = %104, %51
br label %106
106: ; preds = %105
%107 = load i32, i32* %11
%108 = add nsw i32 %107, 1
store i32 %108, i32* %11
br label %44
109: ; preds = %44
br label %28
110: ; preds = %28
%111 = load i32, i32* %6
%112 = call i32 @close(i32 %111)
ret i32 0
}

LLVM IR (-O2):
%struct.input_event = type { i64, i32, i32 }
@.str = [52 x i8] c"/dev/input/by-path/platform-i8042-serio-0-event-kbd\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [20 x i8] c"open %s failed: %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.2 = [20 x i8] c"open %s successful\0A\00"
@.str.3 = [11 x i8] c"read error\00"
@.str.4 = [14 x i8] c"receive char\0A\00"
@EV_KEY = global i64 0
@.str.5 = [4 x i8] c"%d\0A\00"
@KEY_UP = global i32 0
@.str.6 = [4 x i8] c"UP\0A\00"
@KEY_DOWN = global i32 0
@.str.7 = [6 x i8] c"DOWN\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [64 x %struct.input_event]
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @stdout
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
%16 = bitcast [64 x %struct.input_event]* %3 to i8*
%17 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %3, i64 0, i64 0
%18 = call i32 @read(i32 %5, %struct.input_event* %17, i32 1024)
%19 = icmp slt i32 %18, 16
br i1 %19, label %23, label %26
20: ; preds = %57
%21 = call i32 @read(i32 %5, %struct.input_event* %17, i32 1024)
%22 = icmp slt i32 %21, 16
br i1 %22, label %23, label %26
23: ; preds = %20, %13
%24 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %13, %20
%27 = phi i32 [ %21, %20 ], [ %18, %13 ]
%28 = load i32, i32* @stdout
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%30 = lshr i32 %27, 4
%31 = zext i32 %30 to i64
br label %32
32: ; preds = %26, %57
%33 = phi i64 [ 0, %26 ], [ %58, %57 ]
%34 = load i64, i64* @EV_KEY
%35 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %3, i64 0, i64 %33, i32 0
%36 = load i64, i64* %35
%37 = icmp eq i64 %34, %36
br i1 %37, label %38, label %57
38: ; preds = %32
%39 = load i32, i32* @stdout
%40 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %3, i64 0, i64 %33, i32 1
%41 = load i32, i32* %40
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %41)
%43 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %3, i64 0, i64 %33, i32 2
%44 = load i32, i32* %43
%45 = add i32 %44, -1
%46 = icmp ult i32 %45, 2
br i1 %46, label %47, label %57
47: ; preds = %38
%48 = load i32, i32* @KEY_UP
%49 = icmp eq i32 %41, %48
br i1 %49, label %53, label %50
50: ; preds = %47
%51 = load i32, i32* @KEY_DOWN
%52 = icmp eq i32 %41, %51
br i1 %52, label %53, label %57
53: ; preds = %50, %47
%54 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), %47 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), %50 ]
%55 = load i32, i32* @stdout
%56 = tail call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* %54)
br label %57
57: ; preds = %53, %38, %32, %50
%58 = add nuw nsw i64 %33, 1
%59 = icmp eq i64 %58, %31
br i1 %59, label %20, label %32
}