#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char *d_name;
};
struct array {
  int dummy;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ array_add(struct array *, int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strdup(char *);
void recursive_list_dir(char *d, struct array *arr) {
  DIR *dp = opendir(d);
  if (dp == NULL) {
    fprintf(stderr, "Error opening directory: %s\n", d);
    return;
  }
  struct dirent *ep;
  while ((ep = readdir(dp))) {
    if (strcmp(ep->d_name, ".") == 0)
      continue;
    if (strcmp(ep->d_name, "..") == 0)
      continue;
    struct stat cst;
    int ret;
    char blob[PATH_MAX];
    sprintf(blob, "%s/%s", d, ep->d_name);
    if ((ret = lstat(blob, &cst))) {
      fprintf(stderr, "Error opening: %s\n", ep->d_name);
      continue;
    }
    if (S_ISDIR(cst.st_mode)) {
      recursive_list_dir(blob, arr);
      continue;
    }
    array_add(arr, strdup(blob));
  }
  closedir(dp);
}


x86 assembly (clang, -O0):
<recursive_list_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f6 <recursive_list_dir+0x46>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe14(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1352 <recursive_list_dir+0x1a2>
jmp    11fb <recursive_list_dir+0x4b>
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1349 <recursive_list_dir+0x199>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdfe(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1234 <recursive_list_dir+0x84>
jmp    11fb <recursive_list_dir+0x4b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xddb(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1256 <recursive_list_dir+0xa6>
jmp    11fb <recursive_list_dir+0x4b>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
lea    0xd8c(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x50(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <lstat@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     12da <recursive_list_dir+0x12a>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xd5d(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x2,-0x44(%rbp)
jmp    1328 <recursive_list_dir+0x178>
mov    -0x28(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1305 <recursive_list_dir+0x155>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <recursive_list_dir@plt>
movl   $0x2,-0x44(%rbp)
jmp    1328 <recursive_list_dir+0x178>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
call   10d0 <strdup@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%esi
call   1030 <array_add@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x44(%rbp),%eax
test   %eax,%eax
je     1344 <recursive_list_dir+0x194>
jmp    133f <recursive_list_dir+0x18f>
jmp    11fb <recursive_list_dir+0x4b>
jmp    11fb <recursive_list_dir+0x4b>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recursive_list_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,-0x38(%rbp)
mov    %rdi,%r14
call   1040 <opendir@plt>
test   %rax,%rax
je     12e4 <recursive_list_dir+0x134>
mov    %rax,%rbx
mov    %r14,-0x40(%rbp)
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12cd <recursive_list_dir+0x11d>
mov    %rax,%r14
lea    0xe26(%rip),%r12
jmp    122f <recursive_list_dir+0x7f>
nopw   0x0(%rax,%rax,1)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe13(%rip),%rsi
call   1090 <fprintf@plt>
mov    %r15,%rsp
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12cd <recursive_list_dir+0x11d>
mov    (%r14),%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     121b <recursive_list_dir+0x6b>
mov    (%r14),%rdi
lea    0xdd4(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     121b <recursive_list_dir+0x6b>
mov    %rsp,%r15
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    (%r14),%rcx
mov    %r13,%rdi
lea    0xda3(%rip),%rsi
mov    -0x40(%rbp),%rdx
call   10b0 <sprintf@plt>
mov    %r13,%rdi
lea    -0x30(%rbp),%rsi
call   1070 <lstat@plt>
test   %eax,%eax
jne    1200 <recursive_list_dir+0x50>
mov    -0x30(%rbp),%edi
call   1050 <S_ISDIR@plt>
mov    %r13,%rdi
test   %rax,%rax
je     12b8 <recursive_list_dir+0x108>
mov    -0x38(%rbp),%rsi
call   10c0 <recursive_list_dir@plt>
jmp    1218 <recursive_list_dir+0x68>
call   10d0 <strdup@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1030 <array_add@plt>
jmp    1218 <recursive_list_dir+0x68>
mov    %rbx,%rdi
call   1060 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    0xd0c(%rip),%rsi
mov    %r14,%rdx
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <fprintf@plt>

LLVM IR (-O0):
%struct.array = type { i32 }
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [29 x i8] c"Error opening directory: %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@PATH_MAX = global i32 0
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [19 x i8] c"Error opening: %s\0A\00"
define void @recursive_list_dir(i8* %0, %struct.array* %1) {
%3 = alloca i8*
%4 = alloca %struct.array*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %3
store %struct.array* %1, %struct.array** %4
%12 = load i8*, i8** %3
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %3
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %75
20: ; preds = %2
br label %21
21: ; preds = %71, %68, %38, %31, %20
%22 = load i32*, i32** %5
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %6
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %72
25: ; preds = %21
%26 = load %struct.dirent*, %struct.dirent** %6
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
br label %21
32: ; preds = %25
%33 = load %struct.dirent*, %struct.dirent** %6
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i64 @strcmp(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %32
br label %21
39: ; preds = %32
%40 = load i32, i32* @PATH_MAX
%41 = zext i32 %40 to i64
%42 = call i8* @llvm.stacksave()
store i8* %42, i8** %9
%43 = alloca i8, i64 %41
store i64 %41, i64* %10
%44 = load i8*, i8** %3
%45 = load %struct.dirent*, %struct.dirent** %6
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i32 @sprintf(i8* %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %44, i8* %47)
%49 = call i32 @lstat(i8* %43, %struct.stat* %7)
store i32 %49, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %39
%52 = load i32, i32* @stderr
%53 = load %struct.dirent*, %struct.dirent** %6
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %55)
store i32 2, i32* %11
br label %68
57: ; preds = %39
%58 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i64 @S_ISDIR(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = load %struct.array*, %struct.array** %4
store i32 2, i32* %11
br label %68
64: ; preds = %57
%65 = load %struct.array*, %struct.array** %4
%66 = call i32 @strdup(i8* %43)
%67 = call i32 @array_add(%struct.array* %65, i32 %66)
store i32 0, i32* %11
br label %68
68: ; preds = %64, %62, %51
%69 = load i8*, i8** %9
%70 = load i32, i32* %11
switch i32 %70, label %76 [
i32 0, label %71
i32 2, label %21
]
71: ; preds = %68
br label %21
72: ; preds = %21
%73 = load i32*, i32** %5
%74 = call i32 @closedir(i32* %73)
br label %75
75: ; preds = %72, %16
ret void
76: ; preds = %68
unreachable
}

LLVM IR (-O2):
%struct.array = type { i32 }
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [29 x i8] c"Error opening directory: %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@PATH_MAX = global i32 0
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [19 x i8] c"Error opening: %s\0A\00"
define void @recursive_list_dir(i8* %0, %struct.array* %1) {
%3 = alloca %struct.stat
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %50, label %9
9: ; preds = %6
%10 = bitcast %struct.stat* %3 to i8*
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %52
15: ; preds = %9, %21
%16 = phi %struct.dirent* [ %7, %9 ], [ %22, %21 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15, %24, %49
%22 = call %struct.dirent* @readdir(i32* %4)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %50, label %15
24: ; preds = %15
%25 = load i8*, i8** %17
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %21, label %28
28: ; preds = %24
%29 = load i32, i32* @PATH_MAX
%30 = zext i32 %29 to i64
%31 = call i8* @llvm.stacksave()
%32 = alloca i8, i64 %30
%33 = load i8*, i8** %17
%34 = call i32 @sprintf(i8* %32, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %33)
%35 = call i32 @lstat(i8* %32, %struct.stat* %3)
%36 = icmp eq i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %28
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %17
%40 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %39)
br label %49
41: ; preds = %28
%42 = load i32, i32* %11
%43 = call i64 @S_ISDIR(i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %41
br label %49
46: ; preds = %41
%47 = call i32 @strdup(i8* %32)
%48 = call i32 @array_add(%struct.array* %1, i32 %47)
br label %49
49: ; preds = %46, %45, %37
br label %21
50: ; preds = %21, %6
%51 = call i32 @closedir(i32* %4)
br label %52
52: ; preds = %50, %12
ret void
}