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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, size_t);
scalar_t__ listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char const *);
int sock_prepare(const char const *path) {
  int sock;
  size_t sock_addr_len;
  struct sockaddr_un sock_addr = {0};
  sock_addr.sun_family = AF_UNIX;
  strcpy(sock_addr.sun_path, path);
  sock_addr_len = sizeof(sock_addr.sun_family) + strlen(sock_addr.sun_path);
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock == -1)
    return -1;
  unlink(path);
  if (bind(sock, (struct sockaddr *)&sock_addr, sock_addr_len))
    return -1;
  if (listen(sock, 5))
    return -1;
  return sock;
}


x86 assembly (clang, -O0):
<sock_prepare>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%edi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e8 <sock_prepare+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <sock_prepare+0xd0>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <bind@plt>
cmp    $0x0,%rax
je     1217 <sock_prepare+0xa7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <sock_prepare+0xd0>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
je     123a <sock_prepare+0xca>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <sock_prepare+0xd0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_prepare>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
movq   $0x0,(%rsp)
movl   $0x0,0x8(%rsp)
mov    0x2e62(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
xor    %edi,%edi
mov    %r14,%rsi
call   1040 <strcpy@plt>
xor    %edi,%edi
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    (%rbx),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     11f5 <sock_prepare+0x95>
mov    %eax,%ebx
movslq %r15d,%rbp
add    $0x4,%rbp
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1070 <bind@plt>
test   %rax,%rax
jne    11f5 <sock_prepare+0x95>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
neg    %rax
sbb    %r12d,%r12d
or     %ebx,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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