#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*/ *);
char *malloc(scalar_t__);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char const *);
scalar_t__ strcmp(char *, char *);
scalar_t__ strlen(char const *);
char *indexes(const char *path) {
  DIR *directory;
  struct dirent *dir_item;
  char *full_path = NULL;
  directory = opendir(path);
  if (directory != NULL) {
    while ((dir_item = readdir(directory)) != NULL) {
      char *file_name = dir_item->d_name;
      if (strcmp(file_name, "index.html") == 0 ||
          strcmp(file_name, "index.php") == 0) {
        full_path = malloc(strlen(file_name) + strlen(path) + 1);
        full_path[0] = '\0';
        strcat(full_path, path);
        strcat(full_path, file_name);
        break;
      }
    }
    closedir(directory);
    return full_path;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<indexes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1262 <indexes+0xf2>
jmp    11a1 <indexes+0x31>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     124c <indexes+0xdc>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11f7 <indexes+0x87>
mov    -0x30(%rbp),%rdi
lea    0xe23(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1247 <indexes+0xd7>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1090 <strcat@plt>
jmp    124c <indexes+0xdc>
jmp    11a1 <indexes+0x31>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    126a <indexes+0xfa>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<indexes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     1216 <indexes+0xa6>
mov    %rax,%r12
xor    %r15d,%r15d
lea    0xe67(%rip),%r13
lea    0xe6b(%rip),%rbp
mov    %r12,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     120c <indexes+0x9c>
mov    (%rax),%rbx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11d0 <indexes+0x60>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11a0 <indexes+0x30>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,%r15
movb   $0x0,(%rax)
mov    %rax,%rdi
mov    %r14,%rsi
call   1090 <strcat@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %r12,%rdi
call   1050 <closedir@plt>
jmp    1219 <indexes+0xa9>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [11 x i8] c"index.html\00"
@.str.1 = [10 x i8] c"index.php\00"
define i8* @indexes(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %26, label %4
4: ; preds = %1, %12
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %23, label %7
7: ; preds = %4
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %5, i64 0, i32 0
%9 = load i8*, i8** %8
%10 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %4
15: ; preds = %7, %12
%16 = tail call i64 @strlen(i8* dereferenceable(1) %9)
%17 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%18 = add i64 %16, 1
%19 = add i64 %18, %17
%20 = tail call i8* @malloc(i64 %19)
store i8 0, i8* %20
%21 = tail call i32 @strcat(i8* %20, i8* %0)
%22 = tail call i32 @strcat(i8* %20, i8* %9)
br label %23
23: ; preds = %4, %15
%24 = phi i8* [ %20, %15 ], [ null, %4 ]
%25 = tail call i32 @closedir(i32* %2)
br label %26
26: ; preds = %1, %23
%27 = phi i8* [ %24, %23 ], [ null, %1 ]
ret i8* %27
}