#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 */
scalar_t__ file_exists(char const *);
int /*<<< orphan*/ free(void *);
char *local_socket_path(char const *);
int /*<<< orphan*/ log_critical_perror(char *);
char *parent_dir(char const *);
char *strdup(char const *);
const char *find_local_socket_path(const char *start_dir) {
  if (start_dir == NULL || *start_dir == '\0')
    return NULL;
  int max_depth = 8;
  const char *dir = strdup(start_dir);
  if (dir == NULL)
    log_critical_perror("nyx: strdup");
  while (max_depth-- > 0 && dir != NULL) {
    const char *local_dir = local_socket_path(dir);
    if (file_exists(local_dir)) {
      free((void *)dir);
      return local_dir;
    }
    if (local_dir != NULL) {
      free((void *)local_dir);
    }
    const char *parent = parent_dir(dir);
    free((void *)dir);
    dir = parent;
  }
  if (dir)
    free((void *)dir);
  return NULL;
}


x86 assembly (clang, -O0):
<find_local_socket_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1187 <find_local_socket_path+0x27>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1194 <find_local_socket_path+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1281 <find_local_socket_path+0x121>
movl   $0x8,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bf <find_local_socket_path+0x5f>
lea    0xe46(%rip),%rdi
call   1070 <log_critical_perror@plt>
jmp    11c4 <find_local_socket_path+0x64>
mov    -0x14(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
jle    11e8 <find_local_socket_path+0x88>
cmpq   $0x0,-0x20(%rbp)
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11f8 <find_local_socket_path+0x98>
jmp    1265 <find_local_socket_path+0x105>
mov    -0x20(%rbp),%rdi
call   1050 <local_socket_path@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <file_exists@plt>
cmp    $0x0,%rax
je     122e <find_local_socket_path+0xce>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1281 <find_local_socket_path+0x121>
cmpq   $0x0,-0x28(%rbp)
je     1242 <find_local_socket_path+0xe2>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1060 <parent_dir@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11c4 <find_local_socket_path+0x64>
cmpq   $0x0,-0x20(%rbp)
je     1279 <find_local_socket_path+0x119>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_local_socket_path>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     11e2 <find_local_socket_path+0x82>
cmpb   $0x0,(%rdi)
je     11e2 <find_local_socket_path+0x82>
call   1080 <strdup@plt>
test   %rax,%rax
je     11d6 <find_local_socket_path+0x76>
mov    %rax,%r14
mov    $0xfffffff9,%ebp
mov    %r14,%rdi
call   1050 <local_socket_path@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <file_exists@plt>
test   %rax,%rax
jne    11ec <find_local_socket_path+0x8c>
test   %rbx,%rbx
je     11a5 <find_local_socket_path+0x45>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1060 <parent_dir@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
je     11c7 <find_local_socket_path+0x67>
add    $0x1,%ebp
mov    %rbx,%r14
test   %rbx,%rbx
jne    1180 <find_local_socket_path+0x20>
test   %rbx,%rbx
je     11e2 <find_local_socket_path+0x82>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11e2 <find_local_socket_path+0x82>
lea    0xe23(%rip),%rdi
call   1070 <log_critical_perror@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11e4 <find_local_socket_path+0x84>

LLVM IR (-O0):
@.str = [12 x i8] c"nyx: strdup\00"
define i8* @find_local_socket_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %10, %1
store i8* null, i8** %2
br label %62
16: ; preds = %10
store i32 8, i32* %4
%17 = load i8*, i8** %3
%18 = call i8* @strdup(i8* %17)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @log_critical_perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
br label %24
24: ; preds = %49, %23
%25 = load i32, i32* %4
%26 = add nsw i32 %25, -1
store i32 %26, i32* %4
%27 = icmp sgt i32 %25, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i8*, i8** %5
%30 = icmp ne i8* %29, null
br label %31
31: ; preds = %28, %24
%32 = phi i1 [ false, %24 ], [ %30, %28 ]
br i1 %32, label %33, label %55
33: ; preds = %31
%34 = load i8*, i8** %5
%35 = call i8* @local_socket_path(i8* %34)
store i8* %35, i8** %6
%36 = load i8*, i8** %6
%37 = call i64 @file_exists(i8* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %33
%40 = load i8*, i8** %5
%41 = call i32 @free(i8* %40)
%42 = load i8*, i8** %6
store i8* %42, i8** %2
br label %62
43: ; preds = %33
%44 = load i8*, i8** %6
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i8*, i8** %6
%48 = call i32 @free(i8* %47)
br label %49
49: ; preds = %46, %43
%50 = load i8*, i8** %5
%51 = call i8* @parent_dir(i8* %50)
store i8* %51, i8** %7
%52 = load i8*, i8** %5
%53 = call i32 @free(i8* %52)
%54 = load i8*, i8** %7
store i8* %54, i8** %5
br label %24
55: ; preds = %31
%56 = load i8*, i8** %5
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = load i8*, i8** %5
%60 = call i32 @free(i8* %59)
br label %61
61: ; preds = %58, %55
store i8* null, i8** %2
br label %62
62: ; preds = %61, %39, %15
%63 = load i8*, i8** %2
ret i8* %63
}

LLVM IR (-O2):
@.str = [12 x i8] c"nyx: strdup\00"
define i8* @find_local_socket_path(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %33, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %33, label %6
6: ; preds = %3
%7 = tail call i8* @strdup(i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @log_critical_perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %33
11: ; preds = %6, %23
%12 = phi i32 [ %26, %23 ], [ 7, %6 ]
%13 = phi i8* [ %24, %23 ], [ %7, %6 ]
%14 = tail call i8* @local_socket_path(i8* %13)
%15 = tail call i64 @file_exists(i8* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %11
%18 = tail call i32 @free(i8* %13)
br label %33
19: ; preds = %11
%20 = icmp eq i8* %14, null
br i1 %20, label %23, label %21
21: ; preds = %19
%22 = tail call i32 @free(i8* %14)
br label %23
23: ; preds = %19, %21
%24 = tail call i8* @parent_dir(i8* %13)
%25 = tail call i32 @free(i8* %13)
%26 = add nsw i32 %12, -1
%27 = icmp ne i32 %12, 0
%28 = icmp ne i8* %24, null
%29 = select i1 %27, i1 %28, i1 false
br i1 %29, label %11, label %30
30: ; preds = %23
br i1 %28, label %31, label %33
31: ; preds = %30
%32 = tail call i32 @free(i8* %24)
br label %33
33: ; preds = %9, %17, %31, %30, %1, %3
%34 = phi i8* [ null, %3 ], [ null, %1 ], [ null, %31 ], [ null, %30 ], [ %14, %17 ], [ null, %9 ]
ret i8* %34
}