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

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int accept_new_one(int sd, int *isopen, int *cd, int ncons) {
  static int ci_last = 0;
  int ci, ci_loop;
  struct sockaddr_in client_addr;
  socklen_t client_addrlen = sizeof(struct sockaddr);
  for (ci_loop = 0, ci = ci_last; ci_loop < ncons; ci_loop++) {
    if (!isopen[ci])
      break;
    ci++;
    if (ci == ncons)
      ci = 0;
  }
  ci_last = ci;
  if ((cd[ci] = accept(sd, (struct sockaddr *)&client_addr, &client_addrlen)) <
      0) {
    perror("accept");
    exit(errno);
  };
  ;
  return ci;
}


x86 assembly (clang, -O0):
<accept_new_one>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x4,-0x2c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2ee2(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11af <accept_new_one+0x7f>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jne    1180 <accept_new_one+0x50>
jmp    11af <accept_new_one+0x7f>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    119c <accept_new_one+0x6c>
movl   $0x0,-0x20(%rbp)
jmp    11a1 <accept_new_one+0x71>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    115d <accept_new_one+0x2d>
mov    -0x20(%rbp),%eax
mov    %eax,0x2e84(%rip)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1040 <accept@plt>
mov    -0x18(%rbp),%rcx
movslq -0x20(%rbp),%rdx
mov    %eax,(%rcx,%rdx,4)
cmp    $0x0,%eax
jge    11f6 <accept_new_one+0xc6>
lea    0xe1d(%rip),%rdi
call   1030 <perror@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_new_one>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
movl   $0x4,0xc(%rsp)
mov    0x2ef4(%rip),%ebx
test   %ecx,%ecx
jle    1177 <accept_new_one+0x47>
xor    %r8d,%r8d
mov    %ecx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rax
cmpl   $0x0,(%rsi,%rax,4)
je     1177 <accept_new_one+0x47>
add    $0x1,%ebx
cmp    %ecx,%ebx
cmove  %r8d,%ebx
add    $0xffffffff,%edx
jne    1160 <accept_new_one+0x30>
mov    %ebx,0x2ebf(%rip)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1040 <accept@plt>
movslq %ebx,%rcx
mov    %eax,(%r14,%rcx,4)
test   %eax,%eax
js     11a1 <accept_new_one+0x71>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe58(%rip),%rdi
call   1030 <perror@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@accept_new_one.ci_last = internal global i32 0
@.str = [7 x i8] c"accept\00"
@errno = global i32 0
define i32 @accept_new_one(i32 %0, i32* %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32 %3, i32* %8
store i32 4, i32* %12
store i32 0, i32* %10
%13 = load i32, i32* @accept_new_one.ci_last
store i32 %13, i32* %9
br label %14
14: ; preds = %34, %4
%15 = load i32, i32* %10
%16 = load i32, i32* %8
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %37
18: ; preds = %14
%19 = load i32*, i32** %6
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = load i32, i32* %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %18
br label %37
26: ; preds = %18
%27 = load i32, i32* %9
%28 = add nsw i32 %27, 1
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = load i32, i32* %8
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %26
store i32 0, i32* %9
br label %33
33: ; preds = %32, %26
br label %34
34: ; preds = %33
%35 = load i32, i32* %10
%36 = add nsw i32 %35, 1
store i32 %36, i32* %10
br label %14
37: ; preds = %25, %14
%38 = load i32, i32* %9
store i32 %38, i32* @accept_new_one.ci_last
%39 = load i32, i32* %5
%40 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%41 = call i32 @accept(i32 %39, %struct.sockaddr* %40, i32* %12)
%42 = load i32*, i32** %7
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %42, i64 %44
store i32 %41, i32* %45
%46 = icmp slt i32 %41, 0
br i1 %46, label %47, label %51
47: ; preds = %37
%48 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%49 = load i32, i32* @errno
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %37
%52 = load i32, i32* %9
ret i32 %52
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@accept_new_one.ci_last = internal unnamed_addr global i32 0
@.str = [7 x i8] c"accept\00"
@errno = global i32 0
define i32 @accept_new_one(i32 %0, i32* nocapture %1, i32* nocapture writeonly %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%9 = load i32, i32* @accept_new_one.ci_last
%10 = icmp sgt i32 %3, 0
br i1 %10, label %11, label %24
11: ; preds = %4, %18
%12 = phi i32 [ %22, %18 ], [ 0, %4 ]
%13 = phi i32 [ %21, %18 ], [ %9, %4 ]
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i32, i32* %1, i64 %14
%16 = load i32, i32* %15
%17 = icmp eq i32 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %11
%19 = add nsw i32 %13, 1
%20 = icmp eq i32 %19, %3
%21 = select i1 %20, i32 0, i32 %19
%22 = add nuw nsw i32 %12, 1
%23 = icmp eq i32 %22, %3
br i1 %23, label %24, label %11
24: ; preds = %18, %11, %4
%25 = phi i32 [ %9, %4 ], [ %13, %11 ], [ %21, %18 ]
store i32 %25, i32* @accept_new_one.ci_last
%26 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%27 = call i32 @accept(i32 %0, %struct.sockaddr* %26, i32* %6)
%28 = sext i32 %25 to i64
%29 = getelementptr inbounds i32, i32* %2, i64 %28
store i32 %27, i32* %29
%30 = icmp slt i32 %27, 0
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* @errno
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %24
ret i32 %25
}