#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*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
void stub1(char *);
void dirwalk(char *dir, void (*fcn)(char *)) {
  char name[1024];
  struct dirent *dp;
  DIR *dfd;
  if ((dfd = opendir(dir)) == NULL) {
    fprintf(stderr, "dirwalk: can't open %s\n", dir);
    return;
  }
  while ((dp = readdir(dfd)) != NULL) {
    if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
      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 {
      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   1030 <opendir@plt>
mov    %rax,-0x420(%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   1070 <fprintf@plt>
jmp    12c4 <dirwalk+0x154>
jmp    11c2 <dirwalk+0x52>
mov    -0x420(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
je     12b8 <dirwalk+0x148>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xe29(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     121d <dirwalk+0xad>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xe09(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1222 <dirwalk+0xb2>
jmp    11c2 <dirwalk+0x52>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x424(%rbp)
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x424(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
cmp    $0x400,%rax
jbe    1285 <dirwalk+0x115>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x418(%rbp),%rax
mov    (%rax),%rcx
lea    0xda2(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    12b3 <dirwalk+0x143>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x418(%rbp),%rax
mov    (%rax),%rcx
lea    0xd98(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rax
lea    -0x410(%rbp),%rdi
call   *%rax
jmp    11c2 <dirwalk+0x52>
mov    -0x420(%rbp),%rdi
call   1050 <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,%r15
call   1030 <opendir@plt>
test   %rax,%rax
je     1275 <dirwalk+0x105>
mov    %rax,%r13
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     125b <dirwalk+0xeb>
mov    %rax,%rbp
lea    0xe64(%rip),%r12
jmp    11f0 <dirwalk+0x80>
mov    0x0(%rbp),%rcx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
lea    0xe6f(%rip),%rsi
mov    %r15,%rdx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   *0x8(%rsp)
nopl   0x0(%rax)
mov    %r13,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     125b <dirwalk+0xeb>
mov    0x0(%rbp),%r14
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11e0 <dirwalk+0x70>
mov    %r14,%rdi
lea    0xe0b(%rip),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11e0 <dirwalk+0x70>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%rdi
call   1040 <strlen@plt>
add    %ebx,%eax
add    $0xfffffc01,%eax
cmp    $0xfffffbfe,%eax
ja     11b7 <dirwalk+0x47>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rcx
lea    0xdcc(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    11e0 <dirwalk+0x70>
mov    %r13,%rdi
call   1050 <closedir@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fprintf@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [24 x i8] c"dirwalk: can't 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 [1024 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 ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %64
15: ; preds = %2
br label %16
16: ; preds = %60, %32, %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 %61
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 i32 @strcmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %32
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 i32 @strcmp(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %26, %20
br label %16
33: ; preds = %26
%34 = load i8*, i8** %3
%35 = call i32 @strlen(i8* %34)
%36 = load %struct.dirent*, %struct.dirent** %6
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i32 @strlen(i8* %38)
%40 = add nsw i32 %35, %39
%41 = add nsw i32 %40, 2
%42 = sext i32 %41 to i64
%43 = icmp ugt i64 %42, 1024
br i1 %43, label %44, label %51
44: ; preds = %33
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %3
%47 = load %struct.dirent*, %struct.dirent** %6
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %46, i8* %49)
br label %60
51: ; preds = %33
%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 void (i8*)*, void (i8*)** %4
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
br label %60
60: ; preds = %51, %44
br label %16
61: ; preds = %16
%62 = load i32*, i32** %7
%63 = call i32 @closedir(i32* %62)
br label %64
64: ; preds = %61, %11
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [24 x i8] c"dirwalk: can't 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 [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 %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %41
13: ; preds = %7, %22
%14 = phi %struct.dirent* [ %23, %22 ], [ %8, %7 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @strcmp(i8* dereferenceable(1) %16, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %13
%20 = call i32 @strcmp(i8* dereferenceable(1) %16, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %32, %36, %13, %19
%23 = call %struct.dirent* @readdir(i32* %5)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %39, label %13
25: ; preds = %19
%26 = call i32 @strlen(i8* %0)
%27 = load i8*, i8** %15
%28 = call i32 @strlen(i8* %27)
%29 = add i32 %26, -1023
%30 = add i32 %29, %28
%31 = icmp ult i32 %30, -1025
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %15
%35 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %34)
br label %22
36: ; preds = %25
%37 = load i8*, i8** %15
%38 = 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* %37)
br label %22
39: ; preds = %22, %7
%40 = call i32 @closedir(i32* %5)
br label %41
41: ; preds = %39, %10
ret void
}