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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ chmod(char const *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char const *);
int serv_listen(const char *name) {
  int fd, len;
  struct sockaddr_un unix_addr;
  if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
    return (-1);
  fcntl(fd, F_SETFD, FD_CLOEXEC);
  unlink(name);
  memset(&unix_addr, 0, sizeof(unix_addr));
  unix_addr.sun_family = AF_UNIX;
  strcpy(unix_addr.sun_path, name);
  len = sizeof(unix_addr.sun_family) + strlen(unix_addr.sun_path);
  if (bind(fd, (struct sockaddr *)&unix_addr, len) < 0)
    goto error;
  if (chmod(name, 0666) < 0)
    goto error;
  if (listen(fd, 5) < 0)
    goto error;
  return (fd);
error:
  close(fd);
  return (-1);
}


x86 assembly (clang, -O0):
<serv_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11dd <serv_listen+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12aa <serv_listen+0x10a>
mov    -0x14(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
call   1050 <fcntl@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%edi
call   1060 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    1257 <serv_listen+0xb7>
jmp    129b <serv_listen+0xfb>
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
call   10a0 <chmod@plt>
cmp    $0x0,%rax
jge    1274 <serv_listen+0xd4>
jmp    129b <serv_listen+0xfb>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    1290 <serv_listen+0xf0>
jmp    129b <serv_listen+0xfb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12aa <serv_listen+0x10a>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serv_listen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e32(%rip),%r12
mov    (%r12),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     126d <serv_listen+0xcd>
mov    %eax,%ebp
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1050 <fcntl@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    0x8(%rsp),%edi
call   1060 <strlen@plt>
lea    0x4(%rax),%edx
mov    %ebp,%edi
mov    %r15,%rsi
call   10b0 <bind@plt>
test   %rax,%rax
js     1266 <serv_listen+0xc6>
mov    %rbx,%rdi
mov    $0x1b6,%esi
call   10a0 <chmod@plt>
test   %rax,%rax
js     1266 <serv_listen+0xc6>
mov    %ebp,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
js     1266 <serv_listen+0xc6>
mov    %ebp,%r14d
jmp    126d <serv_listen+0xcd>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14d,%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 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @serv_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 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %53
12: ; preds = %1
%13 = load i32, i32* %4
%14 = load i32, i32* @F_SETFD
%15 = load i32, i32* @FD_CLOEXEC
%16 = call i32 @fcntl(i32 %13, i32 %14, i32 %15)
%17 = load i8*, i8** %3
%18 = call i32 @unlink(i8* %17)
%19 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i8*, i8** %3
%25 = call i32 @strcpy(i32 %23, i8* %24)
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @strlen(i32 %27)
%29 = sext i32 %28 to i64
%30 = add i64 4, %29
%31 = trunc i64 %30 to i32
store i32 %31, i32* %5
%32 = load i32, i32* %4
%33 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%34 = load i32, i32* %5
%35 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %12
br label %50
38: ; preds = %12
%39 = load i8*, i8** %3
%40 = call i64 @chmod(i8* %39, i32 438)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
br label %50
43: ; preds = %38
%44 = load i32, i32* %4
%45 = call i64 @listen(i32 %44, i32 5)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %43
br label %50
48: ; preds = %43
%49 = load i32, i32* %4
store i32 %49, i32* %2
br label %53
50: ; preds = %47, %42, %37
%51 = load i32, i32* %4
%52 = call i32 @close(i32 %51)
store i32 -1, i32* %2
br label %53
53: ; preds = %50, %48, %11
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @serv_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 slt i32 %6, 0
br i1 %7, label %33, label %8
8: ; preds = %1
%9 = load i32, i32* @F_SETFD
%10 = load i32, i32* @FD_CLOEXEC
%11 = tail call i32 @fcntl(i32 %6, i32 %9, i32 %10)
%12 = tail call i32 @unlink(i8* %0)
%13 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @strcpy(i32 %17, i8* %0)
%19 = load i32, i32* %16
%20 = call i32 @strlen(i32 %19)
%21 = add i32 %20, 4
%22 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%23 = call i64 @bind(i32 %6, %struct.sockaddr* %22, i32 %21)
%24 = icmp slt i64 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %8
%26 = call i64 @chmod(i8* %0, i32 438)
%27 = icmp slt i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = call i64 @listen(i32 %6, i32 5)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28, %25, %8
%32 = call i32 @close(i32 %6)
br label %33
33: ; preds = %28, %1, %31
%34 = phi i32 [ -1, %31 ], [ -1, %1 ], [ %6, %28 ]
ret i32 %34
}