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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ unlink(char const *);
int unix_server_socket(const char *path) {
  struct sockaddr_un sun;
  int s, rc, saved_errno;
  if (strlen(path) >= sizeof(sun.sun_path))
    return -1;
  s = socket(PF_UNIX, SOCK_STREAM, 0);
  if (s < 0)
    return -1;
  rc = fcntl(s, F_GETFL, 0);
  if (rc < 0)
    goto fail;
  rc = fcntl(s, F_SETFL, rc | O_NONBLOCK);
  if (rc < 0)
    goto fail;
  rc = fcntl(s, F_GETFD, 0);
  if (rc < 0)
    goto fail;
  rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
  if (rc < 0)
    goto fail;
  memset(&sun, 0, sizeof(sun));
  sun.sun_family = AF_UNIX;
  strncpy(sun.sun_path, path, sizeof(sun.sun_path));
  rc = bind(s, (struct sockaddr *)&sun, sizeof(sun));
  if (rc < 0)
    goto fail;
  rc = listen(s, 2);
  if (rc < 0)
    goto fail_unlink;
  return s;
fail_unlink:
  saved_errno = errno;
  unlink(path);
  errno = saved_errno;
fail:
  saved_errno = errno;
  close(s);
  errno = saved_errno;
  return -1;
}


