#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*/ uds_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*/ ENAMETOOLONG;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ unlink(char const *);
int ud_create_socket(const char *name) {
  int fd;
  int r;
  struct sockaddr_un uds_addr;
  if (strlen(name) >= sizeof(uds_addr.sun_path)) {
    errno = ENAMETOOLONG;
    return -1;
  }
  unlink(name);
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd < 0) {
    return fd;
  }
  memset(&uds_addr, 0, sizeof(uds_addr));
  uds_addr.sun_family = AF_UNIX;
  strncpy(uds_addr.sun_path, name, sizeof(uds_addr.sun_path) - 1);
  r = bind(fd, (struct sockaddr *)&uds_addr, sizeof(uds_addr));
  if (r < 0) {
    return r;
  }
  r = listen(fd, 10);
  if (r < 0) {
    return r;
  }
  return fd;
}


x86 assembly (clang, -O0):
<ud_create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
cmp    $0x4,%rax
jb     11af <ud_create_socket+0x3f>
mov    0x2e30(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <ud_create_socket+0xfa>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e9 <ud_create_socket+0x79>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126a <ud_create_socket+0xfa>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    123f <ud_create_socket+0xcf>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126a <ud_create_socket+0xfa>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1264 <ud_create_socket+0xf4>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126a <ud_create_socket+0xfa>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ud_create_socket>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <strlen@plt>
cmp    $0x4,%eax
jb     119e <ud_create_socket+0x2e>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    0x2e3b(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    1219 <ud_create_socket+0xa9>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    0x2e3b(%rip),%r12
mov    (%r12),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1219 <ud_create_socket+0xa9>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r14,%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %eax,%eax
js     1217 <ud_create_socket+0xa7>
mov    %ebx,%edi
mov    $0xa,%esi
call   1070 <listen@plt>
test   %eax,%eax
cmovs  %eax,%ebx
jmp    1219 <ud_create_socket+0xa9>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @ud_create_socket(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 i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = sext i32 %8 to i64
%10 = icmp uge i64 %9, 4
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i32, i32* @ENAMETOOLONG
store i32 %12, i32* @errno
store i32 -1, i32* %2
br label %47
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i32 @unlink(i8* %14)
%16 = load i32, i32* @AF_UNIX
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = load i32, i32* %4
store i32 %22, i32* %2
br label %47
23: ; preds = %13
%24 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%25 = load i32, i32* @AF_UNIX
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load i8*, i8** %3
%30 = call i32 @strncpy(i32 %28, i8* %29, i32 3)
%31 = load i32, i32* %4
%32 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%33 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 8)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %23
%37 = load i32, i32* %5
store i32 %37, i32* %2
br label %47
38: ; preds = %23
%39 = load i32, i32* %4
%40 = call i32 @listen(i32 %39, i32 10)
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = load i32, i32* %5
store i32 %44, i32* %2
br label %47
45: ; preds = %38
%46 = load i32, i32* %4
store i32 %46, i32* %2
br label %47
47: ; preds = %45, %43, %36, %21, %11
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @ud_create_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp ugt i32 %4, 3
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = load i32, i32* @ENAMETOOLONG
store i32 %7, i32* @errno
br label %28
8: ; preds = %1
%9 = tail call i32 @unlink(i8* %0)
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %28, label %14
14: ; preds = %8
%15 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @strncpy(i32 %19, i8* %0, i32 3)
%21 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%22 = call i32 @bind(i32 %12, %struct.sockaddr* %21, i32 8)
%23 = icmp slt i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %14
%25 = call i32 @listen(i32 %12, i32 10)
%26 = icmp slt i32 %25, 0
%27 = select i1 %26, i32 %25, i32 %12
br label %28
28: ; preds = %24, %14, %8, %6
%29 = phi i32 [ -1, %6 ], [ %12, %8 ], [ %22, %14 ], [ %27, %24 ]
ret i32 %29
}