#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*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int 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 /*<<< orphan*/ unlink(char const *);
int ud_create_socket(const char *name) {
  int fd;
  int r;
  struct sockaddr_un uds_addr;
  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;
  strcpy(uds_addr.sun_path, name);
  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   1030 <unlink@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a6 <ud_create_socket+0x46>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1222 <ud_create_socket+0xc2>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f7 <ud_create_socket+0x97>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1222 <ud_create_socket+0xc2>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    121c <ud_create_socket+0xbc>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1222 <ud_create_socket+0xc2>
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   1030 <unlink@plt>
mov    0x2e71(%rip),%r12
mov    (%r12),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11de <ud_create_socket+0x7e>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
test   %eax,%eax
js     11dc <ud_create_socket+0x7c>
mov    %ebx,%edi
mov    $0xa,%esi
call   1060 <listen@plt>
test   %eax,%eax
cmovs  %eax,%ebx
jmp    11de <ud_create_socket+0x7e>
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 }
@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 @unlink(i8* %7)
%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* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = load i32, i32* %4
store i32 %15, i32* %2
br label %40
16: ; preds = %1
%17 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i8*, i8** %3
%23 = call i32 @strcpy(i32 %21, i8* %22)
%24 = load i32, i32* %4
%25 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 8)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = load i32, i32* %5
store i32 %30, i32* %2
br label %40
31: ; preds = %16
%32 = load i32, i32* %4
%33 = call i32 @listen(i32 %32, i32 10)
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 = %31
%37 = load i32, i32* %5
store i32 %37, i32* %2
br label %40
38: ; preds = %31
%39 = load i32, i32* %4
store i32 %39, i32* %2
br label %40
40: ; preds = %38, %36, %29, %14
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@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 @unlink(i8* %0)
%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 %23, label %9
9: ; preds = %1
%10 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @strcpy(i32 %14, i8* %0)
%16 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%17 = call i32 @bind(i32 %7, %struct.sockaddr* %16, i32 8)
%18 = icmp slt i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %9
%20 = call i32 @listen(i32 %7, i32 10)
%21 = icmp slt i32 %20, 0
%22 = select i1 %21, i32 %20, i32 %7
br label %23
23: ; preds = %19, %9, %1
%24 = phi i32 [ %7, %1 ], [ %17, %9 ], [ %22, %19 ]
ret i32 %24
}