#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*/ socklen_t;
typedef int /*<<< orphan*/ n;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket_make_sockaddr_un(char const *, int, struct sockaddr_un *,
                            int /*<<< orphan*/ *);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int socket_local_server_bind(int s, const char *name, int namespaceId) {
  struct sockaddr_un addr;
  socklen_t alen;
  int n;
  int err;
  err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
  if (err < 0) {
    return -1;
  }
  if (1) {
    unlink(addr.sun_path);
  }
  n = 1;
  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
  if (bind(s, (struct sockaddr *)&addr, alen) < 0) {
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<socket_local_server_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x18(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
call   1050 <socket_make_sockaddr_un@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    117f <socket_local_server_bind+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <socket_local_server_bind+0x9d>
mov    -0x18(%rbp),%edi
call   1030 <unlink@plt>
movl   $0x1,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    11d7 <socket_local_server_bind+0x97>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <socket_local_server_bind+0x9d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_local_server_bind>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %rsp,%rax
lea    0xc(%rsp),%rcx
mov    %rsi,%rdi
mov    %edx,%esi
mov    %rax,%rdx
call   1050 <socket_make_sockaddr_un@plt>
test   %eax,%eax
js     11af <socket_local_server_bind+0x6f>
mov    (%rsp),%edi
call   1030 <unlink@plt>
movl   $0x1,0x8(%rsp)
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0xc(%rsp),%edx
mov    %rsp,%rsi
mov    %ebx,%edi
call   1060 <bind@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
jmp    11b4 <socket_local_server_bind+0x74>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @socket_local_server_bind(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_un
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = call i32 @socket_make_sockaddr_un(i8* %12, i32 %13, %struct.sockaddr_un* %8, i32* %9)
store i32 %14, i32* %11
%15 = load i32, i32* %11
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %34
18: ; preds = %3
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @unlink(i32 %20)
store i32 1, i32* %10
%22 = load i32, i32* %5
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_REUSEADDR
%25 = call i32 @setsockopt(i32 %22, i32 %23, i32 %24, i32* %10, i32 4)
%26 = load i32, i32* %5
%27 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%28 = load i32, i32* %9
%29 = call i64 @bind(i32 %26, %struct.sockaddr* %27, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %18
store i32 -1, i32* %4
br label %34
32: ; preds = %18
%33 = load i32, i32* %5
store i32 %33, i32* %4
br label %34
34: ; preds = %32, %31, %17
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @socket_local_server_bind(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
%7 = bitcast %struct.sockaddr_un* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = call i32 @socket_make_sockaddr_un(i8* %1, i32 %2, %struct.sockaddr_un* %4, i32* %5)
%11 = icmp slt i32 %10, 0
br i1 %11, label %24, label %12
12: ; preds = %3
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @unlink(i32 %14)
store i32 1, i32* %6
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i32 @setsockopt(i32 %0, i32 %16, i32 %17, i32* %6, i32 4)
%19 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%20 = load i32, i32* %5
%21 = call i64 @bind(i32 %0, %struct.sockaddr* %19, i32 %20)
%22 = icmp slt i64 %21, 0
%23 = select i1 %22, i32 -1, i32 %0
br label %24
24: ; preds = %12, %3
%25 = phi i32 [ -1, %3 ], [ %23, %12 ]
ret i32 %25
}