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

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ die(char *);
scalar_t__ is_dir(char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
scalar_t__ strcmp(char *, char *);
int strlen(char const *);
void dir_iterator(const char *path, int recursive,
                  void (*f)(const char *the_filename, void *the_userdata),
                  void *userdata) {
  DIR *dir = opendir(path);
  struct dirent *file;
  char fullname[PATH_MAX + 1];
  if (!dir)
    die("can't open directory");
  while ((file = readdir(dir)) != NULL) {
    if (strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)
      continue;
    if (path[strlen(path) - 1] == '/')
      snprintf(fullname, sizeof(fullname), "%s%s", path, file->d_name);
    else
      snprintf(fullname, sizeof(fullname), "%s/%s", path, file->d_name);
    if (recursive) {
      if (is_dir(fullname))
        dir_iterator(fullname, recursive, f, userdata);
    }
    f(fullname, userdata);
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<dir_iterator>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11fe <dir_iterator+0x6e>
lea    0xe07(%rip),%rdi
call   10b0 <die@plt>
jmp    1203 <dir_iterator+0x73>
mov    -0x28(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1313 <dir_iterator+0x183>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xdee(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1254 <dir_iterator+0xc4>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xdd0(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1259 <dir_iterator+0xc9>
jmp    1203 <dir_iterator+0x73>
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    12a9 <dir_iterator+0x119>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    (%rax),%r8
lea    0xd79(%rip),%rdx
call   1070 <snprintf@plt>
jmp    12ca <dir_iterator+0x13a>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    (%rax),%r8
lea    0xd58(%rip),%rdx
call   1070 <snprintf@plt>
cmpl   $0x0,-0xc(%rbp)
je     1300 <dir_iterator+0x170>
mov    -0x48(%rbp),%rdi
call   1060 <is_dir@plt>
cmp    $0x0,%rax
je     12fb <dir_iterator+0x16b>
mov    -0x48(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <dir_iterator@plt>
jmp    1300 <dir_iterator+0x170>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rsi
call   *%rax
jmp    1203 <dir_iterator+0x73>
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_iterator>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,-0x40(%rbp)
mov    %rdx,-0x38(%rbp)
mov    %esi,-0x2c(%rbp)
mov    %rdi,%rbx
call   1040 <opendir@plt>
mov    %rax,%r13
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%r15
mov    %rax,-0x48(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
test   %r13,%r13
jne    11e9 <dir_iterator+0x59>
lea    0xe1c(%rip),%rdi
call   10b0 <die@plt>
mov    %r13,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12b4 <dir_iterator+0x124>
mov    %rax,%r14
lea    0xe12(%rip),%r12
jmp    122e <dir_iterator+0x9e>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    -0x40(%rbp),%rsi
call   *-0x38(%rbp)
mov    %r13,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12b4 <dir_iterator+0x124>
mov    (%r14),%rdi
mov    %r12,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     121a <dir_iterator+0x8a>
mov    (%r14),%rdi
lea    0xdcd(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     121a <dir_iterator+0x8a>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
mov    (%r14),%r8
lea    0xdb2(%rip),%rdx
lea    0xda6(%rip),%rax
cmove  %rax,%rdx
mov    %r15,%rdi
mov    -0x48(%rbp),%rsi
mov    %rbx,%rcx
call   1070 <snprintf@plt>
cmpl   $0x0,-0x2c(%rbp)
je     1210 <dir_iterator+0x80>
mov    %r15,%rdi
call   1060 <is_dir@plt>
test   %rax,%rax
je     1210 <dir_iterator+0x80>
mov    %r15,%rdi
mov    -0x2c(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
call   1030 <dir_iterator@plt>
jmp    1210 <dir_iterator+0x80>
mov    %r13,%rdi
call   1080 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [21 x i8] c"can't open directory\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [5 x i8] c"%s%s\00"
@.str.4 = [6 x i8] c"%s/%s\00"
define void @dir_iterator(i8* %0, i32 %1, void (i8*, i8*)* nocapture %2, i8* %3) {
%5 = tail call i32* @opendir(i8* %0)
%6 = load i32, i32* @PATH_MAX
%7 = add nsw i32 %6, 1
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = icmp eq i32* %5, null
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = tail call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %4
%14 = call %struct.dirent* @readdir(i32* %5)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %46, label %16
16: ; preds = %13
%17 = icmp eq i32 %1, 0
br label %18
18: ; preds = %16, %28
%19 = phi %struct.dirent* [ %14, %16 ], [ %29, %28 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %18
%25 = load i8*, i8** %20
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %18, %24, %45
%29 = call %struct.dirent* @readdir(i32* %5)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %46, label %18
31: ; preds = %24
%32 = call i32 @strlen(i8* %0)
%33 = add nsw i32 %32, -1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %0, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 47
%38 = load i8*, i8** %20
%39 = select i1 %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0)
%40 = call i32 @snprintf(i8* %9, i32 %7, i8* %39, i8* %0, i8* %38)
br i1 %17, label %45, label %41
41: ; preds = %31
%42 = call i64 @is_dir(i8* %9)
%43 = icmp eq i64 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %41
br label %45
45: ; preds = %41, %44, %31
br label %28
46: ; preds = %28, %13
%47 = call i32 @closedir(i32* %5)
ret void
}