#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*/ *);
int /*<<< orphan*/ MSG_DONTWAIT;
int recv(int, void *, size_t, int /*<<< orphan*/);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int proxyrecv(int sockfd, void *buf, size_t len) {
  struct timeval tv;
  fd_set rfds;
  FD_ZERO(&rfds);
  FD_SET(sockfd, &rfds);
  tv.tv_sec = 2;
  tv.tv_usec = 0;
  if (select(sockfd + 1, &rfds, NULL, NULL, &tv) < 0)
    return -1;
  return recv(sockfd, buf, len, MSG_DONTWAIT);
}


x86 assembly (clang, -O0):
<proxyrecv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x2c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1060 <FD_SET@plt>
movl   $0x2,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x0,%rax
jge    11a7 <proxyrecv+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <proxyrecv+0x83>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2e27(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proxyrecv>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
lea    0x4(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
movl   $0x2,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x1(%rbx),%edi
lea    0x8(%rsp),%r8
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %rax,%rax
js     11ac <proxyrecv+0x6c>
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <recv@plt>
jmp    11b1 <proxyrecv+0x71>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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