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

/* Variables and functions */
int S_IEXEC;
int /*<<< orphan*/ S_ISREG(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ do_fuzz(char *, char *, int, int, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char const *);
scalar_t__ p_strlen(char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
char *realloc(char *, scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
scalar_t__ strlen(char *);
int begin_fuzz(const char *path, int fuzztype, int argc, char **argv) {
  DIR *dirp;
  struct dirent *dp;
  char *buff = NULL;
  struct stat statbuf;
  int ix = 0;
  if ((dirp = opendir(path)) == NULL) {
    fprintf(stderr, "Error opening specified directory [%s]\n", path);
    perror("opendir");
    exit(-1);
  }
  while ((dp = readdir(dirp))) {
    buff = realloc(buff, p_strlen(path) + 1 + strlen(dp->d_name) + 1);
    sprintf(buff, "%s/%s", path, dp->d_name);
    if (stat(buff, &statbuf)) {
      perror("stat");
      exit(-1);
    }
    if (!(strcmp(dp->d_name, "ifuzz")) || !(strcmp(dp->d_name, ".")) ||
        !(strcmp(dp->d_name, "..")) || !(statbuf.st_mode & S_IEXEC) ||
        !(S_ISREG(statbuf.st_mode)))
      continue;
    printf("Executable: %s\n", dp->d_name);
    ix++;
    do_fuzz(buff, dp->d_name, fuzztype, argc, argv);
  }
  free(buff);
  closedir(dirp);
  printf("Fuzzed %d files in directory %s\n", ix, path);
  return 0;
}


x86 assembly (clang, -O0):
<begin_fuzz>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    126b <begin_fuzz+0x6b>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdb0(%rip),%rsi
call   10b0 <fprintf@plt>
lea    0xdcc(%rip),%rdi
call   1100 <perror@plt>
mov    $0xffffffff,%edi
call   1120 <exit@plt>
jmp    1270 <begin_fuzz+0x70>
mov    -0x20(%rbp),%rdi
call   10e0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     13c8 <begin_fuzz+0x1c8>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <p_strlen@plt>
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   10f0 <realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
lea    0xd56(%rip),%rsi
call   1110 <sprintf@plt>
mov    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10c0 <stat@plt>
cmp    $0x0,%rax
je     130c <begin_fuzz+0x10c>
lea    0xd39(%rip),%rdi
call   1100 <perror@plt>
mov    $0xffffffff,%edi
call   1120 <exit@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd21(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
je     1386 <begin_fuzz+0x186>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd0c(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
je     1386 <begin_fuzz+0x186>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xcef(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
je     1386 <begin_fuzz+0x186>
mov    -0x38(%rbp),%eax
mov    0x2c6e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1386 <begin_fuzz+0x186>
mov    -0x38(%rbp),%edi
call   10d0 <S_ISREG@plt>
cmp    $0x0,%eax
jne    138b <begin_fuzz+0x18b>
jmp    1270 <begin_fuzz+0x70>
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
lea    0xcab(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0x18(%rbp),%r8
call   1070 <do_fuzz@plt>
jmp    1270 <begin_fuzz+0x70>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x3c(%rbp),%esi
mov    -0x8(%rbp),%rdx
lea    0xc6c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<begin_fuzz>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%rbx
mov    %edx,%r12d
mov    %esi,%ebp
mov    %rdi,%r13
call   1040 <opendir@plt>
test   %rax,%rax
je     13ba <begin_fuzz+0x1ba>
mov    %rax,%r14
mov    %rax,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     1364 <begin_fuzz+0x164>
mov    %rax,%r15
mov    %ebp,0x18(%rsp)
xor    %eax,%eax
mov    %rax,0x8(%rsp)
xor    %ebp,%ebp
mov    %rbx,0x20(%rsp)
mov    %r12d,0x1c(%rsp)
jmp    1274 <begin_fuzz+0x74>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   10e0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     136d <begin_fuzz+0x16d>
mov    %r13,%rdi
call   1090 <p_strlen@plt>
mov    %rax,%rbx
mov    (%r15),%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rsi
add    $0x2,%rsi
mov    %rbp,%rdi
call   10f0 <realloc@plt>
mov    %rax,%rbp
mov    (%r15),%rcx
mov    %rax,%rdi
lea    0xd89(%rip),%rsi
mov    %r13,%rdx
call   1110 <sprintf@plt>
mov    %rbp,%rdi
lea    0x10(%rsp),%rsi
call   10c0 <stat@plt>
test   %rax,%rax
jne    13a4 <begin_fuzz+0x1a4>
mov    (%r15),%r12
mov    %r12,%rdi
lea    0xd69(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1260 <begin_fuzz+0x60>
mov    %r12,%rdi
lea    0xd5d(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1260 <begin_fuzz+0x60>
mov    %r12,%rdi
lea    0xd45(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     1260 <begin_fuzz+0x60>
mov    0x10(%rsp),%edi
mov    0x2cc4(%rip),%rax
test   %edi,(%rax)
je     1260 <begin_fuzz+0x60>
call   10d0 <S_ISREG@plt>
test   %eax,%eax
je     1260 <begin_fuzz+0x60>
mov    (%r15),%rsi
lea    0xd11(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%rax
add    $0x1,%eax
mov    %rax,0x8(%rsp)
mov    (%r15),%rsi
mov    %rbp,%rdi
mov    0x18(%rsp),%edx
mov    0x1c(%rsp),%ecx
mov    0x20(%rsp),%r8
call   1070 <do_fuzz@plt>
jmp    1260 <begin_fuzz+0x60>
xor    %eax,%eax
mov    %rax,0x8(%rsp)
xor    %ebp,%ebp
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1080 <closedir@plt>
lea    0xcd0(%rip),%rdi
mov    0x8(%rsp),%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc8b(%rip),%rdi
call   1100 <perror@plt>
mov    $0xffffffff,%edi
call   1120 <exit@plt>
mov    0x2c1f(%rip),%rax
mov    (%rax),%edi
lea    0xc36(%rip),%rsi
mov    %r13,%rdx
call   10b0 <fprintf@plt>
lea    0xc4f(%rip),%rdi
jmp    13ab <begin_fuzz+0x1ab>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [40 x i8] c"Error opening specified directory [%s]\0A\00"
@.str.1 = [8 x i8] c"opendir\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [5 x i8] c"stat\00"
@.str.4 = [6 x i8] c"ifuzz\00"
@.str.5 = [2 x i8] c".\00"
@.str.6 = [3 x i8] c"..\00"
@S_IEXEC = global i32 0
@.str.7 = [16 x i8] c"Executable: %s\0A\00"
@.str.8 = [33 x i8] c"Fuzzed %d files in directory %s\0A\00"
define i32 @begin_fuzz(i8* %0, i32 %1, i32 %2, i8** %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca %struct.dirent*
%11 = alloca i8*
%12 = alloca %struct.stat
%13 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8** %3, i8*** %8
store i8* null, i8** %11
store i32 0, i32* %13
%14 = load i8*, i8** %5
%15 = call i32* @opendir(i8* %14)
store i32* %15, i32** %9
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %23
17: ; preds = %4
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %5
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %4
br label %24
24: ; preds = %82, %81, %23
%25 = load i32*, i32** %9
%26 = call %struct.dirent* @readdir(i32* %25)
store %struct.dirent* %26, %struct.dirent** %10
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %97
28: ; preds = %24
%29 = load i8*, i8** %11
%30 = load i8*, i8** %5
%31 = call i64 @p_strlen(i8* %30)
%32 = add nsw i64 %31, 1
%33 = load %struct.dirent*, %struct.dirent** %10
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i64 @strlen(i8* %35)
%37 = add nsw i64 %32, %36
%38 = add nsw i64 %37, 1
%39 = call i8* @realloc(i8* %29, i64 %38)
store i8* %39, i8** %11
%40 = load i8*, i8** %11
%41 = load i8*, i8** %5
%42 = load %struct.dirent*, %struct.dirent** %10
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %41, i8* %44)
%46 = load i8*, i8** %11
%47 = call i64 @stat(i8* %46, %struct.stat* %12)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %28
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %28
%53 = load %struct.dirent*, %struct.dirent** %10
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = call i32 @strcmp(i8* %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %81
58: ; preds = %52
%59 = load %struct.dirent*, %struct.dirent** %10
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i32 @strcmp(i8* %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %81
64: ; preds = %58
%65 = load %struct.dirent*, %struct.dirent** %10
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i8*, i8** %66
%68 = call i32 @strcmp(i8* %67, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %81
70: ; preds = %64
%71 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%72 = load i32, i32* %71
%73 = load i32, i32* @S_IEXEC
%74 = and i32 %72, %73
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %81
76: ; preds = %70
%77 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%78 = load i32, i32* %77
%79 = call i32 @S_ISREG(i32 %78)
%80 = icmp ne i32 %79, 0
br i1 %80, label %82, label %81
81: ; preds = %76, %70, %64, %58, %52
br label %24
82: ; preds = %76
%83 = load %struct.dirent*, %struct.dirent** %10
%84 = getelementptr inbounds %struct.dirent, %struct.dirent* %83, i32 0, i32 0
%85 = load i8*, i8** %84
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %85)
%87 = load i32, i32* %13
%88 = add nsw i32 %87, 1
store i32 %88, i32* %13
%89 = load i8*, i8** %11
%90 = load %struct.dirent*, %struct.dirent** %10
%91 = getelementptr inbounds %struct.dirent, %struct.dirent* %90, i32 0, i32 0
%92 = load i8*, i8** %91
%93 = load i32, i32* %6
%94 = load i32, i32* %7
%95 = load i8**, i8*** %8
%96 = call i32 @do_fuzz(i8* %89, i8* %92, i32 %93, i32 %94, i8** %95)
br label %24
97: ; preds = %24
%98 = load i8*, i8** %11
%99 = call i32 @free(i8* %98)
%100 = load i32*, i32** %9
%101 = call i32 @closedir(i32* %100)
%102 = load i32, i32* %13
%103 = load i8*, i8** %5
%104 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %102, i8* %103)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [40 x i8] c"Error opening specified directory [%s]\0A\00"
@.str.1 = [8 x i8] c"opendir\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [5 x i8] c"stat\00"
@.str.4 = [6 x i8] c"ifuzz\00"
@.str.5 = [2 x i8] c".\00"
@.str.6 = [3 x i8] c"..\00"
@S_IEXEC = global i32 0
@.str.7 = [16 x i8] c"Executable: %s\0A\00"
@.str.8 = [33 x i8] c"Fuzzed %d files in directory %s\0A\00"
define i32 @begin_fuzz(i8* %0, i32 %1, i32 %2, i8** %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = tail call i32* @opendir(i8* %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %14, label %9
9: ; preds = %4
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %69, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %65
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %0)
%17 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %65, %54
%20 = phi %struct.dirent* [ %66, %65 ], [ %55, %54 ]
%21 = phi i8* [ %68, %65 ], [ %28, %54 ]
%22 = call i64 @p_strlen(i8* %0)
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = call i64 @strlen(i8* dereferenceable(1) %24)
%26 = add i64 %22, 2
%27 = add i64 %26, %25
%28 = call i8* @realloc(i8* %21, i64 %27)
%29 = load i8*, i8** %23
%30 = call i32 @sprintf(i8* dereferenceable(1) %28, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %29)
%31 = call i64 @stat(i8* %28, %struct.stat* %5)
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %19
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %19
%37 = load i8*, i8** %23
%38 = call i32 @strcmp(i8* dereferenceable(1) %37, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%39 = icmp eq i32 %38, 0
br i1 %39, label %54, label %40
40: ; preds = %36
%41 = call i32 @strcmp(i8* dereferenceable(1) %37, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%42 = icmp eq i32 %41, 0
br i1 %42, label %54, label %43
43: ; preds = %40
%44 = call i32 @strcmp(i8* dereferenceable(1) %37, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%45 = icmp eq i32 %44, 0
br i1 %45, label %54, label %46
46: ; preds = %43
%47 = load i32, i32* %13
%48 = load i32, i32* @S_IEXEC
%49 = and i32 %48, %47
%50 = icmp eq i32 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %46
%52 = call i32 @S_ISREG(i32 %47)
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51, %46, %43, %40, %36
%55 = call %struct.dirent* @readdir(i32* %7)
%56 = icmp eq %struct.dirent* %55, null
br i1 %56, label %69, label %19
57: ; preds = %51
%58 = load i8*, i8** %23
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %58)
%60 = add nuw nsw i32 %67, 1
%61 = load i8*, i8** %23
%62 = call i32 @do_fuzz(i8* %28, i8* %61, i32 %1, i32 %2, i8** %3)
%63 = call %struct.dirent* @readdir(i32* %7)
%64 = icmp eq %struct.dirent* %63, null
br i1 %64, label %69, label %65
65: ; preds = %12, %57
%66 = phi %struct.dirent* [ %10, %12 ], [ %63, %57 ]
%67 = phi i32 [ 0, %12 ], [ %60, %57 ]
%68 = phi i8* [ null, %12 ], [ %28, %57 ]
br label %19
69: ; preds = %57, %54, %9
%70 = phi i32 [ 0, %9 ], [ %67, %54 ], [ %60, %57 ]
%71 = phi i8* [ null, %9 ], [ %28, %54 ], [ %28, %57 ]
%72 = call i32 @free(i8* %71)
%73 = call i32 @closedir(i32* %7)
%74 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %70, i8* %0)
ret i32 0
}