#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 */
typedef int /*<<< orphan*/ uint8_t;
struct dirent {
  scalar_t__ d_type;
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_REG;
int /*<<< orphan*/ LLVMFuzzerTestOneInput(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(int /*<<< orphan*/, char *);
int fread(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  FILE *fp;
  uint8_t Data[0x1000];
  size_t Size;
  DIR *d;
  struct dirent *dir;
  int r = 0;
  if (argc != 2) {
    return 1;
  }
  d = opendir(argv[1]);
  if (d == NULL) {
    printf("Invalid directory\n");
    return 2;
  }
  if (chdir(argv[1]) != 0) {
    closedir(d);
    printf("Invalid directory\n");
    return 2;
  }
  while ((dir = readdir(d)) != NULL) {
    if (dir->d_type != DT_REG) {
      continue;
    }
    fp = fopen(dir->d_name, "rb");
    if (fp == NULL) {
      r = 3;
      break;
    }
    if (fseek(fp, 0L, SEEK_END) != 0) {
      fclose(fp);
      r = 4;
      break;
    }
    Size = ftell(fp);
    if (Size == (size_t)-1) {
      fclose(fp);
      r = 5;
      break;
    } else if (Size > 0x1000) {
      fclose(fp);
      continue;
    }
    if (fseek(fp, 0L, SEEK_SET) != 0) {
      fclose(fp);
      r = 7;
      break;
    }
    if (fread(Data, Size, 1, fp) != 1) {
      fclose(fp);
      r = 8;
      break;
    }
    LLVMFuzzerTestOneInput(Data, Size);
    fclose(fp);
  }
  closedir(d);
  return r;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x403c(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e9 <main+0x39>
movl   $0x1,-0x4(%rbp)
jmp    1410 <main+0x260>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x4030(%rbp)
cmpq   $0x0,-0x4030(%rbp)
jne    1223 <main+0x73>
lea    0xdee(%rip),%rdi
call   1070 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1410 <main+0x260>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
je     125e <main+0xae>
mov    -0x4030(%rbp),%rdi
call   1090 <closedir@plt>
lea    0xdb3(%rip),%rdi
call   1070 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1410 <main+0x260>
jmp    1263 <main+0xb3>
mov    -0x4030(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x4038(%rbp)
cmp    $0x0,%rax
je     13fb <main+0x24b>
mov    -0x4038(%rbp),%rax
mov    (%rax),%rax
mov    0x2d47(%rip),%rcx
cmp    (%rcx),%rax
je     129f <main+0xef>
jmp    1263 <main+0xb3>
mov    -0x4038(%rbp),%rax
mov    0x8(%rax),%edi
lea    0xd63(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12d3 <main+0x123>
movl   $0x3,-0x403c(%rbp)
jmp    13fb <main+0x24b>
mov    -0x18(%rbp),%rdi
mov    0x2d0a(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10c0 <fseek@plt>
cmp    $0x0,%rax
je     130b <main+0x15b>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x4,-0x403c(%rbp)
jmp    13fb <main+0x24b>
mov    -0x18(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %rax,-0x4028(%rbp)
cmpq   $0xffffffffffffffff,-0x4028(%rbp)
ff
jne    1341 <main+0x191>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x5,-0x403c(%rbp)
jmp    13fb <main+0x24b>
cmpq   $0x1000,-0x4028(%rbp)
jbe    1360 <main+0x1b0>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1263 <main+0xb3>
jmp    1365 <main+0x1b5>
mov    -0x18(%rbp),%rdi
mov    0x2c60(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10c0 <fseek@plt>
cmp    $0x0,%rax
je     139d <main+0x1ed>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x7,-0x403c(%rbp)
jmp    13fb <main+0x24b>
lea    -0x4020(%rbp),%rdi
mov    -0x4028(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     13da <main+0x22a>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x8,-0x403c(%rbp)
jmp    13fb <main+0x24b>
lea    -0x4020(%rbp),%rdi
mov    -0x4028(%rbp),%rsi
call   1080 <LLVMFuzzerTestOneInput@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1263 <main+0xb3>
mov    -0x4030(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x403c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4018,%rsp
mov    $0x1,%ebp
cmp    $0x2,%edi
jne    1338 <main+0x188>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     11f9 <main+0x49>
mov    %rax,%r12
mov    0x8(%rbx),%rdi
call   1070 <chdir@plt>
test   %rax,%rax
je     120f <main+0x5f>
mov    %r12,%rdi
call   1090 <closedir@plt>
lea    0xe03(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebp
jmp    1338 <main+0x188>
mov    %r12,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12fe <main+0x14e>
mov    0x2db1(%rip),%r13
lea    0xdd2(%rip),%r15
lea    0x10(%rsp),%r14
mov    %r12,0x8(%rsp)
jmp    1259 <main+0xa9>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12fe <main+0x14e>
mov    (%rax),%rcx
cmp    0x0(%r13),%rcx
jne    1248 <main+0x98>
mov    0x8(%rax),%edi
mov    %r15,%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1302 <main+0x152>
mov    %rax,%rbx
mov    0x2d68(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
test   %rax,%rax
jne    1309 <main+0x159>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     1310 <main+0x160>
mov    %rax,%rbp
cmp    $0x1001,%rax
jae    1240 <main+0x90>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
test   %rax,%rax
jne    1317 <main+0x167>
mov    %r13,%r12
mov    %r15,%r13
mov    %r14,%r15
mov    %r14,%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    131e <main+0x16e>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1080 <LLVMFuzzerTestOneInput@plt>
mov    %r13,%r15
mov    %r12,%r13
mov    0x8(%rsp),%r12
jmp    1240 <main+0x90>
xor    %ebp,%ebp
jmp    1330 <main+0x180>
mov    $0x3,%ebp
jmp    1330 <main+0x180>
mov    $0x4,%ebp
jmp    1328 <main+0x178>
mov    $0x5,%ebp
jmp    1328 <main+0x178>
mov    $0x7,%ebp
jmp    1328 <main+0x178>
mov    $0x8,%ebp
mov    0x8(%rsp),%r12
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1090 <closedir@plt>
mov    %ebp,%eax
add    $0x4018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i32 }
@.str = [19 x i8] c"Invalid directory\0A\00"
@DT_REG = global i64 0
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [4096 x i32]
%8 = alloca i64
%9 = alloca i32*
%10 = alloca %struct.dirent*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %11
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 1, i32* %3
br label %104
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32* @opendir(i8* %18)
store i32* %19, i32** %9
%20 = load i32*, i32** %9
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %3
br label %104
24: ; preds = %15
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i64 @chdir(i8* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = load i32*, i32** %9
%32 = call i32 @closedir(i32* %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %3
br label %104
34: ; preds = %24
br label %35
35: ; preds = %94, %73, %45, %34
%36 = load i32*, i32** %9
%37 = call %struct.dirent* @readdir(i32* %36)
store %struct.dirent* %37, %struct.dirent** %10
%38 = icmp ne %struct.dirent* %37, null
br i1 %38, label %39, label %100
39: ; preds = %35
%40 = load %struct.dirent*, %struct.dirent** %10
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = load i64, i64* @DT_REG
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %39
br label %35
46: ; preds = %39
%47 = load %struct.dirent*, %struct.dirent** %10
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = call i32* @fopen(i32 %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %50, i32** %6
%51 = load i32*, i32** %6
%52 = icmp eq i32* %51, null
br i1 %52, label %53, label %54
53: ; preds = %46
store i32 3, i32* %11
br label %100
54: ; preds = %46
%55 = load i32*, i32** %6
%56 = load i32, i32* @SEEK_END
%57 = call i64 @fseek(i32* %55, i64 0, i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
store i32 4, i32* %11
br label %100
62: ; preds = %54
%63 = load i32*, i32** %6
%64 = call i64 @ftell(i32* %63)
store i64 %64, i64* %8
%65 = load i64, i64* %8
%66 = icmp eq i64 %65, -1
br i1 %66, label %67, label %70
67: ; preds = %62
%68 = load i32*, i32** %6
%69 = call i32 @fclose(i32* %68)
store i32 5, i32* %11
br label %100
70: ; preds = %62
%71 = load i64, i64* %8
%72 = icmp ugt i64 %71, 4096
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = load i32*, i32** %6
%75 = call i32 @fclose(i32* %74)
br label %35
76: ; preds = %70
br label %77
77: ; preds = %76
%78 = load i32*, i32** %6
%79 = load i32, i32* @SEEK_SET
%80 = call i64 @fseek(i32* %78, i64 0, i32 %79)
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %77
%83 = load i32*, i32** %6
%84 = call i32 @fclose(i32* %83)
store i32 7, i32* %11
br label %100
85: ; preds = %77
%86 = getelementptr inbounds [4096 x i32], [4096 x i32]* %7, i64 0, i64 0
%87 = load i64, i64* %8
%88 = load i32*, i32** %6
%89 = call i32 @fread(i32* %86, i64 %87, i32 1, i32* %88)
%90 = icmp ne i32 %89, 1
br i1 %90, label %91, label %94
91: ; preds = %85
%92 = load i32*, i32** %6
%93 = call i32 @fclose(i32* %92)
store i32 8, i32* %11
br label %100
94: ; preds = %85
%95 = getelementptr inbounds [4096 x i32], [4096 x i32]* %7, i64 0, i64 0
%96 = load i64, i64* %8
%97 = call i32 @LLVMFuzzerTestOneInput(i32* %95, i64 %96)
%98 = load i32*, i32** %6
%99 = call i32 @fclose(i32* %98)
br label %35
100: ; preds = %91, %82, %67, %59, %53, %35
%101 = load i32*, i32** %9
%102 = call i32 @closedir(i32* %101)
%103 = load i32, i32* %11
store i32 %103, i32* %3
br label %104
104: ; preds = %100, %30, %22, %14
%105 = load i32, i32* %3
ret i32 %105
}

LLVM IR (-O2):
%struct.dirent = type { i64, i32 }
@DT_REG = global i64 0
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@str.2 = [18 x i8] c"Invalid directory\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i32]
%4 = bitcast [4096 x i32]* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %6, label %65
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @opendir(i8* %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.2, i64 0, i64 0))
br label %65
13: ; preds = %6
%14 = load i8*, i8** %7
%15 = tail call i64 @chdir(i8* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = call %struct.dirent* @readdir(i32* %9)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %62, label %20
20: ; preds = %17
%21 = getelementptr inbounds [4096 x i32], [4096 x i32]* %3, i64 0, i64 0
br label %25
22: ; preds = %13
%23 = tail call i32 @closedir(i32* %9)
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.2, i64 0, i64 0))
br label %65
25: ; preds = %20, %47
%26 = phi %struct.dirent* [ %18, %20 ], [ %48, %47 ]
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i64 0, i32 0
%28 = load i64, i64* %27
%29 = load i64, i64* @DT_REG
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %47
31: ; preds = %25
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i64 0, i32 1
%33 = load i32, i32* %32
%34 = call i32* @fopen(i32 %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%35 = icmp eq i32* %34, null
br i1 %35, label %62, label %36
36: ; preds = %31
%37 = load i32, i32* @SEEK_END
%38 = call i64 @fseek(i32* %34, i64 0, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %59
40: ; preds = %36
%41 = call i64 @ftell(i32* %34)
%42 = icmp eq i64 %41, -1
br i1 %42, label %59, label %43
43: ; preds = %40
%44 = icmp ugt i64 %41, 4096
br i1 %44, label %45, label %50
45: ; preds = %43, %57
%46 = call i32 @fclose(i32* %34)
br label %47
47: ; preds = %45, %25
%48 = call %struct.dirent* @readdir(i32* %9)
%49 = icmp eq %struct.dirent* %48, null
br i1 %49, label %62, label %25
50: ; preds = %43
%51 = load i32, i32* @SEEK_SET
%52 = call i64 @fseek(i32* %34, i64 0, i32 %51)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %50
%55 = call i32 @fread(i32* %21, i64 %41, i32 1, i32* %34)
%56 = icmp eq i32 %55, 1
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = call i32 @LLVMFuzzerTestOneInput(i32* %21, i64 %41)
br label %45
59: ; preds = %54, %50, %40, %36
%60 = phi i32 [ 4, %36 ], [ 5, %40 ], [ 7, %50 ], [ 8, %54 ]
%61 = call i32 @fclose(i32* %34)
br label %62
62: ; preds = %47, %31, %59, %17
%63 = phi i32 [ 0, %17 ], [ %60, %59 ], [ 0, %47 ], [ 3, %31 ]
%64 = call i32 @closedir(i32* %9)
br label %65
65: ; preds = %2, %62, %22, %11
%66 = phi i32 [ 2, %11 ], [ 2, %22 ], [ %63, %62 ], [ 1, %2 ]
ret i32 %66
}