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

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


x86 assembly (clang, -O0):
<FileList_getFileCount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d8 <FileList_getFileCount+0x88>
jmp    1181 <FileList_getFileCount+0x31>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11cf <FileList_getFileCount+0x7f>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
mov    -0x28(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11be <FileList_getFileCount+0x6e>
jmp    1181 <FileList_getFileCount+0x31>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1181 <FileList_getFileCount+0x31>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FileList_getFileCount>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
call   1030 <opendir@plt>
test   %rax,%rax
je     11b2 <FileList_getFileCount+0x62>
mov    %rax,%r14
mov    %rax,%rdi
call   1070 <readdir@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11a8 <FileList_getFileCount+0x58>
lea    0x8(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    (%rax),%edi
mov    %r15,%rsi
call   1060 <lstat@plt>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x1,%rax
adc    $0x0,%rbx
mov    %r14,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    1180 <FileList_getFileCount+0x30>
mov    %r14,%rdi
call   1050 <closedir@plt>
jmp    11b4 <FileList_getFileCount+0x64>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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