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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, struct timeval *);
int recv_nb(int sock, unsigned char *buff, int n) {
  struct timeval tv = {0};
  fd_set rs;
  FD_ZERO(&rs);
  FD_SET(sock, &rs);
  if (!select(sock + 1, &rs, NULL, NULL, &tv))
    return 0;
  return recv(sock, (char *)buff, n, 0);
}


x86 assembly (clang, -O0):
<recv_nb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
lea    -0x1c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
cmp    $0x0,%eax
jne    11b6 <recv_nb+0x66>
movl   $0x0,-0x4(%rbp)
jmp    11ca <recv_nb+0x7a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_nb>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
movl   $0x0,0x8(%rsp)
lea    0xc(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbp),%edi
xor    %ebx,%ebx
lea    0x8(%rsp),%r8
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
je     119e <recv_nb+0x5e>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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