#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 */
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, struct timeval *);
int main(int argc, char *argv[]) {
  int globalvarfd = 0;
  fd_set fds;
  struct timeval tv;
  int input = 0;
  globalvarfd = open("/dev/globalvar0", O_RDONLY);
  if (globalvarfd < 0) {
    perror("open globalvar0 failed\n");
    return -1;
  }
  do {
    FD_ZERO(&fds);
    FD_SET(globalvarfd, &fds);
    tv.tv_sec = 5;
    tv.tv_usec = 0;
    select(globalvarfd + 1, &fds, NULL, NULL, &tv);
    if (!FD_ISSET(globalvarfd, &fds)) {
      printf("no input data in 5 seconds\n");
      continue;
    }
    read(globalvarfd, &input, sizeof(int));
    printf("read:%d\n", input);
  } while (input != 0xFF);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11de <main+0x5e>
lea    0xe43(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <main+0xff>
jmp    11e3 <main+0x63>
lea    -0x18(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10a0 <FD_SET@plt>
movl   $0x5,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
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    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1249 <main+0xc9>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    126b <main+0xeb>
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    -0x2c(%rbp),%esi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0xff,-0x2c(%rbp)
jne    11e3 <main+0x63>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x0,(%rsp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1282 <main+0xf2>
mov    %eax,%ebx
mov    %eax,%r14d
add    $0x1,%r14d
lea    0x4(%rsp),%rbp
lea    0x8(%rsp),%r15
lea    0xe4c(%rip),%r12
xor    %r13d,%r13d
jmp    1219 <main+0x89>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %rsp,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    (%rsp),%r13d
mov    %r12,%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0xff,%r13d
je     1271 <main+0xe1>
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10b0 <FD_SET@plt>
movl   $0x5,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1080 <select@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
jne    11f0 <main+0x60>
lea    0xdce(%rip),%rdi
call   1030 <puts@plt>
cmp    $0xff,%r13d
jne    1219 <main+0x89>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd87(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    1273 <main+0xe3>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [16 x i8] c"/dev/globalvar0\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"open globalvar0 failed\0A\00"
@.str.2 = [28 x i8] c"no input data in 5 seconds\0A\00"
@.str.3 = [9 x i8] c"read:%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.timeval
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %9
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %40
16: ; preds = %2
br label %17
17: ; preds = %36, %16
%18 = call i32 @FD_ZERO(i32* %7)
%19 = load i32, i32* %6
%20 = call i32 @FD_SET(i32 %19, i32* %7)
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 5, i32* %21
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %22
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
%25 = call i32 @select(i32 %24, i32* %7, i32* null, i32* null, %struct.timeval* %8)
%26 = load i32, i32* %6
%27 = call i32 @FD_ISSET(i32 %26, i32* %7)
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %17
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %36
31: ; preds = %17
%32 = load i32, i32* %6
%33 = call i32 @read(i32 %32, i32* %9, i32 4)
%34 = load i32, i32* %9
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %31, %29
%37 = load i32, i32* %9
%38 = icmp ne i32 %37, 255
br i1 %38, label %17, label %39
39: ; preds = %36
store i32 0, i32* %3
br label %40
40: ; preds = %39, %14
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [16 x i8] c"/dev/globalvar0\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"open globalvar0 failed\0A\00"
@.str.3 = [9 x i8] c"read:%d\0A\00"
@str = [27 x i8] c"no input data in 5 seconds\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%15 = add nuw nsw i32 %10, 1
br label %18
16: ; preds = %2
%17 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %34
18: ; preds = %12, %31
%19 = phi i32 [ 0, %12 ], [ %32, %31 ]
%20 = call i32 @FD_ZERO(i32* %3)
%21 = call i32 @FD_SET(i32 %10, i32* %3)
store i32 5, i32* %13
store i64 0, i64* %14
%22 = call i32 @select(i32 %15, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%23 = call i32 @FD_ISSET(i32 %10, i32* %3)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %31
27: ; preds = %18
%28 = call i32 @read(i32 %10, i32* %5, i32 4)
%29 = load i32, i32* %5
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %25
%32 = phi i32 [ %29, %27 ], [ %19, %25 ]
%33 = icmp eq i32 %32, 255
br i1 %33, label %34, label %18
34: ; preds = %31, %16
%35 = phi i32 [ -1, %16 ], [ 0, %31 ]
ret i32 %35
}