#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__ IsImageFile(char *);
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ lstat(char *, struct stat *);
int /*<<< orphan*/ memset(struct stat *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void stub1(char *);
void ForAllImages(char *folder, void (*f)(char *)) {
  char dir_path[1024];
  DIR *dbDir = NULL;
  struct dirent *file = NULL;
  struct stat buf;
  dbDir = opendir(folder);
  if (!dbDir) {
    fprintf(stderr, "ERROR\n");
  }
  while ((file = readdir(dbDir)) != NULL) {
    memset(&buf, 0, sizeof(struct stat));
    strcpy(dir_path, folder);
    strcat(dir_path, "/");
    strcat(dir_path, file->d_name);
    lstat(dir_path, &buf);
    if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0 &&
        S_ISDIR(buf.st_mode)) {
      ForAllImages(dir_path, f);
    } else if (S_ISREG(buf.st_mode)) {
      if (IsImageFile(file->d_name)) {
        (*f)(dir_path);
      }
    }
  }
  closedir(dbDir);
}


x86 assembly (clang, -O0):
<ForAllImages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x418(%rbp)
movq   $0x0,-0x420(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    122c <ForAllImages+0x5c>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    1231 <ForAllImages+0x61>
mov    -0x418(%rbp),%rdi
call   10e0 <readdir@plt>
mov    %rax,-0x420(%rbp)
cmp    $0x0,%rax
je     1361 <ForAllImages+0x191>
lea    -0x428(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x410(%rbp),%rdi
lea    0xd88(%rip),%rsi
call   10f0 <strcat@plt>
lea    -0x410(%rbp),%rdi
mov    -0x420(%rbp),%rax
mov    (%rax),%rsi
call   10f0 <strcat@plt>
lea    -0x410(%rbp),%rdi
lea    -0x428(%rbp),%rsi
call   10a0 <lstat@plt>
mov    -0x420(%rbp),%rax
mov    (%rax),%rdi
lea    0xd4c(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
je     1317 <ForAllImages+0x147>
mov    -0x420(%rbp),%rax
mov    (%rax),%rdi
lea    0xd2b(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
je     1317 <ForAllImages+0x147>
mov    -0x428(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1317 <ForAllImages+0x147>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <ForAllImages@plt>
jmp    135c <ForAllImages+0x18c>
mov    -0x428(%rbp),%edi
call   10d0 <S_ISREG@plt>
cmp    $0x0,%rax
je     1357 <ForAllImages+0x187>
mov    -0x420(%rbp),%rax
mov    (%rax),%rdi
call   1090 <IsImageFile@plt>
cmp    $0x0,%rax
je     1352 <ForAllImages+0x182>
mov    -0x10(%rbp),%rax
lea    -0x410(%rbp),%rdi
call   *%rax
jmp    1357 <ForAllImages+0x187>
jmp    135c <ForAllImages+0x18c>
jmp    1231 <ForAllImages+0x61>
mov    -0x418(%rbp),%rdi
call   1080 <closedir@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ForAllImages>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r15
call   1050 <opendir@plt>
mov    %rax,%r12
test   %rax,%rax
jne    120b <ForAllImages+0x3b>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
call   10c0 <fprintf@plt>
mov    %r12,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     12ff <ForAllImages+0x12f>
mov    %rax,%rbp
mov    %rsp,%r13
lea    0x10(%rsp),%rbx
lea    0xddc(%rip),%r14
jmp    1251 <ForAllImages+0x81>
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
call   1040 <ForAllImages@plt>
mov    %r12,%rdi
call   10e0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12ff <ForAllImages+0x12f>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
lea    0xd92(%rip),%rsi
call   10f0 <strcat@plt>
mov    0x0(%rbp),%rsi
mov    %rbx,%rdi
call   10f0 <strcat@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10a0 <lstat@plt>
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     12d0 <ForAllImages+0x100>
mov    0x0(%rbp),%rdi
lea    0xd5c(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     12d0 <ForAllImages+0x100>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
jne    1230 <ForAllImages+0x60>
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%edi
call   10d0 <S_ISREG@plt>
test   %rax,%rax
je     123d <ForAllImages+0x6d>
mov    0x0(%rbp),%rdi
call   1090 <IsImageFile@plt>
test   %rax,%rax
je     123d <ForAllImages+0x6d>
mov    %rbx,%rdi
call   *0x8(%rsp)
jmp    123d <ForAllImages+0x6d>
mov    %r12,%rdi
call   1080 <closedir@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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