#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*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ listen(int, int /*<<< orphan*/);
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_server(const char *path) {
  int fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd < 0) {
    perror("parrot: socket");
    goto error;
  }
  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);
  if (bind(fd, (struct sockaddr *)&saun, sizeof(saun)) < 0) {
    perror("parrot: bind");
    goto error;
  }
  if (listen(fd, SOMAXCONN) < 0) {
    perror("parrot: listen");
    goto error;
  }
  return fd;
error:
  close(fd);
  return fd;
}


x86 assembly (clang, -O0):
<make_unix_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c3 <make_unix_server+0x43>
lea    0xe47(%rip),%rdi
call   1090 <perror@plt>
jmp    1286 <make_unix_server+0x106>
movl   $0x0,-0x20(%rbp)
mov    0x2e17(%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    120f <make_unix_server+0x8f>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1286 <make_unix_server+0x106>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1060 <memcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    124f <make_unix_server+0xcf>
lea    0xdec(%rip),%rdi
call   1090 <perror@plt>
jmp    1286 <make_unix_server+0x106>
mov    -0x14(%rbp),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    127b <make_unix_server+0xfb>
lea    0xdcd(%rip),%rdi
call   1090 <perror@plt>
jmp    1286 <make_unix_server+0x106>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1294 <make_unix_server+0x114>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_unix_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e56(%rip),%r14
mov    (%r14),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1231 <make_unix_server+0xb1>
movl   $0x0,0x8(%rsp)
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    $0x1,%rax
cmp    $0x5,%rax
jb     11ee <make_unix_server+0x6e>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1226 <make_unix_server+0xa6>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1060 <memcpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     123a <make_unix_server+0xba>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1070 <listen@plt>
test   %rax,%rax
js     1243 <make_unix_server+0xc3>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc8(%rip),%rdi
jmp    124a <make_unix_server+0xca>
lea    0xdf0(%rip),%rdi
jmp    124a <make_unix_server+0xca>
lea    0xdf4(%rip),%rdi
call   1090 <perror@plt>
jmp    11e5 <make_unix_server+0x65>

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

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