#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;
  scalar_t__ tv_usec;
};
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_RDONLY;
int STDIN_FILENO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int main(void) {
  struct timeval tv;
  fd_set readfds;
  int ret, mfd;
  mfd = open("/dev/input/mouse0", O_RDONLY);
  if (mfd < 0) {
    printf("mouse device open fail\n");
    return 1;
  }
  FD_ZERO(&readfds);
  FD_SET(STDIN_FILENO, &readfds);
  FD_SET(mfd, &readfds);
  tv.tv_sec = 5;
  tv.tv_usec = 0;
  ret = select(mfd + 1, &readfds, NULL, NULL, &tv);
  if (ret == -1)
    return 1;
  else if (!ret)
    return 0;
  if (FD_ISSET(STDIN_FILENO, &readfds)) {
    char buf[1024 + 1];
    int len;
    len = read(STDIN_FILENO, buf, 1024);
    if (len == -1)
      return 1;
    if (len) {
      buf[len] = '\0';
      printf("read: %s\n", buf);
    }
    return 0;
  }
  if (FD_ISSET(mfd, &readfds)) {
    printf("mouse event occurred\n");
    return 0;
  }
  fprintf(stderr, "This should not happen!\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ce <main+0x4e>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1330 <main+0x1b0>
lea    -0x1c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x24(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   10a0 <FD_SET@plt>
movl   $0x5,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1237 <main+0xb7>
movl   $0x1,-0x4(%rbp)
jmp    1330 <main+0x1b0>
cmpl   $0x0,-0x20(%rbp)
jne    124d <main+0xcd>
movl   $0x0,-0x4(%rbp)
jmp    1330 <main+0x1b0>
jmp    1252 <main+0xd2>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12e4 <main+0x164>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0xffffffff,-0x434(%rbp)
jne    12a7 <main+0x127>
movl   $0x1,-0x4(%rbp)
jmp    1330 <main+0x1b0>
cmpl   $0x0,-0x434(%rbp)
je     12d8 <main+0x158>
movslq -0x434(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rsi
lea    0xd59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1330 <main+0x1b0>
mov    -0x24(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1314 <main+0x194>
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1330 <main+0x1b0>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
lea    0xd26(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x430,%rsp
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1218 <main+0x88>
mov    %eax,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    0x2e06(%rip),%r14
mov    (%r14),%edi
mov    %rbx,%rsi
call   10b0 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10b0 <FD_SET@plt>
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %ebp,%edi
add    $0x1,%edi
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <select@plt>
mov    $0x1,%ebx
cmp    $0xffffffff,%eax
je     1229 <main+0x99>
test   %eax,%eax
jne    1237 <main+0xa7>
xor    %ebx,%ebx
jmp    1229 <main+0x99>
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x430,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%r14),%edi
lea    0xc(%rsp),%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1280 <main+0xf0>
mov    (%r14),%edi
lea    0x20(%rsp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     1224 <main+0x94>
test   %eax,%eax
je     1214 <main+0x84>
cltq
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xda0(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1214 <main+0x84>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12a1 <main+0x111>
lea    0xd9d(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    1229 <main+0x99>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd6b(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1229 <main+0x99>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [18 x i8] c"/dev/input/mouse0\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"mouse device open fail\0A\00"
@STDIN_FILENO = global i32 0
@.str.2 = [10 x i8] c"read: %s\0A\00"
@.str.3 = [22 x i8] c"mouse event occurred\0A\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"This should not happen!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1025 x i8]
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %63
14: ; preds = %0
%15 = call i32 @FD_ZERO(i32* %3)
%16 = load i32, i32* @STDIN_FILENO
%17 = call i32 @FD_SET(i32 %16, i32* %3)
%18 = load i32, i32* %5
%19 = call i32 @FD_SET(i32 %18, i32* %3)
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
store i32 5, i32* %20
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
store i64 0, i64* %21
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
%24 = call i32 @select(i32 %23, i32* %3, i32* null, i32* null, %struct.timeval* %2)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %14
store i32 1, i32* %1
br label %63
28: ; preds = %14
%29 = load i32, i32* %4
%30 = icmp ne i32 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %28
store i32 0, i32* %1
br label %63
32: ; preds = %28
br label %33
33: ; preds = %32
%34 = load i32, i32* @STDIN_FILENO
%35 = call i64 @FD_ISSET(i32 %34, i32* %3)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %54
37: ; preds = %33
%38 = load i32, i32* @STDIN_FILENO
%39 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 1024)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %37
store i32 1, i32* %1
br label %63
44: ; preds = %37
%45 = load i32, i32* %7
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load i32, i32* %7
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 %49
store i8 0, i8* %50
%51 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %51)
br label %53
53: ; preds = %47, %44
store i32 0, i32* %1
br label %63
54: ; preds = %33
%55 = load i32, i32* %5
%56 = call i64 @FD_ISSET(i32 %55, i32* %3)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %63
60: ; preds = %54
%61 = load i32, i32* @stderr
%62 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %63
63: ; preds = %60, %58, %53, %43, %31, %27, %12
%64 = load i32, i32* %1
ret i32 %64
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [18 x i8] c"/dev/input/mouse0\00"
@O_RDONLY = global i32 0
@STDIN_FILENO = global i32 0
@.str.2 = [10 x i8] c"read: %s\0A\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"This should not happen!\0A\00"
@str = [21 x i8] c"mouse event occurred\00"
@str.5 = [23 x i8] c"mouse device open fail\00"
define i32 @main() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = alloca [1025 x i8]
%4 = bitcast %struct.timeval* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.5, i64 0, i64 0))
br label %44
11: ; preds = %0
%12 = call i32 @FD_ZERO(i32* %2)
%13 = load i32, i32* @STDIN_FILENO
%14 = call i32 @FD_SET(i32 %13, i32* %2)
%15 = call i32 @FD_SET(i32 %7, i32* %2)
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
store i32 5, i32* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
store i64 0, i64* %17
%18 = add nuw nsw i32 %7, 1
%19 = call i32 @select(i32 %18, i32* %2, i32* null, i32* null, %struct.timeval* %1)
switch i32 %19, label %21 [
i32 -1, label %44
i32 0, label %20
]
20: ; preds = %11
br label %44
21: ; preds = %11
%22 = load i32, i32* @STDIN_FILENO
%23 = call i64 @FD_ISSET(i32 %22, i32* %2)
%24 = icmp eq i64 %23, 0
br i1 %24, label %36, label %25
25: ; preds = %21
%26 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @read(i32 %27, i8* %26, i32 1024)
switch i32 %28, label %29 [
i32 -1, label %34
i32 0, label %33
]
29: ; preds = %25
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 %30
store i8 0, i8* %31
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %33
33: ; preds = %25, %29
br label %34
34: ; preds = %25, %33
%35 = phi i32 [ 0, %33 ], [ 1, %25 ]
br label %44
36: ; preds = %21
%37 = call i64 @FD_ISSET(i32 %7, i32* %2)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %44
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %11, %41, %39, %34, %20, %9
%45 = phi i32 [ 1, %9 ], [ %35, %34 ], [ 0, %39 ], [ 1, %41 ], [ 0, %20 ], [ 1, %11 ]
ret i32 %45
}