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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ R_OK;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncat(char *, char *, int);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
void listdir(char *dirname, int lvl, void (*filevisit)(const char *, int),
             void (*foldervisit)(const char *, int)) {
  int i;
  DIR *d_fh;
  struct dirent *entry;
  char longest_name[4096];
  if (access(dirname, R_OK) == -1) {
    fprintf(stderr, "Couldn't open directory: %s\n", dirname);
    return;
  }
  while ((d_fh = opendir(dirname)) == NULL) {
    fprintf(stderr, "Couldn't open directory: %s\n", dirname);
    exit(-1);
  }
  while ((entry = readdir(d_fh)) != NULL) {
    if (strncmp(entry->d_name, "..", 2) != 0 &&
        strncmp(entry->d_name, ".", 1) != 0) {
      if (entry->d_type == DT_DIR) {
        strncpy(longest_name, dirname, 4096);
        strncat(longest_name, "/", 4096);
        strncat(longest_name, entry->d_name, 4096);
        if (access(longest_name, R_OK) == -1) {
          fprintf(stderr, "Couldn't open directory: %s\n", dirname);
          return;
        }
        listdir(longest_name, lvl + 1, filevisit, foldervisit);
        if (foldervisit != NULL) {
          foldervisit(longest_name, lvl);
        }
      } else {
        strncpy(longest_name, dirname, 4096);
        strncat(longest_name, "/", 4096);
        strncat(longest_name, entry->d_name, 4096);
        filevisit(longest_name, lvl);
      }
    }
  }
  closedir(d_fh);
  return;
}


