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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ get_file_path(char *, int /*<<< orphan*/, char *);
scalar_t__ is_dir(char *);
scalar_t__ is_file(char *);
scalar_t__ is_special_dir(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int readdir_r(int /*<<< orphan*/ *, struct dirent *, struct dirent **);
int recurse_dir(char *path, int (*func)(char *, void *)) {
  DIR *dir;
  struct dirent ent;
  struct dirent *result;
  char file_path[PATH_MAX];
  static int level = 0;
  int ret;
  if (is_file(path)) {
    func(path, &level);
    return 0;
  } else if (is_dir(path)) {
    func(path, &level);
    if ((dir = opendir(path)) == NULL) {
      printf("open %s failed\n", path);
      perror("    ");
      return 1;
    }
    level++;
    while (1) {
      ret = readdir_r(dir, &ent, &result);
      if (ret != 0) {
        printf("readdir %s failed\n", path);
        perror("     ");
        break;
      } else if (ret == 0 && result == NULL)
        break;
      get_file_path(path, ent.d_name, file_path);
      if (is_special_dir(ent.d_name))
        continue;
      recurse_dir(file_path, func);
    }
    closedir(dir);
    level--;
  } else {
    printf("un-supported type\n");
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<recurse_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <is_file@plt>
cmp    $0x0,%rax
je     1213 <recurse_dir+0x73>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
lea    0x2e7a(%rip),%rsi
call   *%rax
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    137d <recurse_dir+0x1dd>
mov    -0x10(%rbp),%rdi
call   1070 <is_dir@plt>
cmp    $0x0,%rax
je     1349 <recurse_dir+0x1a9>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rdi
lea    0x2e43(%rip),%rsi
call   *%rax
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    127f <recurse_dir+0xdf>
mov    -0x10(%rbp),%rsi
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdbd(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    137d <recurse_dir+0x1dd>
mov    0x2df3(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2dea(%rip)
jmp    1293 <recurse_dir+0xf3>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1030 <readdir_r@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     12d4 <recurse_dir+0x134>
mov    -0x10(%rbp),%rsi
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd59(%rip),%rdi
call   10c0 <perror@plt>
jmp    132c <recurse_dir+0x18c>
cmpl   $0x0,-0x44(%rbp)
jne    12ee <recurse_dir+0x14e>
cmpq   $0x0,-0x30(%rbp)
jne    12ee <recurse_dir+0x14e>
jmp    132c <recurse_dir+0x18c>
jmp    12f3 <recurse_dir+0x153>
mov    -0x50(%rbp),%rdx
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   10b0 <get_file_path@plt>
mov    -0x28(%rbp),%edi
call   1050 <is_special_dir@plt>
cmp    $0x0,%rax
je     131a <recurse_dir+0x17a>
jmp    128e <recurse_dir+0xee>
mov    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <recurse_dir@plt>
jmp    128e <recurse_dir+0xee>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
mov    0x2d3d(%rip),%eax
add    $0xffffffff,%eax
mov    %eax,0x2d34(%rip)
jmp    136a <recurse_dir+0x1ca>
lea    0xcd9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    137d <recurse_dir+0x1dd>
jmp    136f <recurse_dir+0x1cf>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recurse_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r13
mov    %rdi,%r15
mov    %rsp,%r14
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
call   10b0 <is_file@plt>
test   %rax,%rax
je     1202 <recurse_dir+0x52>
lea    0x2e8b(%rip),%rsi
mov    %r15,%rdi
call   *%r13
xor    %eax,%eax
jmp    12f5 <recurse_dir+0x145>
mov    %r15,%rdi
call   1080 <is_dir@plt>
test   %rax,%rax
je     12b2 <recurse_dir+0x102>
lea    0x2e66(%rip),%rsi
mov    %r15,%rdi
call   *%r13
mov    %r15,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1307 <recurse_dir+0x157>
mov    %rax,%r12
mov    %r13,-0x48(%rbp)
mov    %r14,-0x40(%rbp)
addl   $0x1,0x2e3d(%rip)
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
mov    %rax,%rdi
call   1040 <readdir_r@plt>
test   %eax,%eax
jne    12c3 <recurse_dir+0x113>
lea    -0x30(%rbp),%r13
lea    -0x38(%rbp),%r14
jmp    1282 <recurse_dir+0xd2>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   1040 <readdir_r@plt>
test   %eax,%eax
jne    12c3 <recurse_dir+0x113>
cmpq   $0x0,-0x38(%rbp)
je     12e0 <recurse_dir+0x130>
mov    -0x30(%rbp),%esi
mov    %r15,%rdi
mov    %rbx,%rdx
call   10c0 <get_file_path@plt>
mov    -0x30(%rbp),%edi
call   1060 <is_special_dir@plt>
test   %rax,%rax
jne    1270 <recurse_dir+0xc0>
mov    %rbx,%rdi
mov    -0x48(%rbp),%rsi
call   10a0 <recurse_dir@plt>
jmp    1270 <recurse_dir+0xc0>
lea    0xd70(%rip),%rdi
call   1030 <puts@plt>
jmp    11fb <recurse_dir+0x4b>
lea    0xd46(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xd48(%rip),%rdi
call   10d0 <perror@plt>
mov    %r12,%rdi
call   1090 <closedir@plt>
addl   $0xffffffff,0x2d91(%rip)
xor    %eax,%eax
mov    -0x40(%rbp),%r14
mov    %r14,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf2(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xd05(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%eax
jmp    12f5 <recurse_dir+0x145>

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@recurse_dir.level = internal global i32 0
@.str = [16 x i8] c"open %s failed\0A\00"
@.str.1 = [5 x i8] c" \00"
@.str.2 = [19 x i8] c"readdir %s failed\0A\00"
@.str.3 = [6 x i8] c" \00"
@str = [18 x i8] c"un-supported type\00"
define i32 @recurse_dir(i8* %0, i32 (i8*, i8*)* nocapture %1) {
%3 = alloca %struct.dirent
%4 = alloca %struct.dirent*
%5 = bitcast %struct.dirent* %3 to i8*
%6 = bitcast %struct.dirent** %4 to i8*
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = tail call i64 @is_file(i8* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = tail call i32 %1(i8* %0, i8* bitcast (i32* @recurse_dir.level to i8*))
br label %55
15: ; preds = %2
%16 = tail call i64 @is_dir(i8* %0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %53, label %18
18: ; preds = %15
%19 = tail call i32 %1(i8* %0, i8* bitcast (i32* @recurse_dir.level to i8*))
%20 = tail call i32* @opendir(i8* %0)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0)
%24 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %55
25: ; preds = %18
%26 = load i32, i32* @recurse_dir.level
%27 = add nsw i32 %26, 1
store i32 %27, i32* @recurse_dir.level
%28 = call i32 @readdir_r(i32* %20, %struct.dirent* %3, %struct.dirent** %4)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %3, i64 0, i32 0
br label %35
32: ; preds = %46, %25
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %49
35: ; preds = %30, %46
%36 = load %struct.dirent*, %struct.dirent** %4
%37 = icmp eq %struct.dirent* %36, null
br i1 %37, label %49, label %38
38: ; preds = %35
%39 = load i32, i32* %31
%40 = call i32 @get_file_path(i8* %0, i32 %39, i8* %10)
%41 = load i32, i32* %31
%42 = call i64 @is_special_dir(i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 @recurse_dir(i8* %10, i32 (i8*, i8*)* %1)
br label %46
46: ; preds = %44, %38
%47 = call i32 @readdir_r(i32* %20, %struct.dirent* %3, %struct.dirent** %4)
%48 = icmp eq i32 %47, 0
br i1 %48, label %35, label %32
49: ; preds = %35, %32
%50 = call i32 @closedir(i32* %20)
%51 = load i32, i32* @recurse_dir.level
%52 = add nsw i32 %51, -1
store i32 %52, i32* @recurse_dir.level
br label %55
53: ; preds = %15
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %55
55: ; preds = %49, %53, %22, %13
%56 = phi i32 [ 0, %13 ], [ 1, %22 ], [ 0, %49 ], [ 0, %53 ]
ret i32 %56
}