#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*/ *);
char **createStringArray(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int growStringArray(char ***, int, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int allFilesInDir(char *dirName, char ***result) {
  DIR *dp;
  struct dirent *ep;
  int stringArrSize = 16;
  *result = createStringArray(stringArrSize, 1024);
  dp = opendir(dirName);
  int numEnts = 0;
  if (dp == NULL) {
    fprintf(stderr, "allFilesInDir couldn't open %s\n", dirName);
    perror("error: ");
    exit(1);
  } else {
    while ((ep = readdir(dp))) {
      if (stringArrSize == numEnts) {
        stringArrSize = growStringArray(result, stringArrSize, 1024);
      }
      strcpy((*result)[numEnts], ep->d_name);
      numEnts++;
    }
    closedir(dp);
  }
  int i;
  for (i = numEnts; i < stringArrSize; i++) {
    free((*result)[i]);
  }
  return numEnts;
}


x86 assembly (clang, -O0):
<allFilesInDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x10,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x400,%esi
call   1090 <createStringArray@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121c <allFilesInDir+0x7c>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdff(%rip),%rsi
call   1070 <fprintf@plt>
lea    0xe13(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1221 <allFilesInDir+0x81>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1280 <allFilesInDir+0xe0>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jne    1258 <allFilesInDir+0xb8>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    $0x400,%edx
call   10c0 <growStringArray@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1221 <allFilesInDir+0x81>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12bd <allFilesInDir+0x11d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    128f <allFilesInDir+0xef>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allFilesInDir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x10,%edi
mov    $0x400,%esi
call   1090 <createStringArray@plt>
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1280 <allFilesInDir+0xe0>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     123f <allFilesInDir+0x9f>
mov    %rax,%rbp
mov    $0x10,%r12d
xor    %ebx,%ebx
jmp    1213 <allFilesInDir+0x73>
xchg   %ax,%ax
mov    (%r14),%rax
mov    (%rax,%rbx,8),%rdi
mov    0x0(%rbp),%esi
call   1040 <strcpy@plt>
add    $0x1,%rbx
mov    %r15,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1230 <allFilesInDir+0x90>
mov    %r12d,%eax
cmp    %rax,%rbx
jne    11f0 <allFilesInDir+0x50>
mov    %r14,%rdi
mov    %r12d,%esi
mov    $0x400,%edx
call   10c0 <growStringArray@plt>
mov    %eax,%r12d
jmp    11f0 <allFilesInDir+0x50>
mov    %r15,%rdi
call   1060 <closedir@plt>
cmp    %ebx,%r12d
jg     124f <allFilesInDir+0xaf>
jmp    1275 <allFilesInDir+0xd5>
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    $0x10,%r12d
xor    %ebx,%ebx
mov    %ebx,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r14),%rax
mov    (%rax,%rbp,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbp
cmp    %ebp,%r12d
jg     1260 <allFilesInDir+0xc0>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    0xd70(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
lea    0xd81(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"allFilesInDir couldn't open %s\0A\00"
@.str.1 = [8 x i8] c"error: \00"
define i32 @allFilesInDir(i8* %0, i8*** %1) {
%3 = alloca i8*
%4 = alloca i8***
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8*** %1, i8**** %4
store i32 16, i32* %7
%10 = load i32, i32* %7
%11 = call i8** @createStringArray(i32 %10, i32 1024)
%12 = load i8***, i8**** %4
store i8** %11, i8*** %12
%13 = load i8*, i8** %3
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %5
store i32 0, i32* %8
%15 = load i32*, i32** %5
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %3
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
br label %24
24: ; preds = %36, %23
%25 = load i32*, i32** %5
%26 = call %struct.dirent* @readdir(i32* %25)
store %struct.dirent* %26, %struct.dirent** %6
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %49
28: ; preds = %24
%29 = load i32, i32* %7
%30 = load i32, i32* %8
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i8***, i8**** %4
%34 = load i32, i32* %7
%35 = call i32 @growStringArray(i8*** %33, i32 %34, i32 1024)
store i32 %35, i32* %7
br label %36
36: ; preds = %32, %28
%37 = load i8***, i8**** %4
%38 = load i8**, i8*** %37
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
%42 = load i8*, i8** %41
%43 = load %struct.dirent*, %struct.dirent** %6
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @strcpy(i8* %42, i32 %45)
%47 = load i32, i32* %8
%48 = add nsw i32 %47, 1
store i32 %48, i32* %8
br label %24
49: ; preds = %24
%50 = load i32*, i32** %5
%51 = call i32 @closedir(i32* %50)
br label %52
52: ; preds = %49
%53 = load i32, i32* %8
store i32 %53, i32* %9
br label %54
54: ; preds = %66, %52
%55 = load i32, i32* %9
%56 = load i32, i32* %7
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %69
58: ; preds = %54
%59 = load i8***, i8**** %4
%60 = load i8**, i8*** %59
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8*, i8** %60, i64 %62
%64 = load i8*, i8** %63
%65 = call i32 @free(i8* %64)
br label %66
66: ; preds = %58
%67 = load i32, i32* %9
%68 = add nsw i32 %67, 1
store i32 %68, i32* %9
br label %54
69: ; preds = %54
%70 = load i32, i32* %8
ret i32 %70
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"allFilesInDir couldn't open %s\0A\00"
@.str.1 = [8 x i8] c"error: \00"
define i32 @allFilesInDir(i8* %0, i8*** %1) {
%3 = tail call i8** @createStringArray(i32 16, i32 1024)
store i8** %3, i8*** %1
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = tail call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %9, label %16
9: ; preds = %6
%10 = tail call i32 @closedir(i32* %4)
br label %39
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
%14 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %6, %24
%17 = phi i64 [ %32, %24 ], [ 0, %6 ]
%18 = phi %struct.dirent* [ %33, %24 ], [ %7, %6 ]
%19 = phi i32 [ %25, %24 ], [ 16, %6 ]
%20 = zext i32 %19 to i64
%21 = icmp eq i64 %17, %20
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = tail call i32 @growStringArray(i8*** %1, i32 %19, i32 1024)
br label %24
24: ; preds = %22, %16
%25 = phi i32 [ %23, %22 ], [ %19, %16 ]
%26 = load i8**, i8*** %1
%27 = getelementptr inbounds i8*, i8** %26, i64 %17
%28 = load i8*, i8** %27
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%30 = load i32, i32* %29
%31 = tail call i32 @strcpy(i8* %28, i32 %30)
%32 = add nuw i64 %17, 1
%33 = tail call %struct.dirent* @readdir(i32* %4)
%34 = icmp eq %struct.dirent* %33, null
br i1 %34, label %35, label %16
35: ; preds = %24
%36 = trunc i64 %32 to i32
%37 = tail call i32 @closedir(i32* %4)
%38 = icmp sgt i32 %25, %36
br i1 %38, label %39, label %52
39: ; preds = %9, %35
%40 = phi i32 [ 0, %9 ], [ %36, %35 ]
%41 = phi i32 [ 16, %9 ], [ %25, %35 ]
%42 = zext i32 %40 to i64
br label %43
43: ; preds = %39, %43
%44 = phi i64 [ %42, %39 ], [ %49, %43 ]
%45 = load i8**, i8*** %1
%46 = getelementptr inbounds i8*, i8** %45, i64 %44
%47 = load i8*, i8** %46
%48 = tail call i32 @free(i8* %47)
%49 = add nuw nsw i64 %44, 1
%50 = trunc i64 %49 to i32
%51 = icmp sgt i32 %41, %50
br i1 %51, label %43, label %52
52: ; preds = %43, %35
%53 = phi i32 [ %36, %35 ], [ %40, %43 ]
ret i32 %53
}