#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 *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
void stub1(char *);
void dirwalk(char *dir, void (*fun)(char *)) {
  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);
    }
  }
  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    11af <dirwalk+0x3f>
mov    -0x8(%rbp),%rsi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1080 <error@plt>
jmp    11b4 <dirwalk+0x44>
mov    -0x420(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
je     12a3 <dirwalk+0x133>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xe36(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1211 <dirwalk+0xa1>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xe15(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1216 <dirwalk+0xa6>
jmp    11b4 <dirwalk+0x44>
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    1270 <dirwalk+0x100>
mov    -0x8(%rbp),%rsi
mov    -0x418(%rbp),%rax
mov    (%rax),%rdx
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1080 <error@plt>
jmp    129e <dirwalk+0x12e>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x418(%rbp),%rax
mov    (%rax),%rcx
lea    0xdab(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rax
lea    -0x410(%rbp),%rdi
call   *%rax
jmp    11b4 <dirwalk+0x44>
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>
mov    %rax,%rbx
test   %rax,%rax
jne    11a7 <dirwalk+0x37>
lea    0xe63(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1080 <error@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1259 <dirwalk+0xe9>
mov    %rax,%r14
lea    0xe56(%rip),%r12
lea    0xe4e(%rip),%r13
jmp    1200 <dirwalk+0x90>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
lea    0xe5d(%rip),%rsi
mov    %r15,%rdx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   *0x8(%rsp)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1259 <dirwalk+0xe9>
mov    (%r14),%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11f0 <dirwalk+0x80>
mov    (%r14),%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11f0 <dirwalk+0x80>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    (%r14),%rdi
call   1040 <strlen@plt>
add    %ebp,%eax
add    $0xfffffc01,%eax
mov    (%r14),%rcx
cmp    $0xfffffbfe,%eax
ja     11cb <dirwalk+0x5b>
lea    0xdd0(%rip),%rdi
mov    %r15,%rsi
mov    %rcx,%rdx
xor    %eax,%eax
call   1080 <error@plt>
jmp    11f0 <dirwalk+0x80>
mov    %rbx,%rdi
call   1050 <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"
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 %14
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %11, %2
br label %15
15: ; preds = %58, %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 %59
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 %49
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*, ...) @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %44, i8* %47)
br label %58
49: ; preds = %32
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%51 = load i8*, i8** %3
%52 = load %struct.dirent*, %struct.dirent** %6
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = call i32 @sprintf(i8* %50, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %51, i8* %54)
%56 = load void (i8*)*, void (i8*)** %4
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
br label %58
58: ; preds = %49, %43
br label %15
59: ; preds = %15
%60 = load i32*, i32** %7
%61 = call i32 @closedir(i32* %60)
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"
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 %7, label %9
7: ; preds = %2
%8 = tail call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %2
%10 = call %struct.dirent* @readdir(i32* %5)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %37, label %12
12: ; preds = %9, %22
%13 = phi %struct.dirent* [ %23, %22 ], [ %10, %9 ]
%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 = %33, %35, %12, %18
%23 = call %struct.dirent* @readdir(i32* %5)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %37, 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 %35
33: ; preds = %25
%34 = call i32 (i8*, i8*, ...) @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %32)
br label %22
35: ; preds = %25
%36 = 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)
br label %22
37: ; preds = %22, %9
%38 = call i32 @closedir(i32* %5)
ret void
}