#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*/ chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
void ifolder_list(char *folder, char *list, int *ln) {
  DIR *dir, *subdir;
  struct dirent *ent;
  char the_list[100000] = {'\0'};
  char PWD[256] = ".";
  *ln = 0;
  getcwd(PWD, 256);
  chdir(folder);
  dir = opendir(".");
  if (dir != NULL) {
    while ((ent = readdir(dir)) != NULL)
      if ((subdir = opendir(ent->d_name)) != NULL) {
        strcat(the_list, " ");
        strcat(the_list, ent->d_name);
        closedir(subdir);
      }
    closedir(dir);
    *ln = strlen(the_list);
    sprintf(list, "%s", the_list);
  }
  chdir(PWD);
}


x86 assembly (clang, -O0):
<ifolder_list>:
push   %rbp
mov    %rsp,%rbp
sub    $0x187d0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x186d0(%rbp),%rdi
xor    %esi,%esi
mov    $0x186a0,%edx
call   1060 <memset@plt>
lea    -0x187d0(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
movb   $0x2e,-0x187d0(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
lea    -0x187d0(%rbp),%rdi
mov    $0x100,%esi
call   1070 <getcwd@plt>
mov    -0x8(%rbp),%rdi
call   1050 <chdir@plt>
lea    0xe01(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12b1 <ifolder_list+0x121>
jmp    1218 <ifolder_list+0x88>
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     127d <ifolder_list+0xed>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1278 <ifolder_list+0xe8>
lea    -0x186d0(%rbp),%rdi
lea    0xdab(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x186d0(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   10a0 <strcat@plt>
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
jmp    1218 <ifolder_list+0x88>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
lea    -0x186d0(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x186d0(%rbp),%rdx
lea    0xd58(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x187d0(%rbp),%rdi
call   1050 <chdir@plt>
add    $0x187d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ifolder_list>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x187b8,%rsp
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0x110(%rsp),%rdi
mov    $0x186a0,%edx
xor    %esi,%esi
call   1070 <memset@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movb   $0x2e,0x10(%rsp)
movl   $0x0,(%r15)
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1080 <getcwd@plt>
mov    %rbx,%rdi
call   1060 <chdir@plt>
lea    0xdaa(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12fa <ifolder_list+0x16a>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12d2 <ifolder_list+0x142>
mov    %rax,%rbp
lea    0xd7f(%rip),%r12
lea    0x110(%rsp),%r13
jmp    12a0 <ifolder_list+0x110>
nopl   (%rax)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12d2 <ifolder_list+0x142>
mov    0x0(%rbp),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1290 <ifolder_list+0x100>
mov    %rax,%r14
mov    %r13,%rdi
mov    %r12,%rsi
call   10b0 <strcat@plt>
mov    0x0(%rbp),%rsi
mov    %r13,%rdi
call   10b0 <strcat@plt>
mov    %r14,%rdi
call   1090 <closedir@plt>
jmp    1290 <ifolder_list+0x100>
mov    %rbx,%rdi
call   1090 <closedir@plt>
lea    0x110(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,(%r15)
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0x10(%rsp),%rdi
call   1060 <chdir@plt>
add    $0x187b8,%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 = [2 x i8] c" \00"
@.str.2 = [3 x i8] c"%s\00"
define void @ifolder_list(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca [100000 x i8]
%11 = alloca [256 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* %2, i32** %6
%12 = bitcast [100000 x i8]* %10 to i8*
%13 = bitcast [256 x i8]* %11 to i8*
%14 = bitcast i8* %13 to [256 x i8]*
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %14, i32 0, i32 0
store i8 46, i8* %15
%16 = load i32*, i32** %6
store i32 0, i32* %16
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%18 = call i32 @getcwd(i8* %17, i32 256)
%19 = load i8*, i8** %4
%20 = call i32 @chdir(i8* %19)
%21 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %7
%22 = load i32*, i32** %7
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %55
24: ; preds = %3
br label %25
25: ; preds = %45, %24
%26 = load i32*, i32** %7
%27 = call %struct.dirent* @readdir(i32* %26)
store %struct.dirent* %27, %struct.dirent** %9
%28 = icmp ne %struct.dirent* %27, null
br i1 %28, label %29, label %46
29: ; preds = %25
%30 = load %struct.dirent*, %struct.dirent** %9
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32* @opendir(i8* %32)
store i32* %33, i32** %8
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %45
35: ; preds = %29
%36 = getelementptr inbounds [100000 x i8], [100000 x i8]* %10, i64 0, i64 0
%37 = call i32 @strcat(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%38 = getelementptr inbounds [100000 x i8], [100000 x i8]* %10, i64 0, i64 0
%39 = load %struct.dirent*, %struct.dirent** %9
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @strcat(i8* %38, i8* %41)
%43 = load i32*, i32** %8
%44 = call i32 @closedir(i32* %43)
br label %45
45: ; preds = %35, %29
br label %25
46: ; preds = %25
%47 = load i32*, i32** %7
%48 = call i32 @closedir(i32* %47)
%49 = getelementptr inbounds [100000 x i8], [100000 x i8]* %10, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = load i32*, i32** %6
store i32 %50, i32* %51
%52 = load i8*, i8** %5
%53 = getelementptr inbounds [100000 x i8], [100000 x i8]* %10, i64 0, i64 0
%54 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %46, %3
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%57 = call i32 @chdir(i8* %56)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c" \00"
define void @ifolder_list(i8* %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca [100000 x i8]
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [100000 x i8], [100000 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
store i8 46, i8* %7
store i32 0, i32* %2
%8 = call i32 @getcwd(i8* %7, i32 256)
%9 = call i32 @chdir(i8* %0)
%10 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %33, label %12
12: ; preds = %3
%13 = call %struct.dirent* @readdir(i32* %10)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %29, label %15
15: ; preds = %12, %26
%16 = phi %struct.dirent* [ %27, %26 ], [ %13, %12 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = call i32* @opendir(i8* %18)
%20 = icmp eq i32* %19, null
br i1 %20, label %26, label %21
21: ; preds = %15
%22 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = load i8*, i8** %17
%24 = call i32 @strcat(i8* %6, i8* %23)
%25 = call i32 @closedir(i32* %19)
br label %26
26: ; preds = %21, %15
%27 = call %struct.dirent* @readdir(i32* %10)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %29, label %15
29: ; preds = %26, %12
%30 = call i32 @closedir(i32* %10)
%31 = call i32 @strlen(i8* %6)
store i32 %31, i32* %2
%32 = call i8* @strcpy(i8* dereferenceable(1) %1, i8* %6)
br label %33
33: ; preds = %29, %3
%34 = call i32 @chdir(i8* %7)
ret void
}