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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int connect_unix_socket(char *path, int abstract, int length) {
  struct sockaddr_un addr;
  int sock;
  addr.sun_family = AF_UNIX;
  if (abstract) {
    addr.sun_path[0] = '\0';
    strcpy(addr.sun_path + 1, path);
  } else {
    strcpy(addr.sun_path, path);
  }
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock == -1) {
    printf("couldn't create a unix socket\n");
    return -1;
  }
  if (connect(sock, (const struct sockaddr *)&addr,
              length + (abstract ? 1 : 0) + 2)) {
    perror("connect failure\n");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<connect_unix_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1195 <connect_unix_socket+0x45>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    11a2 <connect_unix_socket+0x52>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11e0 <connect_unix_socket+0x90>
lea    0xe31(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <connect_unix_socket+0xde>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%r8d
cmovne %ecx,%eax
add    %eax,%edx
add    $0x2,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
je     1228 <connect_unix_socket+0xd8>
lea    0xe08(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <connect_unix_socket+0xde>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_unix_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %esi,%r14d
mov    0x2e84(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    %rdi,%rsi
call   1030 <strcpy@plt>
mov    0x0(%rbp),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11b6 <connect_unix_socket+0x66>
mov    %eax,%ebp
cmp    $0x1,%r14d
sbb    $0xffffffff,%ebx
add    $0x2,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    %ebx,%edx
call   1060 <connect@plt>
test   %rax,%rax
je     11c7 <connect_unix_socket+0x77>
lea    0xe51(%rip),%rdi
call   1050 <perror@plt>
jmp    11c2 <connect_unix_socket+0x72>
lea    0xe54(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [31 x i8] c"couldn't create a unix socket\0A\00"
@.str.1 = [17 x i8] c"connect failure\0A\00"
define i32 @connect_unix_socket(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_un
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %3
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%16 = load i8*, i8** %15
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 0, i8* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8*, i8** %5
%22 = call i32 @strcpy(i8* %20, i8* %21)
br label %28
23: ; preds = %3
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = load i8*, i8** %5
%27 = call i32 @strcpy(i8* %25, i8* %26)
br label %28
28: ; preds = %23, %14
%29 = load i32, i32* @AF_UNIX
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %52
36: ; preds = %28
%37 = load i32, i32* %9
%38 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%39 = load i32, i32* %7
%40 = load i32, i32* %6
%41 = icmp ne i32 %40, 0
%42 = zext i1 %41 to i64
%43 = select i1 %41, i32 1, i32 0
%44 = add nsw i32 %39, %43
%45 = add nsw i32 %44, 2
%46 = call i64 @connect(i32 %37, %struct.sockaddr* %38, i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %36
%49 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %52
50: ; preds = %36
%51 = load i32, i32* %9
store i32 %51, i32* %4
br label %52
52: ; preds = %50, %48, %34
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"connect failure\0A\00"
@str = [30 x i8] c"couldn't create a unix socket\00"
define i32 @connect_unix_socket(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_un
%5 = bitcast %struct.sockaddr_un* %4 to i8*
%6 = load i32, i32* @AF_UNIX
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %6, i32* %7
%8 = tail call i32 @strcpy(i8* undef, i8* %0)
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %25
15: ; preds = %3
%16 = icmp ne i32 %1, 0
%17 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%18 = zext i1 %16 to i32
%19 = add i32 %2, 2
%20 = add i32 %19, %18
%21 = call i64 @connect(i32 %11, %struct.sockaddr* %17, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %15, %23, %13
%26 = phi i32 [ -1, %13 ], [ -1, %23 ], [ %11, %15 ]
ret i32 %26
}