#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 **);
void aml_fio_dirwalk_files_only_with_args(char *dir,
                                          void (*fcn)(char *, void **args),
                                          void **args) {
  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, args);
    }
  }
  closedir(dfd);
}


x86 assembly (clang, -O0):
<aml_fio_dirwalk_files_only_with_args>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x1038(%rbp)
cmp    $0x0,%rax
jne    11e6 <aml_fio_dirwalk_files_only_with_args+0x56>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    11eb <aml_fio_dirwalk_files_only_with_args+0x5b>
mov    -0x1038(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
je     1351 <aml_fio_dirwalk_files_only_with_args+0x1c1>
mov    -0x1028(%rbp),%rax
mov    (%rax),%rdi
lea    0xe00(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     1246 <aml_fio_dirwalk_files_only_with_args+0xb6>
mov    -0x1028(%rbp),%rax
mov    (%rax),%rdi
lea    0xde0(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    124b <aml_fio_dirwalk_files_only_with_args+0xbb>
jmp    11eb <aml_fio_dirwalk_files_only_with_args+0x5b>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x103c(%rbp)
mov    -0x1028(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x103c(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
cmp    $0x1000,%rax
jbe    12b3 <aml_fio_dirwalk_files_only_with_args+0x123>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x1028(%rbp),%rax
mov    (%rax),%rcx
lea    0xd79(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x1028(%rbp),%rax
mov    (%rax),%rcx
lea    0xd69(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x1020(%rbp),%rdi
lea    -0x1030(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    1318 <aml_fio_dirwalk_files_only_with_args+0x188>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
lea    -0x1020(%rbp),%rdx
lea    0xd37(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x1030(%rbp),%eax
mov    0x2cb3(%rip),%rcx
and    (%rcx),%eax
mov    0x2cba(%rip),%rcx
cmp    (%rcx),%eax
jne    1347 <aml_fio_dirwalk_files_only_with_args+0x1b7>
mov    -0x10(%rbp),%rax
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   *%rax
jmp    134c <aml_fio_dirwalk_files_only_with_args+0x1bc>
jmp    11eb <aml_fio_dirwalk_files_only_with_args+0x5b>
mov    -0x1038(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aml_fio_dirwalk_files_only_with_args>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    %rdx,0x18(%rsp)
mov    %rsi,0x10(%rsp)
mov    %rdi,%r12
call   1030 <opendir@plt>
test   %rax,%rax
je     1304 <aml_fio_dirwalk_files_only_with_args+0x174>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     129d <aml_fio_dirwalk_files_only_with_args+0x10d>
mov    %rax,%r14
lea    0xe3f(%rip),%r13
lea    0x20(%rsp),%r15
jmp    1204 <aml_fio_dirwalk_files_only_with_args+0x74>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     129d <aml_fio_dirwalk_files_only_with_args+0x10d>
mov    (%r14),%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11f0 <aml_fio_dirwalk_files_only_with_args+0x60>
mov    %rbp,%rdi
lea    0xdf8(%rip),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11f0 <aml_fio_dirwalk_files_only_with_args+0x60>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    (%r14),%rdi
call   1040 <strlen@plt>
add    %ebp,%eax
add    $0xfffff001,%eax
cmp    $0xffffeffe,%eax
jbe    12b7 <aml_fio_dirwalk_files_only_with_args+0x127>
mov    (%r14),%rcx
mov    %r15,%rdi
lea    0xde2(%rip),%rsi
mov    %r12,%rdx
call   10a0 <sprintf@plt>
mov    %r15,%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     12de <aml_fio_dirwalk_files_only_with_args+0x14e>
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
mov    0x2d64(%rip),%rcx
cmp    (%rcx),%eax
jne    11f0 <aml_fio_dirwalk_files_only_with_args+0x60>
mov    %r15,%rdi
mov    0x18(%rsp),%rsi
call   *0x10(%rsp)
jmp    11f0 <aml_fio_dirwalk_files_only_with_args+0x60>
mov    %rbx,%rdi
call   1050 <closedir@plt>
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rcx
lea    0xd51(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    0xd50(%rip),%rsi
lea    0x20(%rsp),%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
lea    0xcec(%rip),%rsi
mov    %r12,%rdx
jmp    12f3 <aml_fio_dirwalk_files_only_with_args+0x163>

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_with_args(i8* %0, void (i8*, i8**)* %1, i8** %2) {
%4 = alloca i8*
%5 = alloca void (i8*, i8**)*
%6 = alloca i8**
%7 = alloca [4096 x i8]
%8 = alloca %struct.dirent*
%9 = alloca %struct.stat
%10 = alloca i32*
store i8* %0, i8** %4
store void (i8*, i8**)* %1, void (i8*, i8**)** %5
store i8** %2, i8*** %6
%11 = load i8*, i8** %4
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %10
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %4
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %3
br label %20
20: ; preds = %83, %36, %19
%21 = load i32*, i32** %10
%22 = call %struct.dirent* @readdir(i32* %21)
store %struct.dirent* %22, %struct.dirent** %8
%23 = icmp ne %struct.dirent* %22, null
br i1 %23, label %24, label %84
24: ; preds = %20
%25 = load %struct.dirent*, %struct.dirent** %8
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = call i32 @strcmp(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %24
%31 = load %struct.dirent*, %struct.dirent** %8
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i32 @strcmp(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %30, %24
br label %20
37: ; preds = %30
%38 = load i8*, i8** %4
%39 = call i32 @strlen(i8* %38)
%40 = load %struct.dirent*, %struct.dirent** %8
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = call i32 @strlen(i8* %42)
%44 = add nsw i32 %39, %43
%45 = add nsw i32 %44, 2
%46 = sext i32 %45 to i64
%47 = icmp ugt i64 %46, 4096
br i1 %47, label %48, label %56
48: ; preds = %37
%49 = load i32, i32* @stderr
%50 = load i8*, i8** %4
%51 = load %struct.dirent*, %struct.dirent** %8
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 0
%53 = load i8*, i8** %52
%54 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %50, i8* %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %37
%57 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%58 = load i8*, i8** %4
%59 = load %struct.dirent*, %struct.dirent** %8
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i32 @sprintf(i8* %57, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %58, i8* %61)
%63 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%64 = call i32 @stat(i8* %63, %struct.stat* %9)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %71
66: ; preds = %56
%67 = load i32, i32* @stderr
%68 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%69 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %68)
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %56
%72 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%73 = load i32, i32* %72
%74 = load i32, i32* @S_IFMT
%75 = and i32 %73, %74
%76 = load i32, i32* @S_IFREG
%77 = icmp eq i32 %75, %76
br i1 %77, label %78, label %82
78: ; preds = %71
%79 = load void (i8*, i8**)*, void (i8*, i8**)** %5
%80 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%81 = load i8**, i8*** %6
br label %82
82: ; preds = %78, %71
br label %83
83: ; preds = %82
br label %20
84: ; preds = %20
%85 = load i32*, i32** %10
%86 = call i32 @closedir(i32* %85)
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_with_args(i8* %0, void (i8*, i8**)* nocapture %1, i8** %2) {
%4 = alloca [4096 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = bitcast %struct.stat* %5 to i8*
%8 = tail call i32* @opendir(i8* %0)
%9 = icmp eq i32* %8, null
br i1 %9, label %15, label %10
10: ; preds = %3
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %59, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %19
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %13, %28
%20 = phi %struct.dirent* [ %11, %13 ], [ %29, %28 ]
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @strcmp(i8* dereferenceable(1) %22, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %19
%26 = call i32 @strcmp(i8* dereferenceable(1) %22, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %58, %52, %19, %25
%29 = call %struct.dirent* @readdir(i32* %8)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %59, label %19
31: ; preds = %25
%32 = call i32 @strlen(i8* %0)
%33 = load i8*, i8** %21
%34 = call i32 @strlen(i8* %33)
%35 = add i32 %32, -4095
%36 = add i32 %35, %34
%37 = icmp ult i32 %36, -4097
br i1 %37, label %38, label %43
38: ; preds = %31
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %21
%41 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %31
%44 = load i8*, i8** %21
%45 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %44)
%46 = call i32 @stat(i8* %6, %struct.stat* %5)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %43
%53 = load i32, i32* %14
%54 = load i32, i32* @S_IFMT
%55 = and i32 %54, %53
%56 = load i32, i32* @S_IFREG
%57 = icmp eq i32 %55, %56
br i1 %57, label %58, label %28
58: ; preds = %52
br label %28
59: ; preds = %28, %10
%60 = call i32 @closedir(i32* %8)
ret void
}