#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 {
  char *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ die(char *, ...);
scalar_t__ listen(int, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int xmkusock();
int xubind_listen(const char *path) {
  int sd = xmkusock();
  int one = 1;
  setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
  struct sockaddr_un addr = {.sun_family = AF_UNIX};
  strncpy(addr.sun_path, path, sizeof addr.sun_path);
  if (addr.sun_path[sizeof addr.sun_path - 1] != '\0')
    die("path '%s' too long", path);
  if (bind(sd, (struct sockaddr *)&addr, sizeof addr) < 0)
    die("bind:");
  if (listen(sd, 5) < 0)
    die("listen:");
  return sd;
}


x86 assembly (clang, -O0):
<xubind_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <xmkusock@plt>
mov    %eax,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
movq   $0x0,-0x20(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movsbl 0x7(%rax),%eax
cmp    $0x0,%eax
je     11ea <xubind_listen+0x8a>
mov    -0x8(%rbp),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1213 <xubind_listen+0xb3>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    1238 <xubind_listen+0xd8>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xubind_listen>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <xmkusock@plt>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
movq   $0x0,0x8(%rsp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
cmpb   $0x0,0x7
je     11df <xubind_listen+0x7f>
lea    0xe2b(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <die@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    1203 <xubind_listen+0xa3>
lea    0xe17(%rip),%rdi
xor    %eax,%eax
call   1080 <die@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
test   %rax,%rax
jns    1222 <xubind_listen+0xc2>
lea    0xdfe(%rip),%rdi
xor    %eax,%eax
call   1080 <die@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@AF_UNIX = global i32 0
@.str = [19 x i8] c"path '%s' too long\00"
@.str.1 = [6 x i8] c"bind:\00"
@.str.2 = [8 x i8] c"listen:\00"
define i32 @xubind_listen(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
store i8* %0, i8** %2
%6 = call i32 (...) @xmkusock()
store i32 %6, i32* %3
store i32 1, i32* %4
%7 = load i32, i32* %3
%8 = load i32, i32* @SOL_SOCKET
%9 = load i32, i32* @SO_REUSEADDR
%10 = call i32 @setsockopt(i32 %7, i32 %8, i32 %9, i32* %4, i32 4)
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
store i8* null, i8** %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
%13 = load i32, i32* @AF_UNIX
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%15 = load i8*, i8** %14
%16 = load i8*, i8** %2
%17 = call i32 @strncpy(i8* %15, i8* %16, i32 8)
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8, i8* %19, i64 7
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %1
%25 = load i8*, i8** %2
%26 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %24, %1
%28 = load i32, i32* %3
%29 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 16)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = load i32, i32* %3
%36 = call i64 @listen(i32 %35, i32 5)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@AF_UNIX = global i32 0
@.str = [19 x i8] c"path '%s' too long\00"
@.str.1 = [6 x i8] c"bind:\00"
@.str.2 = [8 x i8] c"listen:\00"
define i32 @xubind_listen(i8* %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = tail call i32 (...) @xmkusock()
%5 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%6 = load i32, i32* @SOL_SOCKET
%7 = load i32, i32* @SO_REUSEADDR
%8 = call i32 @setsockopt(i32 %4, i32 %6, i32 %7, i32* %2, i32 4)
%9 = bitcast %struct.sockaddr_un* %3 to i8*
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
store i8* null, i8** %10
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
%12 = load i32, i32* @AF_UNIX
store i32 %12, i32* %11
%13 = call i32 @strncpy(i8* null, i8* %0, i32 8)
%14 = load i8, i8* inttoptr (i64 7 to i8*)
%15 = icmp eq i8 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %1
%17 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %16, %1
%19 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%20 = call i64 @bind(i32 %4, %struct.sockaddr* %19, i32 16)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = call i64 @listen(i32 %4, i32 5)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
ret i32 %4
}