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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *, char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int snprintf(char *, int, char *, char const *, char *);
scalar_t__ strcmp(char const *, char *);
int count_files(const char *directory, const char *file_name) {
  DIR *dir;
  struct dirent *entry;
  int file_cnt = 0;
  if (!(dir = opendir(directory)) || !(entry = readdir(dir))) {
    printf("[fdcmp] Could not open directory '%s'\n", directory);
    return file_cnt;
  }
  do {
    if (entry->d_type == DT_DIR) {
      char path[2048];
      int len = snprintf(path, 2048 - 1, "%s/%s", directory, entry->d_name);
      path[len] = '\0';
      if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
        continue;
      }
      file_cnt += count_files(path, file_name);
    } else if (strcmp(file_name, entry->d_name) == 0) {
      file_cnt++;
    }
  } while ((entry = readdir(dir)));
  closedir(dir);
  return file_cnt;
}


x86 assembly (clang, -O0):
<count_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11b8 <count_files+0x48>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d3 <count_files+0x63>
mov    -0x10(%rbp),%rsi
lea    0xe3d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d8 <count_files+0x168>
jmp    11d8 <count_files+0x68>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%rax
jne    1284 <count_files+0x114>
lea    -0x830(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%r8
mov    $0x7ff,%esi
lea    0xe19(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x834(%rbp)
movslq -0x834(%rbp),%rax
movb   $0x0,-0x830(%rbp,%rax,1)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdf7(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1264 <count_files+0xf4>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdd8(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1269 <count_files+0xf9>
jmp    12b2 <count_files+0x142>
lea    -0x830(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <count_files@plt>
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    12ad <count_files+0x13d>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12a8 <count_files+0x138>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12ad <count_files+0x13d>
jmp    12b2 <count_files+0x142>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d8 <count_files+0x68>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     124b <count_files+0xdb>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     124b <count_files+0xdb>
mov    %rax,%r12
xor    %ebp,%ebp
mov    0x2e2b(%rip),%r13
jmp    11e4 <count_files+0x74>
nopw   0x0(%rax,%rax,1)
mov    0x8(%r12),%rsi
mov    %r15,%rdi
call   1070 <strcmp@plt>
cmp    $0x1,%rax
adc    $0x0,%ebp
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     125e <count_files+0xee>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    11c0 <count_files+0x50>
mov    0x8(%r12),%r8
mov    %rsp,%rdi
mov    $0x7ff,%esi
lea    0xe25(%rip),%rdx
mov    %r14,%rcx
call   1050 <snprintf@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
mov    0x8(%r12),%rdi
lea    0xe12(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11d4 <count_files+0x64>
mov    0x8(%r12),%rdi
lea    0xdfb(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11d4 <count_files+0x64>
mov    %rsp,%rdi
mov    %r15,%rsi
call   1090 <count_files@plt>
add    %eax,%ebp
jmp    11d4 <count_files+0x64>
lea    0xdae(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
xor    %ebp,%ebp
jmp    1266 <count_files+0xf6>
mov    %rbx,%rdi
call   1060 <closedir@plt>
mov    %ebp,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@.str = [39 x i8] c"[fdcmp] Could not open directory '%s'\0A\00"
@DT_DIR = global i64 0
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
define i32 @count_files(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32
%9 = alloca [2048 x i8]
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %8
%11 = load i8*, i8** %4
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32*, i32** %6
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %7
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %22, label %18
18: ; preds = %14, %2
%19 = load i8*, i8** %4
%20 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* %8
store i32 %21, i32* %3
br label %77
22: ; preds = %14
br label %23
23: ; preds = %69, %22
%24 = load %struct.dirent*, %struct.dirent** %7
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load i64, i64* @DT_DIR
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %57
29: ; preds = %23
%30 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%31 = load i8*, i8** %4
%32 = load %struct.dirent*, %struct.dirent** %7
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 1
%34 = load i8*, i8** %33
%35 = call i32 @snprintf(i8* %30, i32 2047, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %34)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 %37
store i8 0, i8* %38
%39 = load %struct.dirent*, %struct.dirent** %7
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 1
%41 = load i8*, i8** %40
%42 = call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %50, label %44
44: ; preds = %29
%45 = load %struct.dirent*, %struct.dirent** %7
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 1
%47 = load i8*, i8** %46
%48 = call i64 @strcmp(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %44, %29
br label %69
51: ; preds = %44
%52 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%53 = load i8*, i8** %5
%54 = call i32 @count_files(i8* %52, i8* %53)
%55 = load i32, i32* %8
%56 = add nsw i32 %55, %54
store i32 %56, i32* %8
br label %68
57: ; preds = %23
%58 = load i8*, i8** %5
%59 = load %struct.dirent*, %struct.dirent** %7
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 1
%61 = load i8*, i8** %60
%62 = call i64 @strcmp(i8* %58, i8* %61)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %57
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
br label %67
67: ; preds = %64, %57
br label %68
68: ; preds = %67, %51
br label %69
69: ; preds = %68, %50
%70 = load i32*, i32** %6
%71 = call %struct.dirent* @readdir(i32* %70)
store %struct.dirent* %71, %struct.dirent** %7
%72 = icmp ne %struct.dirent* %71, null
br i1 %72, label %23, label %73
73: ; preds = %69
%74 = load i32*, i32** %6
%75 = call i32 @closedir(i32* %74)
%76 = load i32, i32* %8
store i32 %76, i32* %3
br label %77
77: ; preds = %73, %18
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@.str = [39 x i8] c"[fdcmp] Could not open directory '%s'\0A\00"
@DT_DIR = global i64 0
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
define i32 @count_files(i8* %0, i8* %1) {
%3 = alloca [2048 x i8]
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = tail call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
br label %13
11: ; preds = %6, %2
%12 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %51
13: ; preds = %9, %45
%14 = phi %struct.dirent* [ %47, %45 ], [ %7, %9 ]
%15 = phi i32 [ %46, %45 ], [ 0, %9 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%17 = load i64, i64* %16
%18 = load i64, i64* @DT_DIR
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %38
20: ; preds = %13
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 1
%22 = load i8*, i8** %21
%23 = call i32 @snprintf(i8* %10, i32 2047, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %22)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %24
store i8 0, i8* %25
%26 = load i8*, i8** %21
%27 = call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %36, label %29
29: ; preds = %20
%30 = load i8*, i8** %21
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %29
%34 = call i32 @count_files(i8* %10, i8* %1)
%35 = add nsw i32 %34, %15
br label %36
36: ; preds = %20, %29, %33
%37 = phi i32 [ %35, %33 ], [ %15, %29 ], [ %15, %20 ]
br label %45
38: ; preds = %13
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 1
%40 = load i8*, i8** %39
%41 = call i64 @strcmp(i8* %1, i8* %40)
%42 = icmp eq i64 %41, 0
%43 = zext i1 %42 to i32
%44 = add nsw i32 %15, %43
br label %45
45: ; preds = %38, %36
%46 = phi i32 [ %37, %36 ], [ %44, %38 ]
%47 = call %struct.dirent* @readdir(i32* %4)
%48 = icmp eq %struct.dirent* %47, null
br i1 %48, label %49, label %13
49: ; preds = %45
%50 = call i32 @closedir(i32* %4)
br label %51
51: ; preds = %49, %11
%52 = phi i32 [ %46, %49 ], [ 0, %11 ]
ret i32 %52
}