#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_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int socket_data_waiting(int sock, int timeoutseconds) {
  fd_set rfds;
  struct timeval tv;
  if (sock == 0)
    return 0;
  FD_ZERO(&rfds);
  FD_SET(sock, &rfds);
  tv.tv_sec = timeoutseconds;
  tv.tv_usec = 0;
  return (select(1, &rfds, NULL, NULL, &tv) > 0);
}


x86 assembly (clang, -O0):
<socket_data_waiting>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1154 <socket_data_waiting+0x24>
movl   $0x0,-0x4(%rbp)
jmp    119f <socket_data_waiting+0x6f>
lea    -0x10(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x1,%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
cmp    $0x0,%rax
setg   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_data_waiting>:
test   %edi,%edi
je     118e <socket_data_waiting+0x5e>
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <FD_SET@plt>
mov    %ebx,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setg   %al
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
ret

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

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