#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*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ unlink(char const *);
int create_unix_domain_listener(const char *path, int backlog) {
  struct sockaddr_un addr;
  int fd = 0;
  assert(NULL != path);
  fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (fd < 0) {
    return -1;
  }
  unlink(path);
  memset(&addr, 0, sizeof(struct sockaddr_un));
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, path,
          (((strlen(path)) < (sizeof(addr.sun_path)))
               ? (strlen(path))
               : (sizeof(addr.sun_path))));
  if (0 != bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un))) {
    return -1;
  }
  if (0 != listen(fd, backlog)) {
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<create_unix_domain_listener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11db <create_unix_domain_listener+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <create_unix_domain_listener+0x12d>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
cmp    $0x4,%rax
jae    1237 <create_unix_domain_listener+0xb7>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
mov    %rax,-0x40(%rbp)
jmp    1245 <create_unix_domain_listener+0xc5>
mov    $0x4,%eax
mov    %rax,-0x40(%rbp)
jmp    1245 <create_unix_domain_listener+0xc5>
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1282 <create_unix_domain_listener+0x102>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <create_unix_domain_listener+0x12d>
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1080 <listen@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     12a7 <create_unix_domain_listener+0x127>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <create_unix_domain_listener+0x12d>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_unix_domain_listener>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1070 <assert@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     1240 <create_unix_domain_listener+0xc0>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <unlink@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%r12d
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    $0x4,%edx
cmp    $0x3,%eax
ja     120c <create_unix_domain_listener+0x8c>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%edx
mov    %r12d,%edi
mov    %rbx,%rsi
call   1040 <strncpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1090 <bind@plt>
test   %rax,%rax
jne    1240 <create_unix_domain_listener+0xc0>
mov    %ebp,%edi
mov    %r14d,%esi
call   1080 <listen@plt>
neg    %rax
sbb    %r15d,%r15d
or     %ebp,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
define i32 @create_unix_domain_listener(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %7
%8 = load i8*, i8** %4
%9 = icmp ne i8* null, %8
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i32, i32* @PF_UNIX
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %53
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = call i32 @unlink(i8* %19)
%21 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%22 = load i32, i32* @AF_UNIX
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i8*, i8** %4
%27 = load i8*, i8** %4
%28 = call i32 @strlen(i8* %27)
%29 = sext i32 %28 to i64
%30 = icmp ult i64 %29, 4
br i1 %30, label %31, label %35
31: ; preds = %18
%32 = load i8*, i8** %4
%33 = call i32 @strlen(i8* %32)
%34 = sext i32 %33 to i64
br label %36
35: ; preds = %18
br label %36
36: ; preds = %35, %31
%37 = phi i64 [ %34, %31 ], [ 4, %35 ]
%38 = trunc i64 %37 to i32
%39 = call i32 @strncpy(i32 %25, i8* %26, i32 %38)
%40 = load i32, i32* %7
%41 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%42 = call i64 @bind(i32 %40, %struct.sockaddr* %41, i32 8)
%43 = icmp ne i64 0, %42
br i1 %43, label %44, label %45
44: ; preds = %36
store i32 -1, i32* %3
br label %53
45: ; preds = %36
%46 = load i32, i32* %7
%47 = load i32, i32* %5
%48 = call i64 @listen(i32 %46, i32 %47)
%49 = icmp ne i64 0, %48
br i1 %49, label %50, label %51
50: ; preds = %45
store i32 -1, i32* %3
br label %53
51: ; preds = %45
%52 = load i32, i32* %7
store i32 %52, i32* %3
br label %53
53: ; preds = %51, %50, %44, %17
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
define i32 @create_unix_domain_listener(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = icmp ne i8* %0, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = load i32, i32* @PF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %33, label %12
12: ; preds = %2
%13 = tail call i32 @unlink(i8* %0)
%14 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @strlen(i8* %0)
%20 = icmp ult i32 %19, 4
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @strlen(i8* %0)
br label %23
23: ; preds = %12, %21
%24 = phi i32 [ %22, %21 ], [ 4, %12 ]
%25 = call i32 @strncpy(i32 %18, i8* %0, i32 %24)
%26 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%27 = call i64 @bind(i32 %10, %struct.sockaddr* %26, i32 8)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = call i64 @listen(i32 %10, i32 %1)
%31 = icmp eq i64 %30, 0
%32 = select i1 %31, i32 %10, i32 -1
br label %33
33: ; preds = %29, %23, %2
%34 = phi i32 [ -1, %2 ], [ -1, %23 ], [ %32, %29 ]
ret i32 %34
}