#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 */
scalar_t__ FD_ISSET(int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int accept(int const, struct sockaddr *, int *);
scalar_t__ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timeval *);
void comms_check_for_clients(const int *servers, int *clients, const int n) {
  fd_set fds;
  struct timeval timeout;
  timeout.tv_sec = 0;
  timeout.tv_usec = 1;
  struct sockaddr_in client_addr;
  socklen_t client_len = sizeof(client_addr);
  FD_ZERO(&fds);
  int i;
  for (i = 0; i < n; ++i) {
    FD_SET(servers[i], &fds);
    clients[i] = -1;
  }
  if (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) <= 0)
    return;
  for (i = 0; i < n; ++i) {
    if (FD_ISSET(servers[i], &fds)) {
      clients[i] =
          accept(servers[i], (struct sockaddr *)&client_addr, &client_len);
    }
  }
}


x86 assembly (clang, -O0):
<comms_check_for_clients>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x1,-0x28(%rbp)
movl   $0x4,-0x34(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <FD_ZERO@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c6 <comms_check_for_clients+0x76>
mov    -0x8(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x18(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x10(%rbp),%rax
movslq -0x38(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1189 <comms_check_for_clients+0x39>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x0,%rax
jg     11f2 <comms_check_for_clients+0xa2>
jmp    125b <comms_check_for_clients+0x10b>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    125b <comms_check_for_clients+0x10b>
mov    -0x8(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1248 <comms_check_for_clients+0xf8>
mov    -0x8(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,%edx
mov    -0x10(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    124d <comms_check_for_clients+0xfd>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11f9 <comms_check_for_clients+0xa9>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<comms_check_for_clients>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,0x20(%rsp)
movl   $0x1,0x18(%rsp)
movl   $0x4,0xc(%rsp)
lea    0x8(%rsp),%rdi
call   1040 <FD_ZERO@plt>
mov    %r15d,%r13d
test   %r15d,%r15d
jle    11bc <comms_check_for_clients+0x6c>
xor    %ebp,%ebp
lea    0x8(%rsp),%r12
nopl   0x0(%rax)
mov    (%rbx,%rbp,4),%edi
mov    %r12,%rsi
call   1070 <FD_SET@plt>
movl   $0xffffffff,(%r14,%rbp,4)
ff
add    $0x1,%rbp
cmp    %rbp,%r13
jne    11a0 <comms_check_for_clients+0x50>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%r8
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %rax,%rax
jle    1231 <comms_check_for_clients+0xe1>
test   %r15d,%r15d
jle    1231 <comms_check_for_clients+0xe1>
xor    %r15d,%r15d
lea    0x8(%rsp),%rbp
lea    0xc(%rsp),%r12
jmp    1209 <comms_check_for_clients+0xb9>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r15
cmp    %r15,%r13
je     1231 <comms_check_for_clients+0xe1>
mov    (%rbx,%r15,4),%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1200 <comms_check_for_clients+0xb0>
mov    (%rbx,%r15,4),%edi
lea    0x10(%rsp),%rsi
mov    %r12,%rdx
call   1060 <accept@plt>
mov    %eax,(%r14,%r15,4)
jmp    1200 <comms_check_for_clients+0xb0>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@FD_SETSIZE = global i32 0
define void @comms_check_for_clients(i32* nocapture %0, i32* nocapture writeonly %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = bitcast i32* %4 to i8*
%9 = bitcast %struct.timeval* %5 to i8*
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 1, i32* %11
%12 = bitcast %struct.sockaddr_in* %6 to i8*
%13 = bitcast i32* %7 to i8*
store i32 4, i32* %7
%14 = call i32 @FD_ZERO(i32* %4)
%15 = icmp sgt i32 %2, 0
br i1 %15, label %16, label %26
16: ; preds = %3
%17 = zext i32 %2 to i64
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ 0, %16 ], [ %24, %18 ]
%20 = getelementptr inbounds i32, i32* %0, i64 %19
%21 = load i32, i32* %20
%22 = call i32 @FD_SET(i32 %21, i32* %4)
%23 = getelementptr inbounds i32, i32* %1, i64 %19
store i32 -1, i32* %23
%24 = add nuw nsw i64 %19, 1
%25 = icmp eq i64 %24, %17
br i1 %25, label %26, label %18
26: ; preds = %18, %3
%27 = load i32, i32* @FD_SETSIZE
%28 = call i64 @select(i32 %27, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%29 = icmp sgt i64 %28, 0
%30 = and i1 %29, %15
br i1 %30, label %31, label %47
31: ; preds = %26
%32 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%33 = zext i32 %2 to i64
br label %34
34: ; preds = %31, %44
%35 = phi i64 [ 0, %31 ], [ %45, %44 ]
%36 = getelementptr inbounds i32, i32* %0, i64 %35
%37 = load i32, i32* %36
%38 = call i64 @FD_ISSET(i32 %37, i32* %4)
%39 = icmp eq i64 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %34
%41 = load i32, i32* %36
%42 = call i32 @accept(i32 %41, %struct.sockaddr* %32, i32* %7)
%43 = getelementptr inbounds i32, i32* %1, i64 %35
store i32 %42, i32* %43
br label %44
44: ; preds = %34, %40
%45 = add nuw nsw i64 %35, 1
%46 = icmp eq i64 %45, %33
br i1 %46, label %47, label %34
47: ; preds = %44, %26
ret void
}