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

/* Variables and functions */
int PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ eprintf(char *, ...);
int lstat(char const *, struct stat *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
int strlcat(char *, char *, int);
int strlcpy(char *, char const *, int);
int strlen(char *);
void recurse(const char *path, void (*fn)(const char *)) {
  char buf[PATH_MAX];
  struct dirent *d;
  struct stat st;
  DIR *dp;
  if (lstat(path, &st) == -1 || S_ISDIR(st.st_mode) == 0)
    return;
  if (!(dp = opendir(path)))
    eprintf("opendir %s:", path);
  while ((d = readdir(dp))) {
    if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
      continue;
    if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
      eprintf("path too long\n");
    if (buf[strlen(buf) - 1] != '/')
      if (strlcat(buf, "/", sizeof(buf)) >= sizeof(buf))
        eprintf("path too long\n");
    if (strlcat(buf, d->d_name, sizeof(buf)) >= sizeof(buf))
      eprintf("path too long\n");
    fn(buf);
  }
  closedir(dp);
}


x86 assembly (clang, -O0):
<recurse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%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,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <lstat@plt>
cmp    $0xffffffff,%eax
je     1208 <recurse+0x68>
mov    -0x30(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    1214 <recurse+0x74>
movl   $0x1,-0x3c(%rbp)
jmp    1371 <recurse+0x1d1>
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    123d <recurse+0x9d>
mov    -0x8(%rbp),%rsi
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   10c0 <eprintf@plt>
jmp    1242 <recurse+0xa2>
mov    -0x38(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1361 <recurse+0x1c1>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xda6(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1293 <recurse+0xf3>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd88(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1298 <recurse+0xf8>
jmp    1242 <recurse+0xa2>
mov    -0x50(%rbp),%rax
mov    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    %eax,%edx
call   10b0 <strlcpy@plt>
mov    -0x50(%rbp),%rcx
cltq
cmp    %rcx,%rax
jb     12c8 <recurse+0x128>
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   10c0 <eprintf@plt>
mov    -0x48(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1322 <recurse+0x182>
mov    -0x50(%rbp),%rax
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
lea    0xd23(%rip),%rsi
call   1030 <strlcat@plt>
mov    -0x50(%rbp),%rcx
cltq
cmp    %rcx,%rax
jb     131d <recurse+0x17d>
lea    0xcf9(%rip),%rdi
mov    $0x0,%al
call   10c0 <eprintf@plt>
jmp    1322 <recurse+0x182>
mov    -0x50(%rbp),%rax
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rsi
mov    %eax,%edx
call   1030 <strlcat@plt>
mov    -0x50(%rbp),%rcx
cltq
cmp    %rcx,%rax
jb     1355 <recurse+0x1b5>
lea    0xcc1(%rip),%rdi
mov    $0x0,%al
call   10c0 <eprintf@plt>
mov    -0x48(%rbp),%rdi
call   *-0x10(%rbp)
jmp    1242 <recurse+0xa2>
mov    -0x38(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recurse>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,-0x40(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x38(%rbp),%rsi
mov    %rdi,-0x30(%rbp)
call   1080 <lstat@plt>
cmp    $0xffffffff,%eax
je     130e <recurse+0x16e>
mov    -0x38(%rbp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
je     130e <recurse+0x16e>
mov    -0x30(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,%r13
test   %rax,%rax
jne    121b <recurse+0x7b>
lea    0xdf0(%rip),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
call   10c0 <eprintf@plt>
mov    %r13,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1306 <recurse+0x166>
mov    %rax,%r15
lea    0xdd7(%rip),%r14
jmp    125a <recurse+0xba>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   *-0x40(%rbp)
mov    %r13,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1306 <recurse+0x166>
mov    (%r15),%rdi
mov    %r14,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1246 <recurse+0xa6>
mov    (%r15),%rdi
lea    0xd98(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1246 <recurse+0xa6>
mov    %rbx,%rdi
mov    -0x30(%rbp),%rsi
mov    %r12d,%edx
call   10b0 <strlcpy@plt>
cltq
cmp    %r12,%rax
jb     12a2 <recurse+0x102>
lea    0xd74(%rip),%rdi
xor    %eax,%eax
call   10c0 <eprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
je     12da <recurse+0x13a>
mov    %rbx,%rdi
lea    0xd61(%rip),%rsi
mov    %r12d,%edx
call   1030 <strlcat@plt>
cltq
cmp    %r12,%rax
jb     12da <recurse+0x13a>
lea    0xd3c(%rip),%rdi
xor    %eax,%eax
call   10c0 <eprintf@plt>
mov    (%r15),%rsi
mov    %rbx,%rdi
mov    %r12d,%edx
call   1030 <strlcat@plt>
cltq
cmp    %r12,%rax
jb     1240 <recurse+0xa0>
lea    0xd15(%rip),%rdi
xor    %eax,%eax
call   10c0 <eprintf@plt>
jmp    1240 <recurse+0xa0>
mov    %r13,%rdi
call   1070 <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* }
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [12 x i8] c"opendir %s:\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [15 x i8] c"path too long\0A\00"
@.str.4 = [2 x i8] c"/\00"
define void @recurse(i8* %0, void (i8*)* %1) {
%3 = alloca i8*
%4 = alloca void (i8*)*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.dirent*
%8 = alloca %struct.stat
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %3
store void (i8*)* %1, void (i8*)** %4
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = load i8*, i8** %3
%16 = call i32 @lstat(i8* %15, %struct.stat* %8)
%17 = icmp eq i32 %16, -1
br i1 %17, label %23, label %18
18: ; preds = %2
%19 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @S_ISDIR(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18, %2
store i32 1, i32* %10
br label %88
24: ; preds = %18
%25 = load i8*, i8** %3
%26 = call i32* @opendir(i8* %25)
store i32* %26, i32** %9
%27 = icmp ne i32* %26, null
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = load i8*, i8** %3
%30 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %28, %24
br label %32
32: ; preds = %83, %48, %31
%33 = load i32*, i32** %9
%34 = call %struct.dirent* @readdir(i32* %33)
store %struct.dirent* %34, %struct.dirent** %7
%35 = icmp ne %struct.dirent* %34, null
br i1 %35, label %36, label %85
36: ; preds = %32
%37 = load %struct.dirent*, %struct.dirent** %7
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %48, label %42
42: ; preds = %36
%43 = load %struct.dirent*, %struct.dirent** %7
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i64 @strcmp(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %42, %36
br label %32
49: ; preds = %42
%50 = load i8*, i8** %3
%51 = trunc i64 %12 to i32
%52 = call i32 @strlcpy(i8* %14, i8* %50, i32 %51)
%53 = sext i32 %52 to i64
%54 = icmp uge i64 %53, %12
br i1 %54, label %55, label %57
55: ; preds = %49
%56 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %57
57: ; preds = %55, %49
%58 = call i32 @strlen(i8* %14)
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %14, i64 %60
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp ne i32 %63, 47
br i1 %64, label %65, label %73
65: ; preds = %57
%66 = trunc i64 %12 to i32
%67 = call i32 @strlcat(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %66)
%68 = sext i32 %67 to i64
%69 = icmp uge i64 %68, %12
br i1 %69, label %70, label %72
70: ; preds = %65
%71 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %72
72: ; preds = %70, %65
br label %73
73: ; preds = %72, %57
%74 = load %struct.dirent*, %struct.dirent** %7
%75 = getelementptr inbounds %struct.dirent, %struct.dirent* %74, i32 0, i32 0
%76 = load i8*, i8** %75
%77 = trunc i64 %12 to i32
%78 = call i32 @strlcat(i8* %14, i8* %76, i32 %77)
%79 = sext i32 %78 to i64
%80 = icmp uge i64 %79, %12
br i1 %80, label %81, label %83
81: ; preds = %73
%82 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %83
83: ; preds = %81, %73
%84 = load void (i8*)*, void (i8*)** %4
br label %32
85: ; preds = %32
%86 = load i32*, i32** %9
%87 = call i32 @closedir(i32* %86)
store i32 0, i32* %10
br label %88
88: ; preds = %85, %23
%89 = load i8*, i8** %5
%90 = load i32, i32* %10
switch i32 %90, label %92 [
i32 0, label %91
i32 1, label %91
]
91: ; preds = %88, %88
ret void
92: ; preds = %88
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [12 x i8] c"opendir %s:\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [15 x i8] c"path too long\0A\00"
@.str.4 = [2 x i8] c"/\00"
define void @recurse(i8* %0, void (i8*)* nocapture %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.stat* %3 to i8*
%8 = call i32 @lstat(i8* %0, %struct.stat* %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %65, label %10
10: ; preds = %2
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i64 @S_ISDIR(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %65, label %15
15: ; preds = %10
%16 = tail call i32* @opendir(i8* %0)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %20
20: ; preds = %18, %15
%21 = call %struct.dirent* @readdir(i32* %16)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %63, label %23
23: ; preds = %20, %33
%24 = phi %struct.dirent* [ %34, %33 ], [ %21, %20 ]
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i64 0, i32 0
%26 = load i8*, i8** %25
%27 = call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %23
%30 = load i8*, i8** %25
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %23, %29, %62
%34 = call %struct.dirent* @readdir(i32* %16)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %63, label %23
36: ; preds = %29
%37 = call i32 @strlcpy(i8* %6, i8* %0, i32 %4)
%38 = sext i32 %37 to i64
%39 = icmp ult i64 %38, %5
br i1 %39, label %42, label %40
40: ; preds = %36
%41 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = call i32 @strlen(i8* %6)
%44 = add nsw i32 %43, -1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %6, i64 %45
%47 = load i8, i8* %46
%48 = icmp eq i8 %47, 47
br i1 %48, label %55, label %49
49: ; preds = %42
%50 = call i32 @strlcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %4)
%51 = sext i32 %50 to i64
%52 = icmp ult i64 %51, %5
br i1 %52, label %55, label %53
53: ; preds = %49
%54 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %49, %53, %42
%56 = load i8*, i8** %25
%57 = call i32 @strlcat(i8* %6, i8* %56, i32 %4)
%58 = sext i32 %57 to i64
%59 = icmp ult i64 %58, %5
br i1 %59, label %62, label %60
60: ; preds = %55
%61 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %60, %55
br label %33
63: ; preds = %33, %20
%64 = call i32 @closedir(i32* %16)
br label %65
65: ; preds = %2, %10, %63
ret void
}