#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 */

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, size_t, size_t);
char *getenv(char *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char const *const);
size_t strlen(char const *const);
int find_socket_path(char *socket_path, size_t socket_path_length) {
  static const char *const default_music_home = "/var/lib/witchcraft";
  static const char *const sockname = ".cli-server.sock";
  const char *music_home = getenv("WITCHCRAFT_MUSIC_HOME");
  if (!music_home) {
    music_home = default_music_home;
  }
  size_t music_home_size = strlen(music_home);
  size_t total_size = music_home_size + strlen(sockname) + 2;
  if (total_size > socket_path_length) {
    fprintf(stderr,
            "line %d: "
            "socket_path length cannot exceed %ld bytes, got: %ld"
            "\n",
            138, socket_path_length, total_size);
    return -1;
  }
  memcpy(socket_path, music_home, music_home_size);
  socket_path[music_home_size] = '/';
  strcpy(&socket_path[music_home_size + 1], sockname);
  return 0;
}


x86 assembly (clang, -O0):
<find_socket_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xebe(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1186 <find_socket_path+0x36>
lea    0xe7e(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x10,%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    11df <find_socket_path+0x8f>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    0xe72(%rip),%rsi
mov    $0x8a,%edx
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <find_socket_path+0xce>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
add    $0x1,%rax
add    %rax,%rdi
lea    0xe02(%rip),%rsi
call   1040 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_socket_path>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xec0(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe8c(%rip),%rbx
cmovne %rax,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x12(%rax),%r8
cmp    %r14,%r8
jbe    11ad <find_socket_path+0x5d>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
lea    0xea2(%rip),%rsi
mov    $0x8a,%edx
mov    %r14,%rcx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11d9 <find_socket_path+0x89>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1070 <memcpy@plt>
movb   $0x2f,(%r15,%r12,1)
lea    (%r15,%r12,1),%rdi
add    $0x1,%rdi
lea    0xe42(%rip),%rsi
call   1040 <strcpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@find_socket_path.default_music_home = internal constant i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i32 0, i32 0)
@.str = [20 x i8] c"/var/lib/witchcraft\00"
@find_socket_path.sockname = internal constant i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i32 0, i32 0)
@.str.1 = [17 x i8] c".cli-server.sock\00"
@.str.2 = [22 x i8] c"WITCHCRAFT_MUSIC_HOME\00"
@stderr = global i32 0
@.str.3 = [63 x i8] c"line %d: socket_path length cannot exceed %ld bytes, got: %ld\0A\00"
define i32 @find_socket_path(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = call i8* @getenv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i32 0, i32 0), i8** %6
br label %13
13: ; preds = %12, %2
%14 = load i8*, i8** %6
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = add i64 %16, 16
%18 = add i64 %17, 2
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = load i64, i64* %5
%21 = icmp ugt i64 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = load i64, i64* %5
%25 = load i64, i64* %8
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i32 138, i64 %24, i64 %25)
store i32 -1, i32* %3
br label %40
27: ; preds = %13
%28 = load i8*, i8** %4
%29 = load i8*, i8** %6
%30 = load i64, i64* %7
%31 = call i32 @memcpy(i8* %28, i8* %29, i64 %30)
%32 = load i8*, i8** %4
%33 = load i64, i64* %7
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8 47, i8* %34
%35 = load i8*, i8** %4
%36 = load i64, i64* %7
%37 = add i64 %36, 1
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = call i32 @strcpy(i8* %38, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i32 0, i32 0))
store i32 0, i32* %3
br label %40
40: ; preds = %27, %22
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [20 x i8] c"/var/lib/witchcraft\00"
@.str.1 = [17 x i8] c".cli-server.sock\00"
@.str.2 = [22 x i8] c"WITCHCRAFT_MUSIC_HOME\00"
@stderr = global i32 0
@.str.3 = [63 x i8] c"line %d: socket_path length cannot exceed %ld bytes, got: %ld\0A\00"
define i32 @find_socket_path(i8* %0, i64 %1) {
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%4 = icmp eq i8* %3, null
%5 = select i1 %4, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %3
%6 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%7 = add i64 %6, 18
%8 = icmp ugt i64 %7, %1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i32 138, i64 %1, i64 %7)
br label %18
12: ; preds = %2
%13 = tail call i32 @memcpy(i8* %0, i8* %5, i64 %6)
%14 = getelementptr inbounds i8, i8* %0, i64 %6
store i8 47, i8* %14
%15 = add i64 %6, 1
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = tail call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %12, %9
%19 = phi i32 [ -1, %9 ], [ 0, %12 ]
ret i32 %19
}