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

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ bzero(struct timeval *, int);
int fcntl(int, int /*<<< orphan*/, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/, struct timeval *);
int sock_accept(int sfd, struct sockaddr *serv_addr, socklen_t *addrlen,
                int timeout) {
  int res, slen, flags;
  struct timeval tv;
  struct sockaddr_in addr;
  fd_set rdf, wrf;
  fcntl(sfd, F_SETFL, O_NONBLOCK);
  FD_ZERO(&rdf);
  FD_ZERO(&wrf);
  FD_SET(sfd, &rdf);
  FD_SET(sfd, &wrf);
  bzero(&tv, sizeof(tv));
  tv.tv_sec = timeout;
  if (select(sfd + 1, &rdf, &wrf, 0, &tv) <= 0)
    return -1;
  if (FD_ISSET(sfd, &wrf) || FD_ISSET(sfd, &rdf)) {
    slen = sizeof(addr);
    res = accept(sfd, serv_addr, addrlen);
    flags = fcntl(sfd, F_GETFL, NULL);
    fcntl(sfd, F_SETFL, flags & ~O_NONBLOCK);
    return res;
  }
  return -1;
}


x86 assembly (clang, -O0):
<sock_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
lea    -0x3c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
lea    -0x40(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1090 <FD_SET@plt>
lea    -0x30(%rbp),%rdi
mov    $0x4,%esi
call   1070 <bzero@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x3c(%rbp),%rsi
lea    -0x40(%rbp),%rdx
xor    %ecx,%ecx
lea    -0x30(%rbp),%r8
call   1060 <select@plt>
cmp    $0x0,%rax
jg     120d <sock_accept+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <sock_accept+0x12d>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
jne    1239 <sock_accept+0xc9>
mov    -0x8(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1296 <sock_accept+0x126>
movl   $0x4,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
mov    0x2d69(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
call   1030 <fcntl@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129d <sock_accept+0x12d>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_accept>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
lea    0x14(%rsp),%r13
mov    %r13,%rdi
call   1050 <FD_ZERO@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r13,%rsi
call   1080 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %r12d,0x8(%rsp)
lea    0x1(%rbx),%edi
lea    0x8(%rsp),%r8
mov    %r13,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1060 <select@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jle    1242 <sock_accept+0xe2>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    1204 <sock_accept+0xa4>
lea    0x14(%rsp),%rsi
mov    %ebx,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1242 <sock_accept+0xe2>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <accept@plt>
mov    %eax,%ebp
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2dac(%rip),%rcx
mov    (%rcx),%esi
mov    0x2db3(%rip),%rcx
mov    (%rcx),%edx
not    %edx
and    %eax,%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32 }
%struct.sockaddr_in = type { i32 }
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFL = global i32 0
define i32 @sock_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 i32
%13 = alloca %struct.timeval
%14 = alloca %struct.sockaddr_in
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %6
store %struct.sockaddr* %1, %struct.sockaddr** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%17 = load i32, i32* %6
%18 = load i32, i32* @F_SETFL
%19 = load i32, i32* @O_NONBLOCK
%20 = call i32 @fcntl(i32 %17, i32 %18, i32 %19)
%21 = call i32 @FD_ZERO(i32* %15)
%22 = call i32 @FD_ZERO(i32* %16)
%23 = load i32, i32* %6
%24 = call i32 @FD_SET(i32 %23, i32* %15)
%25 = load i32, i32* %6
%26 = call i32 @FD_SET(i32 %25, i32* %16)
%27 = call i32 @bzero(%struct.timeval* %13, i32 4)
%28 = load i32, i32* %9
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
%32 = call i64 @select(i32 %31, i32* %15, i32* %16, i32 0, %struct.timeval* %13)
%33 = icmp sle i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %4
store i32 -1, i32* %5
br label %60
35: ; preds = %4
%36 = load i32, i32* %6
%37 = call i64 @FD_ISSET(i32 %36, i32* %16)
%38 = icmp ne i64 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = load i32, i32* %6
%41 = call i64 @FD_ISSET(i32 %40, i32* %15)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %59
43: ; preds = %39, %35
store i32 4, i32* %11
%44 = load i32, i32* %6
%45 = load %struct.sockaddr*, %struct.sockaddr** %7
%46 = load i32*, i32** %8
%47 = call i32 @accept(i32 %44, %struct.sockaddr* %45, i32* %46)
store i32 %47, i32* %10
%48 = load i32, i32* %6
%49 = load i32, i32* @F_GETFL
%50 = call i32 @fcntl(i32 %48, i32 %49, i32 0)
store i32 %50, i32* %12
%51 = load i32, i32* %6
%52 = load i32, i32* @F_SETFL
%53 = load i32, i32* %12
%54 = load i32, i32* @O_NONBLOCK
%55 = xor i32 %54, -1
%56 = and i32 %53, %55
%57 = call i32 @fcntl(i32 %51, i32 %52, i32 %56)
%58 = load i32, i32* %10
store i32 %58, i32* %5
br label %60
59: ; preds = %39
store i32 -1, i32* %5
br label %60
60: ; preds = %59, %43, %34
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32 }
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFL = global i32 0
define i32 @sock_accept(i32 %0, %struct.sockaddr* %1, i32* %2, i32 %3) {
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca i32
%8 = bitcast %struct.timeval* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = bitcast i32* %7 to i8*
%11 = load i32, i32* @F_SETFL
%12 = load i32, i32* @O_NONBLOCK
%13 = tail call i32 @fcntl(i32 %0, i32 %11, i32 %12)
%14 = call i32 @FD_ZERO(i32* %6)
%15 = call i32 @FD_ZERO(i32* %7)
%16 = call i32 @FD_SET(i32 %0, i32* %6)
%17 = call i32 @FD_SET(i32 %0, i32* %7)
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 %3, i32* %18
%19 = add nsw i32 %0, 1
%20 = call i64 @select(i32 %19, i32* %6, i32* %7, i32 0, %struct.timeval* %5)
%21 = icmp slt i64 %20, 1
br i1 %21, label %37, label %22
22: ; preds = %4
%23 = call i64 @FD_ISSET(i32 %0, i32* %7)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i64 @FD_ISSET(i32 %0, i32* %6)
%27 = icmp eq i64 %26, 0
br i1 %27, label %37, label %28
28: ; preds = %25, %22
%29 = call i32 @accept(i32 %0, %struct.sockaddr* %1, i32* %2)
%30 = load i32, i32* @F_GETFL
%31 = call i32 @fcntl(i32 %0, i32 %30, i32 0)
%32 = load i32, i32* @F_SETFL
%33 = load i32, i32* @O_NONBLOCK
%34 = xor i32 %33, -1
%35 = and i32 %31, %34
%36 = call i32 @fcntl(i32 %0, i32 %32, i32 %35)
br label %37
37: ; preds = %25, %4, %28
%38 = phi i32 [ %29, %28 ], [ -1, %4 ], [ -1, %25 ]
ret i32 %38
}