#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_un {
  char *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct TYPE_2__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ SOCK_CLOEXEC;
int /*<<< orphan*/ SOCK_STREAM;
int accept4(int, struct sockaddr_in *, int *, int /*<<< orphan*/);
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr_un *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char *, int);
scalar_t__ unlink(char *);
int serversocket(void) {
  int sh, conn;
  socklen_t rlen;
  struct sockaddr_un addr;
  struct sockaddr_in raddr;
  if ((sh = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    perror("server: socket()");
    return -1;
  }
  memset(&addr, 0, sizeof(struct sockaddr_un));
  strncpy(addr.sun_path, "accept4-test", sizeof(addr.sun_path) - 1);
  if (access(addr.sun_path, F_OK) == 0) {
    fprintf(stderr, "server: socket '%s' already exists, removing...\n",
            addr.sun_path);
    if (unlink(addr.sun_path) < 0) {
      perror("server: unlink()");
      return -1;
    }
  }
  addr.sun_family = AF_UNIX;
  if (bind(sh, &addr, sizeof(struct sockaddr_un))) {
    perror("server: bind() for "
           "accept4-test");
    close(sh);
    return -1;
  }
  if (listen(sh, 1)) {
    perror("server: listen()");
    close(sh);
    return -1;
  }
  raddr.sin_addr.s_addr = 0;
  rlen = sizeof(struct sockaddr);
  if ((conn = accept4(sh, &raddr, &rlen, SOCK_CLOEXEC)) < 0) {
    perror("server: accept()");
    return -1;
  }
  close(sh);
  if (unlink(addr.sun_path) < 0) {
    perror("server: unlink()");
  }
  return conn;
}


x86 assembly (clang, -O0):
<serversocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11f5 <serversocket+0x45>
lea    0xe1c(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1376 <serversocket+0x1c6>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    -0x20(%rbp),%rdi
lea    0xe56(%rip),%rsi
mov    $0x7,%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
call   10b0 <access@plt>
cmp    $0x0,%rax
jne    127f <serversocket+0xcf>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdc7(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    127a <serversocket+0xca>
lea    0xdd9(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1376 <serversocket+0x1c6>
jmp    127f <serversocket+0xcf>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     12c6 <serversocket+0x116>
lea    0xda6(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1376 <serversocket+0x1c6>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
je     12fd <serversocket+0x14d>
lea    0xd8f(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1376 <serversocket+0x1c6>
movq   $0x0,-0x28(%rbp)
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cda(%rip),%rax
mov    (%rax),%ecx
lea    -0x28(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1050 <accept4@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1349 <serversocket+0x199>
lea    0xd4c(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1376 <serversocket+0x1c6>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    1370 <serversocket+0x1c0>
lea    0xcd7(%rip),%rdi
call   10c0 <perror@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serversocket>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    0x2e23(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12c4 <serversocket+0x114>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x8(%rsp),%rdi
lea    0xe6f(%rip),%rsi
mov    $0x7,%edx
call   1040 <strncpy@plt>
mov    0x8(%rsp),%rdi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
call   10b0 <access@plt>
test   %rax,%rax
jne    1246 <serversocket+0x96>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xde3(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1030 <unlink@plt>
test   %rax,%rax
js     1307 <serversocket+0x157>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jne    12cd <serversocket+0x11d>
mov    %ebx,%edi
mov    $0x1,%esi
call   1090 <listen@plt>
test   %rax,%rax
jne    12d6 <serversocket+0x126>
movq   $0x0,0x20(%rsp)
movl   $0x4,0x1c(%rsp)
mov    0x2d64(%rip),%rax
mov    (%rax),%ecx
lea    0x20(%rsp),%rsi
lea    0x1c(%rsp),%rdx
mov    %ebx,%edi
call   1050 <accept4@plt>
test   %eax,%eax
js     12f0 <serversocket+0x140>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x8(%rsp),%rdi
call   1030 <unlink@plt>
test   %rax,%rax
js     12f9 <serversocket+0x149>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd35(%rip),%rdi
jmp    130e <serversocket+0x15e>
lea    0xd7f(%rip),%rdi
jmp    12dd <serversocket+0x12d>
lea    0xd96(%rip),%rdi
call   10c0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebp
jmp    12bb <serversocket+0x10b>
lea    0xd8d(%rip),%rdi
jmp    130e <serversocket+0x15e>
lea    0xd42(%rip),%rdi
call   10c0 <perror@plt>
jmp    12bb <serversocket+0x10b>
lea    0xd34(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    12bb <serversocket+0x10b>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [17 x i8] c"server: socket()\00"
@.str.1 = [13 x i8] c"accept4-test\00"
@F_OK = global i32 0
@stderr = global i32 0
@.str.2 = [49 x i8] c"server: socket '%s' already exists, removing...\0A\00"
@.str.3 = [17 x i8] c"server: unlink()\00"
@.str.4 = [32 x i8] c"server: bind() for accept4-test\00"
@.str.5 = [17 x i8] c"server: listen()\00"
@SOCK_CLOEXEC = global i32 0
@.str.6 = [17 x i8] c"server: accept()\00"
define i32 @serversocket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca %struct.sockaddr_in
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %73
13: ; preds = %0
%14 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 16)
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @strncpy(i8* %16, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 7)
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = load i32, i32* @F_OK
%21 = call i64 @access(i8* %19, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %13
%24 = load i32, i32* @stderr
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i64 @unlink(i8* %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %23
%33 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %73
34: ; preds = %23
br label %35
35: ; preds = %34, %13
%36 = load i32, i32* @AF_UNIX
%37 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %36, i32* %37
%38 = load i32, i32* %2
%39 = call i64 @bind(i32 %38, %struct.sockaddr_un* %5, i32 16)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%43 = load i32, i32* %2
%44 = call i32 @close(i32 %43)
store i32 -1, i32* %1
br label %73
45: ; preds = %35
%46 = load i32, i32* %2
%47 = call i64 @listen(i32 %46, i32 1)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%51 = load i32, i32* %2
%52 = call i32 @close(i32 %51)
store i32 -1, i32* %1
br label %73
53: ; preds = %45
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%55 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %54, i32 0, i32 0
store i64 0, i64* %55
store i32 4, i32* %4
%56 = load i32, i32* %2
%57 = load i32, i32* @SOCK_CLOEXEC
%58 = call i32 @accept4(i32 %56, %struct.sockaddr_in* %6, i32* %4, i32 %57)
store i32 %58, i32* %3
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %53
%61 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %1
br label %73
62: ; preds = %53
%63 = load i32, i32* %2
%64 = call i32 @close(i32 %63)
%65 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%66 = load i8*, i8** %65
%67 = call i64 @unlink(i8* %66)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %62
%70 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %71
71: ; preds = %69, %62
%72 = load i32, i32* %3
store i32 %72, i32* %1
br label %73
73: ; preds = %71, %60, %49, %41, %32, %11
%74 = load i32, i32* %1
ret i32 %74
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [17 x i8] c"server: socket()\00"
@.str.1 = [13 x i8] c"accept4-test\00"
@F_OK = global i32 0
@stderr = global i32 0
@.str.2 = [49 x i8] c"server: socket '%s' already exists, removing...\0A\00"
@.str.3 = [17 x i8] c"server: unlink()\00"
@.str.4 = [32 x i8] c"server: bind() for accept4-test\00"
@.str.5 = [17 x i8] c"server: listen()\00"
@SOCK_CLOEXEC = global i32 0
@.str.6 = [17 x i8] c"server: accept()\00"
define i32 @serversocket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.sockaddr_un* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %59
13: ; preds = %0
%14 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 16)
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @strncpy(i8* %16, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 7)
%18 = load i8*, i8** %15
%19 = load i32, i32* @F_OK
%20 = call i64 @access(i8* %18, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %15
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %15
%27 = call i64 @unlink(i8* %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %59
31: ; preds = %22, %13
%32 = load i32, i32* @AF_UNIX
%33 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %32, i32* %33
%34 = call i64 @bind(i32 %9, %struct.sockaddr_un* %2, i32 16)
%35 = icmp eq i64 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @close(i32 %9)
br label %59
39: ; preds = %31
%40 = call i64 @listen(i32 %9, i32 1)
%41 = icmp eq i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 @close(i32 %9)
br label %59
45: ; preds = %39
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i64 0, i64* %46
store i32 4, i32* %1
%47 = load i32, i32* @SOCK_CLOEXEC
%48 = call i32 @accept4(i32 %9, %struct.sockaddr_in* %3, i32* %1, i32 %47)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
br label %59
52: ; preds = %45
%53 = call i32 @close(i32 %9)
%54 = load i8*, i8** %15
%55 = call i64 @unlink(i8* %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %52, %57, %50, %42, %36, %29, %11
%60 = phi i32 [ -1, %11 ], [ -1, %29 ], [ -1, %36 ], [ -1, %42 ], [ -1, %50 ], [ %48, %57 ], [ %48, %52 ]
ret i32 %60
}