#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;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sock;
typedef int /*<<< orphan*/ ifsun;

/* Variables and functions */
int /*<<< orphan*/ PF_LOCAL;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ prepare_ifsun(struct sockaddr_un *, char const *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int create_uds_server(const char *path) {
  struct sockaddr_un ifsun;
  int sock;
  prepare_ifsun(&ifsun, path);
  unlink(ifsun.sun_path);
  sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
  if (sock == -1)
    err(1, "can't create socket");
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &sock, sizeof(sock));
  if (bind(sock, ((struct sockaddr *)(&ifsun)), sizeof(ifsun)) < 0)
    err(1, "can't bind to a socket");
  return sock;
}


x86 assembly (clang, -O0):
<create_uds_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    -0x10(%rbp),%rdi
call   1050 <prepare_ifsun@plt>
mov    -0x10(%rbp),%edi
call   1030 <unlink@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b8 <create_uds_server+0x58>
mov    $0x1,%edi
lea    0xe4d(%rip),%rsi
call   1060 <err@plt>
mov    -0x14(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1208 <create_uds_server+0xa8>
mov    $0x1,%edi
lea    0xe11(%rip),%rsi
call   1060 <err@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_uds_server>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rsi
lea    0x8(%rsp),%rdi
call   1050 <prepare_ifsun@plt>
mov    0x8(%rsp),%edi
call   1030 <unlink@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
mov    %eax,0x4(%rsp)
cmp    $0xffffffff,%eax
jne    11b0 <create_uds_server+0x50>
lea    0xe5a(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x4(%rsp),%edi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    11fd <create_uds_server+0x9d>
lea    0xe21(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    0x4(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@PF_LOCAL = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [20 x i8] c"can't create socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [23 x i8] c"can't bind to a socket\00"
define i32 @create_uds_server(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @prepare_ifsun(%struct.sockaddr_un* %3, i8* %5)
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%8 = load i32, i32* %7
%9 = call i32 @unlink(i32 %8)
%10 = load i32, i32* @PF_LOCAL
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %1
%18 = load i32, i32* %4
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %18, i32 %19, i32 %20, i32* %4, i32 4)
%22 = load i32, i32* %4
%23 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%24 = call i64 @bind(i32 %22, %struct.sockaddr* %23, i32 4)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %17
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@PF_LOCAL = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [20 x i8] c"can't create socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [23 x i8] c"can't bind to a socket\00"
define i32 @create_uds_server(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = bitcast %struct.sockaddr_un* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = call i32 @prepare_ifsun(%struct.sockaddr_un* %2, i8* %0)
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = call i32 @unlink(i32 %8)
%10 = load i32, i32* @PF_LOCAL
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %3
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %12, i32 %17, i32 %18, i32* %3, i32 4)
%20 = load i32, i32* %3
%21 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%22 = call i64 @bind(i32 %20, %struct.sockaddr* %21, i32 4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %16
%27 = load i32, i32* %3
ret i32 %27
}