#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*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, scalar_t__);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(scalar_t__, char const *, scalar_t__);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ wth_debug(char *, char const *, ...);
int connect_to_unix_socket(const char *path) {
  int fd;
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
  struct sockaddr_un addr;
  if (fd < 0) {
    wth_debug("Failed to open socket: %s", strerror(errno));
    return fd;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  memcpy(addr.sun_path + 1, path, strlen(path) + 1);
  if (connect(fd, (struct sockaddr *)&addr,
              sizeof(addr.sun_family) + 1 + strlen(path)) < 0) {
    wth_debug("Failed to connect to %s: %s", path, strerror(errno));
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<connect_to_unix_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11dc <connect_to_unix_socket+0x5c>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1070 <wth_debug@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a1 <connect_to_unix_socket+0x121>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1060 <memcpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
add    $0x5,%rdx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    129b <connect_to_unix_socket+0x11b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1070 <wth_debug@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <connect_to_unix_socket+0x121>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_unix_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e54(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1234 <connect_to_unix_socket+0xb4>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rbx
add    $0x1,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <memcpy@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x5(%rax),%rdx
mov    %ebp,%edi
mov    %r15,%rsi
call   1080 <connect@plt>
test   %rax,%rax
jns    1253 <connect_to_unix_socket+0xd3>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe00(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <wth_debug@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
jmp    1253 <connect_to_unix_socket+0xd3>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
movslq %eax,%rsi
lea    0xdb4(%rip),%rdi
xor    %eax,%eax
call   1070 <wth_debug@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i64, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [26 x i8] c"Failed to open socket: %s\00"
@errno = global i32 0
@.str.1 = [28 x i8] c"Failed to connect to %s: %s\00"
define i32 @connect_to_unix_socket(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* @AF_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 %18
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = call i32 @strerror(i32 %12)
%14 = sext i32 %13 to i64
%15 = inttoptr i64 %14 to i8*
%16 = call i32 (i8*, i8*, ...) @wth_debug(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* %4
store i32 %17, i32* %2
br label %46
18: ; preds = %1
%19 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 16)
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%23 = load i64, i64* %22
%24 = add nsw i64 %23, 1
%25 = load i8*, i8** %3
%26 = load i8*, i8** %3
%27 = call i64 @strlen(i8* %26)
%28 = add nsw i64 %27, 1
%29 = call i32 @memcpy(i64 %24, i8* %25, i64 %28)
%30 = load i32, i32* %4
%31 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%32 = load i8*, i8** %3
%33 = call i64 @strlen(i8* %32)
%34 = add i64 5, %33
%35 = call i64 @connect(i32 %30, %struct.sockaddr* %31, i64 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %18
%38 = load i8*, i8** %3
%39 = load i32, i32* @errno
%40 = call i32 @strerror(i32 %39)
%41 = call i32 (i8*, i8*, ...) @wth_debug(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %38, i32 %40)
%42 = load i32, i32* %4
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %2
br label %46
44: ; preds = %18
%45 = load i32, i32* %4
store i32 %45, i32* %2
br label %46
46: ; preds = %44, %37, %11
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i64, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [26 x i8] c"Failed to open socket: %s\00"
@errno = global i32 0
@.str.1 = [28 x i8] c"Failed to connect to %s: %s\00"
define i32 @connect_to_unix_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = bitcast %struct.sockaddr_un* %2 to i8*
%7 = icmp slt i32 %5, 0
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 (i8*, i8*, ...) @wth_debug(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %34
14: ; preds = %1
%15 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 16)
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%19 = load i64, i64* %18
%20 = add nsw i64 %19, 1
%21 = call i64 @strlen(i8* dereferenceable(1) %0)
%22 = add nsw i64 %21, 1
%23 = call i32 @memcpy(i64 %20, i8* %0, i64 %22)
%24 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%25 = call i64 @strlen(i8* dereferenceable(1) %0)
%26 = add i64 %25, 5
%27 = call i64 @connect(i32 %5, %struct.sockaddr* %24, i64 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %14
%30 = load i32, i32* @errno
%31 = call i32 @strerror(i32 %30)
%32 = call i32 (i8*, i8*, ...) @wth_debug(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %31)
%33 = call i32 @close(i32 %5)
br label %34
34: ; preds = %14, %29, %8
%35 = phi i32 [ %5, %8 ], [ -1, %29 ], [ %5, %14 ]
ret i32 %35
}