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

/* Variables and functions */
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 /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
void stub1(char *);
void aml_fio_dirwalk(char *dir, void (*fcn)(char *)) {
  char name[4096];
  struct dirent *dp;
  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);
      (*fcn)(name);
    }
  }
  closedir(dfd);
}


x86 assembly (clang, -O0):
<aml_fio_dirwalk>:
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,-0x1020(%rbp)
cmp    $0x0,%rax
jne    11d2 <aml_fio_dirwalk+0x52>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11d7 <aml_fio_dirwalk+0x57>
mov    -0x1020(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
je     12d2 <aml_fio_dirwalk+0x152>
mov    -0x1018(%rbp),%rax
mov    (%rax),%rdi
lea    0xe14(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     1232 <aml_fio_dirwalk+0xb2>
mov    -0x1018(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf4(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1237 <aml_fio_dirwalk+0xb7>
jmp    11d7 <aml_fio_dirwalk+0x57>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1024(%rbp)
mov    -0x1018(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1024(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
cmp    $0x1000,%rax
jbe    129f <aml_fio_dirwalk+0x11f>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x1018(%rbp),%rax
mov    (%rax),%rcx
lea    0xd8d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x1018(%rbp),%rax
mov    (%rax),%rcx
lea    0xd7d(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rax
lea    -0x1010(%rbp),%rdi
call   *%rax
jmp    11d7 <aml_fio_dirwalk+0x57>
mov    -0x1020(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aml_fio_dirwalk>:
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     128b <aml_fio_dirwalk+0x10b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1271 <aml_fio_dirwalk+0xf1>
mov    %rax,%r13
lea    0xe54(%rip),%r12
jmp    1202 <aml_fio_dirwalk+0x82>
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%rcx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
lea    0xe55(%rip),%rsi
mov    %r15,%rdx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   *0x8(%rsp)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1271 <aml_fio_dirwalk+0xf1>
mov    0x0(%r13),%r14
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11f2 <aml_fio_dirwalk+0x72>
mov    %r14,%rdi
lea    0xdf9(%rip),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11f2 <aml_fio_dirwalk+0x72>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    0x0(%r13),%rdi
call   1040 <strlen@plt>
add    %ebp,%eax
add    $0xfffff001,%eax
cmp    $0xffffeffe,%eax
ja     11d0 <aml_fio_dirwalk+0x50>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
mov    0x0(%r13),%rcx
lea    0xdbe(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rbx,%rdi
call   1050 <closedir@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xd65(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%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"
define void @aml_fio_dirwalk(i8* %0, void (i8*)* %1) {
%3 = alloca i8*
%4 = alloca void (i8*)*
%5 = alloca [4096 x i8]
%6 = alloca %struct.dirent*
%7 = alloca i32*
store i8* %0, i8** %3
store void (i8*)* %1, void (i8*)** %4
%8 = load i8*, i8** %3
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %7
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %3
%14 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
br label %17
17: ; preds = %62, %33, %16
%18 = load i32*, i32** %7
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %6
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %63
21: ; preds = %17
%22 = load %struct.dirent*, %struct.dirent** %6
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i32 @strcmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load %struct.dirent*, %struct.dirent** %6
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i32 @strcmp(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %27, %21
br label %17
34: ; preds = %27
%35 = load i8*, i8** %3
%36 = call i32 @strlen(i8* %35)
%37 = load %struct.dirent*, %struct.dirent** %6
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 %36, %40
%42 = add nsw i32 %41, 2
%43 = sext i32 %42 to i64
%44 = icmp ugt i64 %43, 4096
br i1 %44, label %45, label %53
45: ; preds = %34
%46 = load i32, i32* @stderr
%47 = load i8*, i8** %3
%48 = load %struct.dirent*, %struct.dirent** %6
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %47, i8* %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %34
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%55 = load i8*, i8** %3
%56 = load %struct.dirent*, %struct.dirent** %6
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @sprintf(i8* %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %55, i8* %58)
%60 = load void (i8*)*, void (i8*)** %4
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
br label %62
62: ; preds = %53
br label %17
63: ; preds = %17
%64 = load i32*, i32** %7
%65 = call i32 @closedir(i32* %64)
ret void
}

LLVM IR (-O2):
%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"
define void @aml_fio_dirwalk(i8* %0, void (i8*)* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %41, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %7, %23
%15 = phi %struct.dirent* [ %24, %23 ], [ %8, %7 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = call i32 @strcmp(i8* dereferenceable(1) %17, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %14
%21 = call i32 @strcmp(i8* dereferenceable(1) %17, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %14, %20, %38
%24 = call %struct.dirent* @readdir(i32* %5)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %41, label %14
26: ; preds = %20
%27 = call i32 @strlen(i8* %0)
%28 = load i8*, i8** %16
%29 = call i32 @strlen(i8* %28)
%30 = add i32 %27, -4095
%31 = add i32 %30, %29
%32 = icmp ult i32 %31, -4097
br i1 %32, label %33, label %38
33: ; preds = %26
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %16
%36 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %26
%39 = load i8*, i8** %16
%40 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %39)
br label %23
41: ; preds = %23, %7
%42 = call i32 @closedir(i32* %5)
ret void
}