#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;
scalar_t__ 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, ".") == 0 || strcmp(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 {
      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    12c6 <dirwalk+0x156>
jmp    11c2 <dirwalk+0x52>
mov    -0x420(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
je     12ba <dirwalk+0x14a>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xe29(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     121f <dirwalk+0xaf>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xe08(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1224 <dirwalk+0xb4>
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    1287 <dirwalk+0x117>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x418(%rbp),%rax
mov    (%rax),%rcx
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    12b5 <dirwalk+0x145>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x418(%rbp),%rax
mov    (%rax),%rcx
lea    0xd96(%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     126f <dirwalk+0xff>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1255 <dirwalk+0xe5>
mov    %rax,%rbp
lea    0xe64(%rip),%r12
lea    0xe5c(%rip),%r13
jmp    11f0 <dirwalk+0x80>
mov    0x0(%rbp),%rcx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
lea    0xe68(%rip),%rsi
mov    %r15,%rdx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   *0x8(%rsp)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1255 <dirwalk+0xe5>
mov    0x0(%rbp),%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11e0 <dirwalk+0x70>
mov    0x0(%rbp),%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11e0 <dirwalk+0x70>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    0x0(%rbp),%rdi
call   1040 <strlen@plt>
add    %r14d,%eax
add    $0xfffffc01,%eax
cmp    $0xfffffbfe,%eax
ja     11be <dirwalk+0x4e>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rcx
lea    0xdd2(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    11e0 <dirwalk+0x70>
mov    %rbx,%rdi
call   1050 <closedir@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%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 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 %32, 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 %32, label %33
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 %40, 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 %42
13: ; preds = %7, %23
%14 = phi %struct.dirent* [ %24, %23 ], [ %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 %23, 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 %23, label %26
23: ; preds = %33, %37, %13, %19
%24 = call %struct.dirent* @readdir(i32* %5)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %40, label %13
26: ; preds = %19
%27 = call i32 @strlen(i8* %0)
%28 = load i8*, i8** %15
%29 = call i32 @strlen(i8* %28)
%30 = add i32 %27, -1023
%31 = add i32 %30, %29
%32 = icmp ult i32 %31, -1025
br i1 %32, label %33, label %37
33: ; preds = %26
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %15
%36 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %35)
br label %23
37: ; preds = %26
%38 = load i8*, i8** %15
%39 = 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* %38)
br label %23
40: ; preds = %23, %7
%41 = call i32 @closedir(i32* %5)
br label %42
42: ; preds = %40, %10
ret void
}