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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(int /*<<< orphan*/);
int dspd_unix_sock_connect(const char *addr, int flags) {
  int s, len, err;
  struct sockaddr_un remote;
  if ((s = socket(AF_UNIX, SOCK_STREAM | flags, 0)) == -1) {
    return -errno;
  }
  memset(&remote, 0, sizeof(remote));
  remote.sun_family = AF_UNIX;
  strcpy(remote.sun_path, addr);
  len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  if (connect(s, (struct sockaddr *)&remote, len) == -1) {
    err = -errno;
    close(s);
    return err;
  }
  return s;
}


x86 assembly (clang, -O0):
<dspd_unix_sock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
or     -0x14(%rbp),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11aa <dspd_unix_sock_connect+0x4a>
xor    %eax,%eax
mov    0x2e30(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1222 <dspd_unix_sock_connect+0xc2>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%edi
call   1040 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    121c <dspd_unix_sock_connect+0xbc>
xor    %eax,%eax
mov    0x2dcc(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1222 <dspd_unix_sock_connect+0xc2>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dspd_unix_sock_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e73(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11ea <dspd_unix_sock_connect+0x8a>
mov    %eax,%ebx
mov    %rsp,%r15
xor    %r12d,%r12d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    (%rsp),%edi
call   1040 <strlen@plt>
lea    0x4(%rax),%edx
mov    %ebx,%edi
mov    %r15,%rsi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    11f5 <dspd_unix_sock_connect+0x95>
mov    0x2df5(%rip),%rax
sub    (%rax),%r12d
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r12d,%ebx
jmp    11f5 <dspd_unix_sock_connect+0x95>
mov    0x2ddf(%rip),%rax
sub    (%rax),%ebp
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
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
@errno = global i32 0
define i32 @dspd_unix_sock_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_un
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = load i32, i32* %5
%13 = or i32 %11, %12
%14 = call i32 @socket(i32 %10, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
store i32 %18, i32* %3
br label %46
19: ; preds = %2
%20 = call i32 @memset(%struct.sockaddr_un* %9, i32 0, i32 8)
%21 = load i32, i32* @AF_UNIX
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i8*, i8** %4
%26 = call i32 @strcpy(i32 %24, i8* %25)
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @strlen(i32 %28)
%30 = sext i32 %29 to i64
%31 = add i64 %30, 4
%32 = trunc i64 %31 to i32
store i32 %32, i32* %7
%33 = load i32, i32* %6
%34 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%35 = load i32, i32* %7
%36 = call i32 @connect(i32 %33, %struct.sockaddr* %34, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %44
38: ; preds = %19
%39 = load i32, i32* @errno
%40 = sub nsw i32 0, %39
store i32 %40, i32* %8
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %8
store i32 %43, i32* %3
br label %46
44: ; preds = %19
%45 = load i32, i32* %6
store i32 %45, i32* %3
br label %46
46: ; preds = %44, %38, %16
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
define i32 @dspd_unix_sock_connect(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = or i32 %6, %1
%8 = tail call i32 @socket(i32 %5, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
br label %30
13: ; preds = %2
%14 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @strcpy(i32 %18, i8* %0)
%20 = load i32, i32* %17
%21 = call i32 @strlen(i32 %20)
%22 = add i32 %21, 4
%23 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%24 = call i32 @connect(i32 %8, %struct.sockaddr* %23, i32 %22)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %13
%27 = load i32, i32* @errno
%28 = sub nsw i32 0, %27
%29 = call i32 @close(i32 %8)
br label %30
30: ; preds = %13, %26, %10
%31 = phi i32 [ %12, %10 ], [ %28, %26 ], [ %8, %13 ]
ret i32 %31
}