x86 assembly (clang, -O0):
<listdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
call   10b0 <access@plt>
cmp    $0xffffffff,%eax
jne    11f3 <listdir+0x53>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe17(%rip),%rsi
call   1090 <fprintf@plt>
jmp    13cd <listdir+0x22d>
jmp    11f8 <listdir+0x58>
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1232 <listdir+0x92>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xddd(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
jmp    1237 <listdir+0x97>
mov    -0x30(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     13c4 <listdir+0x224>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc1(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     13bf <listdir+0x21f>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xda0(%rip),%rsi
mov    $0x1,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     13bf <listdir+0x21f>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d37(%rip),%rcx
cmp    (%rcx),%rax
jne    1365 <listdir+0x1c5>
lea    -0x1040(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
lea    -0x1040(%rbp),%rdi
lea    0xd53(%rip),%rsi
mov    $0x1000,%edx
call   1070 <strncat@plt>
lea    -0x1040(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
mov    $0x1000,%edx
call   1070 <strncat@plt>
lea    -0x1040(%rbp),%rdi
mov    0x2cd3(%rip),%rax
mov    (%rax),%esi
call   10b0 <access@plt>
cmp    $0xffffffff,%eax
jne    132b <listdir+0x18b>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xcdf(%rip),%rsi
call   1090 <fprintf@plt>
jmp    13cd <listdir+0x22d>
lea    -0x1040(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1060 <listdir@plt>
cmpq   $0x0,-0x20(%rbp)
je     1360 <listdir+0x1c0>
mov    -0x20(%rbp),%rax
lea    -0x1040(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   *%rax
jmp    13ba <listdir+0x21a>
lea    -0x1040(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
lea    -0x1040(%rbp),%rdi
lea    0xc98(%rip),%rsi
mov    $0x1000,%edx
call   1070 <strncat@plt>
lea    -0x1040(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
mov    $0x1000,%edx
call   1070 <strncat@plt>
mov    -0x18(%rbp),%rax
lea    -0x1040(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   *%rax
jmp    13bf <listdir+0x21f>
jmp    1237 <listdir+0x97>
mov    -0x30(%rbp),%rdi
call   1080 <closedir@plt>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listdir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
mov    %rcx,0x28(%rsp)
mov    %rdx,0x20(%rsp)
mov    %rsi,0x18(%rsp)
mov    %rdi,%rbx
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   10b0 <access@plt>
cmp    $0xffffffff,%eax
je     1328 <listdir+0x188>
mov    %rbx,0x8(%rsp)
mov    %rbx,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     136d <listdir+0x1cd>
mov    %rax,%r13
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     130e <listdir+0x16e>
mov    %rax,%r14
mov    0x18(%rsp),%rax
add    $0x1,%eax
mov    %eax,0x14(%rsp)
lea    0xe03(%rip),%rbx
lea    0x30(%rsp),%r12
jmp    124e <listdir+0xae>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    0x18(%rsp),%rsi
call   *%rax
mov    %r13,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     130e <listdir+0x16e>
mov    (%r14),%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     123a <listdir+0x9a>
mov    (%r14),%rdi
lea    0xdb1(%rip),%rsi
mov    $0x1,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     123a <listdir+0x9a>
mov    0x8(%r14),%r15
mov    0x2d51(%rip),%rax
mov    (%rax),%rbp
mov    %r12,%rdi
mov    0x8(%rsp),%rsi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
mov    %r12,%rdi
lea    0xd7a(%rip),%rsi
mov    $0x1000,%edx
call   1070 <strncat@plt>
mov    (%r14),%rsi
mov    %r12,%rdi
mov    $0x1000,%edx
call   1070 <strncat@plt>
mov    0x20(%rsp),%rax
cmp    %rbp,%r15
jne    1230 <listdir+0x90>
mov    0x2cfb(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10b0 <access@plt>
cmp    $0xffffffff,%eax
je     1351 <listdir+0x1b1>
mov    %r12,%rdi
mov    0x14(%rsp),%esi
mov    0x20(%rsp),%rdx
mov    0x28(%rsp),%rbp
mov    %rbp,%rcx
call   1060 <listdir@plt>
mov    %rbp,%rax
test   %rbp,%rbp
jne    1230 <listdir+0x90>
jmp    123a <listdir+0x9a>
mov    %r13,%rdi
call   1080 <closedir@plt>
add    $0x1038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
lea    0xcc8(%rip),%rsi
mov    %rbx,%rdx
add    $0x1038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <fprintf@plt>
mov    0x2c88(%rip),%rax
mov    (%rax),%edi
lea    0xc9f(%rip),%rsi
mov    0x8(%rsp),%rdx
call   1090 <fprintf@plt>
jmp    1316 <listdir+0x176>
mov    0x2c6c(%rip),%rax
mov    (%rax),%edi
lea    0xc83(%rip),%rsi
mov    0x8(%rsp),%rdx
call   1090 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@R_OK = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"Couldn't open directory: %s\0A\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c".\00"
@DT_DIR = global i64 0
@.str.3 = [2 x i8] c"/\00"
define void @listdir(i8* %0, i32 %1, void (i8*, i32)* %2, void (i8*, i32)* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca void (i8*, i32)*
%8 = alloca void (i8*, i32)*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca %struct.dirent*
%12 = alloca [4096 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store void (i8*, i32)* %2, void (i8*, i32)** %7
store void (i8*, i32)* %3, void (i8*, i32)** %8
%13 = load i8*, i8** %5
%14 = load i32, i32* @R_OK
%15 = call i32 @access(i8* %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %5
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %105
21: ; preds = %4
br label %22
22: ; preds = %21
%23 = load i8*, i8** %5
%24 = call i32* @opendir(i8* %23)
store i32* %24, i32** %10
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %5
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %22
br label %32
32: ; preds = %101, %31
%33 = load i32*, i32** %10
%34 = call %struct.dirent* @readdir(i32* %33)
store %struct.dirent* %34, %struct.dirent** %11
%35 = icmp ne %struct.dirent* %34, null
br i1 %35, label %36, label %102
36: ; preds = %32
%37 = load %struct.dirent*, %struct.dirent** %11
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i64 @strncmp(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %101
42: ; preds = %36
%43 = load %struct.dirent*, %struct.dirent** %11
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i64 @strncmp(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %101
48: ; preds = %42
%49 = load %struct.dirent*, %struct.dirent** %11
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 1
%51 = load i64, i64* %50
%52 = load i64, i64* @DT_DIR
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %86
54: ; preds = %48
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%56 = load i8*, i8** %5
%57 = call i32 @strncpy(i8* %55, i8* %56, i32 4096)
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%59 = call i32 @strncat(i8* %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 4096)
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%61 = load %struct.dirent*, %struct.dirent** %11
%62 = getelementptr inbounds %struct.dirent, %struct.dirent* %61, i32 0, i32 0
%63 = load i8*, i8** %62
%64 = call i32 @strncat(i8* %60, i8* %63, i32 4096)
%65 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%66 = load i32, i32* @R_OK
%67 = call i32 @access(i8* %65, i32 %66)
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %73
69: ; preds = %54
%70 = load i32, i32* @stderr
%71 = load i8*, i8** %5
%72 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %71)
br label %105
73: ; preds = %54
%74 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%75 = load i32, i32* %6
%76 = add nsw i32 %75, 1
%77 = load void (i8*, i32)*, void (i8*, i32)** %7
%78 = load void (i8*, i32)*, void (i8*, i32)** %8
%79 = load void (i8*, i32)*, void (i8*, i32)** %8
%80 = icmp ne void (i8*, i32)* %79, null
br i1 %80, label %81, label %85
81: ; preds = %73
%82 = load void (i8*, i32)*, void (i8*, i32)** %8
%83 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%84 = load i32, i32* %6
br label %85
85: ; preds = %81, %73
br label %100
86: ; preds = %48
%87 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%88 = load i8*, i8** %5
%89 = call i32 @strncpy(i8* %87, i8* %88, i32 4096)
%90 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%91 = call i32 @strncat(i8* %90, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 4096)
%92 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%93 = load %struct.dirent*, %struct.dirent** %11
%94 = getelementptr inbounds %struct.dirent, %struct.dirent* %93, i32 0, i32 0
%95 = load i8*, i8** %94
%96 = call i32 @strncat(i8* %92, i8* %95, i32 4096)
%97 = load void (i8*, i32)*, void (i8*, i32)** %7
%98 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%99 = load i32, i32* %6
br label %100
100: ; preds = %86, %85
br label %101
101: ; preds = %100, %42, %36
br label %32
102: ; preds = %32
%103 = load i32*, i32** %10
%104 = call i32 @closedir(i32* %103)
br label %105
105: ; preds = %102, %69, %17
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@R_OK = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"Couldn't open directory: %s\0A\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c".\00"
@DT_DIR = global i64 0
@.str.3 = [2 x i8] c"/\00"
define void @listdir(i8* %0, i32 %1, void (i8*, i32)* nocapture %2, void (i8*, i32)* %3) {
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @R_OK
%8 = tail call i32 @access(i8* %0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %60
13: ; preds = %4
%14 = tail call i32* @opendir(i8* %0)
%15 = icmp eq i32* %14, null
br i1 %15, label %22, label %16
16: ; preds = %13
%17 = call %struct.dirent* @readdir(i32* %14)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %58, label %19
19: ; preds = %16
%20 = add nsw i32 %1, 1
%21 = icmp eq void (i8*, i32)* %3, null
br label %26
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0)
%25 = tail call i32 @exit(i32 -1)
unreachable
26: ; preds = %19, %55
%27 = phi %struct.dirent* [ %17, %19 ], [ %56, %55 ]
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%29 = load i8*, i8** %28
%30 = call i64 @strncmp(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%31 = icmp eq i64 %30, 0
br i1 %31, label %55, label %32
32: ; preds = %26
%33 = load i8*, i8** %28
%34 = call i64 @strncmp(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%35 = icmp eq i64 %34, 0
br i1 %35, label %55, label %36
36: ; preds = %32
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 1
%38 = load i64, i64* %37
%39 = load i64, i64* @DT_DIR
%40 = icmp eq i64 %38, %39
%41 = call i32 @strncpy(i8* %6, i8* %0, i32 4096)
%42 = call i32 @strncat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 4096)
%43 = load i8*, i8** %28
%44 = call i32 @strncat(i8* %6, i8* %43, i32 4096)
br i1 %40, label %45, label %53
45: ; preds = %36
%46 = load i32, i32* @R_OK
%47 = call i32 @access(i8* %6, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %60
52: ; preds = %45
br i1 %21, label %55, label %53
53: ; preds = %36, %52
%54 = phi void (i8*, i32)* [ %3, %52 ], [ %2, %36 ]
br label %55
55: ; preds = %53, %52, %32, %26
%56 = call %struct.dirent* @readdir(i32* %14)
%57 = icmp eq %struct.dirent* %56, null
br i1 %57, label %58, label %26
58: ; preds = %55, %16
%59 = call i32 @closedir(i32* %14)
br label %60
60: ; preds = %58, %49, %10
ret void
}