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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char const *, size_t);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
int make_unix_client(const char *path) {
  int fd = -1;
  struct sockaddr_un saun = {.sun_family = AF_UNIX};
  size_t npath = strlen(path);
  if (npath + 1 > sizeof(saun.sun_path)) {
    fprintf(stderr, "parrot: socket path is too long.\n");
    goto error;
  }
  memcpy(saun.sun_path, path, npath + 1);
  fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (fd < 0) {
    perror("parrot: socket");
    goto error;
  }
  if (connect(fd, (struct sockaddr *)&saun, sizeof(saun)) < 0) {
    perror("parrot: connect");
    goto error;
  }
  return fd;
error:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<make_unix_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
cmp    $0x4,%rax
jbe    11cf <make_unix_client+0x5f>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1251 <make_unix_client+0xe1>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1060 <memcpy@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    121a <make_unix_client+0xaa>
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
jmp    1251 <make_unix_client+0xe1>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1246 <make_unix_client+0xd6>
lea    0xdf5(%rip),%rdi
call   1070 <perror@plt>
jmp    1251 <make_unix_client+0xe1>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1260 <make_unix_client+0xf0>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_unix_client>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movl   $0x0,0x8(%rsp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
call   1030 <strlen@plt>
add    $0x1,%rax
cmp    $0x5,%rax
jb     11c4 <make_unix_client+0x54>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    1206 <make_unix_client+0x96>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1060 <memcpy@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     120e <make_unix_client+0x9e>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     1217 <make_unix_client+0xa7>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe0d(%rip),%rdi
jmp    121e <make_unix_client+0xae>
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
jmp    11b6 <make_unix_client+0x46>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"parrot: socket path is too long.\0A\00"
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [15 x i8] c"parrot: socket\00"
@.str.2 = [16 x i8] c"parrot: connect\00"
define i32 @make_unix_client(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca i64
store i8* %0, i8** %3
store i32 -1, i32* %4
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
store i32 0, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
%9 = load i32, i32* @AF_UNIX
store i32 %9, i32* %8
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %6
%12 = load i64, i64* %6
%13 = add i64 %12, 1
%14 = icmp ugt i64 %13, 4
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %41
18: ; preds = %1
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i8*, i8** %3
%22 = load i64, i64* %6
%23 = add i64 %22, 1
%24 = call i32 @memcpy(i32 %20, i8* %21, i64 %23)
%25 = load i32, i32* @PF_UNIX
%26 = load i32, i32* @SOCK_STREAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %41
32: ; preds = %18
%33 = load i32, i32* %4
%34 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%35 = call i64 @connect(i32 %33, %struct.sockaddr* %34, i32 8)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %41
39: ; preds = %32
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %44
41: ; preds = %37, %30, %15
%42 = load i32, i32* %4
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %2
br label %44
44: ; preds = %41, %39
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"parrot: socket path is too long.\0A\00"
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [15 x i8] c"parrot: socket\00"
@.str.2 = [16 x i8] c"parrot: connect\00"
define i32 @make_unix_client(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
store i32 0, i32* %4
%5 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
%6 = load i32, i32* @AF_UNIX
store i32 %6, i32* %5
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = add i64 %7, 1
%9 = icmp ugt i64 %8, 4
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %27
13: ; preds = %1
%14 = tail call i32 @memcpy(i32 0, i8* %0, i64 %8)
%15 = load i32, i32* @PF_UNIX
%16 = load i32, i32* @SOCK_STREAM
%17 = tail call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %27
21: ; preds = %13
%22 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%23 = call i64 @connect(i32 %17, %struct.sockaddr* %22, i32 8)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %19, %10
%28 = phi i32 [ -1, %10 ], [ %17, %19 ], [ %17, %25 ]
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %21, %27
%31 = phi i32 [ -1, %27 ], [ %17, %21 ]
ret i32 %31
}