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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int count_files(const char *folder_path) {
  DIR *d_counting;
  d_counting = opendir(folder_path);
  int number_of_files = 0;
  struct dirent *dir;
  if (d_counting) {
    while ((dir = readdir(d_counting)) != NULL) {
      char *d_name = dir->d_name;
      if (d_name[0] != '.') {
        number_of_files++;
      }
    }
    closedir(d_counting);
  } else {
    perror("ERROR: Failed to access directory.\n");
    exit(1);
  }
  return number_of_files;
}


x86 assembly (clang, -O0):
<count_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ce <count_files+0x7e>
jmp    1180 <count_files+0x30>
mov    -0x10(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11c0 <count_files+0x70>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     11bb <count_files+0x6b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1180 <count_files+0x30>
mov    -0x10(%rbp),%rdi
call   1040 <closedir@plt>
jmp    11e4 <count_files+0x94>
lea    0xe2b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_files>:
push   %rbp
push   %rbx
push   %rax
call   1030 <opendir@plt>
test   %rax,%rax
je     119b <count_files+0x4b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     118a <count_files+0x3a>
nop
mov    (%rax),%rax
xor    %ecx,%ecx
cmpb   $0x2e,(%rax)
setne  %cl
add    %ecx,%ebp
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
jne    1170 <count_files+0x20>
mov    %rbx,%rdi
call   1040 <closedir@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [36 x i8] c"ERROR: Failed to access directory.\0A\00"
define i32 @count_files(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.dirent*
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %3
store i32 0, i32* %4
%9 = load i32*, i32** %3
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %32
11: ; preds = %1
br label %12
12: ; preds = %28, %11
%13 = load i32*, i32** %3
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %5
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %29
16: ; preds = %12
%17 = load %struct.dirent*, %struct.dirent** %5
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 46
br i1 %24, label %25, label %28
25: ; preds = %16
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %28
28: ; preds = %25, %16
br label %12
29: ; preds = %12
%30 = load i32*, i32** %3
%31 = call i32 @closedir(i32* %30)
br label %35
32: ; preds = %1
%33 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %29
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [36 x i8] c"ERROR: Failed to access directory.\0A\00"
define i32 @count_files(i8* nocapture %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %21, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %18, label %7
7: ; preds = %4, %7
%8 = phi %struct.dirent* [ %16, %7 ], [ %5, %4 ]
%9 = phi i32 [ %15, %7 ], [ 0, %4 ]
%10 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%11 = load i8*, i8** %10
%12 = load i8, i8* %11
%13 = icmp ne i8 %12, 46
%14 = zext i1 %13 to i32
%15 = add nuw nsw i32 %9, %14
%16 = tail call %struct.dirent* @readdir(i32* %2)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %18, label %7
18: ; preds = %7, %4
%19 = phi i32 [ 0, %4 ], [ %15, %7 ]
%20 = tail call i32 @closedir(i32* %2)
ret i32 %19
21: ; preds = %1
%22 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
}