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

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
char *agetcwd();
int chdir(char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ eprintf(char *, char const *);
int /*<<< orphan*/ free(char *);
int lstat(char const *, struct stat *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char const *, char *);
void recurse(const char *path, void (*fn)(const char *)) {
  char *cwd;
  struct dirent *d;
  struct stat st;
  DIR *dp;
  if (lstat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
    return;
  } else if (!(dp = opendir(path))) {
    eprintf("opendir %s:", path);
  }
  cwd = agetcwd();
  if (chdir(path) == -1)
    eprintf("chdir %s:", path);
  while ((d = readdir(dp))) {
    if (strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
      fn(d->d_name);
  }
  closedir(dp);
  if (chdir(cwd) == -1)
    eprintf("chdir %s:", cwd);
  free(cwd);
}


x86 assembly (clang, -O0):
<recurse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <lstat@plt>
cmp    $0xffffffff,%eax
je     11d7 <recurse+0x37>
mov    -0x28(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11dc <recurse+0x3c>
jmp    12d1 <recurse+0x131>
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1203 <recurse+0x63>
mov    -0x8(%rbp),%rsi
lea    0xe02(%rip),%rdi
call   10b0 <eprintf@plt>
jmp    1208 <recurse+0x68>
mov    $0x0,%al
call   10c0 <agetcwd@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1235 <recurse+0x95>
mov    -0x8(%rbp),%rsi
lea    0xddc(%rip),%rdi
call   10b0 <eprintf@plt>
jmp    123a <recurse+0x9a>
mov    -0x30(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     129d <recurse+0xfd>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdb8(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1298 <recurse+0xf8>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd9a(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1298 <recurse+0xf8>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rdi
call   *%rax
jmp    123a <recurse+0x9a>
mov    -0x30(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x18(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    12c8 <recurse+0x128>
mov    -0x18(%rbp),%rsi
lea    0xd49(%rip),%rdi
call   10b0 <eprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recurse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsp,%rsi
call   1080 <lstat@plt>
cmp    $0xffffffff,%eax
je     127b <recurse+0xdb>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
je     127b <recurse+0xdb>
mov    %r12,%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f1 <recurse+0x51>
lea    0xe17(%rip),%rdi
mov    %r12,%rsi
call   10b0 <eprintf@plt>
xor    %eax,%eax
call   10c0 <agetcwd@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1217 <recurse+0x77>
lea    0xdfd(%rip),%rdi
mov    %r12,%rsi
call   10b0 <eprintf@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     128a <recurse+0xea>
mov    %rax,%rbp
lea    0xde9(%rip),%r12
lea    0xde1(%rip),%r13
jmp    1250 <recurse+0xb0>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     128a <recurse+0xea>
mov    0x0(%rbp),%rdi
mov    %r12,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1240 <recurse+0xa0>
mov    0x0(%rbp),%rdi
mov    %r13,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1240 <recurse+0xa0>
mov    0x0(%rbp),%rdi
call   *%r14
jmp    1240 <recurse+0xa0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    %r15,%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    12ae <recurse+0x10e>
lea    0xd66(%rip),%rdi
mov    %r15,%rsi
call   10b0 <eprintf@plt>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

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

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