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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
scalar_t__ strcmp(char *, char *);
void traverse(const char *dir, int (*fn)(const char *)) {
  DIR *d;
  struct dirent *de;
  d = opendir(dir);
  if (!d)
    return;
  while ((de = readdir(d))) {
    char fname[1024];
    struct stat st;
    if (strcmp(de->d_name, ".") == 0)
      continue;
    if (strcmp(de->d_name, "..") == 0)
      continue;
    sprintf(fname, "%s/%s", dir, de->d_name);
    if (lstat(fname, &st)) {
      perror(fname);
      continue;
    }
    if (S_ISDIR(st.st_mode)) {
      traverse(fname, fn);
    }
    fn(fname);
  }
  closedir(d);
}


x86 assembly (clang, -O0):
<traverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c0 <traverse+0x30>
jmp    12ac <traverse+0x11c>
jmp    11c5 <traverse+0x35>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12a3 <traverse+0x113>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe17(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    11fe <traverse+0x6e>
jmp    11c5 <traverse+0x35>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf4(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1220 <traverse+0x90>
jmp    11c5 <traverse+0x35>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
lea    0xdca(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
lea    -0x428(%rbp),%rsi
call   1070 <lstat@plt>
cmp    $0x0,%rax
je     126c <traverse+0xdc>
lea    -0x420(%rbp),%rdi
call   10a0 <perror@plt>
jmp    11c5 <traverse+0x35>
mov    -0x428(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1291 <traverse+0x101>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <traverse@plt>
mov    -0x10(%rbp),%rax
lea    -0x420(%rbp),%rdi
call   *%rax
jmp    11c5 <traverse+0x35>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<traverse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     1274 <traverse+0xe4>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     126c <traverse+0xdc>
mov    %rax,%r13
lea    0xe2e(%rip),%r12
lea    0x10(%rsp),%rbp
jmp    11f6 <traverse+0x66>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   *%r15
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     126c <traverse+0xdc>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11e6 <traverse+0x56>
mov    0x0(%r13),%rdi
lea    0xdee(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11e6 <traverse+0x56>
mov    0x0(%r13),%rcx
mov    %rbp,%rdi
lea    0xdd9(%rip),%rsi
mov    %r14,%rdx
call   10b0 <sprintf@plt>
mov    %rbp,%rdi
lea    0x8(%rsp),%rsi
call   1070 <lstat@plt>
test   %rax,%rax
jne    125f <traverse+0xcf>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %rax,%rax
je     11e0 <traverse+0x50>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1050 <traverse@plt>
jmp    11e0 <traverse+0x50>
mov    %rbp,%rdi
call   10a0 <perror@plt>
jmp    11e6 <traverse+0x56>
mov    %rbx,%rdi
call   1060 <closedir@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @traverse(i8* %0, i32 (i8*)* %1) {
%3 = alloca i8*
%4 = alloca i32 (i8*)*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca [1024 x i8]
%8 = alloca %struct.stat
store i8* %0, i8** %3
store i32 (i8*)* %1, i32 (i8*)** %4
%9 = load i8*, i8** %3
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
br label %61
14: ; preds = %2
br label %15
15: ; preds = %54, %43, %32, %25, %14
%16 = load i32*, i32** %5
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %6
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %58
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %6
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
br label %15
26: ; preds = %19
%27 = load %struct.dirent*, %struct.dirent** %6
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
br label %15
33: ; preds = %26
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%35 = load i8*, i8** %3
%36 = load %struct.dirent*, %struct.dirent** %6
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %35, i8* %38)
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%41 = call i64 @lstat(i8* %40, %struct.stat* %8)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %33
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%45 = call i32 @perror(i8* %44)
br label %15
46: ; preds = %33
%47 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i64 @S_ISDIR(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %46
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%53 = load i32 (i8*)*, i32 (i8*)** %4
br label %54
54: ; preds = %51, %46
%55 = load i32 (i8*)*, i32 (i8*)** %4
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%57 = call i32 %55(i8* %56)
br label %15
58: ; preds = %15
%59 = load i32*, i32** %5
%60 = call i32 @closedir(i32* %59)
br label %61
61: ; preds = %58, %13
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @traverse(i8* %0, i32 (i8*)* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %43, 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 %10
10: ; preds = %7
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%12 = bitcast %struct.stat* %4 to i8*
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %14
14: ; preds = %10, %38
%15 = phi %struct.dirent* [ %8, %10 ], [ %39, %38 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %38, label %20
20: ; preds = %14
%21 = load i8*, i8** %16
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %38, label %24
24: ; preds = %20
%25 = load i8*, i8** %16
%26 = call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %25)
%27 = call i64 @lstat(i8* %11, %struct.stat* %4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %24
%30 = call i32 @perror(i8* %11)
br label %38
31: ; preds = %24
%32 = load i32, i32* %13
%33 = call i64 @S_ISDIR(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %31
br label %36
36: ; preds = %35, %31
%37 = call i32 %1(i8* %11)
br label %38
38: ; preds = %20, %14, %36, %29
%39 = call %struct.dirent* @readdir(i32* %5)
%40 = icmp eq %struct.dirent* %39, null
br i1 %40, label %41, label %14
41: ; preds = %38, %7
%42 = call i32 @closedir(i32* %5)
br label %43
43: ; preds = %2, %41
ret void
}