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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ FIONREAD;
scalar_t__ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int has_data(int fd) {
  struct timeval timeout = {0, 0};
  fd_set fds;
  int n = 0;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  if (select(1, &fds, NULL, NULL, &timeout) > 0)
    return ioctl(0, FIONREAD, &n) == 0 && n > 0;
  return 0;
}


x86 assembly (clang, -O0):
<has_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
movl   $0x0,-0x18(%rbp)
lea    -0x14(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
cmp    $0x0,%rax
jle    11ea <has_data+0x9a>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
jne    11da <has_data+0x8a>
cmpl   $0x0,-0x18(%rbp)
setg   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    11f1 <has_data+0xa1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<has_data>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movq   $0x0,0x8(%rsp)
movl   $0x0,0x4(%rsp)
lea    0x14(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
xor    %ebx,%ebx
lea    0x8(%rsp),%r8
mov    $0x1,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %rax,%rax
jle    11b6 <has_data+0x76>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
xor    %edi,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
sete   %al
cmpl   $0x0,0x4(%rsp)
setg   %cl
and    %al,%cl
movzbl %cl,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@FIONREAD = global i32 0
define i32 @has_data(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = bitcast %struct.timeval* %4 to i8*
store i32 0, i32* %6
%8 = call i32 @FD_ZERO(i32* %5)
%9 = load i32, i32* %3
%10 = call i32 @FD_SET(i32 %9, i32* %5)
%11 = call i64 @select(i32 1, i32* %5, i32* null, i32* null, %struct.timeval* %4)
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %1
%14 = load i32, i32* @FIONREAD
%15 = call i64 @ioctl(i32 0, i32 %14, i32* %6)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* %6
%19 = icmp sgt i32 %18, 0
br label %20
20: ; preds = %17, %13
%21 = phi i1 [ false, %13 ], [ %19, %17 ]
%22 = zext i1 %21 to i32
store i32 %22, i32* %2
br label %24
23: ; preds = %1
store i32 0, i32* %2
br label %24
24: ; preds = %23, %20
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@FIONREAD = global i32 0
define i32 @has_data(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.timeval*
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%9 = call i32 @FD_ZERO(i32* %4)
%10 = call i32 @FD_SET(i32 %0, i32* %4)
%11 = call i64 @select(i32 1, i32* %4, i32* null, i32* null, %struct.timeval* %3)
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %1
%14 = load i32, i32* @FIONREAD
%15 = call i64 @ioctl(i32 0, i32 %14, i32* %5)
%16 = icmp eq i64 %15, 0
%17 = load i32, i32* %5
%18 = icmp sgt i32 %17, 0
%19 = select i1 %16, i1 %18, i1 false
%20 = zext i1 %19 to i32
br label %21
21: ; preds = %1, %13
%22 = phi i32 [ %20, %13 ], [ 0, %1 ]
ret i32 %22
}