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

/* Variables and functions */
size_t FileList_getFileCount(char const *);
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *FileList_getFileNameString(const char *directoryPath, size_t fileNumber) {
  if (fileNumber >= FileList_getFileCount(directoryPath)) {
    printf("ERROR: illegal file index");
    return 0;
  } else {
    struct dirent *directoryEntry;
    DIR *directory = opendir(directoryPath);
    if (directory) {
      size_t currentIndex = 0;
      while ((directoryEntry = readdir(directory)) != NULL) {
        struct stat st;
        lstat(directoryEntry->d_name, &st);
        if (S_ISDIR(st.st_mode)) {
          continue;
        } else {
          if (currentIndex == fileNumber) {
            closedir(directory);
            return directoryEntry->d_name;
          }
          currentIndex++;
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<FileList_getFileNameString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <FileList_getFileCount@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jb     11ba <FileList_getFileNameString+0x4a>
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1262 <FileList_getFileNameString+0xf2>
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1255 <FileList_getFileNameString+0xe5>
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1250 <FileList_getFileNameString+0xe0>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <lstat@plt>
mov    -0x38(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1218 <FileList_getFileNameString+0xa8>
jmp    11da <FileList_getFileNameString+0x6a>
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    123f <FileList_getFileNameString+0xcf>
mov    -0x28(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1262 <FileList_getFileNameString+0xf2>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11da <FileList_getFileNameString+0x6a>
jmp    1255 <FileList_getFileNameString+0xe5>
jmp    125a <FileList_getFileNameString+0xea>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FileList_getFileNameString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
call   1080 <FileList_getFileCount@plt>
cmp    %r15,%rax
jbe    11f3 <FileList_getFileNameString+0x83>
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11ff <FileList_getFileNameString+0x8f>
mov    %rax,%r13
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     11ff <FileList_getFileNameString+0x8f>
mov    %rax,%rbx
xor    %r14d,%r14d
mov    %rsp,%r12
xor    %ebp,%ebp
jmp    11d0 <FileList_getFileNameString+0x60>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1202 <FileList_getFileNameString+0x92>
mov    (%rbx),%rdi
mov    %r12,%rsi
call   1070 <lstat@plt>
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
jne    11c0 <FileList_getFileNameString+0x50>
cmp    %r15,%rbp
je     1214 <FileList_getFileNameString+0xa4>
add    $0x1,%rbp
jmp    11c0 <FileList_getFileNameString+0x50>
lea    0xe06(%rip),%rdi
call   1040 <printf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rdi
call   1060 <closedir@plt>
mov    (%rbx),%r14
jmp    1202 <FileList_getFileNameString+0x92>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [26 x i8] c"ERROR: illegal file index\00"
define i8* @FileList_getFileNameString(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.dirent*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca %struct.stat
store i8* %0, i8** %4
store i64 %1, i64* %5
%10 = load i64, i64* %5
%11 = load i8*, i8** %4
%12 = call i64 @FileList_getFileCount(i8* %11)
%13 = icmp uge i64 %10, %12
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %53
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call i32* @opendir(i8* %17)
store i32* %18, i32** %7
%19 = load i32*, i32** %7
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %51
21: ; preds = %16
store i64 0, i64* %8
br label %22
22: ; preds = %49, %35, %21
%23 = load i32*, i32** %7
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %6
%25 = icmp ne %struct.dirent* %24, null
br i1 %25, label %26, label %50
26: ; preds = %22
%27 = load %struct.dirent*, %struct.dirent** %6
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 @lstat(i8* %29, %struct.stat* %9)
%31 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i64 @S_ISDIR(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %26
br label %22
36: ; preds = %26
%37 = load i64, i64* %8
%38 = load i64, i64* %5
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %46
40: ; preds = %36
%41 = load i32*, i32** %7
%42 = call i32 @closedir(i32* %41)
%43 = load %struct.dirent*, %struct.dirent** %6
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
store i8* %45, i8** %3
br label %53
46: ; preds = %36
%47 = load i64, i64* %8
%48 = add i64 %47, 1
store i64 %48, i64* %8
br label %49
49: ; preds = %46
br label %22
50: ; preds = %22
br label %51
51: ; preds = %50, %16
br label %52
52: ; preds = %51
store i8* null, i8** %3
br label %53
53: ; preds = %52, %40, %14
%54 = load i8*, i8** %3
ret i8* %54
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [26 x i8] c"ERROR: illegal file index\00"
define i8* @FileList_getFileNameString(i8* %0, i64 %1) {
%3 = alloca %struct.stat
%4 = tail call i64 @FileList_getFileCount(i8* %0)
%5 = icmp ugt i64 %4, %1
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %38
8: ; preds = %2
%9 = tail call i32* @opendir(i8* %0)
%10 = icmp eq i32* %9, null
br i1 %10, label %38, label %11
11: ; preds = %8
%12 = tail call %struct.dirent* @readdir(i32* %9)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %38, label %14
14: ; preds = %11
%15 = bitcast %struct.stat* %3 to i8*
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %17
17: ; preds = %14, %30
%18 = phi %struct.dirent* [ %12, %14 ], [ %32, %30 ]
%19 = phi i64 [ 0, %14 ], [ %31, %30 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @lstat(i8* %21, %struct.stat* %3)
%23 = load i32, i32* %16
%24 = tail call i64 @S_ISDIR(i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %17
%27 = icmp eq i64 %19, %1
br i1 %27, label %34, label %28
28: ; preds = %26
%29 = add i64 %19, 1
br label %30
30: ; preds = %17, %28
%31 = phi i64 [ %29, %28 ], [ %19, %17 ]
%32 = tail call %struct.dirent* @readdir(i32* %9)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %38, label %17
34: ; preds = %26
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%36 = tail call i32 @closedir(i32* %9)
%37 = load i8*, i8** %35
br label %38
38: ; preds = %30, %11, %34, %8, %6
%39 = phi i8* [ null, %6 ], [ %37, %34 ], [ null, %8 ], [ null, %11 ], [ null, %30 ]
ret i8* %39
}