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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_un *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ unlink(char *);
int unix_bind(char *name) {
  struct sockaddr_un addr;
  int sock;
  (void)unlink(name);
  sock = socket(PF_UNIX, SOCK_STREAM, PF_UNSPEC);
  if (sock == -1) {
    (void)fprintf(stderr, "UNIX socket failed: %s\n", strerror(errno));
    exit(1);
  }
  bzero(&addr, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, name, sizeof(addr.sun_path) - 1);
  if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    (void)fprintf(stderr, "failed to bind socket: %s\n", strerror(errno));
    exit(1);
  }
  if (listen(sock, 100) == -1) {
    (void)fprintf(stderr, "failed to listen: %s\n", strerror(errno));
    exit(1);
  }
  return (sock);
}


x86 assembly (clang, -O0):
<unix_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1208 <unix_bind+0x78>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe07(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   1080 <bzero@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1283 <unix_bind+0xf3>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xda4(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x64,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    12cf <unix_bind+0x13f>
mov    0x2d28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xd73(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_bind>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
call   1030 <unlink@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1229 <unix_bind+0x99>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0x8,%esi
call   1080 <bzero@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     1249 <unix_bind+0xb9>
mov    %ebx,%edi
mov    $0x64,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
je     1269 <unix_bind+0xd9>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d98(%rip),%rax
mov    (%rax),%ebx
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb9(%rip),%rsi
jmp    1287 <unix_bind+0xf7>
mov    0x2d78(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb1(%rip),%rsi
jmp    1287 <unix_bind+0xf7>
mov    0x2d58(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdac(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@PF_UNSPEC = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"UNIX socket failed: %s\0A\00"
@errno = global i32 0
@AF_UNIX = global i32 0
@.str.1 = [27 x i8] c"failed to bind socket: %s\0A\00"
@.str.2 = [22 x i8] c"failed to listen: %s\0A\00"
define i32 @unix_bind(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 @unlink(i8* %5)
%7 = load i32, i32* @PF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = load i32, i32* @PF_UNSPEC
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %1
%20 = call i32 @bzero(%struct.sockaddr_un* %3, i32 8)
%21 = load i32, i32* @AF_UNIX
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i8*, i8** %2
%26 = call i32 @strncpy(i32 %24, i8* %25, i32 3)
%27 = load i32, i32* %4
%28 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%29 = call i32 @bind(i32 %27, %struct.sockaddr* %28, i32 8)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %37
31: ; preds = %19
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %19
%38 = load i32, i32* %4
%39 = call i32 @listen(i32 %38, i32 100)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %47
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %37
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@PF_UNSPEC = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"UNIX socket failed: %s\0A\00"
@errno = global i32 0
@AF_UNIX = global i32 0
@.str.1 = [27 x i8] c"failed to bind socket: %s\0A\00"
@.str.2 = [22 x i8] c"failed to listen: %s\0A\00"
define i32 @unix_bind(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* @PF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @PF_UNSPEC
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = call i32 @bzero(%struct.sockaddr_un* %2, i32 8)
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = tail call i32 @strncpy(i32 %21, i8* %0, i32 3)
%23 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%24 = call i32 @bind(i32 %8, %struct.sockaddr* %23, i32 8)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %16
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %16
%33 = call i32 @listen(i32 %8, i32 100)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %32
ret i32 %8
}