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

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
size_t strlen(char const *);
int sockaddrpathfill(struct sockaddr_un *sa, const char *path) {
  size_t pl;
  pl = strlen(path);
  if (pl >= sizeof(sa->sun_path)) {
    return -1;
  }
  memset(sa, 0, sizeof(struct sockaddr_un));
  sa->sun_family = AF_LOCAL;
  memcpy(sa->sun_path, path, pl);
  sa->sun_path[pl] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<sockaddrpathfill>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x8,-0x20(%rbp)
jb     1164 <sockaddrpathfill+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ae <sockaddrpathfill+0x7e>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockaddrpathfill>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    $0xffffffff,%ebp
cmp    $0x7,%rax
ja     1186 <sockaddrpathfill+0x56>
mov    %rax,%rbx
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%r15)
mov    (%r15),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
mov    (%r15),%rax
movb   $0x0,(%rax,%rbx,1)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
@AF_LOCAL = global i32 0
define i32 @sockaddrpathfill(%struct.sockaddr_un* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_un*
%5 = alloca i8*
%6 = alloca i64
store %struct.sockaddr_un* %0, %struct.sockaddr_un** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp uge i64 %9, 8
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %29
12: ; preds = %2
%13 = load %struct.sockaddr_un*, %struct.sockaddr_un** %4
%14 = call i32 @memset(%struct.sockaddr_un* %13, i32 0, i32 16)
%15 = load i32, i32* @AF_LOCAL
%16 = load %struct.sockaddr_un*, %struct.sockaddr_un** %4
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %16, i32 0, i32 1
store i32 %15, i32* %17
%18 = load %struct.sockaddr_un*, %struct.sockaddr_un** %4
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %18, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = load i8*, i8** %5
%22 = load i64, i64* %6
%23 = call i32 @memcpy(i8* %20, i8* %21, i64 %22)
%24 = load %struct.sockaddr_un*, %struct.sockaddr_un** %4
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = load i64, i64* %6
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 0, i8* %28
store i32 0, i32* %3
br label %29
29: ; preds = %12, %11
%30 = load i32, i32* %3
ret i32 %30
}

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