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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ qsort(char **, size_t, int,
                         int (*)(void const *, void const *));
struct dirent *readdir(int /*<<< orphan*/ *);
char **string_array_append(char **, int /*<<< orphan*/);
char **string_array_new();
int sort_dir(const char *dirname, char ***list,
             int (*sort)(const char *a, const char *b)) {
  DIR *dir;
  size_t n = 0;
  *list = string_array_new();
  dir = opendir(dirname);
  if (dir) {
    struct dirent *d;
    while ((d = readdir(dir))) {
      *list = string_array_append(*list, d->d_name);
      n += 1;
    }
    closedir(dir);
  }
  if (sort) {
    qsort(*list, n, sizeof(char *), (int (*)(const void *, const void *))sort);
  }
  return 1;
}


x86 assembly (clang, -O0):
<sort_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1060 <string_array_new@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f7 <sort_dir+0x97>
jmp    11aa <sort_dir+0x4a>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11ee <sort_dir+0x8e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
call   1080 <string_array_append@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11aa <sort_dir+0x4a>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
cmpq   $0x0,-0x18(%rbp)
je     121b <sort_dir+0xbb>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x8,%edx
call   1030 <qsort@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sort_dir>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <string_array_new@plt>
mov    %rax,(%r15)
mov    %r12,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11c6 <sort_dir+0x66>
mov    %rax,%r12
mov    %rax,%rdi
call   1070 <readdir@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11be <sort_dir+0x5e>
nopl   0x0(%rax)
mov    (%r15),%rdi
mov    (%rax),%esi
call   1080 <string_array_append@plt>
mov    %rax,(%r15)
add    $0x1,%rbx
mov    %r12,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    11a0 <sort_dir+0x40>
mov    %r12,%rdi
call   1050 <closedir@plt>
test   %r14,%r14
je     11de <sort_dir+0x7e>
mov    (%r15),%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
mov    %r14,%rcx
call   1030 <qsort@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
define i32 @sort_dir(i8* nocapture %0, i8*** nocapture %1, i32 (i8*, i8*)* %2) {
%4 = tail call i8** (...) @string_array_new()
store i8** %4, i8*** %1
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %23, label %7
7: ; preds = %3
%8 = tail call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %20, label %10
10: ; preds = %7, %10
%11 = phi %struct.dirent* [ %18, %10 ], [ %8, %7 ]
%12 = phi i64 [ %17, %10 ], [ 0, %7 ]
%13 = load i8**, i8*** %1
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i8** @string_array_append(i8** %13, i32 %15)
store i8** %16, i8*** %1
%17 = add i64 %12, 1
%18 = tail call %struct.dirent* @readdir(i32* %5)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %20, label %10
20: ; preds = %10, %7
%21 = phi i64 [ 0, %7 ], [ %17, %10 ]
%22 = tail call i32 @closedir(i32* %5)
br label %23
23: ; preds = %20, %3
%24 = phi i64 [ %21, %20 ], [ 0, %3 ]
%25 = icmp eq i32 (i8*, i8*)* %2, null
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i8**, i8*** %1
%28 = tail call i32 @qsort(i8** %27, i64 %24, i32 8, i32 (i8*, i8*)* %2)
br label %29
29: ; preds = %26, %23
ret i32 1
}