#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;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ listen(int, int);
scalar_t__ make_pathname(char const *);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
char *strerror(int /*<<< orphan*/);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char const *);
int daemon_create_server_usocket(const char *pathname, int backlog) {
  int sockfd, servlen;
  struct sockaddr_un servaddr;
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sockfd < 0) {
    printf("socket() at daemon_create_server_usocket(): %s", strerror(errno));
    return -1;
  }
  unlink(pathname);
  if (make_pathname(pathname))
    return -2;
  bzero((char *)&servaddr, sizeof(servaddr));
  servaddr.sun_family = AF_UNIX;
  strcpy(servaddr.sun_path, pathname);
  servlen = (sizeof(*(&servaddr)) - sizeof((&servaddr)->sun_path) +
             strlen((&servaddr)->sun_path));
  if (bind(sockfd, (struct sockaddr *)&servaddr, servlen) < 0) {
    close(sockfd);
    printf("bind() at daemon_create_server_usocket(): %s", strerror(errno));
    return -3;
  }
  if (listen(sockfd, backlog)) {
    close(sockfd);
    printf("listen() at daemon_create_server_usocket(): %s", strerror(errno));
    return -4;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<daemon_create_server_usocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    120e <daemon_create_server_usocket+0x5e>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xe03(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1303 <daemon_create_server_usocket+0x153>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rdi
call   10c0 <make_pathname@plt>
cmp    $0x0,%rax
je     1236 <daemon_create_server_usocket+0x86>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1303 <daemon_create_server_usocket+0x153>
lea    -0x28(%rbp),%rdi
mov    $0x8,%esi
call   10a0 <bzero@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%edi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12b7 <daemon_create_server_usocket+0x107>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd89(%rip),%rdi
call   1060 <printf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1303 <daemon_create_server_usocket+0x153>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     12fd <daemon_create_server_usocket+0x14d>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd70(%rip),%rdi
call   1060 <printf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1303 <daemon_create_server_usocket+0x153>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_create_server_usocket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e1f(%rip),%r12
mov    (%r12),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     128a <daemon_create_server_usocket+0xda>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %rbx,%rdi
call   10c0 <make_pathname@plt>
mov    %rax,%rcx
mov    $0xfffffffe,%eax
test   %rcx,%rcx
jne    12ac <daemon_create_server_usocket+0xfc>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0x8,%esi
call   10a0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%r12d
mov    %r12d,%edi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r12d,%edi
call   1050 <strlen@plt>
lea    0x4(%rax),%edx
mov    %ebp,%edi
mov    %r15,%rsi
call   1090 <bind@plt>
test   %rax,%rax
js     12b9 <daemon_create_server_usocket+0x109>
mov    %ebp,%edi
mov    %r14d,%esi
call   1080 <listen@plt>
test   %rax,%rax
je     12e4 <daemon_create_server_usocket+0x134>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xde1(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    $0xfffffffc,%eax
jmp    12ac <daemon_create_server_usocket+0xfc>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd61(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd5a(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    $0xfffffffd,%eax
jmp    12ac <daemon_create_server_usocket+0xfc>
mov    %ebp,%eax
jmp    12ac <daemon_create_server_usocket+0xfc>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [47 x i8] c"socket() at daemon_create_server_usocket(): %s\00"
@errno = global i32 0
@.str.1 = [45 x i8] c"bind() at daemon_create_server_usocket(): %s\00"
@.str.2 = [47 x i8] c"listen() at daemon_create_server_usocket(): %s\00"
define i32 @daemon_create_server_usocket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_un
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %64
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = call i32 @unlink(i8* %19)
%21 = load i8*, i8** %4
%22 = call i64 @make_pathname(i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 -2, i32* %3
br label %64
25: ; preds = %18
%26 = bitcast %struct.sockaddr_un* %8 to i8*
%27 = call i32 @bzero(i8* %26, i32 8)
%28 = load i32, i32* @AF_UNIX
%29 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i8*, i8** %4
%33 = call i32 @strcpy(i32 %31, i8* %32)
%34 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @strlen(i32 %35)
%37 = sext i32 %36 to i64
%38 = add i64 4, %37
%39 = trunc i64 %38 to i32
store i32 %39, i32* %7
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%42 = load i32, i32* %7
%43 = call i64 @bind(i32 %40, %struct.sockaddr* %41, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %25
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %49)
store i32 -3, i32* %3
br label %64
51: ; preds = %25
%52 = load i32, i32* %6
%53 = load i32, i32* %5
%54 = call i64 @listen(i32 %52, i32 %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %51
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %60)
store i32 -4, i32* %3
br label %64
62: ; preds = %51
%63 = load i32, i32* %6
store i32 %63, i32* %3
br label %64
64: ; preds = %62, %56, %45, %24, %14
%65 = load i32, i32* %3
ret i32 %65
}

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