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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ ALLPERMS;
scalar_t__ ENOENT;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
scalar_t__ chmod(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int unlink(int /*<<< orphan*/);
int pt_comm_listen(const char *addrstr) {
  int fd;
  struct sockaddr_un addr;
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd == -1) {
    return -1;
  }
  memset(&addr, 0, sizeof(struct sockaddr_un));
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, addrstr, sizeof(addr.sun_path) - 1);
  if (unlink(addr.sun_path) == -1 && errno != ENOENT) {
    return -1;
  }
  if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
    return -1;
  }
  if (chmod(addr.sun_path, ALLPERMS) == 0) {
    errno = 0;
  }
  if (listen(fd, 128) == -1) {
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<pt_comm_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ae <pt_comm_listen+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1289 <pt_comm_listen+0x119>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%edi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    1212 <pt_comm_listen+0xa2>
mov    0x2dd5(%rip),%rax
mov    (%rax),%rax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
je     1212 <pt_comm_listen+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1289 <pt_comm_listen+0x119>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1238 <pt_comm_listen+0xc8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1289 <pt_comm_listen+0x119>
mov    -0x20(%rbp),%edi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
call   1070 <chmod@plt>
cmp    $0x0,%rax
jne    1261 <pt_comm_listen+0xf1>
mov    0x2d6e(%rip),%rax
movq   $0x0,(%rax)
mov    -0x14(%rbp),%edi
mov    $0x80,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1283 <pt_comm_listen+0x113>
movl   $0xffffffff,-0x4(%rbp)
jmp    1289 <pt_comm_listen+0x119>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pt_comm_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    0x2e67(%rip),%r15
mov    (%r15),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     123b <pt_comm_listen+0xcb>
mov    %eax,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %rbp,%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    (%rsp),%edi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    11ee <pt_comm_listen+0x7e>
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
jne    123b <pt_comm_listen+0xcb>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     123b <pt_comm_listen+0xcb>
mov    (%rsp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
call   1070 <chmod@plt>
test   %rax,%rax
jne    1226 <pt_comm_listen+0xb6>
mov    0x2da9(%rip),%rax
movq   $0x0,(%rax)
mov    %ebx,%edi
mov    $0x80,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%r14d
mov    %r14d,%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
@errno = global i64 0
@ENOENT = global i64 0
@ALLPERMS = global i32 0
define i32 @pt_comm_listen(i8* %0) {
%2 = alloca i32
%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 %12
11: ; preds = %1
store i32 -1, i32* %2
br label %49
12: ; preds = %1
%13 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 8)
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = call i32 @strncpy(i32 %17, i8* %18, i32 3)
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @unlink(i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %12
%25 = load i64, i64* @errno
%26 = load i64, i64* @ENOENT
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %2
br label %49
29: ; preds = %24, %12
%30 = load i32, i32* %4
%31 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 8)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 -1, i32* %2
br label %49
35: ; preds = %29
%36 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32, i32* @ALLPERMS
%39 = call i64 @chmod(i32 %37, i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %35
store i64 0, i64* @errno
br label %42
42: ; preds = %41, %35
%43 = load i32, i32* %4
%44 = call i32 @listen(i32 %43, i32 128)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 -1, i32* %2
br label %49
47: ; preds = %42
%48 = load i32, i32* %4
store i32 %48, i32* %2
br label %49
49: ; preds = %47, %46, %34, %28, %11
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@ALLPERMS = global i32 0
define i32 @pt_comm_listen(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 %36, label %8
8: ; preds = %1
%9 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @strncpy(i32 %13, i8* %0, i32 3)
%15 = load i32, i32* %12
%16 = call i32 @unlink(i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %8
%19 = load i64, i64* @errno
%20 = load i64, i64* @ENOENT
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %36
22: ; preds = %18, %8
%23 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%24 = call i32 @bind(i32 %6, %struct.sockaddr* %23, i32 8)
%25 = icmp eq i32 %24, -1
br i1 %25, label %36, label %26
26: ; preds = %22
%27 = load i32, i32* %12
%28 = load i32, i32* @ALLPERMS
%29 = call i64 @chmod(i32 %27, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
store i64 0, i64* @errno
br label %32
32: ; preds = %31, %26
%33 = call i32 @listen(i32 %6, i32 128)
%34 = icmp eq i32 %33, -1
%35 = select i1 %34, i32 -1, i32 %6
br label %36
36: ; preds = %32, %22, %18, %1
%37 = phi i32 [ -1, %1 ], [ -1, %18 ], [ -1, %22 ], [ %35, %32 ]
ret i32 %37
}