#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;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ set_keepalive(int, int, int, int);
int accept_t(int sock, struct timeval wait, struct sockaddr_in *client) {
  fd_set rset;
  struct timeval tv;
  int ret = -1;
  static socklen_t client_len = sizeof(struct sockaddr_in);
  int csock = -1;
  tv.tv_sec = wait.tv_sec;
  tv.tv_usec = wait.tv_usec;
RE_DO:
  if (tv.tv_sec || tv.tv_usec) {
    FD_ZERO(&rset);
    FD_SET(sock, &rset);
    ret = select(sock + 1, &rset, (fd_set *)0, (fd_set *)0, &tv);
    if (ret == -1) {
      if (errno == EINTR) {
        goto RE_DO;
      }
      return -1;
    } else if (ret == 0) {
      return 0;
    }
  }
  csock = accept(sock, (struct sockaddr *)client, &client_len);
  if (csock < 0) {
    return -1;
  }
  if (!set_keepalive(csock, 1, 60, 10)) {
    close(csock);
    return -1;
  }
  return csock;
}


x86 assembly (clang, -O0):
<accept_t>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %edi,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0xffffffff,-0x44(%rbp)
movl   $0xffffffff,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11ab <accept_t+0x4b>
cmpq   $0x0,-0x40(%rbp)
je     1232 <accept_t+0xd2>
lea    -0x2c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1212 <accept_t+0xb2>
mov    0x2df2(%rip),%rax
mov    (%rax),%rax
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%rax
jne    1206 <accept_t+0xa6>
jmp    1195 <accept_t+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <accept_t+0x138>
cmpl   $0x0,-0x44(%rbp)
jne    1228 <accept_t+0xc8>
movl   $0x0,-0x4(%rbp)
jmp    1298 <accept_t+0x138>
jmp    122d <accept_t+0xcd>
jmp    1232 <accept_t+0xd2>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
lea    0x2e10(%rip),%rdx
call   1070 <accept@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    125e <accept_t+0xfe>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <accept_t+0x138>
mov    -0x48(%rbp),%edi
mov    $0x1,%esi
mov    $0x3c,%edx
mov    $0xa,%ecx
call   1030 <set_keepalive@plt>
cmp    $0x0,%eax
jne    1292 <accept_t+0x132>
mov    -0x48(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <accept_t+0x138>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_t>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x18(%rsp)
mov    %edi,%r12d
mov    %rdx,0x10(%rsp)
mov    %rsi,0x8(%rsp)
or     %rsi,%rdx
je     11ed <accept_t+0x8d>
lea    0x1(%r12),%r15d
lea    0x24(%rsp),%rbx
lea    0x8(%rsp),%r13
mov    0x2e3d(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1060 <select@plt>
cmp    $0xffffffff,%eax
jne    11e7 <accept_t+0x87>
mov    0x2e0f(%rip),%rax
mov    (%rax),%rax
cmp    (%r14),%rax
jne    1233 <accept_t+0xd3>
mov    0x10(%rsp),%rax
or     0x8(%rsp),%rax
jne    11a0 <accept_t+0x40>
jmp    11ed <accept_t+0x8d>
mov    %eax,%ebp
test   %eax,%eax
je     1238 <accept_t+0xd8>
lea    0x2e5c(%rip),%rdx
mov    %r12d,%edi
mov    0x18(%rsp),%rsi
call   1070 <accept@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1238 <accept_t+0xd8>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
mov    $0x3c,%edx
mov    $0xa,%ecx
call   1030 <set_keepalive@plt>
test   %eax,%eax
je     122a <accept_t+0xca>
mov    %ebx,%ebp
jmp    1238 <accept_t+0xd8>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1238 <accept_t+0xd8>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i64, i64 }
%struct.sockaddr = type { i32 }
@accept_t.client_len = internal global i32 4
@errno = global i64 0
@EINTR = global i64 0
define i32 @accept_t(i32 %0, i64 %1, i64 %2, %struct.sockaddr_in* %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca %struct.sockaddr_in*
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = alloca i32
%12 = alloca i32
%13 = bitcast %struct.timeval* %6 to { i64, i64 }*
%14 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %13, i32 0, i32 0
store i64 %1, i64* %14
%15 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %13, i32 0, i32 1
store i64 %2, i64* %15
store i32 %0, i32* %7
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %8
store i32 -1, i32* %11
store i32 -1, i32* %12
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%17 = load i64, i64* %16
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 %17, i64* %18
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i64 %20, i64* %21
br label %22
22: ; preds = %43, %4
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%24 = load i64, i64* %23
%25 = icmp ne i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%28 = load i64, i64* %27
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %51
30: ; preds = %26, %22
%31 = call i32 @FD_ZERO(i32* %9)
%32 = load i32, i32* %7
%33 = call i32 @FD_SET(i32 %32, i32* %9)
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
%36 = call i32 @select(i32 %35, i32* %9, i32* null, i32* null, %struct.timeval* %10)
store i32 %36, i32* %11
%37 = load i32, i32* %11
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %45
39: ; preds = %30
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINTR
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %39
br label %22
44: ; preds = %39
store i32 -1, i32* %5
br label %68
45: ; preds = %30
%46 = load i32, i32* %11
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %45
store i32 0, i32* %5
br label %68
49: ; preds = %45
br label %50
50: ; preds = %49
br label %51
51: ; preds = %50, %26
%52 = load i32, i32* %7
%53 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%54 = bitcast %struct.sockaddr_in* %53 to %struct.sockaddr*
%55 = call i32 @accept(i32 %52, %struct.sockaddr* %54, i32* @accept_t.client_len)
store i32 %55, i32* %12
%56 = load i32, i32* %12
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %51
store i32 -1, i32* %5
br label %68
59: ; preds = %51
%60 = load i32, i32* %12
%61 = call i32 @set_keepalive(i32 %60, i32 1, i32 60, i32 10)
%62 = icmp ne i32 %61, 0
br i1 %62, label %66, label %63
63: ; preds = %59
%64 = load i32, i32* %12
%65 = call i32 @close(i32 %64)
store i32 -1, i32* %5
br label %68
66: ; preds = %59
%67 = load i32, i32* %12
store i32 %67, i32* %5
br label %68
68: ; preds = %66, %63, %58, %48, %44
%69 = load i32, i32* %5
ret i32 %69
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i64, i64 }
%struct.sockaddr = type { i32 }
@accept_t.client_len = internal global i32 4
@errno = global i64 0
@EINTR = global i64 0
define i32 @accept_t(i32 %0, i64 %1, i64 %2, %struct.sockaddr_in* %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 1
store i64 %2, i64* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i64 %1, i64* %10
%11 = add nsw i32 %0, 1
%12 = icmp ne i64 %2, 0
%13 = icmp ne i64 %1, 0
%14 = select i1 %12, i1 true, i1 %13
br i1 %14, label %15, label %29
15: ; preds = %4, %23
%16 = call i32 @FD_ZERO(i32* %5)
%17 = call i32 @FD_SET(i32 %0, i32* %5)
%18 = call i32 @select(i32 %11, i32* %5, i32* null, i32* null, %struct.timeval* %6)
switch i32 %18, label %29 [
i32 -1, label %19
i32 0, label %38
]
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %38
23: ; preds = %19
%24 = load i64, i64* %9
%25 = load i64, i64* %10
%26 = icmp ne i64 %24, 0
%27 = icmp ne i64 %25, 0
%28 = select i1 %26, i1 true, i1 %27
br i1 %28, label %15, label %29
29: ; preds = %23, %15, %4
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i32 @accept(i32 %0, %struct.sockaddr* %30, i32* @accept_t.client_len)
%32 = icmp slt i32 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %29
%34 = call i32 @set_keepalive(i32 %31, i32 1, i32 60, i32 10)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @close(i32 %31)
br label %38
38: ; preds = %15, %19, %33, %29, %36
%39 = phi i32 [ -1, %36 ], [ -1, %29 ], [ %31, %33 ], [ %18, %15 ], [ -1, %19 ]
ret i32 %39
}