#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*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int /*<<< orphan*/);
int listen(int, int);
int set_reuseaddr_unix(char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int unix_socket(const char *path) {
  struct sockaddr_un addr;
  int unix_socket;
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, path);
  unix_socket = socket(AF_UNIX, SOCK_STREAM, 0);
  if (unix_socket == -1)
    return -1;
  if (set_reuseaddr_unix(path) == -1)
    return -1;
  if (bind(unix_socket, (struct sockaddr *)&addr,
           (socklen_t)sizeof(struct sockaddr_un)) == -1)
    return -1;
  if (listen(unix_socket, 5) == -1)
    return -1;
  return unix_socket;
}


x86 assembly (clang, -O0):
<unix_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a6 <unix_socket+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <unix_socket+0xc2>
mov    -0x10(%rbp),%rdi
call   1040 <set_reuseaddr_unix@plt>
cmp    $0xffffffff,%eax
jne    11c4 <unix_socket+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <unix_socket+0xc2>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    11ea <unix_socket+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <unix_socket+0xc2>
mov    -0x1c(%rbp),%edi
mov    $0x5,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    120c <unix_socket+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <unix_socket+0xc2>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbp
mov    0x2e86(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
mov    %rdi,%rsi
call   1030 <strcpy@plt>
mov    (%rbx),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     11c7 <unix_socket+0x77>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <set_reuseaddr_unix@plt>
cmp    $0xffffffff,%eax
je     11c7 <unix_socket+0x77>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     11c7 <unix_socket+0x77>
mov    %ebx,%edi
mov    $0x5,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @unix_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @AF_UNIX
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%9 = load i32, i32* %8
%10 = load i8*, i8** %3
%11 = call i32 @strcpy(i32 %9, i8* %10)
%12 = load i32, i32* @AF_UNIX
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %36
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = call i32 @set_reuseaddr_unix(i8* %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %2
br label %36
23: ; preds = %18
%24 = load i32, i32* %5
%25 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 8)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 -1, i32* %2
br label %36
29: ; preds = %23
%30 = load i32, i32* %5
%31 = call i32 @listen(i32 %30, i32 5)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 -1, i32* %2
br label %36
34: ; preds = %29
%35 = load i32, i32* %5
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %33, %28, %22, %17
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @unix_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %4, i32* %5
%6 = tail call i32 @strcpy(i32 undef, i8* %0)
%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 eq i32 %9, -1
br i1 %10, label %22, label %11
11: ; preds = %1
%12 = tail call i32 @set_reuseaddr_unix(i8* %0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %22, label %14
14: ; preds = %11
%15 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%16 = call i32 @bind(i32 %9, %struct.sockaddr* %15, i32 8)
%17 = icmp eq i32 %16, -1
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = call i32 @listen(i32 %9, i32 5)
%20 = icmp eq i32 %19, -1
%21 = select i1 %20, i32 -1, i32 %9
br label %22
22: ; preds = %18, %14, %11, %1
%23 = phi i32 [ -1, %1 ], [ -1, %11 ], [ -1, %14 ], [ %21, %18 ]
ret i32 %23
}