#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 member_0;
  int /*<<< orphan*/ member_1;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
char *strerror(scalar_t__);
int is_data_available(int fd) {
  fd_set rd, err;
  struct timeval wait = {1, 0};
  FD_ZERO(&rd);
  FD_SET(fd, &rd);
  int ret = select(fd + 1, &rd, NULL, &err, &wait);
  if (ret == 0) {
    return ret;
  }
  if (ret > 0) {
    if (FD_ISSET(fd, &rd)) {
      return ret;
    }
  } else if (ret < 0) {
    if (errno != EINTR) {
      perror("select");
      printf("Error on select(): %s\n", strerror(errno));
      errno = 0;
      ret = -1;
    } else {
      errno = 0;
      ret = 0;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<is_data_available>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0xe7e(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x10(%rbp),%rcx
lea    -0x18(%rbp),%r8
call   1060 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11ce <is_data_available+0x5e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128b <is_data_available+0x11b>
cmpl   $0x0,-0x1c(%rbp)
jle    11fe <is_data_available+0x8e>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11f9 <is_data_available+0x89>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128b <is_data_available+0x11b>
jmp    1285 <is_data_available+0x115>
cmpl   $0x0,-0x1c(%rbp)
jge    1280 <is_data_available+0x110>
mov    0x2dd1(%rip),%rax
mov    (%rax),%rax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%rax
je     1266 <is_data_available+0xf6>
lea    0xddf(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xdc8(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d8d(%rip),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x1c(%rbp)
jmp    127b <is_data_available+0x10b>
mov    0x2d73(%rip),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x1c(%rbp)
jmp    1280 <is_data_available+0x110>
jmp    1285 <is_data_available+0x115>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_data_available>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
movq   $0x1,0x10(%rsp)
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
lea    0x1(%rbx),%edi
xor    %r14d,%r14d
lea    0x1c(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %rbp,%rsi
xor    %edx,%edx
call   1060 <select@plt>
test   %eax,%eax
je     11ea <is_data_available+0x7a>
jle    11cd <is_data_available+0x5d>
mov    %eax,%ebp
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
mov    %ebp,%r14d
jmp    11ea <is_data_available+0x7a>
mov    0x2e0c(%rip),%rbx
mov    (%rbx),%rax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%rax
jne    11f6 <is_data_available+0x86>
movq   $0x0,(%rbx)
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe03(%rip),%rdi
call   1070 <perror@plt>
mov    (%rbx),%rdi
call   1080 <strerror@plt>
lea    0xdf6(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
movq   $0x0,(%rbx)
mov    $0xffffffff,%r14d
jmp    11ea <is_data_available+0x7a>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@__const.is_data_available.wait = %struct.timeval { i32 1, i32 0 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [7 x i8] c"select\00"
@.str.1 = [23 x i8] c"Error on select(): %s\0A\00"
define i32 @is_data_available(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
store i32 %0, i32* %3
%8 = bitcast %struct.timeval* %6 to i8*
%9 = call i32 @FD_ZERO(i32* %4)
%10 = load i32, i32* %3
%11 = call i32 @FD_SET(i32 %10, i32* %4)
%12 = load i32, i32* %3
%13 = add nsw i32 %12, 1
%14 = call i32 @select(i32 %13, i32* %4, i32* null, i32* %5, %struct.timeval* %6)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = load i32, i32* %7
store i32 %18, i32* %2
br label %46
19: ; preds = %1
%20 = load i32, i32* %7
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = load i32, i32* %3
%24 = call i64 @FD_ISSET(i32 %23, i32* %4)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = load i32, i32* %7
store i32 %27, i32* %2
br label %46
28: ; preds = %22
br label %44
29: ; preds = %19
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %29
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%38 = load i64, i64* @errno
%39 = call i8* @strerror(i64 %38)
%40 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %39)
store i64 0, i64* @errno
store i32 -1, i32* %7
br label %42
41: ; preds = %32
store i64 0, i64* @errno
store i32 0, i32* %7
br label %42
42: ; preds = %41, %36
br label %43
43: ; preds = %42, %29
br label %44
44: ; preds = %43, %28
%45 = load i32, i32* %7
store i32 %45, i32* %2
br label %46
46: ; preds = %44, %26, %17
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [7 x i8] c"select\00"
@.str.1 = [23 x i8] c"Error on select(): %s\0A\00"
define i32 @is_data_available(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.timeval*
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast i64* %4 to i8*
store i64 1, i64* %4
%9 = call i32 @FD_ZERO(i32* %2)
%10 = call i32 @FD_SET(i32 %0, i32* %2)
%11 = add nsw i32 %0, 1
%12 = call i32 @select(i32 %11, i32* %2, i32* null, i32* %3, %struct.timeval* %5)
%13 = icmp eq i32 %12, 0
br i1 %13, label %28, label %14
14: ; preds = %1
%15 = icmp sgt i32 %12, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = call i64 @FD_ISSET(i32 %0, i32* %2)
br label %28
18: ; preds = %14
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br i1 %21, label %27, label %22
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%24 = load i64, i64* @errno
%25 = call i8* @strerror(i64 %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i64 0, i64* @errno
br label %28
27: ; preds = %18
store i64 0, i64* @errno
br label %28
28: ; preds = %16, %22, %27, %1
%29 = phi i32 [ 0, %1 ], [ %12, %16 ], [ -1, %22 ], [ 0, %27 ]
ret i32 %29
}