#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_RDWR;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, struct input_event *, int);
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  int fd = -1;
  int i = 0;
  const char *device_name = NULL;
  struct input_event rd_evt;
  int pos[2] = {0};
  device_name = (argc == 1) ? "/dev/event0" : argv[1];
  printf("device_name=%s\n", device_name);
  if ((fd = open(device_name, O_RDWR)) < 0) {
    printf("Usage: elf event\n");
    _exit(-1);
  }
  while (1) {
    if (read(fd, &rd_evt, sizeof(struct input_event)) <= 0) {
      perror("read");
      _exit(-1);
    }
    if ((rd_evt.code == 0) && (rd_evt.value > 0)) {
      pos[0] = 0;
      pos[1] = 0;
      pos[0] = rd_evt.value;
    } else if ((rd_evt.code == 1) && (rd_evt.value > 0)) {
      pos[1] = rd_evt.value;
      printf("the position is x = %d y = %d\n", pos[0], pos[1]);
    }
    usleep(100);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
cmpl   $0x1,-0x8(%rbp)
jne    11c6 <main+0x56>
lea    0xe43(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    11d2 <main+0x62>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1222 <main+0xb2>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1030 <_exit@plt>
jmp    1227 <main+0xb7>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jg     1258 <main+0xe8>
lea    0xde5(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x28(%rbp)
jne    1285 <main+0x115>
cmpl   $0x0,-0x24(%rbp)
jle    1285 <main+0x115>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    12b8 <main+0x148>
cmpl   $0x1,-0x28(%rbp)
jne    12b3 <main+0x143>
cmpl   $0x0,-0x24(%rbp)
jle    12b3 <main+0x143>
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12b8 <main+0x148>
mov    $0x64,%edi
call   1090 <usleep@plt>
jmp    1227 <main+0xb7>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jne    1185 <main+0x15>
lea    0xe7d(%rip),%rbx
jmp    1189 <main+0x19>
mov    0x8(%rsi),%rbx
lea    0xe7c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
jns    11c5 <main+0x55>
lea    0xe8a(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1030 <_exit@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1060 <read@plt>
test   %rax,%rax
jg     11f1 <main+0x81>
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1030 <_exit@plt>
mov    %rsp,%r14
lea    0xe26(%rip),%r15
xor    %ebp,%ebp
jmp    1220 <main+0xb0>
nop
mov    %edx,%ebp
mov    $0x64,%edi
call   1090 <usleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    11db <main+0x6b>
mov    (%rsp),%eax
mov    0x4(%rsp),%edx
test   %eax,%eax
jne    122f <main+0xbf>
test   %edx,%edx
jg     1200 <main+0x90>
cmp    $0x1,%eax
jne    1202 <main+0x92>
test   %edx,%edx
jle    1202 <main+0x92>
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1202 <main+0x92>

LLVM IR (-O0):
%struct.input_event = type { i32, i32 }
@.str = [12 x i8] c"/dev/event0\00"
@.str.1 = [16 x i8] c"device_name=%s\0A\00"
@O_RDWR = global i32 0
@.str.2 = [18 x i8] c"Usage: elf event\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [31 x i8] c"the position is x = %d y = %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 i8*
%9 = alloca %struct.input_event
%10 = alloca [2 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 0, i32* %7
store i8* null, i8** %8
%11 = bitcast [2 x i32]* %10 to i8*
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %15
14: ; preds = %2
br label %19
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
br label %19
19: ; preds = %15, %14
%20 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %14 ], [ %18, %15 ]
store i8* %20, i8** %8
%21 = load i8*, i8** %8
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %8
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @_exit(i32 -1)
unreachable
30: ; preds = %19
br label %31
31: ; preds = %30, %70
%32 = load i32, i32* %6
%33 = call i64 @read(i32 %32, %struct.input_event* %9, i32 8)
%34 = icmp sle i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @_exit(i32 -1)
unreachable
38: ; preds = %31
%39 = getelementptr inbounds %struct.input_event, %struct.input_event* %9, i32 0, i32 0
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %52
42: ; preds = %38
%43 = getelementptr inbounds %struct.input_event, %struct.input_event* %9, i32 0, i32 1
%44 = load i32, i32* %43
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %42
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
store i32 0, i32* %47
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
store i32 0, i32* %48
%49 = getelementptr inbounds %struct.input_event, %struct.input_event* %9, i32 0, i32 1
%50 = load i32, i32* %49
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
store i32 %50, i32* %51
br label %70
52: ; preds = %42, %38
%53 = getelementptr inbounds %struct.input_event, %struct.input_event* %9, i32 0, i32 0
%54 = load i32, i32* %53
%55 = icmp eq i32 %54, 1
br i1 %55, label %56, label %69
56: ; preds = %52
%57 = getelementptr inbounds %struct.input_event, %struct.input_event* %9, i32 0, i32 1
%58 = load i32, i32* %57
%59 = icmp sgt i32 %58, 0
br i1 %59, label %60, label %69
60: ; preds = %56
%61 = getelementptr inbounds %struct.input_event, %struct.input_event* %9, i32 0, i32 1
%62 = load i32, i32* %61
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
store i32 %62, i32* %63
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%65 = load i32, i32* %64
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%67 = load i32, i32* %66
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %65, i32 %67)
br label %69
69: ; preds = %60, %56, %52
br label %70
70: ; preds = %69, %46
%71 = call i32 @usleep(i32 100)
br label %31
}

LLVM IR (-O2):
%struct.input_event = type { i32, i32 }
@.str = [12 x i8] c"/dev/event0\00"
@.str.1 = [16 x i8] c"device_name=%s\0A\00"
@O_RDWR = global i32 0
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [31 x i8] c"the position is x = %d y = %d\0A\00"
@str = [17 x i8] c"Usage: elf event\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.input_event
%4 = bitcast %struct.input_event* %3 to i8*
%5 = icmp eq i32 %0, 1
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %2, %6
%10 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %2 ]
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* %10, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %9
%16 = call i64 @read(i32 %13, %struct.input_event* %3, i32 8)
%17 = icmp slt i64 %16, 1
br i1 %17, label %24, label %18
18: ; preds = %15
%19 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 0
%20 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 1
br label %27
21: ; preds = %9
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @_exit(i32 -1)
unreachable
24: ; preds = %39, %15
%25 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%26 = tail call i32 @_exit(i32 -1)
unreachable
27: ; preds = %18, %39
%28 = phi i32 [ 0, %18 ], [ %40, %39 ]
%29 = load i32, i32* %19
%30 = icmp eq i32 %29, 0
%31 = load i32, i32* %20
%32 = icmp sgt i32 %31, 0
%33 = select i1 %30, i1 %32, i1 false
br i1 %33, label %39, label %34
34: ; preds = %27
%35 = icmp eq i32 %29, 1
%36 = select i1 %35, i1 %32, i1 false
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %28, i32 %31)
br label %39
39: ; preds = %27, %34, %37
%40 = phi i32 [ %28, %37 ], [ %28, %34 ], [ %31, %27 ]
%41 = tail call i32 @usleep(i32 100)
%42 = call i64 @read(i32 %13, %struct.input_event* %3, i32 8)
%43 = icmp slt i64 %42, 1
br i1 %43, label %24, label %27
}