#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*/ memcpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int strlen(char const *);
int OS_BuildSockAddrUn(const char *bindPath, struct sockaddr_un *servAddrPtr,
                       int *servAddrLen) {
  int bindPathLen = strlen(bindPath);
  if (bindPathLen > sizeof(servAddrPtr->sun_path)) {
    return -1;
  }
  memset((char *)servAddrPtr, 0, sizeof(*servAddrPtr));
  servAddrPtr->sun_family = AF_UNIX;
  memcpy(servAddrPtr->sun_path, bindPath, bindPathLen);
  *servAddrLen = sizeof(servAddrPtr->sun_family) + bindPathLen;
  return 0;
}


x86 assembly (clang, -O0):
<OS_BuildSockAddrUn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0x4,%rax
jbe    116a <OS_BuildSockAddrUn+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <OS_BuildSockAddrUn+0x83>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
movslq -0x24(%rbp),%rax
add    $0x4,%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OS_BuildSockAddrUn>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    $0xffffffff,%r15d
cmp    $0x4,%eax
ja     1183 <OS_BuildSockAddrUn+0x53>
mov    %eax,%ebp
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    (%rbx),%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
add    $0x4,%ebp
mov    %ebp,(%r14)
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
@AF_UNIX = global i32 0
define i32 @OS_BuildSockAddrUn(i8* %0, %struct.sockaddr_un* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.sockaddr_un*
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %5
store %struct.sockaddr_un* %1, %struct.sockaddr_un** %6
store i32* %2, i32** %7
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = sext i32 %11 to i64
%13 = icmp ugt i64 %12, 4
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %33
15: ; preds = %3
%16 = load %struct.sockaddr_un*, %struct.sockaddr_un** %6
%17 = bitcast %struct.sockaddr_un* %16 to i8*
%18 = call i32 @memset(i8* %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** %5
%26 = load i32, i32* %8
%27 = call i32 @memcpy(i32 %24, i8* %25, i32 %26)
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = add i64 4, %29
%31 = trunc i64 %30 to i32
%32 = load i32*, i32** %7
store i32 %31, i32* %32
store i32 0, i32* %4
br label %33
33: ; preds = %15, %14
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
@AF_UNIX = global i32 0
define i32 @OS_BuildSockAddrUn(i8* %0, %struct.sockaddr_un* %1, i32* nocapture writeonly %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp ugt i32 %4, 4
br i1 %5, label %15, label %6
6: ; preds = %3
%7 = bitcast %struct.sockaddr_un* %1 to i8*
%8 = tail call i32 @memset(i8* %7, i32 0, i32 8)
%9 = load i32, i32* @AF_UNIX
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i32 @memcpy(i32 %12, i8* %0, i32 %4)
%14 = add nuw nsw i32 %4, 4
store i32 %14, i32* %2
br label %15
15: ; preds = %3, %6
%16 = phi i32 [ 0, %6 ], [ -1, %3 ]
ret i32 %16
}