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

/* Variables and functions */
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 *);
void dirWalk(char *dir, void (*fcn)(char *)) {
  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);
      (*fcn)(name);
      freeChar(&name);
    }
  }
  closedir(dfd);
}


x86 assembly (clang, -O0):
<dirWalk>:
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    11d0 <dirWalk+0x40>
lea    0xe3a(%rip),%rdi
call   10b0 <outerr@plt>
jmp    12fc <dirWalk+0x16c>
jmp    11d5 <dirWalk+0x45>
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12f3 <dirWalk+0x163>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1b(%rip),%rsi
call   1070 <strmcmp@plt>
cmp    $0x0,%rax
je     1226 <dirWalk+0x96>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdfd(%rip),%rsi
call   1070 <strmcmp@plt>
cmp    $0x0,%rax
jne    122b <dirWalk+0x9b>
jmp    11d5 <dirWalk+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    0x2d97(%rip),%rcx
cmp    (%rcx),%rax
jbe    1273 <dirWalk+0xe3>
lea    0xdae(%rip),%rdi
call   10b0 <outerr@plt>
jmp    12ee <dirWalk+0x15e>
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     12cb <dirWalk+0x13b>
mov    -0x8(%rbp),%rsi
lea    -0x18(%rbp),%rdi
call   10a0 <strmcat@plt>
lea    -0x18(%rbp),%rdi
lea    0xd60(%rip),%rsi
call   10a0 <strmcat@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    -0x18(%rbp),%rdi
call   10a0 <strmcat@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rdi
call   *%rax
lea    -0x18(%rbp),%rdi
call   1030 <freeChar@plt>
jmp    11d5 <dirWalk+0x45>
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirWalk>:
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     12cb <dirWalk+0x13b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12b4 <dirWalk+0x124>
mov    %rax,%r14
lea    0xe3a(%rip),%r12
lea    0xe32(%rip),%rbp
jmp    1204 <dirWalk+0x74>
lea    0xe2c(%rip),%rdi
call   10b0 <outerr@plt>
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12b4 <dirWalk+0x124>
mov    (%r14),%rdi
mov    %r12,%rsi
call   1070 <strmcmp@plt>
test   %rax,%rax
je     11f0 <dirWalk+0x60>
mov    (%r14),%rdi
mov    %rbp,%rsi
call   1070 <strmcmp@plt>
test   %rax,%rax
je     11f0 <dirWalk+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    0x2dab(%rip),%rcx
cmp    (%rcx),%rax
ja     11e4 <dirWalk+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     1290 <dirWalk+0x100>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10a0 <strmcat@plt>
mov    %rbp,%rdi
lea    0xd9b(%rip),%rsi
call   10a0 <strmcat@plt>
mov    (%r14),%rsi
mov    %rbp,%rdi
call   10a0 <strmcat@plt>
mov    0x8(%rsp),%rdi
call   *0x10(%rsp)
mov    %rbp,%rdi
call   1030 <freeChar@plt>
mov    %r13,%rbp
jmp    11f0 <dirWalk+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    0xd2e(%rip),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <outerr@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.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"
define void @dirWalk(i8* %0, void (i8*)* %1) {
%3 = alloca i8*
%4 = alloca void (i8*)*
%5 = alloca i8*
%6 = alloca %struct.dirent*
%7 = alloca i32*
%8 = alloca i8*
store i8* %0, i8** %3
store void (i8*)* %1, void (i8*)** %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 %71
14: ; preds = %2
br label %15
15: ; preds = %67, %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 %68
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 %67
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 void (i8*)*, void (i8*)** %4
%65 = load i8*, i8** %5
%66 = call i32 @freeChar(i8** %5)
br label %67
67: ; preds = %59, %43
br label %15
68: ; preds = %15
%69 = load i32*, i32** %7
%70 = call i32 @closedir(i32* %69)
br label %71
71: ; preds = %68, %12
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.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"
define void @dirWalk(i8* %0, void (i8*)* 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 %49, 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 %51
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, %44, %12, %18
%23 = call %struct.dirent* @readdir(i32* %5)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %49, 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 = load i8*, i8** %3
%48 = call i32 @freeChar(i8** %3)
br label %22
49: ; preds = %22, %7
%50 = call i32 @closedir(i32* %5)
br label %51
51: ; preds = %49, %10
ret void
}