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

/* Variables and functions */
scalar_t__ ENOENT;
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ is_rill_file(char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
struct rill_store *rill_store_open(char *);
int /*<<< orphan*/ rill_vfail(char *, int, char *, char const *);
int /*<<< orphan*/ rill_vfail_errno(char *, int, char *, char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
size_t rill_scan_dir(const char *dir, struct rill_store **list, size_t cap) {
  DIR *dir_handle = opendir(dir);
  if (!dir_handle) {
    if (errno == ENOENT)
      return 0;
    rill_vfail_errno(
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/RAttab/rill/refs/heads/master/src/utils.c",
        97, "unable to open dir '%s'", dir);
    return 0;
  }
  size_t len = 0;
  struct dirent *entry = NULL;
  while ((entry = readdir(dir_handle))) {
    if (!is_rill_file(entry->d_name))
      continue;
    char file[PATH_MAX];
    snprintf(file, sizeof(file), "%s/%s", dir, entry->d_name);
    list[len] = rill_store_open(file);
    if (!list[len])
      continue;
    len++;
    if (len == cap) {
      rill_vfail(
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/RAttab/rill/refs/heads/master/src/utils.c",
          115, "rotate: too many files to rotate in '%s'", dir);
      break;
    }
  }
  closedir(dir_handle);
  return len;
}


x86 assembly (clang, -O0):
<rill_scan_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11fc <rill_scan_dir+0x7c>
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%rax
jne    11d3 <rill_scan_dir+0x53>
movq   $0x0,-0x8(%rbp)
jmp    1348 <rill_scan_dir+0x1c8>
mov    -0x10(%rbp),%rcx
lea    0xe22(%rip),%rdi
mov    $0x61,%esi
lea    0xe9a(%rip),%rdx
call   1070 <rill_vfail_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    1348 <rill_scan_dir+0x1c8>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1337 <rill_scan_dir+0x1b7>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <is_rill_file@plt>
cmp    $0x0,%eax
jne    123d <rill_scan_dir+0xbd>
jmp    120c <rill_scan_dir+0x8c>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x48(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    (%rax),%r8
lea    0xe1f(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x58(%rbp),%rdi
call   1080 <rill_store_open@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    12b9 <rill_scan_dir+0x139>
movl   $0x2,-0x4c(%rbp)
jmp    1302 <rill_scan_dir+0x182>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    12fb <rill_scan_dir+0x17b>
mov    -0x10(%rbp),%rcx
lea    0xd22(%rip),%rdi
mov    $0x73,%esi
lea    0xdb8(%rip),%rdx
call   1030 <rill_vfail@plt>
movl   $0x3,-0x4c(%rbp)
jmp    1302 <rill_scan_dir+0x182>
movl   $0x0,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4c(%rbp),%eax
mov    %eax,-0x5c(%rbp)
test   %eax,%eax
je     1332 <rill_scan_dir+0x1b2>
jmp    131c <rill_scan_dir+0x19c>
mov    -0x5c(%rbp),%eax
sub    $0x2,%eax
je     120c <rill_scan_dir+0x8c>
jmp    132d <rill_scan_dir+0x1ad>
jmp    1337 <rill_scan_dir+0x1b7>
jmp    120c <rill_scan_dir+0x8c>
mov    -0x28(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rill_scan_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,-0x30(%rbp)
call   1040 <opendir@plt>
test   %rax,%rax
je     1248 <rill_scan_dir+0xc8>
mov    %rax,%rbx
xor    %r15d,%r15d
nop
mov    %rbx,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     123b <rill_scan_dir+0xbb>
mov    %rax,%r13
mov    (%rax),%rdi
call   10a0 <is_rill_file@plt>
test   %eax,%eax
je     11b0 <rill_scan_dir+0x30>
mov    %r14,-0x40(%rbp)
mov    %rsp,-0x38(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    %r12,%r14
mov    %rsp,%r12
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    0x0(%r13),%r8
mov    %r12,%rdi
lea    0xe9a(%rip),%rdx
mov    -0x30(%rbp),%rcx
call   1050 <snprintf@plt>
mov    %r12,%rdi
mov    %r14,%r12
call   1080 <rill_store_open@plt>
mov    %rax,(%r14,%r15,8)
test   %rax,%rax
je     122e <rill_scan_dir+0xae>
add    $0x1,%r15
mov    -0x40(%rbp),%r14
cmp    %r14,%r15
jne    1232 <rill_scan_dir+0xb2>
jmp    128f <rill_scan_dir+0x10f>
mov    -0x40(%rbp),%r14
mov    -0x38(%rbp),%rsp
jmp    11b0 <rill_scan_dir+0x30>
mov    %r15,%r14
mov    %rbx,%rdi
call   1060 <closedir@plt>
jmp    127d <rill_scan_dir+0xfd>
mov    0x2d89(%rip),%rax
mov    (%rax),%rax
mov    0x2d6f(%rip),%rcx
cmp    (%rcx),%rax
je     127a <rill_scan_dir+0xfa>
lea    0xd9b(%rip),%rdi
lea    0xe18(%rip),%rdx
mov    $0x61,%esi
mov    -0x30(%rbp),%rcx
call   1070 <rill_vfail_errno@plt>
xor    %r14d,%r14d
mov    %r14,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6a(%rip),%rdi
lea    0xe05(%rip),%rdx
mov    $0x73,%esi
mov    -0x30(%rbp),%rcx
call   1030 <rill_vfail@plt>
mov    -0x38(%rbp),%rsp
jmp    123e <rill_scan_dir+0xbe>

LLVM IR (-O0):
%struct.rill_store = type { i32 }
%struct.dirent = type { i8* }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [132 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/RAttab/rill/refs/heads/master/src/utils.c\00"
@.str.1 = [24 x i8] c"unable to open dir '%s'\00"
@PATH_MAX = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [41 x i8] c"rotate: too many files to rotate in '%s'\00"
define i64 @rill_scan_dir(i8* %0, %struct.rill_store** %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca %struct.rill_store**
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca %struct.dirent*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %5
store %struct.rill_store** %1, %struct.rill_store*** %6
store i64 %2, i64* %7
%14 = load i8*, i8** %5
%15 = call i32* @opendir(i8* %14)
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp ne i32* %16, null
br i1 %17, label %26, label %18
18: ; preds = %3
%19 = load i64, i64* @errno
%20 = load i64, i64* @ENOENT
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
store i64 0, i64* %4
br label %77
23: ; preds = %18
%24 = load i8*, i8** %5
%25 = call i32 @rill_vfail_errno(i8* getelementptr inbounds ([132 x i8], [132 x i8]* @.str, i64 0, i64 0), i32 97, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i64 0, i64* %4
br label %77
26: ; preds = %3
store i64 0, i64* %9
store %struct.dirent* null, %struct.dirent** %10
br label %27
27: ; preds = %72, %69, %37, %26
%28 = load i32*, i32** %8
%29 = call %struct.dirent* @readdir(i32* %28)
store %struct.dirent* %29, %struct.dirent** %10
%30 = icmp ne %struct.dirent* %29, null
br i1 %30, label %31, label %73
31: ; preds = %27
%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 @is_rill_file(i8* %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %31
br label %27
38: ; preds = %31
%39 = load i32, i32* @PATH_MAX
%40 = zext i32 %39 to i64
%41 = call i8* @llvm.stacksave()
store i8* %41, i8** %11
%42 = alloca i8, i64 %40
store i64 %40, i64* %12
%43 = trunc i64 %40 to i32
%44 = load i8*, i8** %5
%45 = load %struct.dirent*, %struct.dirent** %10
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i32 @snprintf(i8* %42, i32 %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %47)
%49 = call %struct.rill_store* @rill_store_open(i8* %42)
%50 = load %struct.rill_store**, %struct.rill_store*** %6
%51 = load i64, i64* %9
%52 = getelementptr inbounds %struct.rill_store*, %struct.rill_store** %50, i64 %51
store %struct.rill_store* %49, %struct.rill_store** %52
%53 = load %struct.rill_store**, %struct.rill_store*** %6
%54 = load i64, i64* %9
%55 = getelementptr inbounds %struct.rill_store*, %struct.rill_store** %53, i64 %54
%56 = load %struct.rill_store*, %struct.rill_store** %55
%57 = icmp ne %struct.rill_store* %56, null
br i1 %57, label %59, label %58
58: ; preds = %38
store i32 2, i32* %13
br label %69
59: ; preds = %38
%60 = load i64, i64* %9
%61 = add i64 %60, 1
store i64 %61, i64* %9
%62 = load i64, i64* %9
%63 = load i64, i64* %7
%64 = icmp eq i64 %62, %63
br i1 %64, label %65, label %68
65: ; preds = %59
%66 = load i8*, i8** %5
%67 = call i32 @rill_vfail(i8* getelementptr inbounds ([132 x i8], [132 x i8]* @.str, i64 0, i64 0), i32 115, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %66)
store i32 3, i32* %13
br label %69
68: ; preds = %59
store i32 0, i32* %13
br label %69
69: ; preds = %68, %65, %58
%70 = load i8*, i8** %11
%71 = load i32, i32* %13
switch i32 %71, label %79 [
i32 0, label %72
i32 2, label %27
i32 3, label %73
]
72: ; preds = %69
br label %27
73: ; preds = %69, %27
%74 = load i32*, i32** %8
%75 = call i32 @closedir(i32* %74)
%76 = load i64, i64* %9
store i64 %76, i64* %4
br label %77
77: ; preds = %73, %23, %22
%78 = load i64, i64* %4
ret i64 %78
79: ; preds = %69
unreachable
}

LLVM IR (-O2):
%struct.rill_store = type { i32 }
%struct.dirent = type { i8* }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [132 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/RAttab/rill/refs/heads/master/src/utils.c\00"
@.str.1 = [24 x i8] c"unable to open dir '%s'\00"
@PATH_MAX = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [41 x i8] c"rotate: too many files to rotate in '%s'\00"
define i64 @rill_scan_dir(i8* %0, %struct.rill_store** nocapture writeonly %1, i64 %2) {
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %38
6: ; preds = %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %43, label %10
10: ; preds = %6
%11 = tail call i32 @rill_vfail_errno(i8* getelementptr inbounds ([132 x i8], [132 x i8]* @.str, i64 0, i64 0), i32 97, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %43
12: ; preds = %38, %15
%13 = call %struct.dirent* @readdir(i32* %4)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %40, label %15
15: ; preds = %12
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = call i32 @is_rill_file(i8* %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %12, label %20
20: ; preds = %15
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%22 = load i32, i32* @PATH_MAX
%23 = zext i32 %22 to i64
%24 = call i8* @llvm.stacksave()
%25 = alloca i8, i64 %23
%26 = load i8*, i8** %21
%27 = call i32 @snprintf(i8* %25, i32 %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %26)
%28 = call %struct.rill_store* @rill_store_open(i8* %25)
%29 = getelementptr inbounds %struct.rill_store*, %struct.rill_store** %1, i64 %39
store %struct.rill_store* %28, %struct.rill_store** %29
%30 = icmp eq %struct.rill_store* %28, null
br i1 %30, label %36, label %31
31: ; preds = %20
%32 = add i64 %39, 1
%33 = icmp eq i64 %32, %2
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @rill_vfail(i8* getelementptr inbounds ([132 x i8], [132 x i8]* @.str, i64 0, i64 0), i32 115, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %40
36: ; preds = %31, %20
%37 = phi i64 [ %39, %20 ], [ %32, %31 ]
br label %38
38: ; preds = %3, %36
%39 = phi i64 [ %37, %36 ], [ 0, %3 ]
br label %12
40: ; preds = %12, %34
%41 = phi i64 [ %2, %34 ], [ %39, %12 ]
%42 = call i32 @closedir(i32* %4)
br label %43
43: ; preds = %6, %40, %10
%44 = phi i64 [ %41, %40 ], [ 0, %10 ], [ 0, %6 ]
ret i64 %44
}