#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(int /*<<< orphan*/);
int unixsock_bind(const char *pathname) {
  struct sockaddr_un addr;
  int len, usfd;
  if ((usfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    perror("socket() ");
    return -1;
  }
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, pathname);
  len = strlen(addr.sun_path) + sizeof(addr.sun_family);
  if (bind(usfd, (struct sockaddr *)&addr, len) == -1) {
    perror("bind() ");
    return -1;
  }
  if (listen(usfd, 5) < 0) {
    perror("listen() ");
    return -1;
  }
  return usfd;
}


x86 assembly (clang, -O0):
<unixsock_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11a9 <unixsock_bind+0x49>
lea    0xe68(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <unixsock_bind+0xd7>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%edi
call   1040 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    1202 <unixsock_bind+0xa2>
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <unixsock_bind+0xd7>
mov    -0x20(%rbp),%edi
mov    $0x5,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    1231 <unixsock_bind+0xd1>
lea    0xdf2(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <unixsock_bind+0xd7>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unixsock_bind>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e75(%rip),%r15
mov    (%r15),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11d1 <unixsock_bind+0x71>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14,%rsi
call   1030 <strcpy@plt>
call   1040 <strlen@plt>
lea    0x4(%rax),%edx
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     11da <unixsock_bind+0x7a>
mov    %ebx,%edi
mov    $0x5,%esi
call   1050 <listen@plt>
test   %rax,%rax
js     11e3 <unixsock_bind+0x83>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe28(%rip),%rdi
jmp    11ea <unixsock_bind+0x8a>
lea    0xe29(%rip),%rdi
jmp    11ea <unixsock_bind+0x8a>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11c5 <unixsock_bind+0x65>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"socket() \00"
@.str.1 = [8 x i8] c"bind() \00"
@.str.2 = [10 x i8] c"listen() \00"
define i32 @unixsock_bind(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %41
13: ; preds = %1
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = call i32 @strcpy(i32 %17, i8* %18)
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @strlen(i32 %21)
%23 = sext i32 %22 to i64
%24 = add i64 %23, 4
%25 = trunc i64 %24 to i32
store i32 %25, i32* %5
%26 = load i32, i32* %6
%27 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%28 = load i32, i32* %5
%29 = call i32 @bind(i32 %26, %struct.sockaddr* %27, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %13
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %41
33: ; preds = %13
%34 = load i32, i32* %6
%35 = call i64 @listen(i32 %34, i32 5)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %41
39: ; preds = %33
%40 = load i32, i32* %6
store i32 %40, i32* %2
br label %41
41: ; preds = %39, %37, %31, %11
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"socket() \00"
@.str.1 = [8 x i8] c"bind() \00"
@.str.2 = [10 x i8] c"listen() \00"
define i32 @unixsock_bind(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %26
10: ; preds = %1
%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 = tail call i32 @strcpy(i32 undef, i8* %0)
%14 = tail call i32 @strlen(i32 undef)
%15 = add i32 %14, 4
%16 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%17 = call i32 @bind(i32 %6, %struct.sockaddr* %16, i32 %15)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %26
21: ; preds = %10
%22 = call i64 @listen(i32 %6, i32 5)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %21, %24, %19, %8
%27 = phi i32 [ -1, %8 ], [ -1, %19 ], [ -1, %24 ], [ %6, %21 ]
ret i32 %27
}