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

/* Variables and functions */
int R_OK;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int X_OK;
scalar_t__ access(char const *, int);
scalar_t__ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *);
int /*<<< orphan*/ free(char *);
scalar_t__ lstat(char *, struct stat const *);
char *malloc_nofail(int);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char const *, char *);
int strlen(char const *);
int foreachdir_rec(const char *path, struct stat *st,
                   int (*walk)(const char *, const struct stat *)) {
  DIR *dir;
  struct dirent *dp;
  int failed = 0;
  char *p = malloc_nofail(4096);
  if (access(path, R_OK | X_OK) || !(dir = opendir(path)))
    return walk(path, NULL);
  while ((dp = readdir(dir))) {
    if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
      continue;
    if (strlen(path) + 1 + strlen(dp->d_name) + 1 > 4096) {
      fprintf(stderr, "%s/%s: name too long\n", path, dp->d_name);
      failed = -1;
      goto out;
    }
    sprintf(p, "%s/%s", path, dp->d_name);
    if (lstat(p, st))
      continue;
    if (S_ISDIR(st->st_mode)) {
      failed = foreachdir_rec(p, st, walk);
      if (failed)
        goto out;
    } else {
      failed = walk(p, st);
      if (failed)
        goto out;
    }
  }
  if (closedir(dir) != 0)
    failed = -1;
out:
  free(p);
  return failed;
}