x86 assembly (clang, -O0):
<unix_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
cltq
cmp    $0x4,%rax
jb     11bd <unix_server_socket+0x2d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1359 <unix_server_socket+0x1c9>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ef <unix_server_socket+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1359 <unix_server_socket+0x1c9>
mov    -0x1c(%rbp),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1214 <unix_server_socket+0x84>
jmp    1332 <unix_server_socket+0x1a2>
mov    -0x1c(%rbp),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    0x2db6(%rip),%rax
or     (%rax),%edx
call   1050 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1243 <unix_server_socket+0xb3>
jmp    1332 <unix_server_socket+0x1a2>
mov    -0x1c(%rbp),%edi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1268 <unix_server_socket+0xd8>
jmp    1332 <unix_server_socket+0x1a2>
mov    -0x1c(%rbp),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    0x2d32(%rip),%rax
or     (%rax),%edx
call   1050 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1297 <unix_server_socket+0x107>
jmp    1332 <unix_server_socket+0x1a2>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12e7 <unix_server_socket+0x157>
jmp    1332 <unix_server_socket+0x1a2>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1090 <listen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1306 <unix_server_socket+0x176>
jmp    1311 <unix_server_socket+0x181>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1359 <unix_server_socket+0x1c9>
mov    0x2c90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x24(%rbp),%ecx
mov    0x2c78(%rip),%rax
mov    %ecx,(%rax)
mov    0x2c6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2c58(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_server_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1060 <strlen@plt>
mov    $0xffffffff,%r14d
cmp    $0x3,%eax
ja     12cf <unix_server_socket+0x13f>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     12cf <unix_server_socket+0x13f>
mov    %eax,%ebp
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
test   %eax,%eax
js     12bb <unix_server_socket+0x12b>
mov    0x2dc9(%rip),%rcx
mov    (%rcx),%esi
mov    0x2de0(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %eax,%edx
call   1050 <fcntl@plt>
test   %eax,%eax
js     12bb <unix_server_socket+0x12b>
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
test   %eax,%eax
js     12bb <unix_server_socket+0x12b>
mov    0x2d94(%rip),%rcx
mov    (%rcx),%esi
mov    0x2d73(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %eax,%edx
call   1050 <fcntl@plt>
test   %eax,%eax
js     12bb <unix_server_socket+0x12b>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
test   %eax,%eax
js     12bb <unix_server_socket+0x12b>
mov    %ebp,%edi
mov    $0x2,%esi
call   1090 <listen@plt>
test   %eax,%eax
js     12a6 <unix_server_socket+0x116>
mov    %ebp,%r14d
jmp    12cf <unix_server_socket+0x13f>
mov    0x2cfb(%rip),%r15
mov    (%r15),%r12d
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %r12d,(%r15)
mov    0x2ce6(%rip),%r15
mov    (%r15),%ebx
mov    %ebp,%edi
call   1080 <close@plt>
mov    %ebx,(%r15)
mov    %r14d,%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 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@AF_UNIX = global i32 0
@errno = global i32 0
define i32 @unix_server_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = sext i32 %9 to i64
%11 = icmp uge i64 %10, 4
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %86
13: ; preds = %1
%14 = load i32, i32* @PF_UNIX
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %13
store i32 -1, i32* %2
br label %86
20: ; preds = %13
%21 = load i32, i32* %5
%22 = load i32, i32* @F_GETFL
%23 = call i32 @fcntl(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
br label %81
27: ; preds = %20
%28 = load i32, i32* %5
%29 = load i32, i32* @F_SETFL
%30 = load i32, i32* %6
%31 = load i32, i32* @O_NONBLOCK
%32 = or i32 %30, %31
%33 = call i32 @fcntl(i32 %28, i32 %29, i32 %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %27
br label %81
37: ; preds = %27
%38 = load i32, i32* %5
%39 = load i32, i32* @F_GETFD
%40 = call i32 @fcntl(i32 %38, i32 %39, i32 0)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %37
br label %81
44: ; preds = %37
%45 = load i32, i32* %5
%46 = load i32, i32* @F_SETFD
%47 = load i32, i32* %6
%48 = load i32, i32* @FD_CLOEXEC
%49 = or i32 %47, %48
%50 = call i32 @fcntl(i32 %45, i32 %46, i32 %49)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %44
br label %81
54: ; preds = %44
%55 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 8)
%56 = load i32, i32* @AF_UNIX
%57 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %56, i32* %57
%58 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%59 = load i32, i32* %58
%60 = load i8*, i8** %3
%61 = call i32 @strncpy(i32 %59, i8* %60, i32 4)
%62 = load i32, i32* %5
%63 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%64 = call i32 @bind(i32 %62, %struct.sockaddr* %63, i32 8)
store i32 %64, i32* %6
%65 = load i32, i32* %6
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %54
br label %81
68: ; preds = %54
%69 = load i32, i32* %5
%70 = call i32 @listen(i32 %69, i32 2)
store i32 %70, i32* %6
%71 = load i32, i32* %6
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %68
br label %76
74: ; preds = %68
%75 = load i32, i32* %5
store i32 %75, i32* %2
br label %86
76: ; preds = %73
%77 = load i32, i32* @errno
store i32 %77, i32* %7
%78 = load i8*, i8** %3
%79 = call i32 @unlink(i8* %78)
%80 = load i32, i32* %7
store i32 %80, i32* @errno
br label %81
81: ; preds = %76, %67, %53, %43, %36, %26
%82 = load i32, i32* @errno
store i32 %82, i32* %7
%83 = load i32, i32* %5
%84 = call i32 @close(i32 %83)
%85 = load i32, i32* %7
store i32 %85, i32* @errno
store i32 -1, i32* %2
br label %86
86: ; preds = %81, %74, %19, %12
%87 = load i32, i32* %2
ret i32 %87
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@AF_UNIX = global i32 0
@errno = global i32 0
define i32 @unix_server_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp ugt i32 %4, 3
br i1 %5, label %50, label %6
6: ; preds = %1
%7 = load i32, i32* @PF_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 %50, label %11
11: ; preds = %6
%12 = load i32, i32* @F_GETFL
%13 = tail call i32 @fcntl(i32 %9, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %47, label %15
15: ; preds = %11
%16 = load i32, i32* @F_SETFL
%17 = load i32, i32* @O_NONBLOCK
%18 = or i32 %17, %13
%19 = tail call i32 @fcntl(i32 %9, i32 %16, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %47, label %21
21: ; preds = %15
%22 = load i32, i32* @F_GETFD
%23 = tail call i32 @fcntl(i32 %9, i32 %22, i32 0)
%24 = icmp slt i32 %23, 0
br i1 %24, label %47, label %25
25: ; preds = %21
%26 = load i32, i32* @F_SETFD
%27 = load i32, i32* @FD_CLOEXEC
%28 = or i32 %27, %23
%29 = tail call i32 @fcntl(i32 %9, i32 %26, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %47, label %31
31: ; preds = %25
%32 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%33 = load i32, i32* @AF_UNIX
%34 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @strncpy(i32 %36, i8* %0, i32 4)
%38 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%39 = call i32 @bind(i32 %9, %struct.sockaddr* %38, i32 8)
%40 = icmp slt i32 %39, 0
br i1 %40, label %47, label %41
41: ; preds = %31
%42 = call i32 @listen(i32 %9, i32 2)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = load i32, i32* @errno
%46 = call i32 @unlink(i8* %0)
store i32 %45, i32* @errno
br label %47
47: ; preds = %31, %25, %21, %15, %11, %44
%48 = load i32, i32* @errno
%49 = call i32 @close(i32 %9)
store i32 %48, i32* @errno
br label %50
50: ; preds = %41, %6, %1, %47
%51 = phi i32 [ -1, %47 ], [ -1, %1 ], [ -1, %6 ], [ %9, %41 ]
ret i32 %51
}