#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 {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int recv(int, unsigned char *, size_t, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
ssize_t select_more_data(int sockfd, unsigned char *buffer, size_t max_len) {
  fd_set rfds;
  int ret;
  struct timeval tv;
  FD_ZERO(&rfds);
  FD_SET(sockfd, &rfds);
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  ret = select(sockfd + 1, &rfds, NULL, NULL, &tv);
  if (ret < 0)
    return -1;
  if (ret == 0)
    return 0;
  return recv(sockfd, buffer, max_len, 0);
}


x86 assembly (clang, -O0):
<select_more_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11ab <select_more_data+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <select_more_data+0x96>
cmpl   $0x0,-0x20(%rbp)
jne    11c1 <select_more_data+0x81>
movl   $0x0,-0x4(%rbp)
jmp    11d6 <select_more_data+0x96>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<select_more_data>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
lea    0xc(%rsp),%r13
mov    %r13,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %r13,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%r12),%edi
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
xor    %ebx,%ebx
lea    0x10(%rsp),%r8
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
js     11a8 <select_more_data+0x68>
je     11ad <select_more_data+0x6d>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
jmp    11ad <select_more_data+0x6d>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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