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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ errorf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int strlen(char *);
int setup_back_unix_socket(char *sockname, int f[2]) {
  int fd;
  struct sockaddr_un sa;
  if ((strlen(sockname) + 1) > sizeof(sa.sun_path))
    errorf("Path name is too long for a Unix socket.\n");
  sa.sun_family = AF_UNIX;
  strcpy(sa.sun_path, sockname);
  if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 3)
    errorf("Couldn't open socket: %s\n", strerror(errno));
  if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != 0)
    errorf("Couldn't connect socket: %s\n", strerror(errno));
  return f[0] = f[1] = fd;
}


x86 assembly (clang, -O0):
<setup_back_unix_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    $0x4,%rax
jbe    1196 <setup_back_unix_socket+0x36>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1050 <errorf@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x3,%eax
jge    11f1 <setup_back_unix_socket+0x91>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1050 <errorf@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
je     122a <setup_back_unix_socket+0xca>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1050 <errorf@plt>
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    %eax,0x4(%rcx)
mov    -0x10(%rbp),%rcx
mov    %eax,(%rcx)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_back_unix_socket>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rbp
call   1040 <strlen@plt>
add    $0xfffffffc,%eax
cmp    $0xfffffffa,%eax
ja     1184 <setup_back_unix_socket+0x24>
lea    0xe83(%rip),%rdi
xor    %eax,%eax
call   1050 <errorf@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
cmp    $0x2,%eax
jg     11d7 <setup_back_unix_socket+0x77>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe5c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <errorf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1060 <connect@plt>
test   %rax,%rax
je     1209 <setup_back_unix_socket+0xa9>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe44(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <errorf@plt>
mov    %ebp,0x4(%rbx)
mov    %ebp,(%rbx)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [42 x i8] c"Path name is too long for a Unix socket.\0A\00"
@AF_UNIX = global i32 0
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [26 x i8] c"Couldn't open socket: %s\0A\00"
@errno = global i32 0
@.str.2 = [29 x i8] c"Couldn't connect socket: %s\0A\00"
define i32 @setup_back_unix_socket(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
store i8* %0, i8** %3
store i32* %1, i32** %4
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, 1
%10 = sext i32 %9 to i64
%11 = icmp ugt i64 %10, 4
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i8*, i8** %3
%20 = call i32 @strcpy(i32 %18, i8* %19)
%21 = load i32, i32* @PF_UNIX
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %5
%24 = icmp slt i32 %23, 3
br i1 %24, label %25, label %29
25: ; preds = %14
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %25, %14
%30 = load i32, i32* %5
%31 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%32 = call i64 @connect(i32 %30, %struct.sockaddr* %31, i32 8)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i32, i32* @errno
%36 = call i32 @strerror(i32 %35)
%37 = call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %34, %29
%39 = load i32, i32* %5
%40 = load i32*, i32** %4
%41 = getelementptr inbounds i32, i32* %40, i64 1
store i32 %39, i32* %41
%42 = load i32*, i32** %4
%43 = getelementptr inbounds i32, i32* %42, i64 0
store i32 %39, i32* %43
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [42 x i8] c"Path name is too long for a Unix socket.\0A\00"
@AF_UNIX = global i32 0
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [26 x i8] c"Couldn't open socket: %s\0A\00"
@errno = global i32 0
@.str.2 = [29 x i8] c"Couldn't connect socket: %s\0A\00"
define i32 @setup_back_unix_socket(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = tail call i32 @strlen(i8* %0)
%6 = add i32 %5, -4
%7 = icmp ult i32 %6, -5
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %11, i32* %12
%13 = tail call i32 @strcpy(i32 undef, i8* %0)
%14 = load i32, i32* @PF_UNIX
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 3
br i1 %17, label %18, label %22
18: ; preds = %10
%19 = load i32, i32* @errno
%20 = tail call i32 @strerror(i32 %19)
%21 = tail call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %18, %10
%23 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%24 = call i64 @connect(i32 %16, %struct.sockaddr* %23, i32 8)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = load i32, i32* @errno
%28 = call i32 @strerror(i32 %27)
%29 = call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %26, %22
%31 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %16, i32* %31
store i32 %16, i32* %1
ret i32 %16
}