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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ is_dir(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int exist_index_down(char *path, char *result) {
  DIR *current_dir = opendir(path);
  if (current_dir == NULL)
    return 0;
  struct dirent *direntp;
  while ((direntp = readdir(current_dir)) != NULL) {
    if (!strcmp(direntp->d_name, ".index")) {
      strcpy(result, path);
      closedir(current_dir);
      return 1;
    }
    char new_path[256];
    sprintf(new_path, "%s/%s", path, direntp->d_name);
    if (is_dir(new_path)) {
      if (!strcmp(direntp->d_name, ".") || !strcmp(direntp->d_name, ".."))
        continue;
      if (exist_index_down(new_path, result)) {
        closedir(current_dir);
        return 1;
      }
    }
  }
  closedir(current_dir);
  result = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<exist_index_down>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b7 <exist_index_down+0x37>
movl   $0x0,-0x4(%rbp)
jmp    12d2 <exist_index_down+0x152>
jmp    11bc <exist_index_down+0x3c>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12ba <exist_index_down+0x13a>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1f(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1211 <exist_index_down+0x91>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <exist_index_down+0x152>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xddd(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x130(%rbp),%rdi
call   1050 <is_dir@plt>
cmp    $0x0,%rax
je     12b5 <exist_index_down+0x135>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdbb(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
je     127d <exist_index_down+0xfd>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd9e(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1282 <exist_index_down+0x102>
jmp    11bc <exist_index_down+0x3c>
lea    -0x130(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <exist_index_down@plt>
cmp    $0x0,%eax
je     12b0 <exist_index_down+0x130>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <exist_index_down+0x152>
jmp    12b5 <exist_index_down+0x135>
jmp    11bc <exist_index_down+0x3c>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exist_index_down>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r12
call   1040 <opendir@plt>
mov    %rax,%rbx
xor    %eax,%eax
test   %rbx,%rbx
je     127e <exist_index_down+0xfe>
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1258 <exist_index_down+0xd8>
mov    %rax,%r15
lea    0x10(%rsp),%r14
jmp    11e0 <exist_index_down+0x60>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1258 <exist_index_down+0xd8>
mov    (%r15),%r13
mov    %r13,%rdi
lea    0xe13(%rip),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1264 <exist_index_down+0xe4>
mov    %r14,%rdi
lea    0xe07(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1090 <sprintf@plt>
mov    %r14,%rdi
call   1050 <is_dir@plt>
test   %rax,%rax
je     11d0 <exist_index_down+0x50>
mov    (%r15),%rbp
mov    %rbp,%rdi
lea    0xde9(%rip),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     11d0 <exist_index_down+0x50>
mov    %rbp,%rdi
lea    0xdd5(%rip),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     11d0 <exist_index_down+0x50>
mov    %r14,%rdi
mov    0x8(%rsp),%rsi
call   10a0 <exist_index_down@plt>
test   %eax,%eax
je     11d0 <exist_index_down+0x50>
jmp    1271 <exist_index_down+0xf1>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
jmp    127e <exist_index_down+0xfe>
mov    0x8(%rsp),%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1060 <closedir@plt>
mov    $0x1,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [7 x i8] c".index\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
define i32 @exist_index_down(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca [256 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 0, i32* %3
br label %67
14: ; preds = %2
br label %15
15: ; preds = %63, %53, %14
%16 = load i32*, i32** %6
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %7
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %64
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %7
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @strcmp(i8* %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%24 = icmp ne i32 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %19
%26 = load i8*, i8** %5
%27 = load i8*, i8** %4
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i32*, i32** %6
%30 = call i32 @closedir(i32* %29)
store i32 1, i32* %3
br label %67
31: ; preds = %19
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%33 = load i8*, i8** %4
%34 = load %struct.dirent*, %struct.dirent** %7
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %33, i8* %36)
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%39 = call i64 @is_dir(i8* %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %63
41: ; preds = %31
%42 = load %struct.dirent*, %struct.dirent** %7
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i32 @strcmp(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %41
%48 = load %struct.dirent*, %struct.dirent** %7
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 @strcmp(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%52 = icmp ne i32 %51, 0
br i1 %52, label %54, label %53
53: ; preds = %47, %41
br label %15
54: ; preds = %47
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%56 = load i8*, i8** %5
%57 = call i32 @exist_index_down(i8* %55, i8* %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = load i32*, i32** %6
%61 = call i32 @closedir(i32* %60)
store i32 1, i32* %3
br label %67
62: ; preds = %54
br label %63
63: ; preds = %62, %31
br label %15
64: ; preds = %15
%65 = load i32*, i32** %6
%66 = call i32 @closedir(i32* %65)
store i8* null, i8** %5
store i32 0, i32* %3
br label %67
67: ; preds = %64, %59, %25, %13
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [7 x i8] c".index\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
define i32 @exist_index_down(i8* %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %41, label %6
6: ; preds = %2
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %39, label %9
9: ; preds = %6
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
br label %11
11: ; preds = %9, %36
%12 = phi %struct.dirent* [ %7, %9 ], [ %37, %36 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = call i32 @strcmp(i8* dereferenceable(1) %14, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @strcpy(i8* %1, i8* %0)
%19 = call i32 @closedir(i32* %4)
br label %41
20: ; preds = %11
%21 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %14)
%22 = call i64 @is_dir(i8* %10)
%23 = icmp eq i64 %22, 0
br i1 %23, label %36, label %24
24: ; preds = %20
%25 = load i8*, i8** %13
%26 = call i32 @strcmp(i8* dereferenceable(1) %25, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i32 %26, 0
br i1 %27, label %36, label %28
28: ; preds = %24
%29 = call i32 @strcmp(i8* dereferenceable(1) %25, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i32 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %28
%32 = call i32 @exist_index_down(i8* %10, i8* %1)
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 @closedir(i32* %4)
br label %41
36: ; preds = %20, %31, %24, %28
%37 = call %struct.dirent* @readdir(i32* %4)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %39, label %11
39: ; preds = %36, %6
%40 = call i32 @closedir(i32* %4)
br label %41
41: ; preds = %34, %17, %39, %2
%42 = phi i32 [ 0, %2 ], [ 1, %17 ], [ 0, %39 ], [ 1, %34 ]
ret i32 %42
}