#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;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
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*/ *);
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int timed_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
                 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 accept(sockfd, NULL, NULL);
  }
  errno = ETIMEDOUT;
  return -1;
}


x86 assembly (clang, -O0):
<timed_accept>:
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    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1225 <timed_accept+0xc5>
mov    0x2e11(%rip),%rax
mov    (%rax),%rax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%rax
je     1225 <timed_accept+0xc5>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdec(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <timed_accept+0x106>
cmpl   $0x0,-0x20(%rbp)
jle    1246 <timed_accept+0xe6>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1060 <accept@plt>
mov    %eax,-0x4(%rbp)
jmp    1266 <timed_accept+0x106>
jmp    124b <timed_accept+0xeb>
mov    0x2d7e(%rip),%rax
mov    (%rax),%rcx
mov    0x2d7c(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timed_accept>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    %ecx,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1080 <FD_SET@plt>
lea    0x1(%rbx),%edi
lea    0x10(%rsp),%r8
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
js     11b5 <timed_accept+0x55>
je     11cb <timed_accept+0x6b>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <accept@plt>
jmp    120b <timed_accept+0xab>
mov    0x2e1c(%rip),%rax
mov    (%rax),%rbx
mov    0x2e02(%rip),%rax
cmp    (%rax),%rbx
jne    11e1 <timed_accept+0x81>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    0x2dfc(%rip),%rcx
mov    %rax,(%rcx)
jmp    1206 <timed_accept+0xa6>
mov    0x2df8(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1070 <strerror@plt>
lea    0xe07(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%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_accept(i32 %0, %struct.sockaddr* %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timeval
store i32 %0, i32* %6
store %struct.sockaddr* %1, %struct.sockaddr** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%13 = load i32, i32* %9
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %15
%16 = call i32 @FD_ZERO(i32* %11)
%17 = load i32, i32* %6
%18 = call i32 @FD_SET(i32 %17, i32* %11)
%19 = load i32, i32* %6
%20 = add nsw i32 %19, 1
%21 = call i32 @select(i32 %20, i32* %11, i32* null, i32* null, %struct.timeval* %12)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %4
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = load i64, i64* @errno
%31 = load i64, i64* @errno
%32 = call i8* @strerror(i64 %31)
%33 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %30, i8* %32)
store i32 -1, i32* %5
br label %43
34: ; preds = %24, %4
%35 = load i32, i32* %10
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* %6
%39 = call i32 @accept(i32 %38, i32* null, i32* null)
store i32 %39, i32* %5
br label %43
40: ; preds = %34
br label %41
41: ; preds = %40
%42 = load i64, i64* @ETIMEDOUT
store i64 %42, i64* @errno
store i32 -1, i32* %5
br label %43
43: ; preds = %41, %37, %28
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%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_accept(i32 %0, %struct.sockaddr* nocapture readnone %1, i32* nocapture readnone %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %3, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %10
%11 = call i32 @FD_ZERO(i32* %5)
%12 = call i32 @FD_SET(i32 %0, i32* %5)
%13 = add nsw i32 %0, 1
%14 = call i32 @select(i32 %13, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %4
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %28, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i8* @strerror(i64 %17)
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %17, i8* %22)
br label %30
24: ; preds = %4
%25 = icmp eq i32 %14, 0
br i1 %25, label %28, label %26
26: ; preds = %24
%27 = call i32 @accept(i32 %0, i32* null, i32* null)
br label %30
28: ; preds = %16, %24
%29 = load i64, i64* @ETIMEDOUT
store i64 %29, i64* @errno
br label %30
30: ; preds = %28, %26, %20
%31 = phi i32 [ -1, %20 ], [ %27, %26 ], [ -1, %28 ]
ret i32 %31
}