#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ ENOENT;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ chmod(char const *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int unlink(char const *);
int OpenUnixSocket(const char *path) {
  struct sockaddr_un addr;
  int s;
  int rv;
  s = socket(AF_UNIX, SOCK_STREAM, 0);
  if (s < 0) {
    syslog(LOG_ERR, "socket(AF_UNIX): %m");
    return -1;
  }
  rv = unlink(path);
  if (rv < 0 && errno != ENOENT) {
    syslog(LOG_ERR, "unlink(unixsocket, \"%s\"): %m", path);
    close(s);
    return -1;
  }
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, path, sizeof(addr.sun_path));
  if (bind(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
    syslog(LOG_ERR, "bind(unixsocket, \"%s\"): %m", path);
    close(s);
    return -1;
  } else if (listen(s, 5) < 0) {
    syslog(LOG_ERR, "listen(unixsocket): %m");
    close(s);
    return -1;
  }
  if (chmod(path, 0666) < 0) {
    syslog(LOG_WARNING, "chmod(\"%s\"): %m", path);
  }
  return s;
}


x86 assembly (clang, -O0):
<OpenUnixSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d5 <OpenUnixSocket+0x55>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131b <OpenUnixSocket+0x19b>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1234 <OpenUnixSocket+0xb4>
mov    0x2dd6(%rip),%rax
mov    (%rax),%rax
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%rax
je     1234 <OpenUnixSocket+0xb4>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdfb(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131b <OpenUnixSocket+0x19b>
mov    0x2dad(%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   1090 <bind@plt>
cmp    $0x0,%rax
jge    129b <OpenUnixSocket+0x11b>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdb1(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131b <OpenUnixSocket+0x19b>
mov    -0x1c(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    12dd <OpenUnixSocket+0x15d>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
lea    0xd8a(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131b <OpenUnixSocket+0x19b>
jmp    12e2 <OpenUnixSocket+0x162>
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
call   1080 <chmod@plt>
cmp    $0x0,%rax
jge    1315 <OpenUnixSocket+0x195>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd55(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OpenUnixSocket>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e55(%rip),%r15
mov    (%r15),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1243 <OpenUnixSocket+0xc3>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <unlink@plt>
test   %eax,%eax
jns    11d6 <OpenUnixSocket+0x56>
mov    0x2e05(%rip),%rax
mov    (%rax),%rax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
jne    126e <OpenUnixSocket+0xee>
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     125c <OpenUnixSocket+0xdc>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     128a <OpenUnixSocket+0x10a>
mov    %r14,%rdi
mov    $0x1b6,%esi
call   1080 <chmod@plt>
test   %rax,%rax
jns    12ad <OpenUnixSocket+0x12d>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <syslog@plt>
jmp    12ad <OpenUnixSocket+0x12d>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
xor    %eax,%eax
call   1060 <syslog@plt>
jmp    12a8 <OpenUnixSocket+0x128>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
jmp    127e <OpenUnixSocket+0xfe>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <syslog@plt>
jmp    12a1 <OpenUnixSocket+0x121>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
xor    %eax,%eax
call   1060 <syslog@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@LOG_ERR = global i32 0
@.str = [20 x i8] c"socket(AF_UNIX): %m\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [29 x i8] c"unlink(unixsocket, \22%s\22): %m\00"
@.str.2 = [27 x i8] c"bind(unixsocket, \22%s\22): %m\00"
@.str.3 = [23 x i8] c"listen(unixsocket): %m\00"
@LOG_WARNING = global i32 0
@.str.4 = [16 x i8] c"chmod(\22%s\22): %m\00"
define i32 @OpenUnixSocket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%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* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @LOG_ERR
%14 = call i32 (i32, i8*, ...) @syslog(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %67
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i32 @unlink(i8* %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = load i64, i64* @ENOENT
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i32, i32* @LOG_ERR
%26 = load i8*, i8** %3
%27 = call i32 (i32, i8*, ...) @syslog(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* %5
%29 = call i32 @close(i32 %28)
store i32 -1, i32* %2
br label %67
30: ; preds = %20, %15
%31 = load i32, i32* @AF_UNIX
%32 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i8*, i8** %3
%36 = call i32 @strncpy(i32 %34, i8* %35, i32 4)
%37 = load i32, i32* %5
%38 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%39 = call i64 @bind(i32 %37, %struct.sockaddr* %38, i32 8)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %30
%42 = load i32, i32* @LOG_ERR
%43 = load i8*, i8** %3
%44 = call i32 (i32, i8*, ...) @syslog(i32 %42, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %43)
%45 = load i32, i32* %5
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %2
br label %67
47: ; preds = %30
%48 = load i32, i32* %5
%49 = call i64 @listen(i32 %48, i32 5)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i32, i32* @LOG_ERR
%53 = call i32 (i32, i8*, ...) @syslog(i32 %52, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%54 = load i32, i32* %5
%55 = call i32 @close(i32 %54)
store i32 -1, i32* %2
br label %67
56: ; preds = %47
br label %57
57: ; preds = %56
%58 = load i8*, i8** %3
%59 = call i64 @chmod(i8* %58, i32 438)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %57
%62 = load i32, i32* @LOG_WARNING
%63 = load i8*, i8** %3
%64 = call i32 (i32, i8*, ...) @syslog(i32 %62, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %63)
br label %65
65: ; preds = %61, %57
%66 = load i32, i32* %5
store i32 %66, i32* %2
br label %67
67: ; preds = %65, %51, %41, %24, %12
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@LOG_ERR = global i32 0
@.str = [20 x i8] c"socket(AF_UNIX): %m\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [29 x i8] c"unlink(unixsocket, \22%s\22): %m\00"
@.str.2 = [27 x i8] c"bind(unixsocket, \22%s\22): %m\00"
@.str.3 = [23 x i8] c"listen(unixsocket): %m\00"
@LOG_WARNING = global i32 0
@.str.4 = [16 x i8] c"chmod(\22%s\22): %m\00"
define i32 @OpenUnixSocket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @LOG_ERR
%10 = tail call i32 (i32, i8*, ...) @syslog(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %46
11: ; preds = %1
%12 = tail call i32 @unlink(i8* %0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOENT
%17 = icmp eq i64 %15, %16
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = load i32, i32* @LOG_ERR
%20 = tail call i32 (i32, i8*, ...) @syslog(i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = tail call i32 @close(i32 %6)
br label %46
22: ; preds = %14, %11
%23 = load i32, i32* @AF_UNIX
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %23, i32* %24
%25 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%26 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%27 = call i64 @bind(i32 %6, %struct.sockaddr* %26, i32 8)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %22
%30 = load i32, i32* @LOG_ERR
%31 = call i32 (i32, i8*, ...) @syslog(i32 %30, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%32 = call i32 @close(i32 %6)
br label %46
33: ; preds = %22
%34 = call i64 @listen(i32 %6, i32 5)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* @LOG_ERR
%38 = call i32 (i32, i8*, ...) @syslog(i32 %37, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @close(i32 %6)
br label %46
40: ; preds = %33
%41 = call i64 @chmod(i8* %0, i32 438)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* @LOG_WARNING
%45 = call i32 (i32, i8*, ...) @syslog(i32 %44, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %46
46: ; preds = %40, %43, %36, %29, %18, %8
%47 = phi i32 [ -1, %8 ], [ -1, %18 ], [ -1, %29 ], [ -1, %36 ], [ %6, %43 ], [ %6, %40 ]
ret i32 %47
}