#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_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, ...);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
int /*<<< orphan*/ unlink(char const *);
int ss_listen_on_unix_socket_impl(const char *pathname, int backlog,
                                  int error_on_existing) {
  struct sockaddr_un addr;
  memset(&addr, 0, sizeof(addr));
  if (strlen(pathname) >= sizeof(addr.sun_path)) {
    errno = 0;
    return -1;
  }
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, pathname);
  if (!error_on_existing)
    unlink(pathname);
  int sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock == -1)
    return -2;
  fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC);
  if ((bind(sock, (struct sockaddr *)&addr, sizeof(addr))) == -1) {
    int saved_errno = errno;
    close(sock);
    errno = saved_errno;
    return -3;
  }
  if ((listen(sock, backlog)) == -1) {
    int saved_errno = errno;
    close(sock);
    errno = saved_errno;
    return -4;
  }
  return sock;
}


x86 assembly (clang, -O0):
<ss_listen_on_unix_socket_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
cltq
cmp    $0x4,%rax
jb     11e0 <ss_listen_on_unix_socket_impl+0x50>
mov    0x2df2(%rip),%rax
movl   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <ss_listen_on_unix_socket_impl+0x176>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
cmpl   $0x0,-0x18(%rbp)
jne    120b <ss_listen_on_unix_socket_impl+0x7b>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    123d <ss_listen_on_unix_socket_impl+0xad>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1306 <ss_listen_on_unix_socket_impl+0x176>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <fcntl@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%esi
mov    %eax,%edx
mov    0x2d57(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1050 <fcntl@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12c0 <ss_listen_on_unix_socket_impl+0x130>
mov    0x2d25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%ecx
mov    0x2d0e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xfffffffd,-0x4(%rbp)
jmp    1306 <ss_listen_on_unix_socket_impl+0x176>
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1300 <ss_listen_on_unix_socket_impl+0x170>
mov    0x2ce5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2cce(%rip),%rax
mov    %ecx,(%rax)
movl   $0xfffffffc,-0x4(%rbp)
jmp    1306 <ss_listen_on_unix_socket_impl+0x176>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ss_listen_on_unix_socket_impl>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0x8(%rsp),%rdi
xor    %r12d,%r12d
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    $0xffffffff,%ebx
cmp    $0x3,%eax
ja     1288 <ss_listen_on_unix_socket_impl+0xf8>
mov    0x2e12(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
test   %r15d,%r15d
jne    11f5 <ss_listen_on_unix_socket_impl+0x65>
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    (%rbx),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     126b <ss_listen_on_unix_socket_impl+0xdb>
mov    %eax,%ebp
mov    0x2dc3(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1050 <fcntl@plt>
mov    0x2d98(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fcntl@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     1272 <ss_listen_on_unix_socket_impl+0xe2>
mov    %ebp,%edi
mov    %r14d,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1294 <ss_listen_on_unix_socket_impl+0x104>
mov    $0xfffffffc,%ebx
jmp    1277 <ss_listen_on_unix_socket_impl+0xe7>
mov    $0xfffffffe,%ebp
jmp    1294 <ss_listen_on_unix_socket_impl+0x104>
mov    $0xfffffffd,%ebx
mov    0x2d42(%rip),%rax
mov    (%rax),%r12d
mov    %ebp,%edi
call   1080 <close@plt>
mov    0x2d31(%rip),%rax
mov    %r12d,(%rax)
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @ss_listen_on_unix_socket_impl(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_un
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = call i32 @memset(%struct.sockaddr_un* %8, i32 0, i32 8)
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
%15 = sext i32 %14 to i64
%16 = icmp uge i64 %15, 4
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 0, i32* @errno
store i32 -1, i32* %4
br label %67
18: ; preds = %3
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load i8*, i8** %5
%24 = call i32 @strcpy(i32 %22, i8* %23)
%25 = load i32, i32* %7
%26 = icmp ne i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %18
%28 = load i8*, i8** %5
%29 = call i32 @unlink(i8* %28)
br label %30
30: ; preds = %27, %18
%31 = load i32, i32* @AF_UNIX
%32 = load i32, i32* @SOCK_STREAM
%33 = call i32 @socket(i32 %31, i32 %32, i32 0)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %30
store i32 -2, i32* %4
br label %67
37: ; preds = %30
%38 = load i32, i32* %9
%39 = load i32, i32* @F_SETFD
%40 = load i32, i32* %9
%41 = load i32, i32* @F_GETFD
%42 = call i32 (i32, i32, ...) @fcntl(i32 %40, i32 %41)
%43 = load i32, i32* @FD_CLOEXEC
%44 = or i32 %42, %43
%45 = call i32 (i32, i32, ...) @fcntl(i32 %38, i32 %39, i32 %44)
%46 = load i32, i32* %9
%47 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%48 = call i32 @bind(i32 %46, %struct.sockaddr* %47, i32 8)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %55
50: ; preds = %37
%51 = load i32, i32* @errno
store i32 %51, i32* %10
%52 = load i32, i32* %9
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %10
store i32 %54, i32* @errno
store i32 -3, i32* %4
br label %67
55: ; preds = %37
%56 = load i32, i32* %9
%57 = load i32, i32* %6
%58 = call i32 @listen(i32 %56, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %65
60: ; preds = %55
%61 = load i32, i32* @errno
store i32 %61, i32* %11
%62 = load i32, i32* %9
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* %11
store i32 %64, i32* @errno
store i32 -4, i32* %4
br label %67
65: ; preds = %55
%66 = load i32, i32* %9
store i32 %66, i32* %4
br label %67
67: ; preds = %65, %60, %50, %36, %17
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @ss_listen_on_unix_socket_impl(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_un
%5 = bitcast %struct.sockaddr_un* %4 to i8*
%6 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 8)
%7 = call i32 @strlen(i8* %0)
%8 = icmp ugt i32 %7, 3
br i1 %8, label %40, label %9
9: ; preds = %3
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @strcpy(i32 %13, i8* %0)
%15 = icmp eq i32 %2, 0
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = call i32 @unlink(i8* %0)
br label %18
18: ; preds = %16, %9
%19 = load i32, i32* @AF_UNIX
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %43, label %23
23: ; preds = %18
%24 = load i32, i32* @F_SETFD
%25 = load i32, i32* @F_GETFD
%26 = call i32 (i32, i32, ...) @fcntl(i32 %21, i32 %25)
%27 = load i32, i32* @FD_CLOEXEC
%28 = or i32 %27, %26
%29 = call i32 (i32, i32, ...) @fcntl(i32 %21, i32 %24, i32 %28)
%30 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%31 = call i32 @bind(i32 %21, %struct.sockaddr* %30, i32 8)
%32 = icmp eq i32 %31, -1
br i1 %32, label %36, label %33
33: ; preds = %23
%34 = call i32 @listen(i32 %21, i32 %1)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %43
36: ; preds = %33, %23
%37 = phi i32 [ -3, %23 ], [ -4, %33 ]
%38 = load i32, i32* @errno
%39 = call i32 @close(i32 %21)
br label %40
40: ; preds = %36, %3
%41 = phi i32 [ 0, %3 ], [ %38, %36 ]
%42 = phi i32 [ -1, %3 ], [ %37, %36 ]
store i32 %41, i32* @errno
br label %43
43: ; preds = %40, %18, %33
%44 = phi i32 [ -2, %18 ], [ %21, %33 ], [ %42, %40 ]
ret i32 %44
}