#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(scalar_t__ *, char const *, int);
void *iob_pipe_open(const char *path) {
  int sock;
  struct sockaddr_un sa;
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock == -1) {
    perror("socket");
    return 0;
  }
  memset(&sa, 0, sizeof(struct sockaddr_un));
  sa.sun_family = AF_UNIX;
  strncpy(sa.sun_path, path, sizeof(sa.sun_path));
  sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
  if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr_un)) == -1) {
    perror("connect");
    close(sock);
    return 0;
  }
  return (void *)(size_t)sock;
}


x86 assembly (clang, -O0):
<iob_pipe_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ab <iob_pipe_open+0x4b>
lea    0xe67(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1228 <iob_pipe_open+0xc8>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
movq   $0x0,0x38(%rax)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1220 <iob_pipe_open+0xc0>
lea    0xe01(%rip),%rdi
call   1060 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1228 <iob_pipe_open+0xc8>
movslq -0x14(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iob_pipe_open>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e73(%rip),%r12
mov    (%r12),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11ee <iob_pipe_open+0x8e>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    0x8(%rsp),%rax
movq   $0x0,0x38(%rax)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     11fe <iob_pipe_open+0x9e>
movslq %ebx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe0b(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
jmp    11e2 <iob_pipe_open+0x82>
lea    0xe02(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    11e2 <iob_pipe_open+0x82>

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i64*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [8 x i8] c"connect\00"
define i8* @iob_pipe_open(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %28
10: ; preds = %1
%11 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 16)
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%15 = load i64*, i64** %14
%16 = call i32 @strncpy(i64* %15, i8* %0, i32 8)
%17 = load i64*, i64** %14
%18 = getelementptr inbounds i64, i64* %17, i64 7
store i64 0, i64* %18
%19 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%20 = call i32 @connect(i32 %6, %struct.sockaddr* %19, i32 16)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %10
%23 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @close(i32 %6)
br label %28
25: ; preds = %10
%26 = sext i32 %6 to i64
%27 = inttoptr i64 %26 to i8*
br label %28
28: ; preds = %25, %22, %8
%29 = phi i8* [ null, %8 ], [ null, %22 ], [ %27, %25 ]
ret i8* %29
}