#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 */
typedef scalar_t__ int32_t;

/* Variables and functions */
int /*<<< orphan*/ W_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ log_critical_perror(char *);
int /*<<< orphan*/ log_debug(char *, char const *);
int /*<<< orphan*/ log_error(char *);
scalar_t__ mkdir_p(char const *);
int /*<<< orphan*/ prepare_dir(char const *);
char *strdup(int /*<<< orphan*/);
const char *determine_pid_dir_from(const char **dir_candidates) {
  while (*dir_candidates) {
    const char *prepared = strdup(prepare_dir(*dir_candidates));
    if (prepared == NULL) {
      log_critical_perror("nyx: strdup");
    }
    if (mkdir_p(prepared)) {
      int32_t result = access(prepared, W_OK);
      if (result == 0) {
        log_debug("Using '%s' as nyx PID directory", prepared);
        return prepared;
      }
    }
    free((void *)prepared);
    dir_candidates++;
  }
  log_error("Failed to determine a PID directory for nyx");
  return NULL;
}


x86 assembly (clang, -O0):
<determine_pid_dir_from>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1238 <determine_pid_dir_from+0xb8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <prepare_dir@plt>
mov    %eax,%edi
call   10a0 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <determine_pid_dir_from+0x48>
lea    0xe3d(%rip),%rdi
call   1070 <log_critical_perror@plt>
mov    -0x18(%rbp),%rdi
call   1060 <mkdir_p@plt>
cmp    $0x0,%rax
je     121e <determine_pid_dir_from+0x9e>
mov    -0x18(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1219 <determine_pid_dir_from+0x99>
mov    -0x18(%rbp),%rsi
lea    0xe05(%rip),%rdi
call   1050 <log_debug@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    124c <determine_pid_dir_from+0xcc>
jmp    121e <determine_pid_dir_from+0x9e>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
jmp    118c <determine_pid_dir_from+0xc>
lea    0xded(%rip),%rdi
call   1040 <log_error@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<determine_pid_dir_from>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rdi,%rdi
je     120f <determine_pid_dir_from+0x8f>
add    $0x8,%rbx
lea    0xe62(%rip),%r14
mov    0x2e43(%rip),%r12
jmp    11c4 <determine_pid_dir_from+0x44>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <free@plt>
mov    (%rbx),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
je     120f <determine_pid_dir_from+0x8f>
call   1090 <prepare_dir@plt>
mov    %eax,%edi
call   10a0 <strdup@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11e0 <determine_pid_dir_from+0x60>
mov    %r14,%rdi
call   1070 <log_critical_perror@plt>
mov    %r15,%rdi
call   1060 <mkdir_p@plt>
test   %rax,%rax
je     11b0 <determine_pid_dir_from+0x30>
mov    (%r12),%esi
mov    %r15,%rdi
call   1080 <access@plt>
test   %rax,%rax
jne    11b0 <determine_pid_dir_from+0x30>
lea    0xe07(%rip),%rdi
mov    %r15,%rsi
call   1050 <log_debug@plt>
jmp    121e <determine_pid_dir_from+0x9e>
lea    0xe16(%rip),%rdi
call   1040 <log_error@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"nyx: strdup\00"
@W_OK = global i32 0
@.str.1 = [32 x i8] c"Using '%s' as nyx PID directory\00"
@.str.2 = [44 x i8] c"Failed to determine a PID directory for nyx\00"
define i8* @determine_pid_dir_from(i8** %0) {
%2 = alloca i8*
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i64
store i8** %0, i8*** %3
br label %6
6: ; preds = %34, %1
%7 = load i8**, i8*** %3
%8 = load i8*, i8** %7
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %39
10: ; preds = %6
%11 = load i8**, i8*** %3
%12 = load i8*, i8** %11
%13 = call i32 @prepare_dir(i8* %12)
%14 = call i8* @strdup(i32 %13)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = call i32 @log_critical_perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %10
%20 = load i8*, i8** %4
%21 = call i64 @mkdir_p(i8* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %19
%24 = load i8*, i8** %4
%25 = load i32, i32* @W_OK
%26 = call i64 @access(i8* %24, i32 %25)
store i64 %26, i64* %5
%27 = load i64, i64* %5
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i8*, i8** %4
%31 = call i32 @log_debug(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %4
store i8* %32, i8** %2
br label %41
33: ; preds = %23
br label %34
34: ; preds = %33, %19
%35 = load i8*, i8** %4
%36 = call i32 @free(i8* %35)
%37 = load i8**, i8*** %3
%38 = getelementptr inbounds i8*, i8** %37, i32 1
store i8** %38, i8*** %3
br label %6
39: ; preds = %6
%40 = call i32 @log_error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %41
41: ; preds = %39, %29
%42 = load i8*, i8** %2
ret i8* %42
}

LLVM IR (-O2):
@.str = [12 x i8] c"nyx: strdup\00"
@W_OK = global i32 0
@.str.1 = [32 x i8] c"Using '%s' as nyx PID directory\00"
@.str.2 = [44 x i8] c"Failed to determine a PID directory for nyx\00"
define i8* @determine_pid_dir_from(i8** nocapture %0) {
%2 = load i8*, i8** %0
%3 = icmp eq i8* %2, null
br i1 %3, label %26, label %4
4: ; preds = %1, %21
%5 = phi i8* [ %24, %21 ], [ %2, %1 ]
%6 = phi i8** [ %23, %21 ], [ %0, %1 ]
%7 = tail call i32 @prepare_dir(i8* %5)
%8 = tail call i8* @strdup(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @log_critical_perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %4
%13 = tail call i64 @mkdir_p(i8* %8)
%14 = icmp eq i64 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %12
%16 = load i32, i32* @W_OK
%17 = tail call i64 @access(i8* %8, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @log_debug(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %28
21: ; preds = %12, %15
%22 = tail call i32 @free(i8* %8)
%23 = getelementptr inbounds i8*, i8** %6, i64 1
%24 = load i8*, i8** %23
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %4
26: ; preds = %21, %1
%27 = tail call i32 @log_error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %19, %26
%29 = phi i8* [ null, %26 ], [ %8, %19 ]
ret i8* %29
}