#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;
  void *sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serveraddr;
typedef int /*<<< orphan*/ cliaddr;

/* Variables and functions */
void *AF_UNIX;
int /*<<< orphan*/ ENAMETOOLONG;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ S_IRWXU;
scalar_t__ bind(int, struct sockaddr *, size_t);
scalar_t__ chmod(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, size_t);
int /*<<< orphan*/ errno;
scalar_t__ getpid();
int snprintf(int /*<<< orphan*/, int, char *, char *, ...);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int connect_to(const char *name) {
  int written;
  int sockfd;
  size_t cliaddr_sz;
  size_t servaddr_sz;
  struct sockaddr_un cliaddr;
  struct sockaddr_un serveraddr;
  cliaddr.sun_family = AF_UNIX;
  written = snprintf(cliaddr.sun_path, sizeof(cliaddr.sun_path) - 1, "%s/%ld",
                     "/tmp", (long)getpid());
  if (written >= sizeof(cliaddr.sun_path) - 1) {
    errno = ENAMETOOLONG;
    return -1;
  }
  cliaddr_sz = sizeof(cliaddr) - sizeof(cliaddr.sun_path) + written;
  serveraddr.sun_family = AF_UNIX;
  written = snprintf(serveraddr.sun_path, sizeof(serveraddr.sun_path), "%s/%s",
                     "/tmp", name);
  if (written > sizeof(serveraddr.sun_path)) {
    errno = ENAMETOOLONG;
    return -1;
  }
  servaddr_sz = sizeof(serveraddr) - sizeof(serveraddr.sun_path) + written;
  if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  unlink(cliaddr.sun_path);
  if (bind(sockfd, (struct sockaddr *)&cliaddr, cliaddr_sz) < 0) {
    goto errout;
  }
  if (chmod(cliaddr.sun_path, S_IRWXU) < 0) {
    unlink(cliaddr.sun_path);
    goto errout;
  }
  if (connect(sockfd, (struct sockaddr *)&serveraddr, servaddr_sz) < 0) {
    goto errout;
  }
  return sockfd;
errout:
  close(sockfd);
  return -1;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%r8
mov    $0x3,%esi
lea    0xe47(%rip),%rdx
lea    0xe47(%rip),%rcx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x3,%rax
jb     11f6 <connect_to+0x76>
mov    0x2de1(%rip),%rax
mov    (%rax),%ecx
mov    0x2de0(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <connect_to+0x1a6>
movslq -0x14(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x20(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%edi
mov    -0x10(%rbp),%r8
mov    $0x4,%esi
lea    0xde9(%rip),%rdx
lea    0xddd(%rip),%rcx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
jbe    1260 <connect_to+0xe0>
mov    0x2d77(%rip),%rax
mov    (%rax),%ecx
mov    0x2d76(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <connect_to+0x1a6>
movslq -0x14(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x28(%rbp)
mov    0x2d6d(%rip),%rax
mov    (%rax),%rdi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    129e <connect_to+0x11e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <connect_to+0x1a6>
mov    -0x38(%rbp),%edi
call   1030 <unlink@plt>
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12c5 <connect_to+0x145>
jmp    1317 <connect_to+0x197>
mov    -0x38(%rbp),%edi
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
call   1070 <chmod@plt>
cmp    $0x0,%rax
jge    12ed <connect_to+0x16d>
mov    -0x38(%rbp),%edi
call   1030 <unlink@plt>
jmp    1317 <connect_to+0x197>
mov    -0x18(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    130c <connect_to+0x18c>
jmp    1317 <connect_to+0x197>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1326 <connect_to+0x1a6>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
mov    0x2e4c(%rip),%r14
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe56(%rip),%rdx
lea    0xe56(%rip),%rcx
mov    $0x3,%esi
mov    %rax,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
cmp    $0x3,%eax
jae    11f1 <connect_to+0x71>
mov    %eax,%ebp
mov    (%r14),%rax
mov    %rax,0x20(%rsp)
lea    0xe36(%rip),%rdx
lea    0xe2a(%rip),%rcx
mov    $0x4,%esi
mov    %rbx,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
cmp    $0x5,%eax
jb     1215 <connect_to+0x95>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    0x2dc7(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r15d
mov    (%r14),%rdi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1208 <connect_to+0x88>
mov    %eax,%r14d
mov    %ebp,%ebp
add    $0xc,%rbp
call   1030 <unlink@plt>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    %rbp,%rdx
call   1080 <bind@plt>
test   %rax,%rax
js     1298 <connect_to+0x118>
mov    0x8(%rsp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
call   1070 <chmod@plt>
test   %rax,%rax
js     128f <connect_to+0x10f>
mov    %r15d,%edx
add    $0xc,%rdx
lea    0x18(%rsp),%rsi
mov    %r14d,%edi
call   1090 <connect@plt>
test   %rax,%rax
js     1298 <connect_to+0x118>
mov    %r14d,%ebx
jmp    1208 <connect_to+0x88>
mov    0x8(%rsp),%edi
call   1030 <unlink@plt>
mov    %r14d,%edi
call   1060 <close@plt>
jmp    1208 <connect_to+0x88>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i8* }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i8* null
@.str = [7 x i8] c"%s/%ld\00"
@.str.1 = [5 x i8] c"/tmp\00"
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
@SOCK_STREAM = global i32 0
@S_IRWXU = global i32 0
define i32 @connect_to(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca %struct.sockaddr_un
%9 = alloca %struct.sockaddr_un
store i8* %0, i8** %3
%10 = load i8*, i8** @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i8* %10, i8** %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i64 (...) @getpid()
%15 = call i32 (i32, i32, i8*, i8*, ...) @snprintf(i32 %13, i32 3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = icmp uge i64 %17, 3
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = load i32, i32* @ENAMETOOLONG
store i32 %20, i32* @errno
store i32 -1, i32* %2
br label %77
21: ; preds = %1
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = add i64 12, %23
store i64 %24, i64* %6
%25 = load i8*, i8** @AF_UNIX
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i8* %25, i8** %26
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load i8*, i8** %3
%30 = call i32 (i32, i32, i8*, i8*, ...) @snprintf(i32 %28, i32 4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = icmp ugt i64 %32, 4
br i1 %33, label %34, label %36
34: ; preds = %21
%35 = load i32, i32* @ENAMETOOLONG
store i32 %35, i32* @errno
store i32 -1, i32* %2
br label %77
36: ; preds = %21
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = add i64 12, %38
store i64 %39, i64* %7
%40 = load i8*, i8** @AF_UNIX
%41 = load i32, i32* @SOCK_STREAM
%42 = call i32 @socket(i8* %40, i32 %41, i32 0)
store i32 %42, i32* %5
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %36
store i32 -1, i32* %2
br label %77
45: ; preds = %36
%46 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @unlink(i32 %47)
%49 = load i32, i32* %5
%50 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%51 = load i64, i64* %6
%52 = call i64 @bind(i32 %49, %struct.sockaddr* %50, i64 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %45
br label %74
55: ; preds = %45
%56 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%57 = load i32, i32* %56
%58 = load i32, i32* @S_IRWXU
%59 = call i64 @chmod(i32 %57, i32 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %55
%62 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i32 @unlink(i32 %63)
br label %74
65: ; preds = %55
%66 = load i32, i32* %5
%67 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%68 = load i64, i64* %7
%69 = call i64 @connect(i32 %66, %struct.sockaddr* %67, i64 %68)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %65
br label %74
72: ; preds = %65
%73 = load i32, i32* %5
store i32 %73, i32* %2
br label %77
74: ; preds = %71, %61, %54
%75 = load i32, i32* %5
%76 = call i32 @close(i32 %75)
store i32 -1, i32* %2
br label %77
77: ; preds = %74, %72, %44, %34, %19
%78 = load i32, i32* %2
ret i32 %78
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i8* }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i8* null
@.str = [7 x i8] c"%s/%ld\00"
@.str.1 = [5 x i8] c"/tmp\00"
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
@SOCK_STREAM = global i32 0
@S_IRWXU = global i32 0
define i32 @connect_to(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %2 to i8*
%5 = bitcast %struct.sockaddr_un* %3 to i8*
%6 = load i8*, i8** @AF_UNIX
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i8* %6, i8** %7
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%9 = tail call i64 (...) @getpid()
%10 = tail call i32 (i32, i32, i8*, i8*, ...) @snprintf(i32 undef, i32 3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %9)
%11 = icmp ugt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = load i32, i32* @ENAMETOOLONG
store i32 %13, i32* @errno
br label %49
14: ; preds = %1
%15 = zext i32 %10 to i64
%16 = add nuw nsw i64 %15, 12
%17 = load i8*, i8** @AF_UNIX
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i8* %17, i8** %18
%19 = tail call i32 (i32, i32, i8*, i8*, ...) @snprintf(i32 undef, i32 4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%20 = icmp ugt i32 %19, 4
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = load i32, i32* @ENAMETOOLONG
store i32 %22, i32* @errno
br label %49
23: ; preds = %14
%24 = zext i32 %19 to i64
%25 = add nuw nsw i64 %24, 12
%26 = load i8*, i8** @AF_UNIX
%27 = load i32, i32* @SOCK_STREAM
%28 = tail call i32 @socket(i8* %26, i32 %27, i32 0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %49, label %30
30: ; preds = %23
%31 = tail call i32 @unlink(i32 undef)
%32 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%33 = call i64 @bind(i32 %28, %struct.sockaddr* %32, i64 %16)
%34 = icmp slt i64 %33, 0
br i1 %34, label %47, label %35
35: ; preds = %30
%36 = load i32, i32* %8
%37 = load i32, i32* @S_IRWXU
%38 = call i64 @chmod(i32 %36, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* %8
%42 = call i32 @unlink(i32 %41)
br label %47
43: ; preds = %35
%44 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%45 = call i64 @connect(i32 %28, %struct.sockaddr* %44, i64 %25)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43, %30, %40
%48 = call i32 @close(i32 %28)
br label %49
49: ; preds = %43, %23, %47, %21, %12
%50 = phi i32 [ -1, %12 ], [ -1, %21 ], [ -1, %47 ], [ -1, %23 ], [ %28, %43 ]
ret i32 %50
}