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

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


x86 assembly (clang, -O0):
<dirwalk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x1020(%rbp)
cmp    $0x0,%rax
jne    11bd <dirwalk+0x4d>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    12e1 <dirwalk+0x171>
jmp    11c2 <dirwalk+0x52>
mov    -0x1020(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
je     12d5 <dirwalk+0x165>
mov    -0x1018(%rbp),%rax
mov    (%rax),%rdi
lea    0xe2a(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1235 <dirwalk+0xc5>
mov    -0x1018(%rbp),%rax
mov    (%rax),%rdi
lea    0xe09(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1235 <dirwalk+0xc5>
mov    -0x1018(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    123a <dirwalk+0xca>
jmp    11c2 <dirwalk+0x52>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1024(%rbp)
mov    -0x1018(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1024(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
cmp    $0x1000,%rax
jbe    129d <dirwalk+0x12d>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x1018(%rbp),%rax
mov    (%rax),%rcx
lea    0xd8b(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    12d0 <dirwalk+0x160>
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x1018(%rbp),%rax
mov    (%rax),%r8
mov    $0x1000,%esi
lea    0xd7c(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rax
lea    -0x1010(%rbp),%rdi
call   *%rax
jmp    11c2 <dirwalk+0x52>
mov    -0x1020(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirwalk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r15
call   1030 <opendir@plt>
test   %rax,%rax
je     128f <dirwalk+0x11f>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1275 <dirwalk+0x105>
mov    %rax,%rbp
lea    0xe65(%rip),%r12
lea    0xe5d(%rip),%r13
jmp    1200 <dirwalk+0x90>
mov    0x0(%rbp),%r8
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x1000,%esi
lea    0xe64(%rip),%rdx
mov    %r15,%rcx
call   1050 <snprintf@plt>
mov    %rbp,%rdi
call   *0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1275 <dirwalk+0x105>
mov    0x0(%rbp),%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11f0 <dirwalk+0x80>
mov    0x0(%rbp),%rdi
mov    %r13,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11f0 <dirwalk+0x80>
mov    0x0(%rbp),%rax
cmpb   $0x2e,(%rax)
je     11f0 <dirwalk+0x80>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    0x0(%rbp),%rdi
call   1040 <strlen@plt>
add    %r14d,%eax
add    $0xfffff001,%eax
cmp    $0xffffeffe,%eax
ja     11be <dirwalk+0x4e>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rcx
lea    0xdb6(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    11f0 <dirwalk+0x80>
mov    %rbx,%rdi
call   1060 <closedir@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
lea    0xd61(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <fprintf@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.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 = [30 x i8] c"dirwalk: name %s/%s too long\0A\00"
@.str.4 = [6 x i8] c"%s/%s\00"
define void @dirwalk(i8* %0, void (i8*)* %1) {
%3 = alloca i8*
%4 = alloca void (i8*)*
%5 = alloca [4096 x i8]
%6 = alloca %struct.dirent*
%7 = alloca i32*
store i8* %0, i8** %3
store void (i8*)* %1, void (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 %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %3
%14 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %72
15: ; preds = %2
br label %16
16: ; preds = %68, %40, %15
%17 = load i32*, i32** %7
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %6
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %69
20: ; preds = %16
%21 = load %struct.dirent*, %struct.dirent** %6
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %40, label %26
26: ; preds = %20
%27 = load %struct.dirent*, %struct.dirent** %6
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %40, label %32
32: ; preds = %26
%33 = load %struct.dirent*, %struct.dirent** %6
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 46
br i1 %39, label %40, label %41
40: ; preds = %32, %26, %20
br label %16
41: ; preds = %32
%42 = load i8*, i8** %3
%43 = call i32 @strlen(i8* %42)
%44 = load %struct.dirent*, %struct.dirent** %6
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = call i32 @strlen(i8* %46)
%48 = add nsw i32 %43, %47
%49 = add nsw i32 %48, 2
%50 = sext i32 %49 to i64
%51 = icmp ugt i64 %50, 4096
br i1 %51, label %52, label %59
52: ; preds = %41
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %3
%55 = load %struct.dirent*, %struct.dirent** %6
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %54, i8* %57)
br label %68
59: ; preds = %41
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%61 = load i8*, i8** %3
%62 = load %struct.dirent*, %struct.dirent** %6
%63 = getelementptr inbounds %struct.dirent, %struct.dirent* %62, i32 0, i32 0
%64 = load i8*, i8** %63
%65 = call i32 @snprintf(i8* %60, i32 4096, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %61, i8* %64)
%66 = load void (i8*)*, void (i8*)** %4
%67 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
br label %68
68: ; preds = %59, %52
br label %16
69: ; preds = %16
%70 = load i32*, i32** %7
%71 = call i32 @closedir(i32* %70)
br label %72
72: ; preds = %69, %11
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.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 = [30 x i8] c"dirwalk: name %s/%s too long\0A\00"
@.str.4 = [6 x i8] c"%s/%s\00"
define void @dirwalk(i8* %0, void (i8*)* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 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 %44, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %46
13: ; preds = %7, %27
%14 = phi %struct.dirent* [ %28, %27 ], [ %8, %7 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %13
%20 = load i8*, i8** %15
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i8*, i8** %15
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 46
br i1 %26, label %27, label %30
27: ; preds = %37, %41, %13, %19, %23
%28 = call %struct.dirent* @readdir(i32* %5)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %44, label %13
30: ; preds = %23
%31 = call i32 @strlen(i8* %0)
%32 = load i8*, i8** %15
%33 = call i32 @strlen(i8* %32)
%34 = add i32 %31, -4095
%35 = add i32 %34, %33
%36 = icmp ult i32 %35, -4097
br i1 %36, label %37, label %41
37: ; preds = %30
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %15
%40 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %39)
br label %27
41: ; preds = %30
%42 = load i8*, i8** %15
%43 = call i32 @snprintf(i8* %4, i32 4096, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %42)
br label %27
44: ; preds = %27, %7
%45 = call i32 @closedir(i32* %5)
br label %46
46: ; preds = %44, %10
ret void
}