#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 {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ unix_set_nonblock(int);
int unix_accept(int listenfd) {
  socklen_t length;
  struct sockaddr address;
  int fd;
  length = sizeof(address);
  fd = accept(listenfd, &address, &length);
  if (fd == -1)
    fprintf(stderr, "Accept failed: %s\n", strerror(errno));
  else if (unix_set_nonblock(fd)) {
    close(fd);
    fd = -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<unix_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x8(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b0 <unix_accept+0x60>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe5a(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11d6 <unix_accept+0x86>
mov    -0x14(%rbp),%edi
call   1050 <unix_set_nonblock@plt>
cmp    $0x0,%rax
je     11d1 <unix_accept+0x81>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    11d6 <unix_accept+0x86>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_accept>:
push   %rbx
sub    $0x10,%rsp
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1060 <accept@plt>
cmp    $0xffffffff,%eax
je     1188 <unix_accept+0x38>
mov    %eax,%ebx
mov    %eax,%edi
call   1050 <unix_set_nonblock@plt>
test   %rax,%rax
je     11b5 <unix_accept+0x65>
mov    %ebx,%edi
call   1030 <close@plt>
jmp    11b0 <unix_accept+0x60>
mov    0x2e51(%rip),%rax
mov    (%rax),%ebx
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe5a(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"Accept failed: %s\0A\00"
@errno = global i32 0
define i32 @unix_accept(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr
%5 = alloca i32
store i32 %0, i32* %2
store i32 4, i32* %3
%6 = load i32, i32* %2
%7 = call i32 @accept(i32 %6, %struct.sockaddr* %4, i32* %3)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %23
15: ; preds = %1
%16 = load i32, i32* %5
%17 = call i64 @unix_set_nonblock(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* %5
%21 = call i32 @close(i32 %20)
store i32 -1, i32* %5
br label %22
22: ; preds = %19, %15
br label %23
23: ; preds = %22, %10
%24 = load i32, i32* %5
ret i32 %24
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"Accept failed: %s\0A\00"
@errno = global i32 0
define i32 @unix_accept(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.sockaddr* %3 to i8*
store i32 4, i32* %2
%6 = call i32 @accept(i32 %0, %struct.sockaddr* %3, i32* %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %18
13: ; preds = %1
%14 = call i64 @unix_set_nonblock(i32 %6)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i32 @close(i32 %6)
br label %18
18: ; preds = %13, %16, %8
%19 = phi i32 [ -1, %8 ], [ -1, %16 ], [ %6, %13 ]
ret i32 %19
}