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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
char *calloc(scalar_t__, int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ upstack_push_top(int, int /*<<< orphan*/ *,
                                    int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                    char *, int);
void dir_add_songs(char *filename, int depth, int mem_unit) {
  struct stat statinfo;
  DIR *dir_fd;
  struct dirent *entry;
  char *path_temp = NULL;
  if (depth > 3)
    return;
  dir_fd = opendir(filename);
  while ((entry = readdir(dir_fd)) != NULL) {
    if (path_temp) {
      free(path_temp);
      path_temp = NULL;
    }
    path_temp = calloc(strlen(filename) + strlen(entry->d_name) + 1, 1);
    sprintf(path_temp, "%s/%s", filename, entry->d_name);
    if (entry->d_name[0] == '.')
      continue;
    if (stat(path_temp, &statinfo) < 0)
      continue;
    if (S_ISDIR(statinfo.st_mode)) {
      dir_add_songs(path_temp, depth + 1, mem_unit);
      continue;
    }
    upstack_push_top(mem_unit, NULL, NULL, NULL, path_temp, depth + 1);
  }
  closedir(dir_fd);
}


x86 assembly (clang, -O0):
<dir_add_songs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
cmpl   $0x3,-0xc(%rbp)
jle    11d9 <dir_add_songs+0x29>
jmp    12ef <dir_add_songs+0x13f>
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12e6 <dir_add_songs+0x136>
cmpq   $0x0,-0x30(%rbp)
je     1219 <dir_add_songs+0x69>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xd9f(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    127e <dir_add_songs+0xce>
jmp    11e6 <dir_add_songs+0x36>
mov    -0x30(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
jge    129a <dir_add_songs+0xea>
jmp    11e6 <dir_add_songs+0x36>
mov    -0x18(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12c3 <dir_add_songs+0x113>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%edx
call   10c0 <dir_add_songs@plt>
jmp    11e6 <dir_add_songs+0x36>
mov    -0x10(%rbp),%edi
mov    -0x30(%rbp),%r8
mov    -0xc(%rbp),%r9d
add    $0x1,%r9d
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1040 <upstack_push_top@plt>
jmp    11e6 <dir_add_songs+0x36>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_add_songs>:
cmp    $0x3,%esi
jg     12c8 <dir_add_songs+0x118>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    %esi,0x8(%rsp)
mov    %edx,0xc(%rsp)
call   1050 <opendir@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12b2 <dir_add_songs+0x102>
mov    %rax,%r14
addl   $0x1,0x8(%rsp)
xor    %r13d,%r13d
lea    0xe03(%rip),%r12
jmp    1224 <dir_add_songs+0x74>
mov    %r13,%rdi
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
call   10c0 <dir_add_songs@plt>
nop
mov    %r15,%rdi
call   10b0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12b2 <dir_add_songs+0x102>
test   %r13,%r13
je     1231 <dir_add_songs+0x81>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbp
mov    (%r14),%rdi
call   1060 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,%r13
mov    (%r14),%rcx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   10d0 <sprintf@plt>
mov    (%r14),%rax
cmpb   $0x2e,(%rax)
je     1210 <dir_add_songs+0x60>
mov    %r13,%rdi
lea    0x10(%rsp),%rsi
call   10a0 <stat@plt>
test   %rax,%rax
js     1210 <dir_add_songs+0x60>
mov    0x10(%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
jne    11ff <dir_add_songs+0x4f>
mov    0xc(%rsp),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    0x8(%rsp),%r9d
call   1040 <upstack_push_top@plt>
jmp    1210 <dir_add_songs+0x60>
mov    %r15,%rdi
call   1080 <closedir@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
define void @dir_add_songs(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i8* null, i8** %10
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 3
br i1 %12, label %13, label %14
13: ; preds = %3
br label %75
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i32* @opendir(i8* %15)
store i32* %16, i32** %8
br label %17
17: ; preds = %66, %61, %55, %50, %14
%18 = load i32*, i32** %8
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %9
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %72
21: ; preds = %17
%22 = load i8*, i8** %10
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i8*, i8** %10
%26 = call i32 @free(i8* %25)
store i8* null, i8** %10
br label %27
27: ; preds = %24, %21
%28 = load i8*, i8** %4
%29 = call i64 @strlen(i8* %28)
%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 i64 @strlen(i8* %32)
%34 = add nsw i64 %29, %33
%35 = add nsw i64 %34, 1
%36 = call i8* @calloc(i64 %35, i32 1)
store i8* %36, i8** %10
%37 = load i8*, i8** %10
%38 = load i8*, i8** %4
%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 @sprintf(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %38, i8* %41)
%43 = load %struct.dirent*, %struct.dirent** %9
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = getelementptr inbounds i8, i8* %45, i64 0
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 46
br i1 %49, label %50, label %51
50: ; preds = %27
br label %17
51: ; preds = %27
%52 = load i8*, i8** %10
%53 = call i64 @stat(i8* %52, %struct.stat* %7)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %51
br label %17
56: ; preds = %51
%57 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i64 @S_ISDIR(i32 %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %56
%62 = load i8*, i8** %10
%63 = load i32, i32* %5
%64 = add nsw i32 %63, 1
%65 = load i32, i32* %6
br label %17
66: ; preds = %56
%67 = load i32, i32* %6
%68 = load i8*, i8** %10
%69 = load i32, i32* %5
%70 = add nsw i32 %69, 1
%71 = call i32 @upstack_push_top(i32 %67, i32* null, i32* null, i32* null, i8* %68, i32 %70)
br label %17
72: ; preds = %17
%73 = load i32*, i32** %8
%74 = call i32 @closedir(i32* %73)
br label %75
75: ; preds = %72, %13
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
define void @dir_add_songs(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = icmp sgt i32 %1, 3
br i1 %6, label %48, label %7
7: ; preds = %3
%8 = tail call i32* @opendir(i8* %0)
%9 = call %struct.dirent* @readdir(i32* %8)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %46, label %11
11: ; preds = %7
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%13 = add nsw i32 %1, 1
br label %14
14: ; preds = %11, %41
%15 = phi %struct.dirent* [ %9, %11 ], [ %42, %41 ]
%16 = phi i8* [ null, %11 ], [ %27, %41 ]
%17 = icmp eq i8* %16, null
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = call i32 @free(i8* %16)
br label %20
20: ; preds = %18, %14
%21 = call i64 @strlen(i8* dereferenceable(1) %0)
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = call i64 @strlen(i8* dereferenceable(1) %23)
%25 = add i64 %21, 1
%26 = add i64 %25, %24
%27 = call i8* @calloc(i64 %26, i32 1)
%28 = load i8*, i8** %22
%29 = call i32 @sprintf(i8* dereferenceable(1) %27, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %28)
%30 = load i8*, i8** %22
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 46
br i1 %32, label %41, label %33
33: ; preds = %20
%34 = call i64 @stat(i8* %27, %struct.stat* %4)
%35 = icmp slt i64 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %33
%37 = load i32, i32* %12
%38 = call i64 @S_ISDIR(i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %36
br label %41
41: ; preds = %40, %44, %20, %33
%42 = call %struct.dirent* @readdir(i32* %8)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %46, label %14
44: ; preds = %36
%45 = call i32 @upstack_push_top(i32 %2, i32* null, i32* null, i32* null, i8* %27, i32 %13)
br label %41
46: ; preds = %41, %7
%47 = call i32 @closedir(i32* %8)
br label %48
48: ; preds = %3, %46
ret void
}