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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int getsockname(int, struct sockaddr *, int *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
int ipc_sockname(int fd, char *sock, int size) {
  struct sockaddr_un addr = {};
  socklen_t addrlen = sizeof(addr);
  if (-1 == getsockname(fd, (struct sockaddr *)&addr, &addrlen))
    return -1;
  if (addrlen != sizeof(addr)) {
    errno = EINVAL;
    return -1;
  }
  size = size > strlen(addr.sun_path) ? strlen(addr.sun_path) : size;
  strncpy(sock, addr.sun_path, size);
  return 0;
}


x86 assembly (clang, -O0):
<ipc_sockname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <getsockname@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1194 <ipc_sockname+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <ipc_sockname+0xcd>
movslq -0x1c(%rbp),%rax
cmp    $0x4,%rax
je     11c0 <ipc_sockname+0x80>
mov    0x2e2f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <ipc_sockname+0xcd>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jle    11eb <ipc_sockname+0xab>
mov    -0x18(%rbp),%edi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
jmp    11f1 <ipc_sockname+0xb1>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_sockname>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
movl   $0x0,(%rsp)
movl   $0x4,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1050 <getsockname@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     11a0 <ipc_sockname+0x70>
cmpl   $0x4,0x4(%rsp)
jne    118e <ipc_sockname+0x5e>
mov    (%rsp),%edi
call   1040 <strlen@plt>
cmp    %ebp,%eax
jge    117d <ipc_sockname+0x4d>
mov    (%rsp),%edi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    (%rsp),%esi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
jmp    11a0 <ipc_sockname+0x70>
mov    0x2e43(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e42(%rip),%rdx
mov    %ecx,(%rdx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @ipc_sockname(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = bitcast %struct.sockaddr_un* %4 to i8*
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 0
store i32 0, i32* %7
%8 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%9 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%10 = call i32 @getsockname(i32 %0, %struct.sockaddr* %9, i32* %5)
%11 = icmp eq i32 %10, -1
br i1 %11, label %28, label %12
12: ; preds = %3
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 4
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = load i32, i32* @EINVAL
store i32 %16, i32* @errno
br label %28
17: ; preds = %12
%18 = load i32, i32* %7
%19 = call i32 @strlen(i32 %18)
%20 = icmp slt i32 %19, %2
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %7
%23 = call i32 @strlen(i32 %22)
br label %24
24: ; preds = %17, %21
%25 = phi i32 [ %23, %21 ], [ %2, %17 ]
%26 = load i32, i32* %7
%27 = call i32 @strncpy(i8* %1, i32 %26, i32 %25)
br label %28
28: ; preds = %3, %24, %15
%29 = phi i32 [ -1, %15 ], [ 0, %24 ], [ -1, %3 ]
ret i32 %29
}