x86 assembly (clang, -O0):
<foreachdir_rec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
mov    $0x1000,%edi
call   1030 <malloc_nofail@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
or     (%rax),%esi
call   10d0 <access@plt>
cmp    $0x0,%rax
jne    1235 <foreachdir_rec+0x65>
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    124b <foreachdir_rec+0x7b>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rdi
xor    %ecx,%ecx
mov    %ecx,%esi
call   *%rax
mov    %eax,-0x4(%rbp)
jmp    13cc <foreachdir_rec+0x1fc>
jmp    1250 <foreachdir_rec+0x80>
mov    -0x28(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     139e <foreachdir_rec+0x1ce>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xd8c(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
je     129f <foreachdir_rec+0xcf>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xd6f(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    12a4 <foreachdir_rec+0xd4>
jmp    1250 <foreachdir_rec+0x80>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cmp    $0x1000,%eax
jle    1300 <foreachdir_rec+0x130>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xd14(%rip),%rsi
call   10b0 <fprintf@plt>
movl   $0xffffffff,-0x34(%rbp)
jmp    13bd <foreachdir_rec+0x1ed>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xd03(%rip),%rsi
call   10e0 <sprintf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <lstat@plt>
cmp    $0x0,%rax
je     1337 <foreachdir_rec+0x167>
jmp    1250 <foreachdir_rec+0x80>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1374 <foreachdir_rec+0x1a4>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   10f0 <foreachdir_rec@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     136f <foreachdir_rec+0x19f>
jmp    13bd <foreachdir_rec+0x1ed>
jmp    1399 <foreachdir_rec+0x1c9>
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   *%rax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1394 <foreachdir_rec+0x1c4>
jmp    13bd <foreachdir_rec+0x1ed>
jmp    1399 <foreachdir_rec+0x1c9>
jmp    1250 <foreachdir_rec+0x80>
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
cmp    $0x0,%rax
je     13b8 <foreachdir_rec+0x1e8>
movl   $0xffffffff,-0x34(%rbp)
jmp    13bd <foreachdir_rec+0x1ed>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foreachdir_rec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %rsi,%rbx
mov    %rdi,%r15
mov    $0x1000,%edi
call   1030 <malloc_nofail@plt>
mov    %rax,%r14
mov    0x2dd7(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15,%rdi
call   10d0 <access@plt>
test   %rax,%rax
je     122a <foreachdir_rec+0x5a>
mov    %r15,%rdi
xor    %esi,%esi
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    *%rax
mov    %r15,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1211 <foreachdir_rec+0x41>
mov    %rax,%r12
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     131c <foreachdir_rec+0x14c>
mov    %rax,%rbp
jmp    1270 <foreachdir_rec+0xa0>
mov    (%rsp),%rdx
call   10f0 <foreachdir_rec@plt>
test   %eax,%eax
je     12e5 <foreachdir_rec+0x115>
jmp    1318 <foreachdir_rec+0x148>
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%rbp),%r13
mov    %r13,%rdi
lea    0xd83(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     12e5 <foreachdir_rec+0x115>
mov    %r13,%rdi
lea    0xd6f(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     12e5 <foreachdir_rec+0x115>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%r13d
mov    0x0(%rbp),%rdi
call   1060 <strlen@plt>
add    %r13d,%eax
add    $0x1,%eax
cmp    $0x1000,%eax
jge    1342 <foreachdir_rec+0x172>
mov    0x0(%rbp),%rcx
mov    %r14,%rdi
lea    0xd4c(%rip),%rsi
mov    %r15,%rdx
call   10e0 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <lstat@plt>
test   %rax,%rax
je     12fb <foreachdir_rec+0x12b>
mov    %r12,%rdi
call   10c0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1270 <foreachdir_rec+0xa0>
jmp    131c <foreachdir_rec+0x14c>
mov    (%rbx),%edi
call   1070 <S_ISDIR@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
test   %rax,%rax
jne    1250 <foreachdir_rec+0x80>
call   *(%rsp)
test   %eax,%eax
je     12e5 <foreachdir_rec+0x115>
mov    %eax,%ebp
jmp    1329 <foreachdir_rec+0x159>
mov    %r12,%rdi
call   1080 <closedir@plt>
neg    %rax
sbb    %ebp,%ebp
mov    %r14,%rdi
call   1040 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rcx
lea    0xcad(%rip),%rsi
mov    %r15,%rdx
call   10b0 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1329 <foreachdir_rec+0x159>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@R_OK = global i32 0
@X_OK = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"%s/%s: name too long\0A\00"
@.str.3 = [6 x i8] c"%s/%s\00"
define i32 @foreachdir_rec(i8* %0, %struct.stat* %1, i32 (i8*, %struct.stat*)* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.stat*
%7 = alloca i32 (i8*, %struct.stat*)*
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %5
store %struct.stat* %1, %struct.stat** %6
store i32 (i8*, %struct.stat*)* %2, i32 (i8*, %struct.stat*)** %7
store i32 0, i32* %10
%12 = call i8* @malloc_nofail(i32 4096)
store i8* %12, i8** %11
%13 = load i8*, i8** %5
%14 = load i32, i32* @R_OK
%15 = load i32, i32* @X_OK
%16 = or i32 %14, %15
%17 = call i64 @access(i8* %13, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = call i32* @opendir(i8* %20)
store i32* %21, i32** %8
%22 = icmp ne i32* %21, null
br i1 %22, label %27, label %23
23: ; preds = %19, %3
%24 = load i32 (i8*, %struct.stat*)*, i32 (i8*, %struct.stat*)** %7
%25 = load i8*, i8** %5
%26 = call i32 %24(i8* %25, %struct.stat* null)
store i32 %26, i32* %4
br label %110
27: ; preds = %19
br label %28
28: ; preds = %99, %74, %44, %27
%29 = load i32*, i32** %8
%30 = call %struct.dirent* @readdir(i32* %29)
store %struct.dirent* %30, %struct.dirent** %9
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %32, label %100
32: ; preds = %28
%33 = load %struct.dirent*, %struct.dirent** %9
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @strcmp(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %32
%39 = load %struct.dirent*, %struct.dirent** %9
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @strcmp(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %38, %32
br label %28
45: ; preds = %38
%46 = load i8*, i8** %5
%47 = call i32 @strlen(i8* %46)
%48 = add nsw i32 %47, 1
%49 = load %struct.dirent*, %struct.dirent** %9
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = call i32 @strlen(i8* %51)
%53 = add nsw i32 %48, %52
%54 = add nsw i32 %53, 1
%55 = icmp sgt i32 %54, 4096
br i1 %55, label %56, label %63
56: ; preds = %45
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %5
%59 = load %struct.dirent*, %struct.dirent** %9
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %58, i8* %61)
store i32 -1, i32* %10
br label %106
63: ; preds = %45
%64 = load i8*, i8** %11
%65 = load i8*, i8** %5
%66 = load %struct.dirent*, %struct.dirent** %9
%67 = getelementptr inbounds %struct.dirent, %struct.dirent* %66, i32 0, i32 0
%68 = load i8*, i8** %67
%69 = call i32 @sprintf(i8* %64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %65, i8* %68)
%70 = load i8*, i8** %11
%71 = load %struct.stat*, %struct.stat** %6
%72 = call i64 @lstat(i8* %70, %struct.stat* %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %63
br label %28
75: ; preds = %63
%76 = load %struct.stat*, %struct.stat** %6
%77 = getelementptr inbounds %struct.stat, %struct.stat* %76, i32 0, i32 0
%78 = load i32, i32* %77
%79 = call i64 @S_ISDIR(i32 %78)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %90
81: ; preds = %75
%82 = load i8*, i8** %11
%83 = load %struct.stat*, %struct.stat** %6
%84 = load i32 (i8*, %struct.stat*)*, i32 (i8*, %struct.stat*)** %7
%85 = call i32 @foreachdir_rec(i8* %82, %struct.stat* %83, i32 (i8*, %struct.stat*)* %84)
store i32 %85, i32* %10
%86 = load i32, i32* %10
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %89
88: ; preds = %81
br label %106
89: ; preds = %81
br label %99
90: ; preds = %75
%91 = load i32 (i8*, %struct.stat*)*, i32 (i8*, %struct.stat*)** %7
%92 = load i8*, i8** %11
%93 = load %struct.stat*, %struct.stat** %6
%94 = call i32 %91(i8* %92, %struct.stat* %93)
store i32 %94, i32* %10
%95 = load i32, i32* %10
%96 = icmp ne i32 %95, 0
br i1 %96, label %97, label %98
97: ; preds = %90
br label %106
98: ; preds = %90
br label %99
99: ; preds = %98, %89
br label %28
100: ; preds = %28
%101 = load i32*, i32** %8
%102 = call i64 @closedir(i32* %101)
%103 = icmp ne i64 %102, 0
br i1 %103, label %104, label %105
104: ; preds = %100
store i32 -1, i32* %10
br label %105
105: ; preds = %104, %100
br label %106
106: ; preds = %105, %97, %88, %56
%107 = load i8*, i8** %11
%108 = call i32 @free(i8* %107)
%109 = load i32, i32* %10
store i32 %109, i32* %4
br label %110
110: ; preds = %106, %23
%111 = load i32, i32* %4
ret i32 %111
}

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