#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 */
char *dirname(char *);
int /*<<< orphan*/ empty_or_whitespace(char const *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ log_critical_perror(char *);
void *strdup(char const *);
int strlen(char const *);
const char *parent_dir(const char *directory) {
  if (directory == NULL || *directory != '/' || strlen(directory) <= 1)
    return NULL;
  char *input = strdup(directory);
  if (input == NULL)
    log_critical_perror("nyx: strdup");
  const char *parent = dirname(input);
  if (parent && *parent != '.' && !empty_or_whitespace(parent)) {
    const char *result = strdup(parent);
    free(input);
    return result;
  }
  free(input);
  return NULL;
}


x86 assembly (clang, -O0):
<parent_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1199 <parent_dir+0x39>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1199 <parent_dir+0x39>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x1,%eax
jg     11a6 <parent_dir+0x46>
movq   $0x0,-0x8(%rbp)
jmp    1238 <parent_dir+0xd8>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ca <parent_dir+0x6a>
lea    0xe3b(%rip),%rdi
call   1070 <log_critical_perror@plt>
mov    -0x18(%rbp),%rdi
call   1050 <dirname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1227 <parent_dir+0xc7>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     1227 <parent_dir+0xc7>
mov    -0x20(%rbp),%rdi
call   1060 <empty_or_whitespace@plt>
cmp    $0x0,%eax
jne    1227 <parent_dir+0xc7>
mov    -0x20(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1238 <parent_dir+0xd8>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parent_dir>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     117e <parent_dir+0x1e>
mov    %rdi,%rbx
cmpb   $0x2f,(%rdi)
jne    117e <parent_dir+0x1e>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jge    118b <parent_dir+0x2b>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
call   1080 <strdup@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11a7 <parent_dir+0x47>
lea    0xe5e(%rip),%rdi
call   1070 <log_critical_perror@plt>
mov    %r14,%rdi
call   1050 <dirname@plt>
test   %rax,%rax
je     11c8 <parent_dir+0x68>
mov    %rax,%rbx
cmpb   $0x2e,(%rax)
je     11c8 <parent_dir+0x68>
mov    %rbx,%rdi
call   1060 <empty_or_whitespace@plt>
test   %eax,%eax
je     11d4 <parent_dir+0x74>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1180 <parent_dir+0x20>
mov    %rbx,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
jmp    11ca <parent_dir+0x6a>

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

LLVM IR (-O2):
@.str = [12 x i8] c"nyx: strdup\00"
define noalias i8* @parent_dir(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %28, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 47
br i1 %5, label %6, label %28
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp slt i32 %7, 2
br i1 %8, label %28, label %9
9: ; preds = %6
%10 = tail call i8* @strdup(i8* %0)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @log_critical_perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = tail call i8* @dirname(i8* %10)
%16 = icmp eq i8* %15, null
br i1 %16, label %25, label %17
17: ; preds = %14
%18 = load i8, i8* %15
%19 = icmp eq i8 %18, 46
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = tail call i32 @empty_or_whitespace(i8* %15)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i8* @strdup(i8* %15)
br label %25
25: ; preds = %14, %17, %20, %23
%26 = phi i8* [ %24, %23 ], [ null, %20 ], [ null, %17 ], [ null, %14 ]
%27 = tail call i32 @free(i8* %10)
br label %28
28: ; preds = %25, %1, %3, %6
%29 = phi i8* [ null, %6 ], [ null, %3 ], [ null, %1 ], [ %26, %25 ]
ret i8* %29
}