#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 */

/* Variables and functions */
scalar_t__ EAGAIN;
int FD_SETSIZE;
int /*<<< orphan*/ _makeNonBlocking(int);
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ logWrite(char *);
char *strerror(scalar_t__);
int socketAccept(int fd) {
  int newFd = accept(fd, NULL, NULL);
  if (newFd >= 0) {
    _makeNonBlocking(newFd);
    return newFd;
  } else if (errno == EAGAIN) {
    return FD_SETSIZE;
  }
  logWrite("ERROR accept()");
  logWrite(strerror(errno));
  return (-1);
}


x86 assembly (clang, -O0):
<socketAccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1050 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     117a <socketAccept+0x3a>
mov    -0xc(%rbp),%edi
call   1040 <_makeNonBlocking@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <socketAccept+0x94>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
jne    11a5 <socketAccept+0x65>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <socketAccept+0x94>
jmp    11aa <socketAccept+0x6a>
lea    0xe4f(%rip),%rdi
call   1030 <logWrite@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %rax,%rdi
call   1030 <logWrite@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketAccept>:
push   %rbx
xor    %esi,%esi
xor    %edx,%edx
call   1050 <accept@plt>
test   %eax,%eax
js     1159 <socketAccept+0x19>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <_makeNonBlocking@plt>
jmp    119b <socketAccept+0x5b>
mov    0x2e78(%rip),%rbx
mov    (%rbx),%rax
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
jne    117a <socketAccept+0x3a>
mov    0x2e6a(%rip),%rax
mov    (%rax),%ebx
jmp    119b <socketAccept+0x5b>
lea    0xe7f(%rip),%rdi
call   1030 <logWrite@plt>
mov    (%rbx),%rdi
call   1060 <strerror@plt>
mov    %rax,%rdi
call   1030 <logWrite@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@FD_SETSIZE = global i32 0
@.str = [15 x i8] c"ERROR accept()\00"
define i32 @socketAccept(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @accept(i32 %5, i32* null, i32* null)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* %4
%11 = call i32 @_makeNonBlocking(i32 %10)
%12 = load i32, i32* %4
store i32 %12, i32* %2
br label %25
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = load i64, i64* @EAGAIN
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = load i32, i32* @FD_SETSIZE
store i32 %18, i32* %2
br label %25
19: ; preds = %13
br label %20
20: ; preds = %19
%21 = call i32 @logWrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%22 = load i64, i64* @errno
%23 = call i8* @strerror(i64 %22)
%24 = call i32 @logWrite(i8* %23)
store i32 -1, i32* %2
br label %25
25: ; preds = %20, %17, %9
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@FD_SETSIZE = global i32 0
@.str = [15 x i8] c"ERROR accept()\00"
define i32 @socketAccept(i32 %0) {
%2 = tail call i32 @accept(i32 %0, i32* null, i32* null)
%3 = icmp sgt i32 %2, -1
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @_makeNonBlocking(i32 %2)
br label %17
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @EAGAIN
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = load i32, i32* @FD_SETSIZE
br label %17
12: ; preds = %6
%13 = tail call i32 @logWrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%14 = load i64, i64* @errno
%15 = tail call i8* @strerror(i64 %14)
%16 = tail call i32 @logWrite(i8* %15)
br label %17
17: ; preds = %12, %10, %4
%18 = phi i32 [ %2, %4 ], [ %11, %10 ], [ -1, %12 ]
ret i32 %18
}