#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 /*<<< 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))) {
    close(fd);
    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    1241 <lxc_af_unix_connect+0xe1>
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,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%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,-0x38(%rbp)
jmp    11fa <lxc_af_unix_connect+0x9a>
mov    $0x4,%eax
mov    %rax,-0x38(%rbp)
jmp    11fa <lxc_af_unix_connect+0x9a>
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edi
mov    -0x38(%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     123b <lxc_af_unix_connect+0xdb>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <lxc_af_unix_connect+0xe1>
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     11f0 <lxc_af_unix_connect+0x90>
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     11ed <lxc_af_unix_connect+0x8d>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11f0 <lxc_af_unix_connect+0x90>
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
define i32 @lxc_af_unix_connect(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* @PF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %42
12: ; preds = %1
%13 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 8)
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %12
%25 = load i8*, i8** %3
%26 = call i32 @strlen(i8* %25)
%27 = sext i32 %26 to i64
br label %29
28: ; preds = %12
br label %29
29: ; preds = %28, %24
%30 = phi i64 [ %27, %24 ], [ 4, %28 ]
%31 = trunc i64 %30 to i32
%32 = call i32 @memcpy(i32 %17, i8* %18, i32 %31)
%33 = load i32, i32* %4
%34 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%35 = call i64 @connect(i32 %33, %struct.sockaddr* %34, i32 8)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %29
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %2
br label %42
40: ; preds = %29
%41 = load i32, i32* %4
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %37, %11
%43 = load i32, i32* %2
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
@AF_UNIX = 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 %26, 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 %26, label %24
24: ; preds = %18
%25 = call i32 @close(i32 %6)
br label %26
26: ; preds = %18, %1, %24
%27 = phi i32 [ -1, %24 ], [ -1, %1 ], [ %6, %18 ]
ret i32 %27
}