#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;
};

/* Variables and functions */
int /*<<< orphan*/ alphasort;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(int /*<<< orphan*/);
int scandir(char *, struct dirent ***, int /*<<< orphan*/ *,
            int /*<<< orphan*/);
int main(void) {
  struct dirent **dirs;
  int n = scandir("./", &dirs, NULL, alphasort);
  if (n >= 0) {
    for (int cnt = 0; cnt < n; ++cnt)
      puts(dirs[cnt]->d_name);
  } else
    perror("Cannot open directory");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%ecx
lea    0xeb1(%rip),%rdi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <scandir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11a2 <main+0x72>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119d <main+0x6d>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1030 <puts@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1170 <main+0x40>
jmp    11ae <main+0x7e>
lea    0xe5a(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2ea5(%rip),%rax
mov    (%rax),%ecx
lea    0xebc(%rip),%rdi
mov    %rsp,%rsi
xor    %edx,%edx
call   1040 <scandir@plt>
test   %eax,%eax
js     1182 <main+0x52>
je     1178 <main+0x48>
mov    %eax,%r14d
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    (%rsp),%rax
mov    (%rax,%rbx,8),%rax
mov    (%rax),%edi
call   1030 <puts@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1160 <main+0x30>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe7a(%rip),%rdi
call   1050 <perror@plt>
jmp    1178 <main+0x48>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [3 x i8] c"./\00"
@alphasort = global i32 0
@.str.1 = [22 x i8] c"Cannot open directory\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.dirent**
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @alphasort
%6 = call i32 @scandir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %struct.dirent*** %2, i32* null, i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %27
9: ; preds = %0
store i32 0, i32* %4
br label %10
10: ; preds = %23, %9
%11 = load i32, i32* %4
%12 = load i32, i32* %3
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = load %struct.dirent**, %struct.dirent*** %2
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds %struct.dirent*, %struct.dirent** %15, i64 %17
%19 = load %struct.dirent*, %struct.dirent** %18
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @puts(i32 %21)
br label %23
23: ; preds = %14
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %10
26: ; preds = %10
br label %29
27: ; preds = %0
%28 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %26
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [3 x i8] c"./\00"
@alphasort = global i32 0
@.str.1 = [22 x i8] c"Cannot open directory\00"
define i32 @main() {
%1 = alloca %struct.dirent**
%2 = bitcast %struct.dirent*** %1 to i8*
%3 = load i32, i32* @alphasort
%4 = call i32 @scandir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %struct.dirent*** %1, i32* null, i32 %3)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %20
6: ; preds = %0
%7 = icmp eq i32 %4, 0
br i1 %7, label %22, label %8
8: ; preds = %6
%9 = zext i32 %4 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %18, %10 ]
%12 = load %struct.dirent**, %struct.dirent*** %1
%13 = getelementptr inbounds %struct.dirent*, %struct.dirent** %12, i64 %11
%14 = load %struct.dirent*, %struct.dirent** %13
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @puts(i32 %16)
%18 = add nuw nsw i64 %11, 1
%19 = icmp eq i64 %18, %9
br i1 %19, label %22, label %10
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %10, %6, %20
ret i32 0
}