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

/* Variables and functions */
int S_IFMT;
int S_IFREG;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
void stub1(char *);
void aml_fio_dirwalk_files_only(char *dir, void (*fcn)(char *)) {
  char name[4096];
  struct dirent *dp;
  struct stat stbuf;
  DIR *dfd;
  if ((dfd = opendir(dir)) == NULL) {
    fprintf(stderr, "dirwalk: can't open %s\n", dir);
    exit(1);
  }
  while ((dp = readdir(dfd)) != NULL) {
    if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
      continue;
    if (strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name)) {
      fprintf(stderr, "dirwalk: name %s/%s to long\n", dir, dp->d_name);
      exit(1);
    } else {
      sprintf(name, "%s/%s", dir, dp->d_name);
      if (stat(name, &stbuf) == -1) {
        fprintf(stderr, "fileops: cannot access %s\n", name);
        exit(1);
      }
      if ((stbuf.st_mode & S_IFMT) == S_IFREG)
        (*fcn)(name);
    }
  }
  closedir(dfd);
}


x86 assembly (clang, -O0):
<aml_fio_dirwalk_files_only>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
jne    11e2 <aml_fio_dirwalk_files_only+0x52>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    11e7 <aml_fio_dirwalk_files_only+0x57>
mov    -0x1028(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
je     1349 <aml_fio_dirwalk_files_only+0x1b9>
mov    -0x1018(%rbp),%rax
mov    (%rax),%rdi
lea    0xe04(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     1242 <aml_fio_dirwalk_files_only+0xb2>
mov    -0x1018(%rbp),%rax
mov    (%rax),%rdi
lea    0xde4(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1247 <aml_fio_dirwalk_files_only+0xb7>
jmp    11e7 <aml_fio_dirwalk_files_only+0x57>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x102c(%rbp)
mov    -0x1018(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x102c(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
cmp    $0x1000,%rax
jbe    12af <aml_fio_dirwalk_files_only+0x11f>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x1018(%rbp),%rax
mov    (%rax),%rcx
lea    0xd7d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x1018(%rbp),%rax
mov    (%rax),%rcx
lea    0xd6d(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x1010(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    1314 <aml_fio_dirwalk_files_only+0x184>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rdx
lea    0xd3b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x1020(%rbp),%eax
mov    0x2cb7(%rip),%rcx
and    (%rcx),%eax
mov    0x2cbe(%rip),%rcx
cmp    (%rcx),%eax
jne    133f <aml_fio_dirwalk_files_only+0x1af>
mov    -0x10(%rbp),%rax
lea    -0x1010(%rbp),%rdi
call   *%rax
jmp    1344 <aml_fio_dirwalk_files_only+0x1b4>
jmp    11e7 <aml_fio_dirwalk_files_only+0x57>
mov    -0x1028(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aml_fio_dirwalk_files_only>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r15
call   1030 <opendir@plt>
test   %rax,%rax
je     12f2 <aml_fio_dirwalk_files_only+0x162>
mov    %rax,%r12
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     128a <aml_fio_dirwalk_files_only+0xfa>
mov    %rax,%rbp
lea    0xe44(%rip),%rbx
lea    0x10(%rsp),%r14
jmp    11f4 <aml_fio_dirwalk_files_only+0x64>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     128a <aml_fio_dirwalk_files_only+0xfa>
mov    0x0(%rbp),%r13
mov    %r13,%rdi
mov    %rbx,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11e0 <aml_fio_dirwalk_files_only+0x50>
mov    %r13,%rdi
lea    0xe07(%rip),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11e0 <aml_fio_dirwalk_files_only+0x50>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
mov    0x0(%rbp),%rdi
call   1040 <strlen@plt>
add    %r13d,%eax
add    $0xfffff001,%eax
cmp    $0xffffeffe,%eax
jbe    12a4 <aml_fio_dirwalk_files_only+0x114>
mov    0x0(%rbp),%rcx
mov    %r14,%rdi
lea    0xded(%rip),%rsi
mov    %r15,%rdx
call   10a0 <sprintf@plt>
mov    %r14,%rdi
mov    %rsp,%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     12cc <aml_fio_dirwalk_files_only+0x13c>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
and    (%rsp),%eax
mov    0x2d72(%rip),%rcx
cmp    (%rcx),%eax
jne    11e0 <aml_fio_dirwalk_files_only+0x50>
mov    %r14,%rdi
call   *0x8(%rsp)
jmp    11e0 <aml_fio_dirwalk_files_only+0x50>
mov    %r12,%rdi
call   1050 <closedir@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rcx
lea    0xd63(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
lea    0xd62(%rip),%rsi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
lea    0xcfe(%rip),%rsi
mov    %r15,%rdx
jmp    12e1 <aml_fio_dirwalk_files_only+0x151>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"dirwalk: can't open %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [29 x i8] c"dirwalk: name %s/%s to long\0A\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [27 x i8] c"fileops: cannot access %s\0A\00"
@S_IFMT = global i32 0
@S_IFREG = global i32 0
define void @aml_fio_dirwalk_files_only(i8* %0, void (i8*)* %1) {
%3 = alloca i8*
%4 = alloca void (i8*)*
%5 = alloca [4096 x i8]
%6 = alloca %struct.dirent*
%7 = alloca %struct.stat
%8 = alloca i32*
store i8* %0, i8** %3
store void (i8*)* %1, void (i8*)** %4
%9 = load i8*, i8** %3
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %8
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
br label %18
18: ; preds = %80, %34, %17
%19 = load i32*, i32** %8
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %6
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %81
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %6
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = call i32 @strcmp(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = load %struct.dirent*, %struct.dirent** %6
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = call i32 @strcmp(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp ne i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %28, %22
br label %18
35: ; preds = %28
%36 = load i8*, i8** %3
%37 = call i32 @strlen(i8* %36)
%38 = load %struct.dirent*, %struct.dirent** %6
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i32 @strlen(i8* %40)
%42 = add nsw i32 %37, %41
%43 = add nsw i32 %42, 2
%44 = sext i32 %43 to i64
%45 = icmp ugt i64 %44, 4096
br i1 %45, label %46, label %54
46: ; preds = %35
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %3
%49 = load %struct.dirent*, %struct.dirent** %6
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %48, i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %35
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%56 = load i8*, i8** %3
%57 = load %struct.dirent*, %struct.dirent** %6
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 0
%59 = load i8*, i8** %58
%60 = call i32 @sprintf(i8* %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %56, i8* %59)
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%62 = call i32 @stat(i8* %61, %struct.stat* %7)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %69
64: ; preds = %54
%65 = load i32, i32* @stderr
%66 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%67 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %66)
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %54
%70 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%71 = load i32, i32* %70
%72 = load i32, i32* @S_IFMT
%73 = and i32 %71, %72
%74 = load i32, i32* @S_IFREG
%75 = icmp eq i32 %73, %74
br i1 %75, label %76, label %79
76: ; preds = %69
%77 = load void (i8*)*, void (i8*)** %4
%78 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
br label %79
79: ; preds = %76, %69
br label %80
80: ; preds = %79
br label %18
81: ; preds = %18
%82 = load i32*, i32** %8
%83 = call i32 @closedir(i32* %82)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [24 x i8] c"dirwalk: can't open %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [29 x i8] c"dirwalk: name %s/%s to long\0A\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [27 x i8] c"fileops: cannot access %s\0A\00"
@S_IFMT = global i32 0
@S_IFREG = global i32 0
define void @aml_fio_dirwalk_files_only(i8* %0, void (i8*)* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [4096 x i8], [4096 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 %14, label %9
9: ; preds = %2
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %58, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %12, %27
%19 = phi %struct.dirent* [ %10, %12 ], [ %28, %27 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @strcmp(i8* dereferenceable(1) %21, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %18
%25 = call i32 @strcmp(i8* dereferenceable(1) %21, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %57, %51, %18, %24
%28 = call %struct.dirent* @readdir(i32* %7)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %58, label %18
30: ; preds = %24
%31 = call i32 @strlen(i8* %0)
%32 = load i8*, i8** %20
%33 = call i32 @strlen(i8* %32)
%34 = add i32 %31, -4095
%35 = add i32 %34, %33
%36 = icmp ult i32 %35, -4097
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %20
%40 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %30
%43 = load i8*, i8** %20
%44 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %43)
%45 = call i32 @stat(i8* %5, %struct.stat* %4)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %5)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %42
%52 = load i32, i32* %13
%53 = load i32, i32* @S_IFMT
%54 = and i32 %53, %52
%55 = load i32, i32* @S_IFREG
%56 = icmp eq i32 %54, %55
br i1 %56, label %57, label %27
57: ; preds = %51
br label %27
58: ; preds = %27, %9
%59 = call i32 @closedir(i32* %7)
ret void
}