#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;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
void sun_set_path(struct sockaddr_un *sa, const char *Path) {
  int len, val;
  char *ptr1, *ptr2;
  off_t pos;
  memset(sa, 0, sizeof(struct sockaddr_un));
  sa->sun_family = AF_UNIX;
  ptr1 = sa->sun_path;
  ptr2 = (char *)sa;
  len = sizeof(struct sockaddr_un) - (ptr1 - ptr2);
  if (len > 0) {
    len--;
    val = ((Path) ? strlen(Path) : 0);
    if (val < len)
      len = val;
    strncpy(sa->sun_path, Path, len);
    sa->sun_path[len] = '\0';
  }
}


x86 assembly (clang, -O0):
<sun_set_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e91(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rax,%rcx
mov    $0x10,%eax
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11fc <sun_set_path+0xcc>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b8 <sun_set_path+0x88>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
jmp    11c2 <sun_set_path+0x92>
xor    %eax,%eax
mov    %eax,-0x30(%rbp)
jmp    11c2 <sun_set_path+0x92>
mov    -0x30(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11da <sun_set_path+0xaa>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sun_set_path>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%r15)
mov    (%r15),%rdi
mov    %r15d,%ebx
sub    %edi,%ebx
lea    0x10(%rbx),%eax
test   %eax,%eax
jle    1195 <sun_set_path+0x65>
add    $0xf,%ebx
test   %r14,%r14
je     117c <sun_set_path+0x4c>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    (%r15),%rdi
cmp    %ebx,%ebp
cmovl  %ebp,%ebx
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    (%r15),%rax
movslq %ebx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
@AF_UNIX = global i32 0
define void @sun_set_path(%struct.sockaddr_un* %0, i8* %1) {
%3 = alloca %struct.sockaddr_un*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store %struct.sockaddr_un* %0, %struct.sockaddr_un** %3
store i8* %1, i8** %4
%10 = load %struct.sockaddr_un*, %struct.sockaddr_un** %3
%11 = call i32 @memset(%struct.sockaddr_un* %10, i32 0, i32 16)
%12 = load i32, i32* @AF_UNIX
%13 = load %struct.sockaddr_un*, %struct.sockaddr_un** %3
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %13, i32 0, i32 1
store i32 %12, i32* %14
%15 = load %struct.sockaddr_un*, %struct.sockaddr_un** %3
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %15, i32 0, i32 0
%17 = load i8*, i8** %16
store i8* %17, i8** %7
%18 = load %struct.sockaddr_un*, %struct.sockaddr_un** %3
%19 = bitcast %struct.sockaddr_un* %18 to i8*
store i8* %19, i8** %8
%20 = load i8*, i8** %7
%21 = load i8*, i8** %8
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = sub i64 16, %24
%26 = trunc i64 %25 to i32
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %58
29: ; preds = %2
%30 = load i32, i32* %5
%31 = add nsw i32 %30, -1
store i32 %31, i32* %5
%32 = load i8*, i8** %4
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i8*, i8** %4
%36 = call i32 @strlen(i8* %35)
br label %38
37: ; preds = %29
br label %38
38: ; preds = %37, %34
%39 = phi i32 [ %36, %34 ], [ 0, %37 ]
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = load i32, i32* %5
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = load i32, i32* %6
store i32 %44, i32* %5
br label %45
45: ; preds = %43, %38
%46 = load %struct.sockaddr_un*, %struct.sockaddr_un** %3
%47 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = load i8*, i8** %4
%50 = load i32, i32* %5
%51 = call i32 @strncpy(i8* %48, i8* %49, i32 %50)
%52 = load %struct.sockaddr_un*, %struct.sockaddr_un** %3
%53 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %52, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = load i32, i32* %5
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
store i8 0, i8* %57
br label %58
58: ; preds = %45, %2
ret void
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
@AF_UNIX = global i32 0
define void @sun_set_path(%struct.sockaddr_un* %0, i8* %1) {
%3 = tail call i32 @memset(%struct.sockaddr_un* %0, i32 0, i32 16)
%4 = load i32, i32* @AF_UNIX
%5 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %0, i64 0, i32 1
store i32 %4, i32* %5
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %0, i64 0, i32 0
%7 = load i8*, i8** %6
%8 = ptrtoint i8* %7 to i64
%9 = ptrtoint %struct.sockaddr_un* %0 to i64
%10 = sub i64 %9, %8
%11 = trunc i64 %10 to i32
%12 = add i32 %11, 16
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %2
%15 = add nsw i32 %11, 15
%16 = icmp eq i8* %1, null
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = tail call i32 @strlen(i8* %1)
%19 = load i8*, i8** %6
br label %20
20: ; preds = %14, %17
%21 = phi i8* [ %19, %17 ], [ %7, %14 ]
%22 = phi i32 [ %18, %17 ], [ 0, %14 ]
%23 = icmp slt i32 %22, %15
%24 = select i1 %23, i32 %22, i32 %15
%25 = tail call i32 @strncpy(i8* %21, i8* %1, i32 %24)
%26 = load i8*, i8** %6
%27 = sext i32 %24 to i64
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %20, %2
ret void
}