#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 */
typedef int /*<<< orphan*/ sunaddr;
struct sockaddr_un {
  int /*<<< orphan*/ sun_family;
  int /*<<< orphan*/ sun_path;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *const, ...);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int snprintf(int /*<<< orphan*/, int, char *, char const *const);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void ConnectToUnixSocket(const char *const name, int *const psock) {
  struct sockaddr_un sunaddr;
  int sock = -1;
  int rval;
  memset(&sunaddr, 0, sizeof(sunaddr));
  rval = snprintf(sunaddr.sun_path, sizeof(sunaddr.sun_path), "%s", name);
  if (rval < 0 || (size_t)rval >= sizeof(sunaddr.sun_path)) {
    fprintf(stderr, "Socket name '%s' is too long\n", name);
    exit(-1);
  }
  sunaddr.sun_family = AF_UNIX;
  if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    fprintf(stderr, "Error opening socket: %s\n", strerror(errno));
    exit(-1);
  }
  if (connect(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
    fprintf(stderr, "Unable to connect to UNIX socket at %s: %s\n", name,
            strerror(errno));
    close(sock);
    exit(-1);
  }
  *psock = sock;
}


x86 assembly (clang, -O0):
<ConnectToUnixSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rcx
mov    $0x4,%esi
lea    0xe46(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     11da <ConnectToUnixSocket+0x5a>
movslq -0x20(%rbp),%rax
cmp    $0x4,%rax
jb     11ff <ConnectToUnixSocket+0x7f>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1268 <ConnectToUnixSocket+0xe8>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    12ce <ConnectToUnixSocket+0x14e>
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xd86(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ConnectToUnixSocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0xc(%rsp),%edi
lea    0xe56(%rip),%rdx
mov    $0x4,%esi
mov    %r14,%rcx
call   1030 <snprintf@plt>
cmp    $0x4,%eax
jae    1201 <ConnectToUnixSocket+0x81>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x8(%rsp)
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1216 <ConnectToUnixSocket+0x96>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     124a <ConnectToUnixSocket+0xca>
mov    %ebp,(%rbx)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
mov    %r14,%rdx
jmp    1239 <ConnectToUnixSocket+0xb9>
mov    0x2dbb(%rip),%rax
mov    (%rax),%ebx
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xded(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd3(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Socket name '%s' is too long\0A\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [26 x i8] c"Error opening socket: %s\0A\00"
@errno = global i32 0
@.str.3 = [44 x i8] c"Unable to connect to UNIX socket at %s: %s\0A\00"
define void @ConnectToUnixSocket(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 -1, i32* %6
%8 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 8)
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
%10 = load i32, i32* %9
%11 = load i8*, i8** %3
%12 = call i32 @snprintf(i32 %10, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %2
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = icmp uge i64 %17, 4
br i1 %18, label %19, label %24
19: ; preds = %15, %2
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %3
%22 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %15
%25 = load i32, i32* @AF_UNIX
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* @AF_UNIX
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %37
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %24
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%40 = call i32 @connect(i32 %38, %struct.sockaddr* %39, i32 8)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %51
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %3
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %44, i8* %46)
%48 = load i32, i32* %6
%49 = call i32 @close(i32 %48)
%50 = call i32 @exit(i32 -1)
unreachable
51: ; preds = %37
%52 = load i32, i32* %6
%53 = load i32*, i32** %4
store i32 %52, i32* %53
ret void
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Socket name '%s' is too long\0A\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [26 x i8] c"Error opening socket: %s\0A\00"
@errno = global i32 0
@.str.3 = [44 x i8] c"Unable to connect to UNIX socket at %s: %s\0A\00"
define void @ConnectToUnixSocket(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
%7 = load i32, i32* %6
%8 = call i32 @snprintf(i32 %7, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = icmp ugt i32 %8, 3
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %15, i32 %17, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %14
%27 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%28 = call i32 @connect(i32 %18, %struct.sockaddr* %27, i32 8)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %37
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %33)
%35 = call i32 @close(i32 %18)
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %26
store i32 %18, i32* %1
ret void
}