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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, struct timeval *);
int accept_nb(int sock, struct sockaddr *addr, socklen_t *len) {
  struct timeval tv = {0};
  fd_set rs;
  FD_ZERO(&rs);
  FD_SET(sock, &rs);
  if (!select(sock + 1, &rs, NULL, NULL, &tv))
    return 0;
  return accept(sock, addr, len);
}


x86 assembly (clang, -O0):
<accept_nb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x0,%eax
jne    11b7 <accept_nb+0x67>
movl   $0x0,-0x4(%rbp)
jmp    11ca <accept_nb+0x7a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_nb>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
movl   $0x0,0x8(%rsp)
lea    0xc(%rsp),%r13
mov    %r13,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r13,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbx),%edi
xor    %r12d,%r12d
lea    0x8(%rsp),%r8
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
test   %eax,%eax
je     119f <accept_nb+0x5f>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <accept@plt>
mov    %eax,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32 }
define i32 @accept_nb(i32 %0, %struct.sockaddr* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr*
%7 = alloca i32*
%8 = alloca %struct.timeval
%9 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr* %1, %struct.sockaddr** %6
store i32* %2, i32** %7
%10 = bitcast %struct.timeval* %8 to i8*
%11 = call i32 @FD_ZERO(i32* %9)
%12 = load i32, i32* %5
%13 = call i32 @FD_SET(i32 %12, i32* %9)
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
%16 = call i32 @select(i32 %15, i32* %9, i32* null, i32* null, %struct.timeval* %8)
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %3
store i32 0, i32* %4
br label %24
19: ; preds = %3
%20 = load i32, i32* %5
%21 = load %struct.sockaddr*, %struct.sockaddr** %6
%22 = load i32*, i32** %7
%23 = call i32 @accept(i32 %20, %struct.sockaddr* %21, i32* %22)
store i32 %23, i32* %4
br label %24
24: ; preds = %19, %18
%25 = load i32, i32* %4
ret i32 %25
}

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