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

/* Variables and functions */
int /*<<< orphan*/ asfPrintWarning(char *, char const *, ...);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
scalar_t__ strcmp(char const *, char *);
int strlen(char const *);
int stub1(char const *);
void dirwalk(const char *dir, int (*fcn)(const char *)) {
  char name[1024];
  struct dirent *dp;
  DIR *dfd;
  if ((dfd = opendir(dir)) == NULL) {
    asfPrintWarning("dirwalk: cannot open %s\n", dir);
    return;
  }
  while ((dp = readdir(dfd)) != NULL) {
    if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) {
      continue;
    }
    if (strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name)) {
      asfPrintWarning("dirwalk: name %s/%s exceeds buffersize.\n", dir,
                      dp->d_name);
      closedir(dfd);
      return;
    } else {
      sprintf(name, "%s/%s", dir, dp->d_name);
      (*fcn)(name);
    }
  }
  closedir(dfd);
}


x86 assembly (clang, -O0):
<dirwalk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x420(%rbp)
cmp    $0x0,%rax
jne    11b4 <dirwalk+0x44>
mov    -0x8(%rbp),%rsi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1030 <asfPrintWarning@plt>
jmp    12c0 <dirwalk+0x150>
jmp    11b9 <dirwalk+0x49>
mov    -0x420(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
je     12b4 <dirwalk+0x144>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xe33(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1216 <dirwalk+0xa6>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xe12(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    121b <dirwalk+0xab>
jmp    11b9 <dirwalk+0x49>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x424(%rbp)
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x424(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
cmp    $0x400,%rax
jbe    1281 <dirwalk+0x111>
mov    -0x8(%rbp),%rsi
mov    -0x418(%rbp),%rax
mov    (%rax),%rdx
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1030 <asfPrintWarning@plt>
mov    -0x420(%rbp),%rdi
call   1060 <closedir@plt>
jmp    12c0 <dirwalk+0x150>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x418(%rbp),%rax
mov    (%rax),%rcx
lea    0xda8(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rax
lea    -0x410(%rbp),%rdi
call   *%rax
jmp    11b9 <dirwalk+0x49>
mov    -0x420(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirwalk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r14
call   1040 <opendir@plt>
test   %rax,%rax
je     123c <dirwalk+0xcc>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1272 <dirwalk+0x102>
mov    %rax,%r13
lea    0xe65(%rip),%r12
lea    0x10(%rsp),%rbp
jmp    11d4 <dirwalk+0x64>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1272 <dirwalk+0x102>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11c0 <dirwalk+0x50>
mov    0x0(%r13),%rdi
lea    0xe29(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11c0 <dirwalk+0x50>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    0x0(%r13),%rdi
call   1050 <strlen@plt>
add    %r15d,%eax
add    $0xfffffc01,%eax
mov    0x0(%r13),%rcx
cmp    $0xfffffbfe,%eax
jbe    125e <dirwalk+0xee>
mov    %rbp,%rdi
lea    0xe1a(%rip),%rsi
mov    %r14,%rdx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   *0x8(%rsp)
jmp    11c0 <dirwalk+0x50>
lea    0xdbd(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <asfPrintWarning@plt>
lea    0xdb7(%rip),%rdi
mov    %r14,%rsi
mov    %rcx,%rdx
xor    %eax,%eax
call   1030 <asfPrintWarning@plt>
mov    %rbx,%rdi
call   1060 <closedir@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [25 x i8] c"dirwalk: cannot open %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [41 x i8] c"dirwalk: name %s/%s exceeds buffersize.\0A\00"
@.str.4 = [6 x i8] c"%s/%s\00"
define void @dirwalk(i8* %0, i32 (i8*)* %1) {
%3 = alloca i8*
%4 = alloca i32 (i8*)*
%5 = alloca [1024 x i8]
%6 = alloca %struct.dirent*
%7 = alloca i32*
store i8* %0, i8** %3
store i32 (i8*)* %1, i32 (i8*)** %4
%8 = load i8*, i8** %3
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %7
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = call i32 (i8*, i8*, ...) @asfPrintWarning(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %65
14: ; preds = %2
br label %15
15: ; preds = %61, %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 %62
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 @strcmp(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 @strcmp(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 i32 @strlen(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 i32 @strlen(i8* %37)
%39 = add nsw i32 %34, %38
%40 = add nsw i32 %39, 2
%41 = sext i32 %40 to i64
%42 = icmp ugt i64 %41, 1024
br i1 %42, label %43, label %51
43: ; preds = %32
%44 = load i8*, i8** %3
%45 = load %struct.dirent*, %struct.dirent** %6
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i32 (i8*, i8*, ...) @asfPrintWarning(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %44, i8* %47)
%49 = load i32*, i32** %7
%50 = call i32 @closedir(i32* %49)
br label %65
51: ; preds = %32
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%53 = load i8*, i8** %3
%54 = load %struct.dirent*, %struct.dirent** %6
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i32 0, i32 0
%56 = load i8*, i8** %55
%57 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %53, i8* %56)
%58 = load i32 (i8*)*, i32 (i8*)** %4
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%60 = call i32 %58(i8* %59)
br label %61
61: ; preds = %51
br label %15
62: ; preds = %15
%63 = load i32*, i32** %7
%64 = call i32 @closedir(i32* %63)
br label %65
65: ; preds = %62, %43, %11
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [25 x i8] c"dirwalk: cannot open %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [41 x i8] c"dirwalk: name %s/%s exceeds buffersize.\0A\00"
@.str.4 = [6 x i8] c"%s/%s\00"
define void @dirwalk(i8* %0, i32 (i8*)* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%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 %39, label %12
10: ; preds = %2
%11 = tail call i32 (i8*, i8*, ...) @asfPrintWarning(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %41
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 @strcmp(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 @strcmp(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 = %12, %18, %36
%23 = call %struct.dirent* @readdir(i32* %5)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %39, label %12
25: ; preds = %18
%26 = call i32 @strlen(i8* %0)
%27 = load i8*, i8** %14
%28 = call i32 @strlen(i8* %27)
%29 = add i32 %26, -1023
%30 = add i32 %29, %28
%31 = icmp ult i32 %30, -1025
%32 = load i8*, i8** %14
br i1 %31, label %33, label %36
33: ; preds = %25
%34 = call i32 (i8*, i8*, ...) @asfPrintWarning(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %32)
%35 = call i32 @closedir(i32* %5)
br label %41
36: ; preds = %25
%37 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %32)
%38 = call i32 %1(i8* %4)
br label %22
39: ; preds = %22, %7
%40 = call i32 @closedir(i32* %5)
br label %41
41: ; preds = %39, %33, %10
ret void
}