#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 ENAMETOOLONG;
int ENOENT;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int errno;
scalar_t__ listen(int, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
scalar_t__ unlink(char *);
int /*<<< orphan*/ zclose(int);
int /*<<< orphan*/ zfatal(char *, char *);
int zunix_listen(char *addr, int backlog) {
  struct sockaddr_un sun;
  int len = strlen(addr);
  int sock = -1;
  int errno2;
  if (len >= (int)sizeof(sun.sun_path)) {
    errno = ENAMETOOLONG;
    return -1;
  }
  memset((char *)&sun, 0, sizeof(struct sockaddr_un));
  sun.sun_family = AF_UNIX;
  memcpy(sun.sun_path, addr, len + 1);
  if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  if (unlink(addr) < 0 && errno != ENOENT) {
    zfatal("FATAL unlink: %s(%m)", addr);
  }
  if (bind(sock, (struct sockaddr *)&sun, sizeof(struct sockaddr_un)) < 0) {
    goto err;
  }
  if (listen(sock, backlog) < 0) {
    goto err;
  }
  return (sock);
err:
  errno2 = errno;
  if (sock > -1) {
    zclose(sock);
  }
  errno = errno2;
  return -1;
}


x86 assembly (clang, -O0):
<zunix_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
cmpl   $0x4,-0x24(%rbp)
jl     11da <zunix_listen+0x4a>
mov    0x2e05(%rip),%rax
mov    (%rax),%ecx
mov    0x2e04(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <zunix_listen+0x15a>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
add    $0x1,%edx
call   1080 <memcpy@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1239 <zunix_listen+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <zunix_listen+0x15a>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    1274 <zunix_listen+0xe4>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%eax
je     1274 <zunix_listen+0xe4>
mov    -0x10(%rbp),%rsi
lea    0xd91(%rip),%rdi
call   1040 <zfatal@plt>
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1294 <zunix_listen+0x104>
jmp    12b9 <zunix_listen+0x129>
mov    -0x28(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    12ae <zunix_listen+0x11e>
jmp    12b9 <zunix_listen+0x129>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ea <zunix_listen+0x15a>
mov    0x2d10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jle    12d7 <zunix_listen+0x147>
mov    -0x28(%rbp),%edi
call   1060 <zclose@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2cef(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zunix_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
call   1050 <strlen@plt>
cmp    $0x4,%eax
jl     11b5 <zunix_listen+0x25>
mov    0x2e1a(%rip),%rax
mov    (%rax),%ebx
jmp    1260 <zunix_listen+0xd0>
mov    %eax,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2e1b(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
add    $0x1,%ebp
mov    %rbx,%rsi
mov    %ebp,%edx
call   1080 <memcpy@plt>
mov    (%r15),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1269 <zunix_listen+0xd9>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jns    122d <zunix_listen+0x9d>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    0x2d9e(%rip),%rcx
cmp    (%rcx),%eax
je     122d <zunix_listen+0x9d>
lea    0xddb(%rip),%rdi
mov    %rbx,%rsi
call   1040 <zfatal@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     1250 <zunix_listen+0xc0>
mov    %ebp,%edi
mov    %r14d,%esi
call   1090 <listen@plt>
test   %rax,%rax
jns    126e <zunix_listen+0xde>
mov    0x2d79(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1060 <zclose@plt>
mov    0x2d69(%rip),%rax
mov    %ebx,(%rax)
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 }
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@ENOENT = global i32 0
@.str = [21 x i8] c"FATAL unlink: %s(%m)\00"
define i32 @zunix_listen(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %7
store i32 -1, i32* %8
%12 = load i32, i32* %7
%13 = icmp sge i32 %12, 4
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* @ENAMETOOLONG
store i32 %15, i32* @errno
store i32 -1, i32* %3
br label %66
16: ; preds = %2
%17 = bitcast %struct.sockaddr_un* %6 to i8*
%18 = call i32 @memset(i8* %17, i32 0, i32 8)
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load i8*, i8** %4
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
%26 = call i32 @memcpy(i32 %22, i8* %23, i32 %25)
%27 = load i32, i32* @AF_UNIX
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %8
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %16
store i32 -1, i32* %3
br label %66
32: ; preds = %16
%33 = load i8*, i8** %4
%34 = call i64 @unlink(i8* %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %32
%37 = load i32, i32* @errno
%38 = load i32, i32* @ENOENT
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i8*, i8** %4
%42 = call i32 @zfatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %40, %36, %32
%44 = load i32, i32* %8
%45 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%46 = call i64 @bind(i32 %44, %struct.sockaddr* %45, i32 8)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
br label %57
49: ; preds = %43
%50 = load i32, i32* %8
%51 = load i32, i32* %5
%52 = call i64 @listen(i32 %50, i32 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %49
br label %57
55: ; preds = %49
%56 = load i32, i32* %8
store i32 %56, i32* %3
br label %66
57: ; preds = %54, %48
%58 = load i32, i32* @errno
store i32 %58, i32* %9
%59 = load i32, i32* %8
%60 = icmp sgt i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = load i32, i32* %8
%63 = call i32 @zclose(i32 %62)
br label %64
64: ; preds = %61, %57
%65 = load i32, i32* %9
store i32 %65, i32* @errno
store i32 -1, i32* %3
br label %66
66: ; preds = %64, %55, %31, %14
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@ENOENT = global i32 0
@.str = [21 x i8] c"FATAL unlink: %s(%m)\00"
define i32 @zunix_listen(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = tail call i32 @strlen(i8* %0)
%6 = icmp sgt i32 %5, 3
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = load i32, i32* @ENAMETOOLONG
br label %40
9: ; preds = %2
%10 = call i32 @memset(i8* %4, i32 0, i32 8)
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = add nsw i32 %5, 1
%16 = call i32 @memcpy(i32 %14, i8* %0, i32 %15)
%17 = load i32, i32* @AF_UNIX
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %42, label %21
21: ; preds = %9
%22 = call i64 @unlink(i8* %0)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = load i32, i32* @errno
%26 = load i32, i32* @ENOENT
%27 = icmp eq i32 %25, %26
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = call i32 @zfatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %30
30: ; preds = %28, %24, %21
%31 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%32 = call i64 @bind(i32 %19, %struct.sockaddr* %31, i32 8)
%33 = icmp slt i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = call i64 @listen(i32 %19, i32 %1)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34, %30
%38 = load i32, i32* @errno
%39 = call i32 @zclose(i32 %19)
br label %40
40: ; preds = %7, %37
%41 = phi i32 [ %38, %37 ], [ %8, %7 ]
store i32 %41, i32* @errno
br label %42
42: ; preds = %40, %34, %9
%43 = phi i32 [ -1, %9 ], [ %19, %34 ], [ -1, %40 ]
ret i32 %43
}