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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
void prepare_connection(const char *socket_path, int *soc,
                        struct sockaddr_un *addr) {
  if ((*soc = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    perror("Cannot create socket");
    exit(EXIT_FAILURE);
  }
  memset(addr, 0, sizeof(*addr));
  addr->sun_family = AF_UNIX;
  strncpy(addr->sun_path, socket_path, sizeof(addr->sun_path) - 1);
}


x86 assembly (clang, -O0):
<prepare_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    -0x10(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    11a6 <prepare_connection+0x56>
lea    0xe6d(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_connection>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e80(%rip),%r12
mov    (%r12),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,(%r15)
cmp    $0xffffffff,%eax
je     11b4 <prepare_connection+0x64>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x4(%rbx)
mov    (%rbx),%edi
mov    %r14,%rsi
mov    $0x3,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <strncpy@plt>
lea    0xe45(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"Cannot create socket\00"
@EXIT_FAILURE = global i32 0
define void @prepare_connection(i8* %0, i32* %1, %struct.sockaddr_un* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.sockaddr_un*
store i8* %0, i8** %4
store i32* %1, i32** %5
store %struct.sockaddr_un* %2, %struct.sockaddr_un** %6
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = load i32*, i32** %5
store i32 %9, i32* %10
%11 = icmp eq i32 %9, -1
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %3
%17 = load %struct.sockaddr_un*, %struct.sockaddr_un** %6
%18 = call i32 @memset(%struct.sockaddr_un* %17, i32 0, i32 8)
%19 = load i32, i32* @AF_UNIX
%20 = load %struct.sockaddr_un*, %struct.sockaddr_un** %6
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %20, i32 0, i32 1
store i32 %19, i32* %21
%22 = load %struct.sockaddr_un*, %struct.sockaddr_un** %6
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i8*, i8** %4
%26 = call i32 @strncpy(i32 %24, i8* %25, i32 3)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"Cannot create socket\00"
@EXIT_FAILURE = global i32 0
define void @prepare_connection(i8* %0, i32* nocapture writeonly %1, %struct.sockaddr_un* %2) {
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %1
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %3
%13 = tail call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @strncpy(i32 %17, i8* %0, i32 3)
ret void
}