#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 {
  scalar_t__ *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;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ m_close(int);
int /*<<< orphan*/ memset(void *, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(scalar_t__ *, char const *, int);
int connect_unix(const char *path) {
  struct sockaddr_un addr;
  int fd = -1;
  memset((void *)&addr, 0x0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
  addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
  fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (fd < 0)
    return -1;
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    m_close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<connect_unix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x24(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,0x38(%rax)
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11cf <connect_unix+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <connect_unix+0xb4>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    11fe <connect_unix+0xae>
mov    -0x24(%rbp),%edi
call   1040 <m_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <connect_unix+0xb4>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_unix>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    0x8(%rsp),%rax
movq   $0x0,0x38(%rax)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11db <connect_unix+0x8b>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1060 <connect@plt>
test   %rax,%rax
js     11d4 <connect_unix+0x84>
mov    %ebp,%ebx
jmp    11db <connect_unix+0x8b>
mov    %ebp,%edi
call   1040 <m_close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i64*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @connect_unix(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = call i32 @memset(i8* %3, i32 0, i32 16)
%5 = load i32, i32* @AF_UNIX
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%8 = load i64*, i64** %7
%9 = call i32 @strncpy(i64* %8, i8* %0, i32 7)
%10 = load i64*, i64** %7
%11 = getelementptr inbounds i64, i64* %10, i64 7
store i64 0, i64* %11
%12 = load i32, i32* @PF_UNIX
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %1
%17 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%18 = call i64 @connect(i32 %14, %struct.sockaddr* %17, i32 16)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @m_close(i32 %14)
br label %22
22: ; preds = %16, %1, %20
%23 = phi i32 [ -1, %20 ], [ -1, %1 ], [ %14, %16 ]
ret i32 %23
}