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

/* Variables and functions */
scalar_t__ DT_REG;
char **__array_grow(char **, int, int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, char *, char *, char *, char *);
int /*<<< orphan*/ file_is_verse_source(char *, int);
char *malloc(int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
char **package_source_files(int from_line, char *from_file,
                            char *package_path) {
  DIR *d = opendir(package_path);
  if (!d) {
    error(from_line, from_file, "Could not load package with path '%s': %s",
          package_path, strerror(errno));
  }
  char **out = NULL;
  struct dirent *ent = NULL;
  while ((ent = readdir(d)) != NULL) {
    if (ent->d_type != DT_REG) {
      continue;
    }
    int namelen = strlen(ent->d_name);
    if (!file_is_verse_source(ent->d_name, namelen)) {
      continue;
    }
    int len = strlen(package_path) + namelen;
    char *filepath = malloc(sizeof(char) * (len + 2));
    snprintf(filepath, len + 2, "%s/%s", package_path, ent->d_name);
    (((((out)) ? (((int *)((out)) - 2)[1] - ((int *)((out)) - 2)[0]) : 0) < (1)
          ? ((((out))) = __array_grow((((out))), ((1)), sizeof(*(((out))))))
          : 0),
     (out)[((int *)(out)-2)[0]++] = (filepath));
  }
  closedir(d);
  return out;
}


x86 assembly (clang, -O0):
<package_source_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1209 <package_source_files+0x69>
mov    -0x4(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rcx
mov    %rax,%r8
lea    0xdfc(%rip),%rdx
call   10b0 <error@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     133d <package_source_files+0x19d>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%rax
je     124c <package_source_files+0xac>
jmp    1219 <package_source_files+0x79>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x34(%rbp),%esi
call   10a0 <file_is_verse_source@plt>
cmp    $0x0,%eax
jne    127a <package_source_files+0xda>
jmp    1219 <package_source_files+0x79>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%esi
add    $0x2,%esi
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%r8
lea    0xd6d(%rip),%rdx
call   1050 <snprintf@plt>
cmpq   $0x0,-0x28(%rbp)
je     12e3 <package_source_files+0x143>
mov    -0x28(%rbp),%rax
mov    -0x4(%rax),%eax
mov    -0x28(%rbp),%rcx
sub    -0x8(%rcx),%eax
mov    %eax,-0x58(%rbp)
jmp    12ed <package_source_files+0x14d>
xor    %eax,%eax
mov    %eax,-0x58(%rbp)
jmp    12ed <package_source_files+0x14d>
mov    -0x58(%rbp),%eax
cmp    $0x1,%eax
jge    1315 <package_source_files+0x175>
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
mov    $0x8,%edx
call   1070 <__array_grow@plt>
mov    %rax,-0x28(%rbp)
jmp    131a <package_source_files+0x17a>
jmp    131a <package_source_files+0x17a>
mov    -0x40(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    -0x28(%rbp),%rsi
mov    -0x8(%rsi),%ecx
mov    %ecx,%edi
add    $0x1,%edi
mov    %edi,-0x8(%rsi)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    1219 <package_source_files+0x79>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x28(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<package_source_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rdx,%rdi
call   1030 <opendir@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11e8 <package_source_files+0x48>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xe28(%rip),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rcx
mov    %rax,%r8
call   10b0 <error@plt>
xor    %r15d,%r15d
mov    0x2dee(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     129f <package_source_files+0xff>
mov    %rax,%rbx
mov    (%rax),%rax
cmp    (%r12),%rax
jne    1200 <package_source_files+0x60>
mov    0x8(%rbx),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
mov    %eax,%esi
call   10a0 <file_is_verse_source@plt>
test   %eax,%eax
je     1200 <package_source_files+0x60>
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %eax,%ebp
add    $0x2,%ebp
mov    %ebp,%edi
call   1090 <malloc@plt>
mov    0x8(%rbx),%r8
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
lea    0xdcc(%rip),%rdx
mov    %r14,%rcx
call   1050 <snprintf@plt>
test   %r15,%r15
je     1275 <package_source_files+0xd5>
mov    -0x8(%r15),%eax
cmp    %eax,-0x4(%r15)
jg     128d <package_source_files+0xed>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
call   1070 <__array_grow@plt>
mov    %rax,%r15
mov    -0x8(%rax),%eax
lea    0x1(%rax),%ecx
mov    %ecx,-0x8(%r15)
cltq
mov    %rbx,(%r15,%rax,8)
jmp    1200 <package_source_files+0x60>
mov    %r13,%rdi
call   1060 <closedir@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@.str = [42 x i8] c"Could not load package with path '%s': %s\00"
@errno = global i32 0
@DT_REG = global i64 0
@.str.1 = [6 x i8] c"%s/%s\00"
define i8** @package_source_files(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8**
%9 = alloca %struct.dirent*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%13 = load i8*, i8** %6
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %24, label %17
17: ; preds = %3
%18 = load i32, i32* %4
%19 = load i8*, i8** %5
%20 = load i8*, i8** %6
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @error(i32 %18, i8* %19, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %22)
br label %24
24: ; preds = %17, %3
store i8** null, i8*** %8
store %struct.dirent* null, %struct.dirent** %9
br label %25
25: ; preds = %89, %47, %35, %24
%26 = load i32*, i32** %7
%27 = call %struct.dirent* @readdir(i32* %26)
store %struct.dirent* %27, %struct.dirent** %9
%28 = icmp ne %struct.dirent* %27, null
br i1 %28, label %29, label %101
29: ; preds = %25
%30 = load %struct.dirent*, %struct.dirent** %9
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i64, i64* %31
%33 = load i64, i64* @DT_REG
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %29
br label %25
36: ; preds = %29
%37 = load %struct.dirent*, %struct.dirent** %9
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 1
%39 = load i8*, i8** %38
%40 = call i32 @strlen(i8* %39)
store i32 %40, i32* %10
%41 = load %struct.dirent*, %struct.dirent** %9
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 1
%43 = load i8*, i8** %42
%44 = load i32, i32* %10
%45 = call i32 @file_is_verse_source(i8* %43, i32 %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %48, label %47
47: ; preds = %36
br label %25
48: ; preds = %36
%49 = load i8*, i8** %6
%50 = call i32 @strlen(i8* %49)
%51 = load i32, i32* %10
%52 = add nsw i32 %50, %51
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 2
%55 = sext i32 %54 to i64
%56 = mul i64 1, %55
%57 = trunc i64 %56 to i32
%58 = call i8* @malloc(i32 %57)
store i8* %58, i8** %12
%59 = load i8*, i8** %12
%60 = load i32, i32* %11
%61 = add nsw i32 %60, 2
%62 = load i8*, i8** %6
%63 = load %struct.dirent*, %struct.dirent** %9
%64 = getelementptr inbounds %struct.dirent, %struct.dirent* %63, i32 0, i32 1
%65 = load i8*, i8** %64
%66 = call i32 @snprintf(i8* %59, i32 %61, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %62, i8* %65)
%67 = load i8**, i8*** %8
%68 = icmp ne i8** %67, null
br i1 %68, label %69, label %81
69: ; preds = %48
%70 = load i8**, i8*** %8
%71 = bitcast i8** %70 to i32*
%72 = getelementptr inbounds i32, i32* %71, i64 -2
%73 = getelementptr inbounds i32, i32* %72, i64 1
%74 = load i32, i32* %73
%75 = load i8**, i8*** %8
%76 = bitcast i8** %75 to i32*
%77 = getelementptr inbounds i32, i32* %76, i64 -2
%78 = getelementptr inbounds i32, i32* %77, i64 0
%79 = load i32, i32* %78
%80 = sub nsw i32 %74, %79
br label %82
81: ; preds = %48
br label %82
82: ; preds = %81, %69
%83 = phi i32 [ %80, %69 ], [ 0, %81 ]
%84 = icmp slt i32 %83, 1
br i1 %84, label %85, label %88
85: ; preds = %82
%86 = load i8**, i8*** %8
%87 = call i8** @__array_grow(i8** %86, i32 1, i32 8)
store i8** %87, i8*** %8
br label %89
88: ; preds = %82
br label %89
89: ; preds = %88, %85
%90 = phi i8** [ %87, %85 ], [ null, %88 ]
%91 = load i8*, i8** %12
%92 = load i8**, i8*** %8
%93 = load i8**, i8*** %8
%94 = bitcast i8** %93 to i32*
%95 = getelementptr inbounds i32, i32* %94, i64 -2
%96 = getelementptr inbounds i32, i32* %95, i64 0
%97 = load i32, i32* %96
%98 = add nsw i32 %97, 1
store i32 %98, i32* %96
%99 = sext i32 %97 to i64
%100 = getelementptr inbounds i8*, i8** %92, i64 %99
store i8* %91, i8** %100
br label %25
101: ; preds = %25
%102 = load i32*, i32** %7
%103 = call i32 @closedir(i32* %102)
%104 = load i8**, i8*** %8
ret i8** %104
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@.str = [42 x i8] c"Could not load package with path '%s': %s\00"
@errno = global i32 0
@DT_REG = global i64 0
@.str.1 = [6 x i8] c"%s/%s\00"
define i8** @package_source_files(i32 %0, i8* %1, i8* %2) {
%4 = tail call i32* @opendir(i8* %2)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @error(i32 %0, i8* %1, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %8)
br label %10
10: ; preds = %6, %3
br label %11
11: ; preds = %10, %50
%12 = phi i8** [ null, %10 ], [ %53, %50 ]
br label %13
13: ; preds = %11, %22
br label %14
14: ; preds = %13, %17
%15 = tail call %struct.dirent* @readdir(i32* %4)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %57, label %17
17: ; preds = %14
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%19 = load i64, i64* %18
%20 = load i64, i64* @DT_REG
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %14
22: ; preds = %17
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 1
%24 = load i8*, i8** %23
%25 = tail call i32 @strlen(i8* %24)
%26 = load i8*, i8** %23
%27 = tail call i32 @file_is_verse_source(i8* %26, i32 %25)
%28 = icmp eq i32 %27, 0
br i1 %28, label %13, label %29
29: ; preds = %22
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 1
%31 = tail call i32 @strlen(i8* %2)
%32 = add i32 %25, 2
%33 = add i32 %32, %31
%34 = tail call i8* @malloc(i32 %33)
%35 = load i8*, i8** %30
%36 = tail call i32 @snprintf(i8* %34, i32 %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %35)
%37 = icmp eq i8** %12, null
br i1 %37, label %45, label %38
38: ; preds = %29
%39 = getelementptr inbounds i8*, i8** %12, i64 -1
%40 = bitcast i8** %39 to i32*
%41 = getelementptr inbounds i32, i32* %40, i64 1
%42 = load i32, i32* %41
%43 = load i32, i32* %40
%44 = icmp sgt i32 %42, %43
br i1 %44, label %50, label %45
45: ; preds = %29, %38
%46 = tail call i8** @__array_grow(i8** %12, i32 1, i32 8)
%47 = getelementptr inbounds i8*, i8** %46, i64 -1
%48 = bitcast i8** %47 to i32*
%49 = load i32, i32* %48
br label %50
50: ; preds = %38, %45
%51 = phi i32* [ %40, %38 ], [ %48, %45 ]
%52 = phi i32 [ %43, %38 ], [ %49, %45 ]
%53 = phi i8** [ %12, %38 ], [ %46, %45 ]
%54 = add nsw i32 %52, 1
store i32 %54, i32* %51
%55 = sext i32 %52 to i64
%56 = getelementptr inbounds i8*, i8** %53, i64 %55
store i8* %34, i8** %56
br label %11
57: ; preds = %14
%58 = tail call i32 @closedir(i32* %4)
ret i8** %12
}