#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*/ SOCK_STREAM;
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 /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
int /*<<< orphan*/ unlink(char const *);
int /*<<< orphan*/ warn(char *);
int /*<<< orphan*/ warnx(char *);
int serv_listen(const char *name) {
  int sockfd, addrlen;
  struct sockaddr_un unix_addr;
  struct sockaddr *sock_addr;
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sockfd < 0) {
    warn("socket");
    return -1;
  }
  addrlen = strlen(name);
  if (addrlen >= sizeof(unix_addr.sun_path)) {
    warnx("serv_listen: socket name too long");
    return -2;
  }
  addrlen += sizeof(unix_addr.sun_family);
  memset(&unix_addr, 0, sizeof(unix_addr));
  unix_addr.sun_family = AF_UNIX;
  strcpy(unix_addr.sun_path, name);
  unlink(name);
  sock_addr = (struct sockaddr *)&unix_addr;
  if (bind(sockfd, sock_addr, addrlen) < 0) {
    warn("bind");
    return -3;
  }
  if (listen(sockfd, 5) < 0) {
    warn("listen");
    return -4;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<serv_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11da <serv_listen+0x4a>
lea    0xe37(%rip),%rdi
call   1050 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b6 <serv_listen+0x126>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
cmp    $0x4,%rax
jb     120c <serv_listen+0x7c>
lea    0xe0c(%rip),%rdi
call   1070 <warnx@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12b6 <serv_listen+0x126>
movslq -0x18(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1281 <serv_listen+0xf1>
lea    0xdb9(%rip),%rdi
call   1050 <warn@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12b6 <serv_listen+0x126>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    12b0 <serv_listen+0x120>
lea    0xd8f(%rip),%rdi
call   1050 <warn@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12b6 <serv_listen+0x126>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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,%r14
mov    0x2e42(%rip),%r12
mov    (%r12),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     11e3 <serv_listen+0x53>
mov    %eax,%ebx
mov    %r14,%rdi
call   1060 <strlen@plt>
cmp    $0x4,%eax
jb     11f6 <serv_listen+0x66>
lea    0xe33(%rip),%rdi
call   1070 <warnx@plt>
mov    $0xfffffffe,%ebx
jmp    1271 <serv_listen+0xe1>
lea    0xe16(%rip),%rdi
call   1050 <warn@plt>
mov    $0xffffffff,%ebx
jmp    1271 <serv_listen+0xe1>
mov    %eax,%ebp
add    $0x4,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     1260 <serv_listen+0xd0>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
jns    1271 <serv_listen+0xe1>
lea    0xdda(%rip),%rdi
call   1050 <warn@plt>
mov    $0xfffffffc,%ebx
jmp    1271 <serv_listen+0xe1>
lea    0xdc2(%rip),%rdi
call   1050 <warn@plt>
mov    $0xfffffffd,%ebx
mov    %ebx,%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
@.str = [7 x i8] c"socket\00"
@.str.1 = [34 x i8] c"serv_listen: socket name too long\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
define i32 @serv_listen(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca %struct.sockaddr*
store i8* %0, i8** %3
%8 = load i32, i32* @AF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
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 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %53
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = icmp uge i64 %19, 4
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @warnx(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %2
br label %53
23: ; preds = %15
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = add i64 %25, 4
%27 = trunc i64 %26 to i32
store i32 %27, i32* %5
%28 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%29 = load i32, i32* @AF_UNIX
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i8*, i8** %3
%34 = call i32 @strcpy(i32 %32, i8* %33)
%35 = load i8*, i8** %3
%36 = call i32 @unlink(i8* %35)
%37 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
store %struct.sockaddr* %37, %struct.sockaddr** %7
%38 = load i32, i32* %4
%39 = load %struct.sockaddr*, %struct.sockaddr** %7
%40 = load i32, i32* %5
%41 = call i64 @bind(i32 %38, %struct.sockaddr* %39, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %23
%44 = call i32 @warn(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %2
br label %53
45: ; preds = %23
%46 = load i32, i32* %4
%47 = call i64 @listen(i32 %46, i32 5)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 -4, i32* %2
br label %53
51: ; preds = %45
%52 = load i32, i32* %4
store i32 %52, i32* %2
br label %53
53: ; preds = %51, %49, %43, %21, %13
%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
@.str = [7 x i8] c"socket\00"
@.str.1 = [34 x i8] c"serv_listen: socket name too long\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
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 %8, label %10
8: ; preds = %1
%9 = tail call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %34
10: ; preds = %1
%11 = tail call i32 @strlen(i8* %0)
%12 = icmp ugt i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @warnx(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %34
15: ; preds = %10
%16 = add nuw nsw i32 %11, 4
%17 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @strcpy(i32 %21, i8* %0)
%23 = call i32 @unlink(i8* %0)
%24 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%25 = call i64 @bind(i32 %6, %struct.sockaddr* %24, i32 %16)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 @warn(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %34
29: ; preds = %15
%30 = call i64 @listen(i32 %6, i32 5)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %29, %32, %27, %13, %8
%35 = phi i32 [ -1, %8 ], [ -2, %13 ], [ -3, %27 ], [ -4, %32 ], [ %6, %29 ]
ret i32 %35
}