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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ error(char *);
void *malloc(int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strlen(int /*<<< orphan*/);
char **list_file_directory() {
  DIR *dp;
  struct dirent *ep;
  int i = 1;
  char num_files;
  dp = opendir("./files/");
  if (dp != NULL) {
    ep = readdir(dp);
    while (ep != NULL) {
      num_files++;
      ep = readdir(dp);
    }
    closedir(dp);
  } else
    error("ERROR OPENING FILE DIRECTORY");
  num_files -= 2;
  char **files = malloc(num_files * sizeof(char *));
  files[0] = malloc(sizeof(char *));
  *files[0] = num_files;
  dp = opendir("./files/");
  if (dp == NULL) {
    error("ERROR OPENING FILE DIRECTORY");
  }
  ep = readdir(dp);
  while (ep != NULL) {
    if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) {
      ep = readdir(dp);
      continue;
    }
    files[i] = malloc(strlen(ep->d_name));
    strcpy(files[i], ep->d_name);
    i++;
    ep = readdir(dp);
  }
  closedir(dp);
  return files;
}


x86 assembly (clang, -O0):
<list_file_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1,-0x14(%rbp)
lea    0xe6a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11ea <list_file_directory+0x6a>
mov    -0x8(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11dc <list_file_directory+0x5c>
mov    -0x15(%rbp),%al
add    $0x1,%al
mov    %al,-0x15(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
jmp    11b7 <list_file_directory+0x37>
mov    -0x8(%rbp),%rdi
call   1060 <closedir@plt>
jmp    11f6 <list_file_directory+0x76>
lea    0xe18(%rip),%rdi
call   10a0 <error@plt>
movsbl -0x15(%rbp),%eax
sub    $0x2,%eax
mov    %al,-0x15(%rbp)
movsbq -0x15(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x8,%edi
call   1090 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x15(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %cl,(%rax)
lea    0xdc5(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    125b <list_file_directory+0xdb>
lea    0xdb3(%rip),%rdi
call   10a0 <error@plt>
mov    -0x8(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1310 <list_file_directory+0x190>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    0xda7(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12ab <list_file_directory+0x12b>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    0xd8a(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12bd <list_file_directory+0x13d>
mov    -0x8(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
jmp    1268 <list_file_directory+0xe8>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
jmp    1268 <list_file_directory+0xe8>
mov    -0x8(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_file_directory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe6e(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11ca <list_file_directory+0x4a>
mov    %rax,%rbp
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11c0 <list_file_directory+0x40>
nopl   0x0(%rax)
add    $0x1,%bl
mov    %rbp,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    11b0 <list_file_directory+0x30>
mov    %rbp,%rdi
call   1060 <closedir@plt>
jmp    11d6 <list_file_directory+0x56>
lea    0xe38(%rip),%rdi
call   10a0 <error@plt>
add    $0xfe,%bl
movsbl %bl,%ebx
lea    0x0(,%rbx,8),%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    $0x8,%edi
call   1090 <malloc@plt>
mov    %rax,(%r14)
mov    %bl,(%rax)
lea    0xdff(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r13
test   %rax,%rax
jne    121a <list_file_directory+0x9a>
lea    0xdf4(%rip),%rdi
call   10a0 <error@plt>
mov    %r13,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1290 <list_file_directory+0x110>
mov    %rax,%rbx
mov    $0x1,%ebp
lea    0xdf1(%rip),%r15
lea    0xde9(%rip),%r12
jmp    1250 <list_file_directory+0xd0>
nop
mov    %r13,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1290 <list_file_directory+0x110>
mov    (%rbx),%edi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1240 <list_file_directory+0xc0>
mov    (%rbx),%edi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1240 <list_file_directory+0xc0>
mov    (%rbx),%edi
call   1050 <strlen@plt>
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,(%r14,%rbp,8)
mov    (%rbx),%esi
mov    %rax,%rdi
call   1030 <strcpy@plt>
add    $0x1,%rbp
jmp    1240 <list_file_directory+0xc0>
mov    %r13,%rdi
call   1060 <closedir@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"./files/\00"
@.str.1 = [29 x i8] c"ERROR OPENING FILE DIRECTORY\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
define i8** @list_file_directory() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8**
store i32 1, i32* %3
%6 = call i32* @opendir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %1
%7 = load i32*, i32** %1
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %23
9: ; preds = %0
%10 = load i32*, i32** %1
%11 = call %struct.dirent* @readdir(i32* %10)
store %struct.dirent* %11, %struct.dirent** %2
br label %12
12: ; preds = %15, %9
%13 = load %struct.dirent*, %struct.dirent** %2
%14 = icmp ne %struct.dirent* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i8, i8* %4
%17 = add i8 %16, 1
store i8 %17, i8* %4
%18 = load i32*, i32** %1
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %2
br label %12
20: ; preds = %12
%21 = load i32*, i32** %1
%22 = call i32 @closedir(i32* %21)
br label %25
23: ; preds = %0
%24 = call i32 @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
%26 = load i8, i8* %4
%27 = sext i8 %26 to i32
%28 = sub nsw i32 %27, 2
%29 = trunc i32 %28 to i8
store i8 %29, i8* %4
%30 = load i8, i8* %4
%31 = sext i8 %30 to i64
%32 = mul i64 %31, 8
%33 = trunc i64 %32 to i32
%34 = call i8* @malloc(i32 %33)
%35 = bitcast i8* %34 to i8**
store i8** %35, i8*** %5
%36 = call i8* @malloc(i32 8)
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 0
store i8* %36, i8** %38
%39 = load i8, i8* %4
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 0
%42 = load i8*, i8** %41
store i8 %39, i8* %42
%43 = call i32* @opendir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32* %43, i32** %1
%44 = load i32*, i32** %1
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %48
46: ; preds = %25
%47 = call i32 @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %48
48: ; preds = %46, %25
%49 = load i32*, i32** %1
%50 = call %struct.dirent* @readdir(i32* %49)
store %struct.dirent* %50, %struct.dirent** %2
br label %51
51: ; preds = %69, %66, %48
%52 = load %struct.dirent*, %struct.dirent** %2
%53 = icmp ne %struct.dirent* %52, null
br i1 %53, label %54, label %92
54: ; preds = %51
%55 = load %struct.dirent*, %struct.dirent** %2
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i64 @strcmp(i32 %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%59 = icmp eq i64 %58, 0
br i1 %59, label %66, label %60
60: ; preds = %54
%61 = load %struct.dirent*, %struct.dirent** %2
%62 = getelementptr inbounds %struct.dirent, %struct.dirent* %61, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i64 @strcmp(i32 %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %60, %54
%67 = load i32*, i32** %1
%68 = call %struct.dirent* @readdir(i32* %67)
store %struct.dirent* %68, %struct.dirent** %2
br label %51
69: ; preds = %60
%70 = load %struct.dirent*, %struct.dirent** %2
%71 = getelementptr inbounds %struct.dirent, %struct.dirent* %70, i32 0, i32 0
%72 = load i32, i32* %71
%73 = call i32 @strlen(i32 %72)
%74 = call i8* @malloc(i32 %73)
%75 = load i8**, i8*** %5
%76 = load i32, i32* %3
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i8*, i8** %75, i64 %77
store i8* %74, i8** %78
%79 = load i8**, i8*** %5
%80 = load i32, i32* %3
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8*, i8** %79, i64 %81
%83 = load i8*, i8** %82
%84 = load %struct.dirent*, %struct.dirent** %2
%85 = getelementptr inbounds %struct.dirent, %struct.dirent* %84, i32 0, i32 0
%86 = load i32, i32* %85
%87 = call i32 @strcpy(i8* %83, i32 %86)
%88 = load i32, i32* %3
%89 = add nsw i32 %88, 1
store i32 %89, i32* %3
%90 = load i32*, i32** %1
%91 = call %struct.dirent* @readdir(i32* %90)
store %struct.dirent* %91, %struct.dirent** %2
br label %51
92: ; preds = %51
%93 = load i32*, i32** %1
%94 = call i32 @closedir(i32* %93)
%95 = load i8**, i8*** %5
ret i8** %95
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"./files/\00"
@.str.1 = [29 x i8] c"ERROR OPENING FILE DIRECTORY\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
define noalias i8** @list_file_directory() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %14, label %3
3: ; preds = %0
%4 = tail call %struct.dirent* @readdir(i32* %1)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %11, label %6
6: ; preds = %3, %6
%7 = phi i8 [ %8, %6 ], [ undef, %3 ]
%8 = add i8 %7, 1
%9 = tail call %struct.dirent* @readdir(i32* %1)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %11, label %6
11: ; preds = %6, %3
%12 = phi i8 [ undef, %3 ], [ %8, %6 ]
%13 = tail call i32 @closedir(i32* %1)
br label %16
14: ; preds = %0
%15 = tail call i32 @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = phi i8 [ %12, %11 ], [ undef, %14 ]
%18 = add i8 %17, -2
%19 = sext i8 %18 to i32
%20 = shl nsw i32 %19, 3
%21 = tail call i8* @malloc(i32 %20)
%22 = bitcast i8* %21 to i8**
%23 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
store i8* %23, i8** %22
store i8 %18, i8* %23
%24 = tail call i32* @opendir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = tail call i32 @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %16
%29 = tail call %struct.dirent* @readdir(i32* %24)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %57, label %31
31: ; preds = %28, %47
%32 = phi i64 [ %54, %47 ], [ 1, %28 ]
%33 = phi %struct.dirent* [ %55, %47 ], [ %29, %28 ]
br label %34
34: ; preds = %31, %44
%35 = phi %struct.dirent* [ %33, %31 ], [ %45, %44 ]
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i64 0, i32 0
%37 = load i32, i32* %36
%38 = tail call i64 @strcmp(i32 %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %34
%41 = load i32, i32* %36
%42 = tail call i64 @strcmp(i32 %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40, %34
%45 = tail call %struct.dirent* @readdir(i32* %24)
%46 = icmp eq %struct.dirent* %45, null
br i1 %46, label %57, label %34
47: ; preds = %40
%48 = load i32, i32* %36
%49 = tail call i32 @strlen(i32 %48)
%50 = tail call i8* @malloc(i32 %49)
%51 = getelementptr inbounds i8*, i8** %22, i64 %32
store i8* %50, i8** %51
%52 = load i32, i32* %36
%53 = tail call i32 @strcpy(i8* %50, i32 %52)
%54 = add nuw i64 %32, 1
%55 = tail call %struct.dirent* @readdir(i32* %24)
%56 = icmp eq %struct.dirent* %55, null
br i1 %56, label %57, label %31
57: ; preds = %47, %44, %28
%58 = tail call i32 @closedir(i32* %24)
ret i8** %22
}