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

/* 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*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int open_named_pipe(const char *name) {
  int sfd, len, ret;
  struct sockaddr_un remote;
  sfd = socket(AF_UNIX, SOCK_STREAM, 0);
  memset(&remote, 0, sizeof(remote));
  remote.sun_family = AF_UNIX;
  strncpy((char *)&remote.sun_path, name, sizeof(remote.sun_path) - 1);
  len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  ret = connect(sfd, (struct sockaddr *)&remote, len);
  if (ret < 0) {
    fprintf(stderr, "Unable to open named pipe '%s' (%s)\n", name,
            strerror(errno));
    close(sfd);
    sfd = -1;
  }
  return sfd;
}


x86 assembly (clang, -O0):
<open_named_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%edi
call   1040 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1080 <connect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124a <open_named_pipe+0xca>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xdca(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_named_pipe>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e57(%rip),%r15
mov    (%r15),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    (%rsp),%edi
call   1040 <strlen@plt>
lea    0x4(%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1080 <connect@plt>
test   %eax,%eax
jns    121f <open_named_pipe+0x9f>
mov    0x2de9(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdfa(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [37 x i8] c"Unable to open named pipe '%s' (%s)\0A\00"
@errno = global i32 0
define i32 @open_named_pipe(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
store i8* %0, i8** %2
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %3
%10 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%14 = bitcast i32* %13 to i8*
%15 = load i8*, i8** %2
%16 = call i32 @strncpy(i8* %14, i8* %15, i32 3)
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @strlen(i32 %18)
%20 = sext i32 %19 to i64
%21 = add i64 %20, 4
%22 = trunc i64 %21 to i32
store i32 %22, i32* %4
%23 = load i32, i32* %3
%24 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%25 = load i32, i32* %4
%26 = call i32 @connect(i32 %23, %struct.sockaddr* %24, i32 %25)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %1
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %2
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %33)
%35 = load i32, i32* %3
%36 = call i32 @close(i32 %35)
store i32 -1, i32* %3
br label %37
37: ; preds = %29, %1
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [37 x i8] c"Unable to open named pipe '%s' (%s)\0A\00"
@errno = global i32 0
define i32 @open_named_pipe(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 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%8 = load i32, i32* @AF_UNIX
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%11 = call i32 @strncpy(i8* %3, i8* %0, i32 3)
%12 = load i32, i32* %10
%13 = call i32 @strlen(i32 %12)
%14 = add i32 %13, 4
%15 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%16 = call i32 @connect(i32 %6, %struct.sockaddr* %15, i32 %14)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %21)
%23 = call i32 @close(i32 %6)
br label %24
24: ; preds = %18, %1
%25 = phi i32 [ -1, %18 ], [ %6, %1 ]
ret i32 %25
}