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

/* Variables and functions */
int AF_UNIX;
int bind(int, struct sockaddr const *, size_t const);
int errno;
int listen(int, int);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int socket_listen_unix(int fd, struct sockaddr_un *addr, const char *path,
                       int backlog) {
  const int addr_family = AF_UNIX;
  addr->sun_family = addr_family;
  strncpy(addr->sun_path, path, strlen(path));
  const size_t addrsize = sizeof(struct sockaddr_un);
  int err = bind(fd, (const struct sockaddr *)addr, addrsize);
  if (err == -1)
    return errno;
  err = listen(fd, backlog);
  if (err == -1)
    return errno;
  return 0;
}


x86 assembly (clang, -O0):
<socket_listen_unix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
movq   $0x8,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11cb <socket_listen_unix+0x8b>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11fb <socket_listen_unix+0xbb>
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1050 <listen@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11f4 <socket_listen_unix+0xb4>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11fb <socket_listen_unix+0xbb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_listen_unix>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsi)
mov    0x4(%rsi),%r12d
mov    %rdx,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    $0x8,%edx
mov    %r15d,%edi
mov    %rbp,%rsi
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     11a1 <socket_listen_unix+0x61>
mov    %r15d,%edi
mov    %r14d,%esi
call   1050 <listen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11aa <socket_listen_unix+0x6a>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@errno = global i32 0
define i32 @socket_listen_unix(i32 %0, %struct.sockaddr_un* %1, i8* %2, i32 %3) {
%5 = load i32, i32* @AF_UNIX
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
%8 = load i32, i32* %7
%9 = tail call i32 @strlen(i8* %2)
%10 = tail call i32 @strncpy(i32 %8, i8* %2, i32 %9)
%11 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%12 = tail call i32 @bind(i32 %0, %struct.sockaddr* %11, i64 8)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = load i32, i32* @errno
br label %21
16: ; preds = %4
%17 = tail call i32 @listen(i32 %0, i32 %3)
%18 = icmp eq i32 %17, -1
%19 = load i32, i32* @errno
%20 = select i1 %18, i32 %19, i32 0
br label %21
21: ; preds = %16, %14
%22 = phi i32 [ %15, %14 ], [ %20, %16 ]
ret i32 %22
}