#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*/ DIR;

/* Variables and functions */
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 *);
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;
    else {
      sprintf(name, "%s/%s", dir, dp->d_name);
      (*fcn)(name);
    }
  }
}


x86 assembly (clang, -O0):
<dirwalk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%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    119b <dirwalk+0x4b>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe6f(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1235 <dirwalk+0xe5>
jmp    11a0 <dirwalk+0x50>
mov    -0x420(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
je     1235 <dirwalk+0xe5>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xe4b(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11fd <dirwalk+0xad>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xe2a(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1202 <dirwalk+0xb2>
jmp    11a0 <dirwalk+0x50>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x418(%rbp),%rax
mov    (%rax),%rcx
lea    0xdfd(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rax
lea    -0x410(%rbp),%rdi
call   *%rax
jmp    11a0 <dirwalk+0x50>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirwalk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <opendir@plt>
test   %rax,%rax
je     1206 <dirwalk+0xb6>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11f4 <dirwalk+0xa4>
mov    %rax,%r13
lea    0xe8a(%rip),%r12
mov    %rsp,%rbp
jmp    11b0 <dirwalk+0x60>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1060 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     11f4 <dirwalk+0xa4>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11a0 <dirwalk+0x50>
mov    0x0(%r13),%rdi
lea    0xe4c(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11a0 <dirwalk+0x50>
mov    0x0(%r13),%rcx
mov    %rbp,%rdi
lea    0xe37(%rip),%rsi
mov    %r15,%rdx
call   1070 <sprintf@plt>
mov    %rbp,%rdi
call   *%r14
jmp    11a0 <dirwalk+0x50>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
mov    %r15,%rdx
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <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 = [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 @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %43
15: ; preds = %2
br label %16
16: ; preds = %42, %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 %43
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 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%35 = load i8*, i8** %3
%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 @sprintf(i8* %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %35, i8* %38)
%40 = load void (i8*)*, void (i8*)** %4
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
br label %42
42: ; preds = %33
br label %16
43: ; preds = %11, %16
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 = [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 %29, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %29
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 = %13, %19, %26
%24 = call %struct.dirent* @readdir(i32* %5)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %29, label %13
26: ; preds = %19
%27 = load i8*, i8** %15
%28 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %27)
br label %23
29: ; preds = %23, %7, %10
ret void
}