#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*/ chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ getcwd(char *, int);
char *getenv(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int exist_index_up(char *path, char *result) {
  char *aux_path, tmp_path[256];
  aux_path = getenv("PWD");
  getcwd(tmp_path, 256);
  while (strcmp(tmp_path, "/")) {
    chdir("..");
    getcwd(tmp_path, 256);
    DIR *dir = opendir(tmp_path);
    struct dirent *direntp;
    while ((direntp = readdir(dir)) != NULL) {
      if (!strcmp(direntp->d_name, ".index")) {
        strcpy(result, tmp_path);
        closedir(dir);
        return 1;
      }
    }
    closedir(dir);
  }
  chdir(aux_path);
  result = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<exist_index_up>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe66(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
lea    -0x120(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     127c <exist_index_up+0xfc>
lea    0xe2e(%rip),%rdi
call   1060 <chdir@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
lea    -0x120(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x130(%rbp)
cmp    $0x0,%rax
je     126b <exist_index_up+0xeb>
mov    -0x130(%rbp),%rax
mov    (%rax),%rdi
lea    0xdda(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1266 <exist_index_up+0xe6>
mov    -0x18(%rbp),%rdi
lea    -0x120(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x128(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    1294 <exist_index_up+0x114>
jmp    1201 <exist_index_up+0x81>
mov    -0x128(%rbp),%rdi
call   1080 <closedir@plt>
jmp    11b4 <exist_index_up+0x34>
mov    -0x20(%rbp),%rdi
call   1060 <chdir@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_up>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%r12
lea    0xe65(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
lea    0xe4a(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     123c <exist_index_up+0xbc>
lea    0xe38(%rip),%r15
mov    %rsp,%rbx
lea    0xe31(%rip),%rbp
mov    %r15,%rdi
call   1060 <chdir@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
mov    %rbx,%rdi
call   1050 <opendir@plt>
mov    %rax,%r13
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1220 <exist_index_up+0xa0>
mov    (%rax),%rdi
mov    %rbp,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1200 <exist_index_up+0x80>
jmp    1248 <exist_index_up+0xc8>
nop
mov    %r13,%rdi
call   1080 <closedir@plt>
mov    %rbx,%rdi
lea    0xdd2(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    11d8 <exist_index_up+0x58>
mov    %r14,%rdi
call   1060 <chdir@plt>
xor    %eax,%eax
jmp    1260 <exist_index_up+0xe0>
mov    %rsp,%rsi
mov    %r12,%rdi
call   1040 <strcpy@plt>
mov    %r13,%rdi
call   1080 <closedir@plt>
mov    $0x1,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [4 x i8] c"PWD\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [7 x i8] c".index\00"
define i32 @exist_index_up(i8* nocapture readnone %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @getcwd(i8* %4, i32 256)
%7 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %28, label %9
9: ; preds = %2, %24
%10 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @getcwd(i8* %4, i32 256)
%12 = call i32* @opendir(i8* %4)
br label %13
13: ; preds = %16, %9
%14 = call %struct.dirent* @readdir(i32* %12)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %24, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %13
21: ; preds = %16
%22 = call i32 @strcpy(i8* %1, i8* %4)
%23 = call i32 @closedir(i32* %12)
br label %30
24: ; preds = %13
%25 = call i32 @closedir(i32* %12)
%26 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %9
28: ; preds = %24, %2
%29 = call i32 @chdir(i8* %5)
br label %30
30: ; preds = %21, %28
%31 = phi i32 [ 0, %28 ], [ 1, %21 ]
ret i32 %31
}