#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 */
scalar_t__ EINTR;
scalar_t__ ETIMEDOUT;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, char *);
int recv(int, void *, size_t, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int timed_recv(int sockfd, void *buf, size_t len, int flags, int secs) {
  int res;
  fd_set myset;
  struct timeval tv;
  tv.tv_sec = secs;
  tv.tv_usec = 0;
  FD_ZERO(&myset);
  FD_SET(sockfd, &myset);
  res = select(sockfd + 1, &myset, NULL, NULL, &tv);
  if (res < 0 && errno != EINTR) {
    fprintf(stderr, "Error accepting %d - %s\n", errno, strerror(errno));
    return -1;
  } else if (res > 0) {
    return recv(sockfd, buf, len, flags);
  }
  errno = ETIMEDOUT;
  return -1;
}


x86 assembly (clang, -O0):
<timed_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1229 <timed_recv+0xc9>
mov    0x2e0d(%rip),%rax
mov    (%rax),%rax
mov    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
je     1229 <timed_recv+0xc9>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xde8(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <timed_recv+0x10e>
cmpl   $0x0,-0x24(%rbp)
jle    124e <timed_recv+0xee>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <recv@plt>
mov    %eax,-0x4(%rbp)
jmp    126e <timed_recv+0x10e>
jmp    1253 <timed_recv+0xf3>
mov    0x2d76(%rip),%rax
mov    (%rax),%rcx
mov    0x2d74(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timed_recv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebx
mov    %r8d,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
lea    0x1(%rbx),%edi
lea    0x10(%rsp),%r8
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
js     11c8 <timed_recv+0x68>
je     11de <timed_recv+0x7e>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <recv@plt>
jmp    121e <timed_recv+0xbe>
mov    0x2e09(%rip),%rax
mov    (%rax),%rbx
mov    0x2def(%rip),%rax
cmp    (%rax),%rbx
jne    11f4 <timed_recv+0x94>
mov    0x2deb(%rip),%rax
mov    (%rax),%rax
mov    0x2de9(%rip),%rcx
mov    %rax,(%rcx)
jmp    1219 <timed_recv+0xb9>
mov    0x2de5(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1070 <strerror@plt>
lea    0xdf4(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"Error accepting %d - %s\0A\00"
@ETIMEDOUT = global i64 0
define i32 @timed_recv(i32 %0, i8* %1, i64 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.timeval
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%15 = load i32, i32* %11
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 1
store i64 0, i64* %17
%18 = call i32 @FD_ZERO(i32* %13)
%19 = load i32, i32* %7
%20 = call i32 @FD_SET(i32 %19, i32* %13)
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 1
%23 = call i32 @select(i32 %22, i32* %13, i32* null, i32* null, %struct.timeval* %14)
store i32 %23, i32* %12
%24 = load i32, i32* %12
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %5
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i64, i64* @errno
%33 = load i64, i64* @errno
%34 = call i8* @strerror(i64 %33)
%35 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %32, i8* %34)
store i32 -1, i32* %6
br label %48
36: ; preds = %26, %5
%37 = load i32, i32* %12
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %36
%40 = load i32, i32* %7
%41 = load i8*, i8** %8
%42 = load i64, i64* %9
%43 = load i32, i32* %10
%44 = call i32 @recv(i32 %40, i8* %41, i64 %42, i32 %43)
store i32 %44, i32* %6
br label %48
45: ; preds = %36
br label %46
46: ; preds = %45
%47 = load i64, i64* @ETIMEDOUT
store i64 %47, i64* @errno
store i32 -1, i32* %6
br label %48
48: ; preds = %46, %39, %30
%49 = load i32, i32* %6
ret i32 %49
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"Error accepting %d - %s\0A\00"
@ETIMEDOUT = global i64 0
define i32 @timed_recv(i32 %0, i8* %1, i64 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = bitcast i32* %6 to i8*
%9 = bitcast %struct.timeval* %7 to i8*
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i32 %4, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i64 0, i64* %11
%12 = call i32 @FD_ZERO(i32* %6)
%13 = call i32 @FD_SET(i32 %0, i32* %6)
%14 = add nsw i32 %0, 1
%15 = call i32 @select(i32 %14, i32* %6, i32* null, i32* null, %struct.timeval* %7)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %5
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %29, label %21
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i8* @strerror(i64 %18)
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %18, i8* %23)
br label %31
25: ; preds = %5
%26 = icmp eq i32 %15, 0
br i1 %26, label %29, label %27
27: ; preds = %25
%28 = call i32 @recv(i32 %0, i8* %1, i64 %2, i32 %3)
br label %31
29: ; preds = %17, %25
%30 = load i64, i64* @ETIMEDOUT
store i64 %30, i64* @errno
br label %31
31: ; preds = %29, %27, %21
%32 = phi i32 [ -1, %21 ], [ %28, %27 ], [ -1, %29 ]
ret i32 %32
}