#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*/ local;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int dspd_unix_sock_create(const char *addr, int flags) {
  struct sockaddr_un local;
  int fd, len, err;
  memset(&local, 0, sizeof(local));
  if ((fd = socket(AF_UNIX, SOCK_STREAM | flags, 0)) < 0)
    return -errno;
  local.sun_family = AF_UNIX;
  strcpy(local.sun_path, addr);
  unlink(local.sun_path);
  len = strlen(local.sun_path) + sizeof(local.sun_family);
  if (bind(fd, (struct sockaddr *)&local, len) == -1) {
    err = -errno;
    close(fd);
    return err;
  }
  return fd;
}


x86 assembly (clang, -O0):
<dspd_unix_sock_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
or     -0x14(%rbp),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11ca <dspd_unix_sock_create+0x5a>
xor    %eax,%eax
mov    0x2e10(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    123a <dspd_unix_sock_create+0xca>
mov    0x2e17(%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   1030 <unlink@plt>
mov    -0x20(%rbp),%edi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1234 <dspd_unix_sock_create+0xc4>
xor    %eax,%eax
mov    0x2db4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123a <dspd_unix_sock_create+0xca>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dspd_unix_sock_create>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e54(%rip),%r15
mov    (%r15),%edi
mov    0x2e3a(%rip),%rax
or     (%rax),%ebx
mov    %ebx,%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11f7 <dspd_unix_sock_create+0x87>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    (%rsp),%edi
call   1030 <unlink@plt>
mov    (%rsp),%edi
call   1050 <strlen@plt>
lea    0x4(%rax),%edx
mov    %rsp,%rsi
mov    %ebx,%edi
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1202 <dspd_unix_sock_create+0x92>
mov    0x2de6(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1200 <dspd_unix_sock_create+0x90>
mov    0x2dd2(%rip),%rax
sub    (%rax),%ebp
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
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
@errno = global i32 0
define i32 @dspd_unix_sock_create(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%11 = load i32, i32* @AF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = load i32, i32* %5
%14 = or i32 %12, %13
%15 = call i32 @socket(i32 %11, i32 %14, i32 0)
store i32 %15, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
store i32 %19, i32* %3
br label %49
20: ; preds = %2
%21 = load i32, i32* @AF_UNIX
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i8*, i8** %4
%26 = call i32 @strcpy(i32 %24, i8* %25)
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @unlink(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @strlen(i32 %31)
%33 = sext i32 %32 to i64
%34 = add i64 %33, 4
%35 = trunc i64 %34 to i32
store i32 %35, i32* %8
%36 = load i32, i32* %7
%37 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%38 = load i32, i32* %8
%39 = call i32 @bind(i32 %36, %struct.sockaddr* %37, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %47
41: ; preds = %20
%42 = load i32, i32* @errno
%43 = sub nsw i32 0, %42
store i32 %43, i32* %9
%44 = load i32, i32* %7
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %9
store i32 %46, i32* %3
br label %49
47: ; preds = %20
%48 = load i32, i32* %7
store i32 %48, i32* %3
br label %49
49: ; preds = %47, %41, %17
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
define i32 @dspd_unix_sock_create(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = or i32 %7, %1
%9 = call i32 @socket(i32 %6, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
br label %32
14: ; preds = %2
%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 @strcpy(i32 %18, i8* %0)
%20 = load i32, i32* %17
%21 = call i32 @unlink(i32 %20)
%22 = load i32, i32* %17
%23 = call i32 @strlen(i32 %22)
%24 = add i32 %23, 4
%25 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%26 = call i32 @bind(i32 %9, %struct.sockaddr* %25, i32 %24)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %14
%29 = load i32, i32* @errno
%30 = sub nsw i32 0, %29
%31 = call i32 @close(i32 %9)
br label %32
32: ; preds = %14, %28, %11
%33 = phi i32 [ %13, %11 ], [ %30, %28 ], [ %9, %14 ]
ret i32 %33
}