#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 timeval {
  int tv_sec;
  int tv_usec;
};
struct input_event {
  int type;
  int code;
  int value;
};
typedef int /*<<< orphan*/ levt;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ O_RDWR;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
unsigned int read(int, struct input_event *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int main(int argc, char *argv[]) {
  int intput_fd = -1;
  fd_set rfds, wfds;
  struct timeval tv;
  int retval;
  int exit_enable = 0;
  int lace = 0x1000;
  unsigned int i, readlen;
  struct input_event levt[64];
  if (argc > 1)
    intput_fd = open(argv[1], O_RDWR);
  else
    intput_fd = open("/dev/input/event0", O_RDWR);
  if (intput_fd < 0)
    printf("can not open event device....\n");
  else
    printf("open event success\n");
  while (!exit_enable) {
    tv.tv_sec = 1;
    tv.tv_usec = 10;
    FD_ZERO(&rfds);
    FD_SET(intput_fd, &rfds);
    retval = select(intput_fd + 1, &rfds, NULL, NULL, &tv);
    if (retval == -1) {
      exit_enable = 1;
    } else if (retval) {
      if (FD_ISSET(intput_fd, &rfds)) {
        readlen = read(intput_fd, levt, sizeof(levt));
        for (i = 0; i < readlen / sizeof(levt[0]); i++) {
          printf("----type = %d, code = 0x%x, value = %d-------------\n",
                 levt[i].type, levt[i].code, levt[i].value);
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x350,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x1000,-0x34(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11c6 <main+0x56>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
jmp    11de <main+0x6e>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    0xe2a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fb <main+0x8b>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1209 <main+0x99>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120e <main+0x9e>
cmpl   $0x0,-0x30(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1224 <main+0xb4>
jmp    1353 <main+0x1e3>
movl   $0x1,-0x28(%rbp)
movl   $0xa,-0x24(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    127a <main+0x10a>
movl   $0x1,-0x30(%rbp)
jmp    134e <main+0x1de>
cmpl   $0x0,-0x2c(%rbp)
je     1349 <main+0x1d9>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1344 <main+0x1d4>
mov    -0x14(%rbp),%edi
lea    -0x340(%rbp),%rsi
mov    $0x300,%edx
call   1050 <read@plt>
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %rax,-0x348(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0xc,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rcx
mov    -0x348(%rbp),%rax
cmp    %rcx,%rax
jae    133f <main+0x1cf>
mov    -0x38(%rbp),%eax
mov    %eax,%ecx
lea    -0x340(%rbp),%rax
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%eax
mov    %eax,%ecx
lea    -0x340(%rbp),%rax
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    0x4(%rax),%edx
mov    -0x38(%rbp),%eax
mov    %eax,%ecx
lea    -0x340(%rbp),%rax
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    0x8(%rax),%ecx
lea    0xd1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    12b8 <main+0x148>
jmp    1344 <main+0x1d4>
jmp    1349 <main+0x1d9>
jmp    134e <main+0x1de>
jmp    120e <main+0x9e>
xor    %eax,%eax
add    $0x350,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x318,%rsp
cmp    $0x2,%edi
jl     119c <main+0x1c>
mov    0x8(%rsi),%rdi
jmp    11a3 <main+0x23>
lea    0xe5d(%rip),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r13d
test   %eax,%eax
lea    0xe9d(%rip),%rax
lea    0xe83(%rip),%rdi
cmovs  %rax,%rdi
call   1030 <puts@plt>
mov    %r13d,%r14d
add    $0x1,%r14d
lea    0xc(%rsp),%rbp
lea    0xe32(%rip),%rbx
movabs $0xa00000001,%rax
mov    %rax,(%rsp)
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
call   10a0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rsp,%r8
call   1080 <select@plt>
test   %eax,%eax
je     11e0 <main+0x60>
cmp    $0xffffffff,%eax
je     1287 <main+0x107>
mov    %r13d,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11e0 <main+0x60>
mov    %r13d,%edi
lea    0x10(%rsp),%rsi
mov    $0x300,%edx
call   1060 <read@plt>
cmp    $0xc,%eax
jb     11e0 <main+0x60>
mov    %eax,%eax
mov    $0xaaaaaaab,%ecx
imul   %rcx,%rax
shr    $0x21,%rax
and    $0xfffffffc,%eax
lea    (%rax,%rax,2),%r12
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    0x10(%rsp,%r15,1),%esi
mov    0x14(%rsp,%r15,1),%edx
mov    0x18(%rsp,%r15,1),%ecx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xc,%r15
cmp    %r15,%r12
jne    1260 <main+0xe0>
jmp    11e0 <main+0x60>
xor    %eax,%eax
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.input_event = type { i32, i32, i32 }
@O_RDWR = global i32 0
@.str = [18 x i8] c"/dev/input/event0\00"
@.str.1 = [31 x i8] c"can not open event device....\0A\00"
@.str.2 = [20 x i8] c"open event success\0A\00"
@.str.3 = [53 x i8] c"----type = %d, code = 0x%x, 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 i32
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [64 x %struct.input_event]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 0, i32* %11
store i32 4096, i32* %12
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 1
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDWR
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %6
br label %27
24: ; preds = %2
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %25)
store i32 %26, i32* %6
br label %27
27: ; preds = %24, %18
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %34
32: ; preds = %27
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %30
br label %35
35: ; preds = %92, %34
%36 = load i32, i32* %11
%37 = icmp ne i32 %36, 0
%38 = xor i1 %37, true
br i1 %38, label %39, label %93
39: ; preds = %35
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 1, i32* %40
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i32 10, i32* %41
%42 = call i32 @FD_ZERO(i32* %7)
%43 = load i32, i32* %6
%44 = call i32 @FD_SET(i32 %43, i32* %7)
%45 = load i32, i32* %6
%46 = add nsw i32 %45, 1
%47 = call i32 @select(i32 %46, i32* %7, i32* null, i32* null, %struct.timeval* %9)
store i32 %47, i32* %10
%48 = load i32, i32* %10
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %51
50: ; preds = %39
store i32 1, i32* %11
br label %92
51: ; preds = %39
%52 = load i32, i32* %10
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %91
54: ; preds = %51
%55 = load i32, i32* %6
%56 = call i64 @FD_ISSET(i32 %55, i32* %7)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %90
58: ; preds = %54
%59 = load i32, i32* %6
%60 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %15, i64 0, i64 0
%61 = call i32 @read(i32 %59, %struct.input_event* %60, i32 768)
store i32 %61, i32* %14
store i32 0, i32* %13
br label %62
62: ; preds = %86, %58
%63 = load i32, i32* %13
%64 = zext i32 %63 to i64
%65 = load i32, i32* %14
%66 = zext i32 %65 to i64
%67 = udiv i64 %66, 12
%68 = icmp ult i64 %64, %67
br i1 %68, label %69, label %89
69: ; preds = %62
%70 = load i32, i32* %13
%71 = zext i32 %70 to i64
%72 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %15, i64 0, i64 %71
%73 = getelementptr inbounds %struct.input_event, %struct.input_event* %72, i32 0, i32 0
%74 = load i32, i32* %73
%75 = load i32, i32* %13
%76 = zext i32 %75 to i64
%77 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %15, i64 0, i64 %76
%78 = getelementptr inbounds %struct.input_event, %struct.input_event* %77, i32 0, i32 1
%79 = load i32, i32* %78
%80 = load i32, i32* %13
%81 = zext i32 %80 to i64
%82 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %15, i64 0, i64 %81
%83 = getelementptr inbounds %struct.input_event, %struct.input_event* %82, i32 0, i32 2
%84 = load i32, i32* %83
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %74, i32 %79, i32 %84)
br label %86
86: ; preds = %69
%87 = load i32, i32* %13
%88 = add i32 %87, 1
store i32 %88, i32* %13
br label %62
89: ; preds = %62
br label %90
90: ; preds = %89, %54
br label %91
91: ; preds = %90, %51
br label %92
92: ; preds = %91, %50
br label %35
93: ; preds = %35
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.input_event = type { i32, i32, i32 }
@O_RDWR = global i32 0
@.str = [18 x i8] c"/dev/input/event0\00"
@.str.3 = [53 x i8] c"----type = %d, code = 0x%x, value = %d-------------\0A\00"
@str = [19 x i8] c"open event success\00"
@str.4 = [30 x i8] c"can not open event device....\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca [64 x %struct.input_event]
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = bitcast [64 x %struct.input_event]* %5 to i8*
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
br label %13
13: ; preds = %2, %10
%14 = phi i8* [ %12, %10 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %2 ]
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = icmp slt i32 %16, 0
%18 = select i1 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @str.4, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0)
%19 = tail call i32 @puts(i8* dereferenceable(1) %18)
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%22 = add nsw i32 %16, 1
%23 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %5, i64 0, i64 0
br label %24
24: ; preds = %28, %13
store i32 1, i32* %20
store i32 10, i32* %21
%25 = call i32 @FD_ZERO(i32* %3)
%26 = call i32 @FD_SET(i32 %16, i32* %3)
%27 = call i32 @select(i32 %22, i32* %3, i32* null, i32* null, %struct.timeval* %4)
switch i32 %27, label %29 [
i32 -1, label %49
i32 0, label %28
]
28: ; preds = %38, %24, %32, %29
br label %24
29: ; preds = %24
%30 = call i64 @FD_ISSET(i32 %16, i32* %3)
%31 = icmp eq i64 %30, 0
br i1 %31, label %28, label %32
32: ; preds = %29
%33 = call i32 @read(i32 %16, %struct.input_event* %23, i32 768)
%34 = icmp ult i32 %33, 12
br i1 %34, label %28, label %35
35: ; preds = %32
%36 = udiv i32 %33, 12
%37 = zext i32 %36 to i64
br label %38
38: ; preds = %35, %38
%39 = phi i64 [ 0, %35 ], [ %47, %38 ]
%40 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %5, i64 0, i64 %39, i32 0
%41 = load i32, i32* %40
%42 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %5, i64 0, i64 %39, i32 1
%43 = load i32, i32* %42
%44 = getelementptr inbounds [64 x %struct.input_event], [64 x %struct.input_event]* %5, i64 0, i64 %39, i32 2
%45 = load i32, i32* %44
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %41, i32 %43, i32 %45)
%47 = add nuw nsw i64 %39, 1
%48 = icmp eq i64 %47, %37
br i1 %48, label %28, label %38
49: ; preds = %24
ret i32 0
}