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

/* Variables and functions */
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*/ spx_utils_str_ends_with(char *, char *);
size_t spx_reporter_full_metadata_list_files(const char *data_dir,
                                             void (*callback)(const char *,
                                                              size_t)) {
  DIR *dir = opendir(data_dir);
  if (!dir) {
    return 0;
  }
  char file_path[512];
  size_t count = 0;
  const struct dirent *entry;
  while ((entry = readdir(dir)) != NULL) {
    if (!spx_utils_str_ends_with(entry->d_name, ".json")) {
      continue;
    }
    snprintf(file_path, sizeof(file_path), "%s/%s", data_dir, entry->d_name);
    callback(file_path, count);
    count++;
  }
  closedir(dir);
  return count;
}


x86 assembly (clang, -O0):
<spx_reporter_full_metadata_list_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1188 <spx_reporter_full_metadata_list_files+0x38>
movq   $0x0,-0x8(%rbp)
jmp    1236 <spx_reporter_full_metadata_list_files+0xe6>
movq   $0x0,-0x228(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x230(%rbp)
cmp    $0x0,%rax
je     1222 <spx_reporter_full_metadata_list_files+0xd2>
mov    -0x230(%rbp),%rax
mov    (%rax),%rdi
lea    0xe42(%rip),%rsi
call   1070 <spx_utils_str_ends_with@plt>
cmp    $0x0,%eax
jne    11d1 <spx_reporter_full_metadata_list_files+0x81>
jmp    1193 <spx_reporter_full_metadata_list_files+0x43>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x230(%rbp),%rax
mov    (%rax),%r8
mov    $0x200,%esi
lea    0xe14(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x18(%rbp),%rax
lea    -0x220(%rbp),%rdi
mov    -0x228(%rbp),%rsi
call   *%rax
mov    -0x228(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x228(%rbp)
jmp    1193 <spx_reporter_full_metadata_list_files+0x43>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x228(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spx_reporter_full_metadata_list_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <opendir@plt>
test   %rax,%rax
je     11d6 <spx_reporter_full_metadata_list_files+0x86>
mov    %rax,%rbx
xor    %r12d,%r12d
lea    0xe82(%rip),%r13
xchg   %ax,%ax
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11cc <spx_reporter_full_metadata_list_files+0x7c>
mov    %rax,%rbp
mov    (%rax),%rdi
mov    %r13,%rsi
call   1070 <spx_utils_str_ends_with@plt>
test   %eax,%eax
je     1180 <spx_reporter_full_metadata_list_files+0x30>
mov    0x0(%rbp),%r8
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
lea    0xe51(%rip),%rdx
mov    %r15,%rcx
call   1040 <snprintf@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
call   *%r14
add    $0x1,%r12
jmp    1180 <spx_reporter_full_metadata_list_files+0x30>
mov    %rbx,%rdi
call   1050 <closedir@plt>
jmp    11d9 <spx_reporter_full_metadata_list_files+0x89>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c".json\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define i64 @spx_reporter_full_metadata_list_files(i8* %0, void (i8*, i64)* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca void (i8*, i64)*
%6 = alloca i32*
%7 = alloca [512 x i8]
%8 = alloca i64
%9 = alloca %struct.dirent*
store i8* %0, i8** %4
store void (i8*, i64)* %1, void (i8*, i64)** %5
%10 = load i8*, i8** %4
%11 = call i32* @opendir(i8* %10)
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %2
store i64 0, i64* %3
br label %43
15: ; preds = %2
store i64 0, i64* %8
br label %16
16: ; preds = %27, %26, %15
%17 = load i32*, i32** %6
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %9
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %39
20: ; preds = %16
%21 = load %struct.dirent*, %struct.dirent** %9
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 @spx_utils_str_ends_with(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %20
br label %16
27: ; preds = %20
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%29 = load i8*, i8** %4
%30 = load %struct.dirent*, %struct.dirent** %9
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 @snprintf(i8* %28, i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %29, i8* %32)
%34 = load void (i8*, i64)*, void (i8*, i64)** %5
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%36 = load i64, i64* %8
%37 = load i64, i64* %8
%38 = add i64 %37, 1
store i64 %38, i64* %8
br label %16
39: ; preds = %16
%40 = load i32*, i32** %6
%41 = call i32 @closedir(i32* %40)
%42 = load i64, i64* %8
store i64 %42, i64* %3
br label %43
43: ; preds = %39, %14
%44 = load i64, i64* %3
ret i64 %44
}

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