#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 FILENAME_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ malloc(unsigned int);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewinddir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
char *strdup(char *);
int strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
char **readfilenames(const char *dirname, unsigned int *nbfiles) {
  struct dirent *dir_entry;
  DIR *dir = opendir(dirname);
  if (!dir) {
    return NULL;
  }
  *nbfiles = 0;
  while ((dir_entry = readdir(dir)) != NULL) {
    if (strcmp(dir_entry->d_name, ".") && strcmp(dir_entry->d_name, "..")) {
      (*nbfiles)++;
    }
  }
  char **files = (char **)malloc((*nbfiles) * sizeof(char *));
  if (!files)
    return NULL;
  int index = 0;
  char path[FILENAME_MAX];
  path[0] = '\0';
  rewinddir(dir);
  while ((dir_entry = readdir(dir)) != 0) {
    if (strcmp(dir_entry->d_name, ".") && strcmp(dir_entry->d_name, "..")) {
      strncat(path, dirname, strlen(dirname));
      strncat(path, "/", 1);
      strncat(path, dir_entry->d_name, strlen(dir_entry->d_name));
      files[index++] = strdup(path);
      path[0] = '\0';
    }
  }
  closedir(dir);
  return files;
}


x86 assembly (clang, -O0):
<readfilenames>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c5 <readfilenames+0x35>
movq   $0x0,-0x8(%rbp)
jmp    1390 <readfilenames+0x200>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1230 <readfilenames+0xa0>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe0d(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     122b <readfilenames+0x9b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdef(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     122b <readfilenames+0x9b>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    11cf <readfilenames+0x3f>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
shl    $0x3,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    125d <readfilenames+0xcd>
movq   $0x0,-0x8(%rbp)
jmp    1390 <readfilenames+0x200>
movl   $0x0,-0x34(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x40(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x50(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x48(%rbp)
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
call   1060 <rewinddir@plt>
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1378 <readfilenames+0x1e8>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd3e(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1373 <readfilenames+0x1e3>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd20(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1373 <readfilenames+0x1e3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x60(%rbp),%rsi
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
call   1050 <strncat@plt>
mov    -0x50(%rbp),%rdi
lea    0xce9(%rip),%rsi
mov    $0x1,%edx
call   1050 <strncat@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rsi
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
call   1050 <strncat@plt>
mov    -0x50(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,%rsi
mov    -0x50(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    -0x34(%rbp),%edx
mov    %edx,%edi
add    $0x1,%edi
mov    %edi,-0x34(%rbp)
movslq %edx,%rdx
mov    %rsi,(%rcx,%rdx,8)
movb   $0x0,(%rax)
jmp    129e <readfilenames+0x10e>
mov    -0x28(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfilenames>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,-0x40(%rbp)
call   1030 <opendir@plt>
test   %rax,%rax
je     1327 <readfilenames+0x197>
mov    %rax,%r12
movl   $0x0,(%r15)
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1216 <readfilenames+0x86>
mov    %rax,%r14
lea    0xe2a(%rip),%rbx
lea    0xe22(%rip),%r13
jmp    11f0 <readfilenames+0x60>
mov    %r12,%rdi
call   1090 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1216 <readfilenames+0x86>
mov    (%r14),%rdi
mov    %rbx,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11e0 <readfilenames+0x50>
mov    (%r14),%rdi
mov    %r13,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11e0 <readfilenames+0x50>
addl   $0x1,(%r15)
jmp    11e0 <readfilenames+0x50>
mov    (%r15),%edi
shl    $0x3,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1327 <readfilenames+0x197>
mov    %rax,%r13
mov    %rsp,-0x30(%rbp)
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rcx,%r14
sub    %rax,%r14
mov    %r14,%rsp
neg    %rax
movb   $0x0,(%rcx,%rax,1)
mov    %r12,%rdi
call   1060 <rewinddir@plt>
mov    %r12,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     132c <readfilenames+0x19c>
mov    %rax,%r15
xor    %ebx,%ebx
mov    %r12,-0x38(%rbp)
jmp    1294 <readfilenames+0x104>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1090 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     132c <readfilenames+0x19c>
mov    (%r15),%rdi
lea    0xd63(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1280 <readfilenames+0xf0>
mov    (%r15),%rdi
lea    0xd4e(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1280 <readfilenames+0xf0>
mov    %ebx,%r12d
mov    -0x40(%rbp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <strncat@plt>
mov    %r14,%rdi
lea    0xd21(%rip),%rsi
mov    $0x1,%edx
call   1050 <strncat@plt>
mov    (%r15),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12d,%ebx
mov    -0x38(%rbp),%r12
mov    %eax,%edx
call   1050 <strncat@plt>
mov    %r14,%rdi
call   10b0 <strdup@plt>
movslq %ebx,%rcx
add    $0x1,%ebx
mov    %rax,0x0(%r13,%rcx,8)
movb   $0x0,(%r14)
jmp    1280 <readfilenames+0xf0>
xor    %r13d,%r13d
jmp    1338 <readfilenames+0x1a8>
mov    %r12,%rdi
call   1070 <closedir@plt>
mov    -0x30(%rbp),%rsp
mov    %r13,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@FILENAME_MAX = global i32 0
@.str.2 = [2 x i8] c"/\00"
define i8** @readfilenames(i8* %0, i32* nocapture %1) {
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %66, label %5
5: ; preds = %2
store i32 0, i32* %1
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %24, label %8
8: ; preds = %5, %21
%9 = phi %struct.dirent* [ %22, %21 ], [ %6, %5 ]
%10 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%11 = load i8*, i8** %10
%12 = tail call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %8
%15 = load i8*, i8** %10
%16 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i32, i32* %1
%20 = add i32 %19, 1
store i32 %20, i32* %1
br label %21
21: ; preds = %18, %14, %8
%22 = tail call %struct.dirent* @readdir(i32* %3)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %24, label %8
24: ; preds = %21, %5
%25 = load i32, i32* %1
%26 = shl i32 %25, 3
%27 = tail call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8**
%29 = icmp eq i64 %27, 0
br i1 %29, label %66, label %30
30: ; preds = %24
%31 = load i32, i32* @FILENAME_MAX
%32 = zext i32 %31 to i64
%33 = tail call i8* @llvm.stacksave()
%34 = alloca i8, i64 %32
store i8 0, i8* %34
%35 = tail call i32 @rewinddir(i32* %3)
%36 = call %struct.dirent* @readdir(i32* %3)
%37 = icmp eq %struct.dirent* %36, null
br i1 %37, label %64, label %38
38: ; preds = %30, %60
%39 = phi %struct.dirent* [ %62, %60 ], [ %36, %30 ]
%40 = phi i32 [ %61, %60 ], [ 0, %30 ]
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i64 0, i32 0
%42 = load i8*, i8** %41
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %60, label %45
45: ; preds = %38
%46 = load i8*, i8** %41
%47 = call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %60, label %49
49: ; preds = %45
%50 = call i32 @strlen(i8* %0)
%51 = call i32 @strncat(i8* %34, i8* %0, i32 %50)
%52 = call i32 @strncat(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%53 = load i8*, i8** %41
%54 = call i32 @strlen(i8* %53)
%55 = call i32 @strncat(i8* %34, i8* %53, i32 %54)
%56 = call i8* @strdup(i8* %34)
%57 = add nsw i32 %40, 1
%58 = sext i32 %40 to i64
%59 = getelementptr inbounds i8*, i8** %28, i64 %58
store i8* %56, i8** %59
store i8 0, i8* %34
br label %60
60: ; preds = %49, %45, %38
%61 = phi i32 [ %57, %49 ], [ %40, %45 ], [ %40, %38 ]
%62 = call %struct.dirent* @readdir(i32* %3)
%63 = icmp eq %struct.dirent* %62, null
br i1 %63, label %64, label %38
64: ; preds = %60, %30
%65 = call i32 @closedir(i32* %3)
br label %66
66: ; preds = %64, %24, %2
%67 = phi i8** [ null, %2 ], [ %28, %64 ], [ null, %24 ]
ret i8** %67
}