#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_LOCAL;
char *get_current_dir_name();
int /*<<< orphan*/ sprintf(int /*<<< orphan*/, char *, char *, char const *);
int strlen(char const *);
int fill_sock_name(struct sockaddr_un *name, const char *filename) {
  char *cwd;
  cwd = get_current_dir_name();
  if (strlen(filename) + strlen(cwd) + 1 >= sizeof(name->sun_path))
    return -1;
  name->sun_family = AF_LOCAL;
  sprintf(name->sun_path, "%s/%s", cwd, filename);
  return 0;
}


x86 assembly (clang, -O0):
<fill_sock_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1040 <get_current_dir_name@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
cmp    $0x4,%rax
jb     1182 <fill_sock_name+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <fill_sock_name+0x83>
mov    0x2e5f(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe59(%rip),%rsi
call   1050 <sprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_sock_name>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <get_current_dir_name@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,1),%ecx
add    $0xfffffffd,%ecx
mov    $0xffffffff,%eax
cmp    $0xfffffffc,%ecx
jb     118e <fill_sock_name+0x5e>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%r14)
mov    (%r14),%edi
lea    0xe7f(%rip),%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1050 <sprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
@AF_LOCAL = global i32 0
@.str = [6 x i8] c"%s/%s\00"
define i32 @fill_sock_name(%struct.sockaddr_un* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_un*
%5 = alloca i8*
%6 = alloca i8*
store %struct.sockaddr_un* %0, %struct.sockaddr_un** %4
store i8* %1, i8** %5
%7 = call i8* (...) @get_current_dir_name()
store i8* %7, i8** %6
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
%10 = load i8*, i8** %6
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %9, %11
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = icmp uge i64 %14, 4
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %27
17: ; preds = %2
%18 = load i32, i32* @AF_LOCAL
%19 = load %struct.sockaddr_un*, %struct.sockaddr_un** %4
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %19, i32 0, i32 1
store i32 %18, i32* %20
%21 = load %struct.sockaddr_un*, %struct.sockaddr_un** %4
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i8*, i8** %6
%25 = load i8*, i8** %5
%26 = call i32 @sprintf(i32 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %24, i8* %25)
store i32 0, i32* %3
br label %27
27: ; preds = %17, %16
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
@AF_LOCAL = global i32 0
@.str = [6 x i8] c"%s/%s\00"
define i32 @fill_sock_name(%struct.sockaddr_un* nocapture %0, i8* %1) {
%3 = tail call i8* (...) @get_current_dir_name()
%4 = tail call i32 @strlen(i8* %1)
%5 = tail call i32 @strlen(i8* %3)
%6 = add i32 %4, -3
%7 = add i32 %6, %5
%8 = icmp ult i32 %7, -4
br i1 %8, label %15, label %9
9: ; preds = %2
%10 = load i32, i32* @AF_LOCAL
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %0, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %0, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i32 @sprintf(i32 %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %3, i8* %1)
br label %15
15: ; preds = %2, %9
%16 = phi i32 [ 0, %9 ], [ -1, %2 ]
ret i32 %16
}