#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EINTR;
int FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
scalar_t__ fileno(int /*<<< orphan*/ *);
scalar_t__ select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int inputAvailable(FILE *fp) {
  struct timeval tv;
  fd_set fds;
  tv.tv_sec = 30;
  tv.tv_usec = 0;
  FD_ZERO(&fds);
  FD_SET(fileno(fp), &fds);
  errno = 0;
  while (1) {
    if (select(fileno(fp) + 1, &fds, NULL, NULL, &tv) < 0) {
      if (errno == EINTR)
        continue;
    }
    break;
  }
  return (FD_ISSET(0, &fds));
}


x86 assembly (clang, -O0):
<inputAvailable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1e,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fileno@plt>
mov    %rax,%rdi
lea    -0x1c(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    0x2e50(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <fileno@plt>
mov    %rax,%rdi
add    $0x1,%rdi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
cmp    $0x0,%rax
jge    11e9 <inputAvailable+0x99>
mov    0x2e14(%rip),%rax
mov    (%rax),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
jne    11e4 <inputAvailable+0x94>
jmp    1197 <inputAvailable+0x47>
jmp    11e9 <inputAvailable+0x99>
jmp    11ee <inputAvailable+0x9e>
xor    %edi,%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inputAvailable>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
movl   $0x1e,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1050 <FD_ZERO@plt>
mov    %rbx,%rdi
call   1040 <fileno@plt>
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <FD_SET@plt>
mov    0x2e48(%rip),%r12
movq   $0x0,(%r12)
lea    0x10(%rsp),%r15
mov    0x2e2c(%rip),%r13
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1040 <fileno@plt>
lea    0x1(%rax),%rdi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1060 <select@plt>
test   %rax,%rax
jns    11da <inputAvailable+0x8a>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     11b0 <inputAvailable+0x60>
lea    0xc(%rsp),%rsi
xor    %edi,%edi
call   1030 <FD_ISSET@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @inputAvailable(i32* %0) {
%2 = alloca i32*
%3 = alloca %struct.timeval
%4 = alloca i32
store i32* %0, i32** %2
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 30, i32* %5
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 0, i64* %6
%7 = call i32 @FD_ZERO(i32* %4)
%8 = load i32*, i32** %2
%9 = call i64 @fileno(i32* %8)
%10 = call i32 @FD_SET(i64 %9, i32* %4)
store i64 0, i64* @errno
br label %11
11: ; preds = %1, %21
%12 = load i32*, i32** %2
%13 = call i64 @fileno(i32* %12)
%14 = add nsw i64 %13, 1
%15 = call i64 @select(i64 %14, i32* %4, i32* null, i32* null, %struct.timeval* %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17
br label %11
22: ; preds = %17
br label %23
23: ; preds = %22, %11
br label %24
24: ; preds = %23
%25 = call i32 @FD_ISSET(i32 0, i32* %4)
ret i32 %25
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @inputAvailable(i32* nocapture %0) {
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 30, i32* %6
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %7
%8 = call i32 @FD_ZERO(i32* %3)
%9 = call i64 @fileno(i32* %0)
%10 = call i32 @FD_SET(i64 %9, i32* %3)
store i64 0, i64* @errno
br label %11
11: ; preds = %16, %1
%12 = call i64 @fileno(i32* %0)
%13 = add nsw i64 %12, 1
%14 = call i64 @select(i64 %13, i32* %3, i32* null, i32* null, %struct.timeval* %2)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %11, label %20
20: ; preds = %11, %16
%21 = call i32 @FD_ISSET(i32 0, i32* %3)
ret i32 %21
}