#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;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr_un *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int unix_domain_socket(const char *source) {
  int sockfd;
  struct sockaddr_un servaddr;
  if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
    return -1;
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sun_family = AF_UNIX;
  strncpy(servaddr.sun_path, source, sizeof(servaddr.sun_path) - 1);
  if (connect(sockfd, &servaddr, sizeof(servaddr)))
    return -1;
  return sockfd;
}


x86 assembly (clang, -O0):
<unix_domain_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    117d <unix_domain_socket+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <unix_domain_socket+0x97>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
je     11d1 <unix_domain_socket+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <unix_domain_socket+0x97>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_domain_socket>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e96(%rip),%r12
mov    (%r12),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11af <unix_domain_socket+0x6f>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1050 <connect@plt>
neg    %rax
sbb    %eax,%eax
or     %ebx,%eax
jmp    11b4 <unix_domain_socket+0x74>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @unix_domain_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
store i8* %0, i8** %3
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %25
11: ; preds = %1
%12 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 8)
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i8*, i8** %3
%18 = call i32 @strncpy(i32 %16, i8* %17, i32 3)
%19 = load i32, i32* %4
%20 = call i64 @connect(i32 %19, %struct.sockaddr_un* %5, i32 8)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %11
store i32 -1, i32* %2
br label %25
23: ; preds = %11
%24 = load i32, i32* %4
store i32 %24, i32* %2
br label %25
25: ; preds = %23, %22, %10
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @unix_domain_socket(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 eq i32 %6, -1
br i1 %7, label %18, label %8
8: ; preds = %1
%9 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @strncpy(i32 %13, i8* %0, i32 3)
%15 = call i64 @connect(i32 %6, %struct.sockaddr_un* %2, i32 8)
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i32 %6, i32 -1
br label %18
18: ; preds = %8, %1
%19 = phi i32 [ -1, %1 ], [ %17, %8 ]
ret i32 %19
}