#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;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ srv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ unlink(char *);
int xyz_domain_listen(char *addr) {
  int listen_fd;
  int ret;
  struct sockaddr_un srv_addr;
  listen_fd = socket(AF_UNIX, SOCK_STREAM, IPPROTO_TCP);
  if (listen_fd < 0) {
    perror("socket() error");
    return -1;
  }
  strncpy(srv_addr.sun_path, addr, sizeof(srv_addr.sun_path) - 1);
  ret = bind(listen_fd, (struct sockaddr *)&srv_addr, sizeof(srv_addr));
  if (ret == -1) {
    perror("bind() error");
    close(listen_fd);
    unlink(addr);
    return -1;
  }
  ret = listen(listen_fd, 1);
  if (ret == -1) {
    perror("listen() error");
    close(listen_fd);
    unlink(addr);
    return -1;
  }
  unlink(addr);
  return listen_fd;
}


x86 assembly (clang, -O0):
<xyz_domain_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
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c1 <xyz_domain_listen+0x51>
lea    0xe50(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <xyz_domain_listen+0xfb>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1219 <xyz_domain_listen+0xa9>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <xyz_domain_listen+0xfb>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1060 <listen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    125c <xyz_domain_listen+0xec>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <xyz_domain_listen+0xfb>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xyz_domain_listen>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11df <xyz_domain_listen+0x6f>
mov    %eax,%ebp
mov    %rbx,%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     11f2 <xyz_domain_listen+0x82>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
je     11fb <xyz_domain_listen+0x8b>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11d6 <xyz_domain_listen+0x66>
lea    0xe16(%rip),%rdi
jmp    1202 <xyz_domain_listen+0x92>
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%ebp
jmp    11d6 <xyz_domain_listen+0x66>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [15 x i8] c"socket() error\00"
@.str.1 = [13 x i8] c"bind() error\00"
@.str.2 = [15 x i8] c"listen() error\00"
define i32 @xyz_domain_listen(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
store i8* %0, i8** %3
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = load i32, i32* @IPPROTO_TCP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %46
15: ; preds = %1
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, 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 = load i32, i32* %4
%21 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%22 = call i32 @bind(i32 %20, %struct.sockaddr* %21, i32 4)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %15
%26 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
%29 = load i8*, i8** %3
%30 = call i32 @unlink(i8* %29)
store i32 -1, i32* %2
br label %46
31: ; preds = %15
%32 = load i32, i32* %4
%33 = call i32 @listen(i32 %32, i32 1)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %42
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
%40 = load i8*, i8** %3
%41 = call i32 @unlink(i8* %40)
store i32 -1, i32* %2
br label %46
42: ; preds = %31
%43 = load i8*, i8** %3
%44 = call i32 @unlink(i8* %43)
%45 = load i32, i32* %4
store i32 %45, i32* %2
br label %46
46: ; preds = %42, %36, %25, %13
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [15 x i8] c"socket() error\00"
@.str.1 = [13 x i8] c"bind() error\00"
@.str.2 = [15 x i8] c"listen() error\00"
define i32 @xyz_domain_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 = load i32, i32* @IPPROTO_TCP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %29
11: ; preds = %1
%12 = tail call i32 @strncpy(i32 undef, i8* %0, i32 3)
%13 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%14 = call i32 @bind(i32 %7, %struct.sockaddr* %13, i32 4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @close(i32 %7)
%19 = call i32 @unlink(i8* %0)
br label %29
20: ; preds = %11
%21 = call i32 @listen(i32 %7, i32 1)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @close(i32 %7)
%26 = call i32 @unlink(i8* %0)
br label %29
27: ; preds = %20
%28 = call i32 @unlink(i8* %0)
br label %29
29: ; preds = %27, %23, %16, %9
%30 = phi i32 [ -1, %9 ], [ -1, %16 ], [ -1, %23 ], [ %7, %27 ]
ret i32 %30
}