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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ debug(int, char *, char *);
int /*<<< orphan*/ errno;
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(scalar_t__ *, char *, int);
int socket_connect(char *socket_name) {
  struct sockaddr_un sock_addr;
  int daemon_socket;
  daemon_socket = socket(AF_UNIX, SOCK_STREAM, 0);
  if (daemon_socket == -1) {
    debug(1, "Could not create socket: %s", strerror(errno));
    return -1;
  }
  sock_addr.sun_family = AF_UNIX;
  strncpy(sock_addr.sun_path, socket_name, sizeof(sock_addr.sun_path) - 1);
  sock_addr.sun_path[sizeof(sock_addr.sun_path) - 1] = 0;
  if (connect(daemon_socket, (struct sockaddr *)&sock_addr,
              sizeof(sock_addr)) != 0) {
    debug(1, "Could not connect to socket '%s'.", socket_name);
    return -1;
  }
  return (daemon_socket);
}


x86 assembly (clang, -O0):
<socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11b0 <socket_connect+0x60>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rdx
mov    $0x1,%edi
lea    0xe61(%rip),%rsi
call   1040 <debug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <socket_connect+0xcc>
mov    0x2e31(%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    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
je     1216 <socket_connect+0xc6>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe17(%rip),%rsi
call   1040 <debug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <socket_connect+0xcc>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_connect>:
push   %rbx
mov    %rdi,%rbx
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
jne    118b <socket_connect+0x4b>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe89(%rip),%rsi
mov    $0x1,%edi
mov    %rax,%rdx
call   1040 <debug@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret
mov    %rbx,%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i64*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [28 x i8] c"Could not create socket: %s\00"
@errno = global i32 0
@.str.1 = [34 x i8] c"Could not connect to socket '%s'.\00"
define i32 @socket_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
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* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @debug(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %2
br label %34
15: ; preds = %1
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%19 = load i64*, i64** %18
%20 = load i8*, i8** %3
%21 = call i32 @strncpy(i64* %19, i8* %20, i32 7)
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%23 = load i64*, i64** %22
%24 = getelementptr inbounds i64, i64* %23, i64 7
store i64 0, i64* %24
%25 = load i32, i32* %5
%26 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%27 = call i64 @connect(i32 %25, %struct.sockaddr* %26, i32 16)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %15
%30 = load i8*, i8** %3
%31 = call i32 @debug(i32 1, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %30)
store i32 -1, i32* %2
br label %34
32: ; preds = %15
%33 = load i32, i32* %5
store i32 %33, i32* %2
br label %34
34: ; preds = %32, %29, %11
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [28 x i8] c"Could not create socket: %s\00"
@errno = global i32 0
define i32 @socket_connect(i8* %0) {
%2 = load i32, i32* @AF_UNIX
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @debug(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %8)
ret i32 -1
10: ; preds = %1
%11 = tail call i32 @strncpy(i64* undef, i8* %0, i32 7)
unreachable
}