#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*/ sun;
struct sockaddr_un {
  int /*<<< orphan*/ *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ socklen_t;
typedef int /*<<< orphan*/ sa_family_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, scalar_t__);
int errno;
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/ *, char const *);
scalar_t__ strlen(int /*<<< orphan*/ *);
int client_connect(const char *path) {
  struct sockaddr_un sun;
  socklen_t addrlen;
  int rv, fd;
  fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (fd < 0) {
    fd = -errno;
    goto out;
  }
  memset(&sun, 0, sizeof(sun));
  sun.sun_family = AF_UNIX;
  strcpy(&sun.sun_path[1], path);
  addrlen = sizeof(sa_family_t) + strlen(sun.sun_path + 1) + 1;
  rv = connect(fd, (struct sockaddr *)&sun, addrlen);
  if (rv < 0) {
    close(fd);
    fd = -errno;
  }
out:
  return fd;
}


x86 assembly (clang, -O0):
<client_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11a5 <client_connect+0x45>
xor    %eax,%eax
mov    0x2e2d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    1223 <client_connect+0xc3>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x4,%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
add    $0x4,%rdi
call   1040 <strlen@plt>
add    $0x4,%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    121e <client_connect+0xbe>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
mov    0x2daf(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
jmp    1223 <client_connect+0xc3>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11e9 <client_connect+0x89>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rdi
add    $0x4,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%rdi
add    $0x4,%rdi
call   1040 <strlen@plt>
lea    0x5(%rax),%rdx
mov    %ebx,%edi
mov    %r15,%rsi
call   1070 <connect@plt>
test   %eax,%eax
jns    11f4 <client_connect+0x94>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2dd8(%rip),%rax
sub    (%rax),%ebp
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32*, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
@AF_UNIX = global i32 0
define i32 @client_connect(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_un
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i32, i32* @PF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
store i32 %14, i32* %6
br label %42
15: ; preds = %1
%16 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 16)
%17 = load i32, i32* @AF_UNIX
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%20 = load i32*, i32** %19
%21 = getelementptr inbounds i32, i32* %20, i64 1
%22 = load i8*, i8** %2
%23 = call i32 @strcpy(i32* %21, i8* %22)
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%25 = load i32*, i32** %24
%26 = getelementptr inbounds i32, i32* %25, i64 1
%27 = call i64 @strlen(i32* %26)
%28 = add i64 4, %27
%29 = add i64 %28, 1
store i64 %29, i64* %4
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%32 = load i64, i64* %4
%33 = call i32 @connect(i32 %30, %struct.sockaddr* %31, i64 %32)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %15
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* @errno
%40 = sub nsw i32 0, %39
store i32 %40, i32* %6
br label %41
41: ; preds = %36, %15
br label %42
42: ; preds = %41, %12
%43 = load i32, i32* %6
ret i32 %43
}

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