#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 stack {
  int dummy;
};
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ dtree_error_from_errno();
scalar_t__ dtree_iserror();
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ *go_next_dir(int /*<<< orphan*/ *, struct stack **);
int /*<<< orphan*/ *opendir_on_stack(struct stack **);
struct dirent *readdir(int /*<<< orphan*/ *);
int stack_depth(struct stack **);
scalar_t__ stack_pop_fname(struct stack **);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char const *);
DIR *go_up_next_dir(struct stack **path) {
  DIR *next = NULL;
  do {
    if (stack_depth(path) == 1)
      return NULL;
    const char *dname = (const char *)stack_pop_fname(path);
    DIR *dir = opendir_on_stack(path);
    if (dir == NULL) {
      dtree_error_from_errno();
      free((void *)dname);
      return NULL;
    }
    struct dirent *d;
    while ((d = readdir(dir)) != NULL) {
      if (!strcmp(d->d_name, dname))
        break;
    }
    free((void *)dname);
    next = go_next_dir(dir, path);
    if (next == NULL && dtree_iserror()) {
      closedir(dir);
      return NULL;
    }
    closedir(dir);
  } while (next == NULL);
  return next;
}


x86 assembly (clang, -O0):
<go_up_next_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <stack_depth@plt>
cmp    $0x1,%eax
jne    11d3 <go_up_next_dir+0x33>
movq   $0x0,-0x8(%rbp)
jmp    12bb <go_up_next_dir+0x11b>
mov    -0x10(%rbp),%rdi
call   10b0 <stack_pop_fname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <opendir_on_stack@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1215 <go_up_next_dir+0x75>
mov    $0x0,%al
call   10c0 <dtree_error_from_errno@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bb <go_up_next_dir+0x11b>
jmp    121a <go_up_next_dir+0x7a>
mov    -0x28(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1253 <go_up_next_dir+0xb3>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    124e <go_up_next_dir+0xae>
jmp    1253 <go_up_next_dir+0xb3>
jmp    121a <go_up_next_dir+0x7a>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <go_next_dir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    129f <go_up_next_dir+0xff>
mov    $0x0,%al
call   10a0 <dtree_iserror@plt>
cmp    $0x0,%rax
je     129f <go_up_next_dir+0xff>
mov    -0x28(%rbp),%rdi
call   1040 <closedir@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bb <go_up_next_dir+0x11b>
mov    -0x28(%rbp),%rdi
call   1040 <closedir@plt>
cmpq   $0x0,-0x18(%rbp)
je     11b4 <go_up_next_dir+0x14>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<go_up_next_dir>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1070 <stack_depth@plt>
cmp    $0x1,%eax
je     121f <go_up_next_dir+0x7f>
mov    %r14,%rdi
call   10b0 <stack_pop_fname@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1090 <opendir_on_stack@plt>
mov    %rax,%r15
test   %rax,%rax
je     1231 <go_up_next_dir+0x91>
mov    %r15,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11eb <go_up_next_dir+0x4b>
mov    (%rax),%edi
mov    %rbx,%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
jne    11d0 <go_up_next_dir+0x30>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <go_next_dir@plt>
test   %rax,%rax
jne    1245 <go_up_next_dir+0xa5>
xor    %eax,%eax
call   10a0 <dtree_iserror@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <closedir@plt>
test   %rbx,%rbx
jne    121f <go_up_next_dir+0x7f>
mov    %r14,%rdi
jmp    11ab <go_up_next_dir+0xb>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %r12d,%r12d
xor    %eax,%eax
call   10c0 <dtree_error_from_errno@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1222 <go_up_next_dir+0x82>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <closedir@plt>
jmp    1222 <go_up_next_dir+0x82>

LLVM IR (-O0):
%struct.stack = type { i32 }
%struct.dirent = type { i32 }
define i32* @go_up_next_dir(%struct.stack** %0) {
%2 = alloca i32*
%3 = alloca %struct.stack**
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
store %struct.stack** %0, %struct.stack*** %3
store i32* null, i32** %4
br label %8
8: ; preds = %56, %1
%9 = load %struct.stack**, %struct.stack*** %3
%10 = call i32 @stack_depth(%struct.stack** %9)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %13
12: ; preds = %8
store i32* null, i32** %2
br label %61
13: ; preds = %8
%14 = load %struct.stack**, %struct.stack*** %3
%15 = call i64 @stack_pop_fname(%struct.stack** %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %5
%17 = load %struct.stack**, %struct.stack*** %3
%18 = call i32* @opendir_on_stack(%struct.stack** %17)
store i32* %18, i32** %6
%19 = load i32*, i32** %6
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %25
21: ; preds = %13
%22 = call i32 (...) @dtree_error_from_errno()
%23 = load i8*, i8** %5
%24 = call i32 @free(i8* %23)
store i32* null, i32** %2
br label %61
25: ; preds = %13
br label %26
26: ; preds = %38, %25
%27 = load i32*, i32** %6
%28 = call %struct.dirent* @readdir(i32* %27)
store %struct.dirent* %28, %struct.dirent** %7
%29 = icmp ne %struct.dirent* %28, null
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = load %struct.dirent*, %struct.dirent** %7
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = load i8*, i8** %5
%35 = call i32 @strcmp(i32 %33, i8* %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %30
br label %39
38: ; preds = %30
br label %26
39: ; preds = %37, %26
%40 = load i8*, i8** %5
%41 = call i32 @free(i8* %40)
%42 = load i32*, i32** %6
%43 = load %struct.stack**, %struct.stack*** %3
%44 = call i32* @go_next_dir(i32* %42, %struct.stack** %43)
store i32* %44, i32** %4
%45 = load i32*, i32** %4
%46 = icmp eq i32* %45, null
br i1 %46, label %47, label %53
47: ; preds = %39
%48 = call i64 (...) @dtree_iserror()
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32*, i32** %6
%52 = call i32 @closedir(i32* %51)
store i32* null, i32** %2
br label %61
53: ; preds = %47, %39
%54 = load i32*, i32** %6
%55 = call i32 @closedir(i32* %54)
br label %56
56: ; preds = %53
%57 = load i32*, i32** %4
%58 = icmp eq i32* %57, null
br i1 %58, label %8, label %59
59: ; preds = %56
%60 = load i32*, i32** %4
store i32* %60, i32** %2
br label %61
61: ; preds = %59, %50, %21, %12
%62 = load i32*, i32** %2
ret i32* %62
}

LLVM IR (-O2):
%struct.stack = type { i32 }
%struct.dirent = type { i32 }
define i32* @go_up_next_dir(%struct.stack** %0) {
%2 = tail call i32 @stack_depth(%struct.stack** %0)
%3 = icmp eq i32 %2, 1
br i1 %3, label %34, label %4
4: ; preds = %1, %31
%5 = tail call i64 @stack_pop_fname(%struct.stack** %0)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32* @opendir_on_stack(%struct.stack** %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %13
9: ; preds = %4
%10 = inttoptr i64 %5 to i8*
%11 = tail call i32 (...) @dtree_error_from_errno()
%12 = tail call i32 @free(i8* %10)
br label %34
13: ; preds = %4, %16
%14 = tail call %struct.dirent* @readdir(i32* %7)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i32 @strcmp(i32 %18, i8* %6)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %13
21: ; preds = %16, %13
%22 = tail call i32 @free(i8* %6)
%23 = tail call i32* @go_next_dir(i32* %7, %struct.stack** %0)
%24 = icmp eq i32* %23, null
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = tail call i32 @closedir(i32* %7)
br label %34
27: ; preds = %21
%28 = tail call i64 (...) @dtree_iserror()
%29 = icmp eq i64 %28, 0
%30 = tail call i32 @closedir(i32* %7)
br i1 %29, label %31, label %34
31: ; preds = %27
%32 = tail call i32 @stack_depth(%struct.stack** %0)
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %4
34: ; preds = %31, %27, %1, %9, %25
%35 = phi i32* [ %23, %25 ], [ null, %9 ], [ null, %1 ], [ null, %27 ], [ null, %31 ]
ret i32* %35
}