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

/* Variables and functions */
scalar_t__ DT_DIR;
scalar_t__ PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ freeChar(char **);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ outerr(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strmcat(char **, char *);
scalar_t__ strmcmp(char *, char *);
size_t strmlen(char *);
char *strmrchr(char *, char);
void stub1(char *, int);
void stub2(char *, int);
void dirWalkR(char *dir, void (*fcn)(char *, int)) {
  char *name = NULL;
  struct dirent *dp;
  DIR *dfd;
  if ((dfd = opendir(dir)) == NULL) {
    outerr("dirWalk: can't open");
    return;
  }
  while ((dp = readdir(dfd)) != NULL) {
    if (strmcmp(dp->d_name, ".") == 0 || strmcmp(dp->d_name, "..") == 0)
      continue;
    if (strmlen(dir) + strmlen(dp->d_name) + 1 > PATH_MAX)
      outerr("name too long\n");
    else {
      char *separator = strmrchr(dir, '/');
      if (&dir[strmlen(dir) - 1] != separator) {
        strmcat(&name, dir);
        strmcat(&name, "/");
      }
      strmcat(&name, dp->d_name);
      if (dp->d_type == DT_DIR) {
        (*fcn)(name, 0);
        dirWalkR(name, fcn);
      } else
        (*fcn)(name, 1);
      freeChar(&name);
    }
  }
  closedir(dfd);
}


x86 assembly (clang, -O0):
<dirWalkR>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11e0 <dirWalkR+0x40>
lea    0xe2a(%rip),%rdi
call   10c0 <outerr@plt>
jmp    1347 <dirWalkR+0x1a7>
jmp    11e5 <dirWalkR+0x45>
mov    -0x28(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     133e <dirWalkR+0x19e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe0b(%rip),%rsi
call   1070 <strmcmp@plt>
cmp    $0x0,%rax
je     1236 <dirWalkR+0x96>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xded(%rip),%rsi
call   1070 <strmcmp@plt>
cmp    $0x0,%rax
jne    123b <dirWalkR+0x9b>
jmp    11e5 <dirWalkR+0x45>
mov    -0x8(%rbp),%rdi
call   1050 <strmlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strmlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
add    $0x1,%rax
mov    0x2d87(%rip),%rcx
cmp    (%rcx),%rax
jbe    1283 <dirWalkR+0xe3>
lea    0xd9e(%rip),%rdi
call   10c0 <outerr@plt>
jmp    1339 <dirWalkR+0x199>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strmrchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strmlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
sub    $0x1,%rcx
add    %rcx,%rax
cmp    -0x30(%rbp),%rax
je     12db <dirWalkR+0x13b>
mov    -0x8(%rbp),%rsi
lea    -0x18(%rbp),%rdi
call   10b0 <strmcat@plt>
lea    -0x18(%rbp),%rdi
lea    0xd50(%rip),%rsi
call   10b0 <strmcat@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    -0x18(%rbp),%rdi
call   10b0 <strmcat@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2cde(%rip),%rcx
cmp    (%rcx),%rax
jne    1321 <dirWalkR+0x181>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   *%rax
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <dirWalkR@plt>
jmp    1330 <dirWalkR+0x190>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   *%rax
lea    -0x18(%rbp),%rdi
call   1030 <freeChar@plt>
jmp    11e5 <dirWalkR+0x45>
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirWalkR>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdi,%r15
movq   $0x0,0x8(%rsp)
call   1060 <opendir@plt>
test   %rax,%rax
je     130a <dirWalkR+0x16a>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12f3 <dirWalkR+0x153>
mov    %rax,%r14
lea    0xe2a(%rip),%r12
lea    0xe22(%rip),%rbp
jmp    1214 <dirWalkR+0x74>
lea    0xe1c(%rip),%rdi
call   10c0 <outerr@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12f3 <dirWalkR+0x153>
mov    (%r14),%rdi
mov    %r12,%rsi
call   1070 <strmcmp@plt>
test   %rax,%rax
je     1200 <dirWalkR+0x60>
mov    (%r14),%rdi
mov    %rbp,%rsi
call   1070 <strmcmp@plt>
test   %rax,%rax
je     1200 <dirWalkR+0x60>
mov    %r15,%rdi
call   1050 <strmlen@plt>
mov    %rax,%r13
mov    (%r14),%rdi
call   1050 <strmlen@plt>
add    %r13,%rax
add    $0x1,%rax
mov    0x2d9b(%rip),%rcx
cmp    (%rcx),%rax
ja     11f4 <dirWalkR+0x54>
mov    %rbp,%r13
mov    %r15,%rdi
mov    $0x2f,%esi
call   1040 <strmrchr@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1050 <strmlen@plt>
add    %r15,%rax
add    $0xffffffffffffffff,%rax
cmp    %rbp,%rax
lea    0x8(%rsp),%rbp
je     12a0 <dirWalkR+0x100>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10b0 <strmcat@plt>
mov    %rbp,%rdi
lea    0xd8b(%rip),%rsi
call   10b0 <strmcat@plt>
mov    (%r14),%rsi
mov    %rbp,%rdi
call   10b0 <strmcat@plt>
mov    0x8(%r14),%rax
mov    0x8(%rsp),%rdi
mov    0x2d1d(%rip),%rcx
cmp    (%rcx),%rax
jne    12d8 <dirWalkR+0x138>
xor    %esi,%esi
mov    0x10(%rsp),%rbp
call   *%rbp
mov    0x8(%rsp),%rdi
mov    %rbp,%rsi
call   1090 <dirWalkR@plt>
jmp    12e1 <dirWalkR+0x141>
mov    $0x1,%esi
call   *0x10(%rsp)
mov    %r13,%rbp
lea    0x8(%rsp),%rdi
call   1030 <freeChar@plt>
jmp    1200 <dirWalkR+0x60>
mov    %rbx,%rdi
call   1080 <closedir@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcef(%rip),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10c0 <outerr@plt>

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@.str = [20 x i8] c"dirWalk: can't open\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@PATH_MAX = global i64 0
@.str.3 = [15 x i8] c"name too long\0A\00"
@.str.4 = [2 x i8] c"/\00"
@DT_DIR = global i64 0
define void @dirWalkR(i8* %0, void (i8*, i32)* %1) {
%3 = alloca i8*
%4 = alloca void (i8*, i32)*
%5 = alloca i8*
%6 = alloca %struct.dirent*
%7 = alloca i32*
%8 = alloca i8*
store i8* %0, i8** %3
store void (i8*, i32)* %1, void (i8*, i32)** %4
store i8* null, i8** %5
%9 = load i8*, i8** %3
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @outerr(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %83
14: ; preds = %2
br label %15
15: ; preds = %79, %31, %14
%16 = load i32*, i32** %7
%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 %80
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 @strmcmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %19
%26 = load %struct.dirent*, %struct.dirent** %6
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i64 @strmcmp(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25, %19
br label %15
32: ; preds = %25
%33 = load i8*, i8** %3
%34 = call i64 @strmlen(i8* %33)
%35 = load %struct.dirent*, %struct.dirent** %6
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i64 @strmlen(i8* %37)
%39 = add i64 %34, %38
%40 = add i64 %39, 1
%41 = load i64, i64* @PATH_MAX
%42 = icmp ugt i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %32
%44 = call i32 @outerr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %79
45: ; preds = %32
%46 = load i8*, i8** %3
%47 = call i8* @strmrchr(i8* %46, i8 signext 47)
store i8* %47, i8** %8
%48 = load i8*, i8** %3
%49 = load i8*, i8** %3
%50 = call i64 @strmlen(i8* %49)
%51 = sub i64 %50, 1
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = load i8*, i8** %8
%54 = icmp ne i8* %52, %53
br i1 %54, label %55, label %59
55: ; preds = %45
%56 = load i8*, i8** %3
%57 = call i32 @strmcat(i8** %5, i8* %56)
%58 = call i32 @strmcat(i8** %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %59
59: ; preds = %55, %45
%60 = load %struct.dirent*, %struct.dirent** %6
%61 = getelementptr inbounds %struct.dirent, %struct.dirent* %60, i32 0, i32 0
%62 = load i8*, i8** %61
%63 = call i32 @strmcat(i8** %5, i8* %62)
%64 = load %struct.dirent*, %struct.dirent** %6
%65 = getelementptr inbounds %struct.dirent, %struct.dirent* %64, i32 0, i32 1
%66 = load i64, i64* %65
%67 = load i64, i64* @DT_DIR
%68 = icmp eq i64 %66, %67
br i1 %68, label %69, label %74
69: ; preds = %59
%70 = load void (i8*, i32)*, void (i8*, i32)** %4
%71 = load i8*, i8** %5
%72 = load i8*, i8** %5
%73 = load void (i8*, i32)*, void (i8*, i32)** %4
br label %77
74: ; preds = %59
%75 = load void (i8*, i32)*, void (i8*, i32)** %4
%76 = load i8*, i8** %5
br label %77
77: ; preds = %74, %69
%78 = call i32 @freeChar(i8** %5)
br label %79
79: ; preds = %77, %43
br label %15
80: ; preds = %15
%81 = load i32*, i32** %7
%82 = call i32 @closedir(i32* %81)
br label %83
83: ; preds = %80, %12
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@.str = [20 x i8] c"dirWalk: can't open\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@PATH_MAX = global i64 0
@.str.3 = [15 x i8] c"name too long\0A\00"
@.str.4 = [2 x i8] c"/\00"
@DT_DIR = global i64 0
define void @dirWalkR(i8* %0, void (i8*, i32)* nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %57, label %12
10: ; preds = %2
%11 = tail call i32 @outerr(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %59
12: ; preds = %7, %22
%13 = phi %struct.dirent* [ %23, %22 ], [ %8, %7 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = call i64 @strmcmp(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %12
%19 = load i8*, i8** %14
%20 = call i64 @strmcmp(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %33, %55, %12, %18
%23 = call %struct.dirent* @readdir(i32* %5)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %57, label %12
25: ; preds = %18
%26 = call i64 @strmlen(i8* %0)
%27 = load i8*, i8** %14
%28 = call i64 @strmlen(i8* %27)
%29 = add i64 %26, 1
%30 = add i64 %29, %28
%31 = load i64, i64* @PATH_MAX
%32 = icmp ugt i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 @outerr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %22
35: ; preds = %25
%36 = call i8* @strmrchr(i8* %0, i8 signext 47)
%37 = call i64 @strmlen(i8* %0)
%38 = add i64 %37, -1
%39 = getelementptr inbounds i8, i8* %0, i64 %38
%40 = icmp eq i8* %39, %36
br i1 %40, label %44, label %41
41: ; preds = %35
%42 = call i32 @strmcat(i8** %3, i8* %0)
%43 = call i32 @strmcat(i8** %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %41, %35
%45 = load i8*, i8** %14
%46 = call i32 @strmcat(i8** %3, i8* %45)
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 1
%48 = load i64, i64* %47
%49 = load i64, i64* @DT_DIR
%50 = icmp eq i64 %48, %49
%51 = load i8*, i8** %3
br i1 %50, label %52, label %54
52: ; preds = %44
%53 = load i8*, i8** %3
br label %55
54: ; preds = %44
br label %55
55: ; preds = %54, %52
%56 = call i32 @freeChar(i8** %3)
br label %22
57: ; preds = %22, %7
%58 = call i32 @closedir(i32* %5)
br label %59
59: ; preds = %57, %10
ret void
}