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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
void _scandir(const char *path, int (*fn)(const char *, void *), void *arg) {
  char vpath[PATH_MAX];
  DIR *dir;
  struct dirent *de;
  if ((dir = opendir(path)) == NULL)
    return;
  while ((de = readdir(dir)) != NULL) {
    if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
      continue;
    snprintf(vpath, sizeof(vpath), "%s/%s", path, de->d_name);
    fn(vpath, arg);
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<_scandir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11b7 <_scandir+0x67>
movl   $0x1,-0x3c(%rbp)
jmp    1254 <_scandir+0x104>
jmp    11bc <_scandir+0x6c>
mov    -0x30(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1244 <_scandir+0xf4>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xe20(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     120b <_scandir+0xbb>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xe03(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1210 <_scandir+0xc0>
jmp    11bc <_scandir+0x6c>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    (%rax),%r8
lea    0xdd7(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rsi
call   *%rax
jmp    11bc <_scandir+0x6c>
mov    -0x30(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_scandir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,-0x40(%rbp)
mov    %rsi,-0x38(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rdi,-0x30(%rbp)
call   1030 <opendir@plt>
test   %rax,%rax
je     120f <_scandir+0xbf>
mov    %rax,%r14
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1207 <_scandir+0xb7>
lea    0xe58(%rip),%r15
jmp    11bd <_scandir+0x6d>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1207 <_scandir+0xb7>
mov    (%rax),%r12
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11b0 <_scandir+0x60>
mov    %r12,%rdi
lea    0xe27(%rip),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11b0 <_scandir+0x60>
mov    %rbx,%rdi
mov    %r13d,%esi
lea    0xe14(%rip),%rdx
mov    -0x30(%rbp),%rcx
mov    %r12,%r8
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    -0x40(%rbp),%rsi
call   *-0x38(%rbp)
jmp    11b0 <_scandir+0x60>
mov    %r14,%rdi
call   1050 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @_scandir(i8* %0, i32 (i8*, i8*)* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32 (i8*, i8*)*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca %struct.dirent*
%11 = alloca i32
store i8* %0, i8** %4
store i32 (i8*, i8*)* %1, i32 (i8*, i8*)** %5
store i8* %2, i8** %6
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %7
%15 = alloca i8, i64 %13
store i64 %13, i64* %8
%16 = load i8*, i8** %4
%17 = call i32* @opendir(i8* %16)
store i32* %17, i32** %9
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 1, i32* %11
br label %51
20: ; preds = %3
br label %21
21: ; preds = %38, %37, %20
%22 = load i32*, i32** %9
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %10
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %48
25: ; preds = %21
%26 = load %struct.dirent*, %struct.dirent** %10
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i32 @strcmp(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %25
%32 = load %struct.dirent*, %struct.dirent** %10
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = call i32 @strcmp(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %31, %25
br label %21
38: ; preds = %31
%39 = trunc i64 %13 to i32
%40 = load i8*, i8** %4
%41 = load %struct.dirent*, %struct.dirent** %10
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = call i32 @snprintf(i8* %15, i32 %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %40, i8* %43)
%45 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %5
%46 = load i8*, i8** %6
%47 = call i32 %45(i8* %15, i8* %46)
br label %21
48: ; preds = %21
%49 = load i32*, i32** %9
%50 = call i32 @closedir(i32* %49)
store i32 0, i32* %11
br label %51
51: ; preds = %48, %19
%52 = load i8*, i8** %7
%53 = load i32, i32* %11
switch i32 %53, label %55 [
i32 0, label %54
i32 1, label %54
]
54: ; preds = %51, %51
ret void
55: ; preds = %51
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @_scandir(i8* %0, i32 (i8*, i8*)* nocapture %1, i8* %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i32* @opendir(i8* %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %29, label %9
9: ; preds = %3
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %27, label %12
12: ; preds = %9, %21
%13 = phi %struct.dirent* [ %22, %21 ], [ %10, %9 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = call i32 @strcmp(i8* dereferenceable(1) %15, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = call i32 @strcmp(i8* dereferenceable(1) %15, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %12, %18, %24
%22 = call %struct.dirent* @readdir(i32* %7)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %27, label %12
24: ; preds = %18
%25 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %15)
%26 = call i32 %1(i8* %6, i8* %2)
br label %21
27: ; preds = %21, %9
%28 = call i32 @closedir(i32* %7)
br label %29
29: ; preds = %3, %27
ret void
}