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

/* Variables and functions */
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int accept(int, struct sockaddr *, int *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int comms_accept_client(int socketfd) {
  fd_set fds;
  struct timeval timeout;
  struct sockaddr_in client_addr;
  socklen_t client_len;
  int client_socket;
  timeout.tv_sec = 0;
  timeout.tv_usec = 1;
  FD_ZERO(&fds);
  FD_SET(socketfd, &fds);
  if (select(socketfd + 1, &fds, NULL, NULL, &timeout) <= 0)
    return -1;
  if (!FD_ISSET(socketfd, &fds))
    return -1;
  client_len = sizeof(client_addr);
  client_socket =
      accept(socketfd, (struct sockaddr *)&client_addr, &client_len);
  if (client_socket < 0)
    return -1;
  return client_socket;
}


x86 assembly (clang, -O0):
<comms_accept_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x0,%rax
jg     11af <comms_accept_client+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <comms_accept_client+0xb6>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    11d0 <comms_accept_client+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <comms_accept_client+0xb6>
movl   $0x4,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1200 <comms_accept_client+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <comms_accept_client+0xb6>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<comms_accept_client>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
movq   $0x0,0x20(%rsp)
movl   $0x1,0x18(%rsp)
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1070 <FD_SET@plt>
lea    0x1(%rbx),%edi
lea    0x18(%rsp),%r8
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jle    11d1 <comms_accept_client+0x81>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
test   %eax,%eax
je     11d1 <comms_accept_client+0x81>
movl   $0x4,0x8(%rsp)
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <accept@plt>
test   %eax,%eax
mov    $0xffffffff,%ebp
cmovns %eax,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @comms_accept_client(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i64 0, i64* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i32 1, i32* %10
%11 = call i32 @FD_ZERO(i32* %4)
%12 = load i32, i32* %3
%13 = call i32 @FD_SET(i32 %12, i32* %4)
%14 = load i32, i32* %3
%15 = add nsw i32 %14, 1
%16 = call i64 @select(i32 %15, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%17 = icmp sle i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %1
store i32 -1, i32* %2
br label %33
19: ; preds = %1
%20 = load i32, i32* %3
%21 = call i32 @FD_ISSET(i32 %20, i32* %4)
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %19
store i32 -1, i32* %2
br label %33
24: ; preds = %19
store i32 4, i32* %7
%25 = load i32, i32* %3
%26 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%27 = call i32 @accept(i32 %25, %struct.sockaddr* %26, i32* %7)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 -1, i32* %2
br label %33
31: ; preds = %24
%32 = load i32, i32* %8
store i32 %32, i32* %2
br label %33
33: ; preds = %31, %30, %23, %18
%34 = load i32, i32* %2
ret i32 %34
}

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