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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ error(char *, char *, ...);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
void stub1(char *, char *);
void dirwalk(char *dir, void (*fun)(char *, char *), char *dest) {
  char dname[1024];
  struct dirent *dp;
  DIR *dfd;
  if ((dfd = opendir(dir)) == NULL)
    error("dirwalk: can't open %s", dir);
  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(dname))
      error("dirwalk: name %s/%s too long", dir, dp->d_name);
    else {
      sprintf(dname, "%s/%s", dir, dp->d_name);
      (*fun)(dname, dest);
      printf("%s", dname);
    }
  }
  closedir(dfd);
}


x86 assembly (clang, -O0):
<dirwalk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x430(%rbp)
cmp    $0x0,%rax
jne    11c3 <dirwalk+0x43>
mov    -0x8(%rbp),%rsi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1090 <error@plt>
jmp    11c8 <dirwalk+0x48>
mov    -0x430(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
je     12ce <dirwalk+0x14e>
mov    -0x428(%rbp),%rax
mov    (%rax),%rdi
lea    0xe22(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1225 <dirwalk+0xa5>
mov    -0x428(%rbp),%rax
mov    (%rax),%rdi
lea    0xe01(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    122a <dirwalk+0xaa>
jmp    11c8 <dirwalk+0x48>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x434(%rbp)
mov    -0x428(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x434(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
cmp    $0x400,%rax
jbe    1284 <dirwalk+0x104>
mov    -0x8(%rbp),%rsi
mov    -0x428(%rbp),%rax
mov    (%rax),%rdx
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1090 <error@plt>
jmp    12c9 <dirwalk+0x149>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x428(%rbp),%rax
mov    (%rax),%rcx
lea    0xd97(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x10(%rbp),%rax
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   *%rax
lea    -0x420(%rbp),%rsi
lea    0xd50(%rip),%rdi
call   1050 <printf@plt>
jmp    11c8 <dirwalk+0x48>
mov    -0x430(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x440,%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    %rdx,0x8(%rsp)
mov    %rsi,(%rsp)
mov    %rdi,%r12
call   1030 <opendir@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11bb <dirwalk+0x3b>
lea    0xe4f(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1090 <error@plt>
mov    %r13,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     128f <dirwalk+0x10f>
mov    %rax,%r15
lea    0xe42(%rip),%rbx
lea    0xe3a(%rip),%rbp
jmp    1230 <dirwalk+0xb0>
mov    %rbx,%r14
mov    %rbp,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
lea    0xe43(%rip),%rsi
mov    %r12,%rdx
call   10a0 <sprintf@plt>
mov    %rbp,%rdi
mov    0x8(%rsp),%rsi
call   *(%rsp)
lea    0xe06(%rip),%rdi
mov    %rbp,%rsi
mov    %rbx,%rbp
mov    %r14,%rbx
call   1050 <printf@plt>
nopl   0x0(%rax)
mov    %r13,%rdi
call   1080 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     128f <dirwalk+0x10f>
mov    (%r15),%rdi
mov    %rbx,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1220 <dirwalk+0xa0>
mov    (%r15),%rdi
mov    %rbp,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1220 <dirwalk+0xa0>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    (%r15),%rdi
call   1040 <strlen@plt>
add    %r14d,%eax
add    $0xfffffc01,%eax
mov    (%r15),%rcx
cmp    $0xfffffbfe,%eax
ja     11df <dirwalk+0x5f>
lea    0xd9a(%rip),%rdi
mov    %r12,%rsi
mov    %rcx,%rdx
xor    %eax,%eax
call   1090 <error@plt>
jmp    1220 <dirwalk+0xa0>
mov    %r13,%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 = [23 x i8] c"dirwalk: can't open %s\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [29 x i8] c"dirwalk: name %s/%s too long\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [3 x i8] c"%s\00"
define void @dirwalk(i8* %0, void (i8*, i8*)* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca void (i8*, i8*)*
%6 = alloca i8*
%7 = alloca [1024 x i8]
%8 = alloca %struct.dirent*
%9 = alloca i32*
store i8* %0, i8** %4
store void (i8*, i8*)* %1, void (i8*, i8*)** %5
store i8* %2, i8** %6
%10 = load i8*, i8** %4
%11 = call i32* @opendir(i8* %10)
store i32* %11, i32** %9
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %3
br label %17
17: ; preds = %63, %33, %16
%18 = load i32*, i32** %9
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %8
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %64
21: ; preds = %17
%22 = load %struct.dirent*, %struct.dirent** %8
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %21
%28 = load %struct.dirent*, %struct.dirent** %8
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27, %21
br label %17
34: ; preds = %27
%35 = load i8*, i8** %4
%36 = call i32 @strlen(i8* %35)
%37 = load %struct.dirent*, %struct.dirent** %8
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 %36, %40
%42 = add nsw i32 %41, 2
%43 = sext i32 %42 to i64
%44 = icmp ugt i64 %43, 1024
br i1 %44, label %45, label %51
45: ; preds = %34
%46 = load i8*, i8** %4
%47 = load %struct.dirent*, %struct.dirent** %8
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %46, i8* %49)
br label %63
51: ; preds = %34
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%53 = load i8*, i8** %4
%54 = load %struct.dirent*, %struct.dirent** %8
%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*, i8*)*, void (i8*, i8*)** %5
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%60 = load i8*, i8** %6
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%62 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %61)
br label %63
63: ; preds = %51, %45
br label %17
64: ; preds = %17
%65 = load i32*, i32** %9
%66 = call i32 @closedir(i32* %65)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [23 x i8] c"dirwalk: can't open %s\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [29 x i8] c"dirwalk: name %s/%s too long\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [3 x i8] c"%s\00"
define void @dirwalk(i8* %0, void (i8*, i8*)* nocapture %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @opendir(i8* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %10
10: ; preds = %8, %3
%11 = call %struct.dirent* @readdir(i32* %6)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %39, label %13
13: ; preds = %10, %23
%14 = phi %struct.dirent* [ %24, %23 ], [ %11, %10 ]
%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 = %34, %36, %13, %19
%24 = call %struct.dirent* @readdir(i32* %6)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %39, 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
%33 = load i8*, i8** %15
br i1 %32, label %34, label %36
34: ; preds = %26
%35 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %33)
br label %23
36: ; preds = %26
%37 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %33)
%38 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %5)
br label %23
39: ; preds = %23, %10
%40 = call i32 @closedir(i32* %6)
ret void
}