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

/* 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 errno;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char const *);
int lxc_af_unix_connect(const char *path) {
  int fd;
  struct sockaddr_un addr;
  fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (fd < 0)
    return -1;
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  memcpy(addr.sun_path, path, path[0] ? strlen(path) : sizeof(addr.sun_path));
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
    int tmp = errno;
    close(fd);
    errno = tmp;
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<lxc_af_unix_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119e <lxc_af_unix_connect+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <lxc_af_unix_connect+0xf9>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ec <lxc_af_unix_connect+0x8c>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cltq
mov    %rax,-0x40(%rbp)
jmp    11fa <lxc_af_unix_connect+0x9a>
mov    $0x4,%eax
mov    %rax,-0x40(%rbp)
jmp    11fa <lxc_af_unix_connect+0x9a>
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
je     1253 <lxc_af_unix_connect+0xf3>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2d83(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <lxc_af_unix_connect+0xf9>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lxc_af_unix_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     11fd <lxc_af_unix_connect+0x9d>
mov    %eax,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%r15d
cmpb   $0x0,(%rbx)
je     11c0 <lxc_af_unix_connect+0x60>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%edx
jmp    11c5 <lxc_af_unix_connect+0x65>
mov    $0x4,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1060 <memcpy@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1070 <connect@plt>
test   %rax,%rax
je     11fa <lxc_af_unix_connect+0x9a>
mov    0x2ddd(%rip),%r15
mov    (%r15),%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,(%r15)
jmp    11fd <lxc_af_unix_connect+0x9d>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%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
@AF_UNIX = global i32 0
@errno = global i32 0
define i32 @lxc_af_unix_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
store i8* %0, i8** %3
%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* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %45
13: ; preds = %1
%14 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 8)
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i8*, i8** %3
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %13
%26 = load i8*, i8** %3
%27 = call i32 @strlen(i8* %26)
%28 = sext i32 %27 to i64
br label %30
29: ; preds = %13
br label %30
30: ; preds = %29, %25
%31 = phi i64 [ %28, %25 ], [ 4, %29 ]
%32 = trunc i64 %31 to i32
%33 = call i32 @memcpy(i32 %18, i8* %19, i32 %32)
%34 = load i32, i32* %4
%35 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%36 = call i64 @connect(i32 %34, %struct.sockaddr* %35, i32 8)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %30
%39 = load i32, i32* @errno
store i32 %39, i32* %6
%40 = load i32, i32* %4
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %6
store i32 %42, i32* @errno
store i32 -1, i32* %2
br label %45
43: ; preds = %30
%44 = load i32, i32* %4
store i32 %44, i32* %2
br label %45
45: ; preds = %43, %38, %12
%46 = load i32, i32* %2
ret i32 %46
}

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
@errno = global i32 0
define i32 @lxc_af_unix_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 %27, 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 = load i8, i8* %0
%15 = icmp eq i8 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %8
%17 = call i32 @strlen(i8* %0)
br label %18
18: ; preds = %8, %16
%19 = phi i32 [ %17, %16 ], [ 4, %8 ]
%20 = call i32 @memcpy(i32 %13, i8* %0, i32 %19)
%21 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%22 = call i64 @connect(i32 %6, %struct.sockaddr* %21, i32 8)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %18
%25 = load i32, i32* @errno
%26 = call i32 @close(i32 %6)
store i32 %25, i32* @errno
br label %27
27: ; preds = %18, %1, %24
%28 = phi i32 [ -1, %24 ], [ -1, %1 ], [ %6, %18 ]
ret i32 %28
}