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

/* Variables and functions */
scalar_t__ NAME_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strlen(char const *);
void *stub1(char *);
int walk_dir(const char *path, void *func(const char *)) {
  DIR *dirp;
  struct dirent *dp;
  char *p, *full_path;
  int len;
  if ((dirp = opendir(path)) == NULL) {
    return -1;
  }
  len = strlen(path);
  if ((full_path = malloc(len + NAME_MAX + 2)) == NULL) {
    closedir(dirp);
    return -1;
  }
  memcpy(full_path, path, len);
  p = full_path + len;
  *p++ = '/';
  while ((dp = readdir(dirp)) != NULL) {
    if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
      continue;
    strcpy(p, dp->d_name);
    (*func)(full_path);
  }
  free(full_path);
  closedir(dirp);
  return 0;
}


x86 assembly (clang, -O0):
<walk_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c3 <walk_dir+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <walk_dir+0x135>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x3c(%rbp)
movslq -0x3c(%rbp),%rdi
mov    0x2dfe(%rip),%rax
add    (%rax),%rdi
add    $0x2,%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1209 <walk_dir+0x79>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <walk_dir+0x135>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1090 <memcpy@plt>
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x2f,(%rax)
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12ac <walk_dir+0x11c>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
lea    0xda3(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1289 <walk_dir+0xf9>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    128e <walk_dir+0xfe>
jmp    123a <walk_dir+0xaa>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rdi
call   *%rax
jmp    123a <walk_dir+0xaa>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<walk_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,(%rsp)
mov    %rdi,%rbp
call   1050 <opendir@plt>
test   %rax,%rax
je     126f <walk_dir+0xdf>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <strlen@plt>
movslq %eax,%r12
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
lea    (%r12,%rax,1),%rdi
add    $0x2,%rdi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1276 <walk_dir+0xe6>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1090 <memcpy@plt>
movb   $0x2f,(%r15,%r12,1)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1263 <walk_dir+0xd3>
mov    %rax,%r14
add    %r15,%r12
add    $0x1,%r12
lea    0xdef(%rip),%r13
lea    0xde7(%rip),%rbp
jmp    1230 <walk_dir+0xa0>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1263 <walk_dir+0xd3>
mov    (%r14),%edi
mov    %r13,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1220 <walk_dir+0x90>
mov    (%r14),%edi
mov    %rbp,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1220 <walk_dir+0x90>
mov    (%r14),%esi
mov    %r12,%rdi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   *(%rsp)
jmp    1220 <walk_dir+0x90>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
jmp    127b <walk_dir+0xeb>
mov    $0xffffffff,%ebp
jmp    1283 <walk_dir+0xf3>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@NAME_MAX = global i64 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
define i32 @walk_dir(i8* %0, i8* (i8*)* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8* (i8*)*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i8* (i8*)* %1, i8* (i8*)** %5
%11 = load i8*, i8** %4
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %70
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = sext i32 %18 to i64
%20 = load i64, i64* @NAME_MAX
%21 = add nsw i64 %19, %20
%22 = add nsw i64 %21, 2
%23 = call i8* @malloc(i64 %22)
store i8* %23, i8** %9
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %15
%26 = load i32*, i32** %6
%27 = call i32 @closedir(i32* %26)
store i32 -1, i32* %3
br label %70
28: ; preds = %15
%29 = load i8*, i8** %9
%30 = load i8*, i8** %4
%31 = load i32, i32* %10
%32 = call i32 @memcpy(i8* %29, i8* %30, i32 %31)
%33 = load i8*, i8** %9
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %8
store i8 47, i8* %37
br label %39
39: ; preds = %56, %55, %28
%40 = load i32*, i32** %6
%41 = call %struct.dirent* @readdir(i32* %40)
store %struct.dirent* %41, %struct.dirent** %7
%42 = icmp ne %struct.dirent* %41, null
br i1 %42, label %43, label %65
43: ; preds = %39
%44 = load %struct.dirent*, %struct.dirent** %7
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i64 @strcmp(i32 %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %55, label %49
49: ; preds = %43
%50 = load %struct.dirent*, %struct.dirent** %7
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i64 @strcmp(i32 %52, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %49, %43
br label %39
56: ; preds = %49
%57 = load i8*, i8** %8
%58 = load %struct.dirent*, %struct.dirent** %7
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i32 @strcpy(i8* %57, i32 %60)
%62 = load i8* (i8*)*, i8* (i8*)** %5
%63 = load i8*, i8** %9
%64 = call i8* %62(i8* %63)
br label %39
65: ; preds = %39
%66 = load i8*, i8** %9
%67 = call i32 @free(i8* %66)
%68 = load i32*, i32** %6
%69 = call i32 @closedir(i32* %68)
store i32 0, i32* %3
br label %70
70: ; preds = %65, %25, %14
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@NAME_MAX = global i64 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
define i32 @walk_dir(i8* %0, i8* (i8*)* nocapture %1) {
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %41, label %5
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %0)
%7 = sext i32 %6 to i64
%8 = load i64, i64* @NAME_MAX
%9 = add nsw i64 %7, 2
%10 = add i64 %9, %8
%11 = tail call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %38, label %13
13: ; preds = %5
%14 = tail call i32 @memcpy(i8* %11, i8* %0, i32 %6)
%15 = getelementptr inbounds i8, i8* %11, i64 %7
%16 = getelementptr inbounds i8, i8* %15, i64 1
store i8 47, i8* %15
%17 = tail call %struct.dirent* @readdir(i32* %3)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %36, label %19
19: ; preds = %13, %29
%20 = phi %struct.dirent* [ %30, %29 ], [ %17, %13 ]
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%22 = load i32, i32* %21
%23 = tail call i64 @strcmp(i32 %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %19
%26 = load i32, i32* %21
%27 = tail call i64 @strcmp(i32 %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %19, %25, %32
%30 = tail call %struct.dirent* @readdir(i32* %3)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %36, label %19
32: ; preds = %25
%33 = load i32, i32* %21
%34 = tail call i32 @strcpy(i8* %16, i32 %33)
%35 = tail call i8* %1(i8* %11)
br label %29
36: ; preds = %29, %13
%37 = tail call i32 @free(i8* %11)
br label %38
38: ; preds = %5, %36
%39 = phi i32 [ 0, %36 ], [ -1, %5 ]
%40 = tail call i32 @closedir(i32* %3)
br label %41
41: ; preds = %38, %2
%42 = phi i32 [ -1, %2 ], [ %39, %38 ]
ret i32 %42
}