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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct sockaddr_un *, char, int);
int ret_err(char **, char *, int /*<<< orphan*/, ...);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int unix_ipc_connect(char *address, char **errstr) {
  int s;
  struct sockaddr_un un;
  if ((s = socket(PF_UNIX, SOCK_STREAM, 0)) >= 0) {
    memset(&un, '\0', sizeof(un));
    un.sun_family = AF_UNIX;
    snprintf(un.sun_path, sizeof(un.sun_path), "%s", address);
    if (connect(s, (struct sockaddr *)&un, sizeof(un)) == 0) {
      return s;
    }
    close(s);
    return ret_err(errstr, "connect to %s: %s", un.sun_path, strerror(errno));
  }
  return ret_err(errstr, "socket: %s", strerror(errno));
}


x86 assembly (clang, -O0):
<unix_ipc_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jl     1242 <unix_ipc_connect+0xd2>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xe3b(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jne    11ff <unix_ipc_connect+0x8f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <unix_ipc_connect+0xff>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
lea    0xdcd(%rip),%rsi
mov    $0x0,%al
call   1060 <ret_err@plt>
mov    %eax,-0x4(%rbp)
jmp    126f <unix_ipc_connect+0xff>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1060 <ret_err@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_ipc_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1226 <unix_ipc_connect+0xb6>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
lea    0xe46(%rip),%rdx
mov    $0x4,%esi
mov    %rbx,%rcx
call   1030 <snprintf@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1070 <connect@plt>
test   %rax,%rax
je     1219 <unix_ipc_connect+0xa9>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%rsp),%ebx
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdf7(%rip),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <ret_err@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdd7(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <ret_err@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [18 x i8] c"connect to %s: %s\00"
@errno = global i32 0
@.str.2 = [11 x i8] c"socket: %s\00"
define i32 @unix_ipc_connect(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
store i8* %0, i8** %4
store i8** %1, i8*** %5
%8 = load i32, i32* @PF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %35
12: ; preds = %2
%13 = call i32 @memset(%struct.sockaddr_un* %7, i8 signext 0, i32 8)
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i8*, i8** %4
%19 = call i32 @snprintf(i32 %17, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* %6
%21 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%22 = call i64 @connect(i32 %20, %struct.sockaddr* %21, i32 8)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = load i32, i32* %6
store i32 %25, i32* %3
br label %40
26: ; preds = %12
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = call i32 (i8**, i8*, i32, ...) @ret_err(i8** %29, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32 %33)
store i32 %34, i32* %3
br label %40
35: ; preds = %2
%36 = load i8**, i8*** %5
%37 = load i32, i32* @errno
%38 = call i32 @strerror(i32 %37)
%39 = call i32 (i8**, i8*, i32, ...) @ret_err(i8** %36, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %38)
store i32 %39, i32* %3
br label %40
40: ; preds = %35, %26, %24
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [18 x i8] c"connect to %s: %s\00"
@errno = global i32 0
@.str.2 = [11 x i8] c"socket: %s\00"
define i32 @unix_ipc_connect(i8* %0, i8** %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = load i32, i32* @PF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %25
9: ; preds = %2
%10 = call i32 @memset(%struct.sockaddr_un* %3, i8 signext 0, i32 8)
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @snprintf(i32 %14, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%16 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%17 = call i64 @connect(i32 %7, %struct.sockaddr* %16, i32 8)
%18 = icmp eq i64 %17, 0
br i1 %18, label %29, label %19
19: ; preds = %9
%20 = call i32 @close(i32 %7)
%21 = load i32, i32* %13
%22 = load i32, i32* @errno
%23 = call i32 @strerror(i32 %22)
%24 = call i32 (i8**, i8*, i32, ...) @ret_err(i8** %1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %23)
br label %29
25: ; preds = %2
%26 = load i32, i32* @errno
%27 = tail call i32 @strerror(i32 %26)
%28 = tail call i32 (i8**, i8*, i32, ...) @ret_err(i8** %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %9, %25, %19
%30 = phi i32 [ %24, %19 ], [ %28, %25 ], [ %7, %9 ]
ret i32 %30
}