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

/* Variables and functions */
scalar_t__ DT_DIR;
scalar_t__ DT_REG;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int getFileCount(char *path) {
  if (!path)
    return 0;
  DIR *d = NULL;
  struct dirent *de = NULL;
  if (!(d = opendir(path))) {
    printf("error opening directory! Doesn't exist?\n");
    exit(1);
  }
  int i = 0;
  for (i = 0; (de = readdir(d)) != NULL; i++) {
    if (de->d_name[0] == '.') {
      i--;
      continue;
    }
    if (de->d_type == DT_DIR) {
      char newPath[1024] = "";
      strcpy(newPath, path);
      strcat(newPath, "/");
      strcat(newPath, de->d_name);
      int count = getFileCount(newPath);
      i += count;
    } else if (de->d_type != DT_REG) {
      printf("unknown filetype! (%d)\n", de->d_type);
      exit(1);
    }
  }
  closedir(d);
  return i;
}


x86 assembly (clang, -O0):
<getFileCount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b6 <getFileCount+0x26>
movl   $0x0,-0x4(%rbp)
jmp    131e <getFileCount+0x18e>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f5 <getFileCount+0x65>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     130f <getFileCount+0x17f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    123b <getFileCount+0xab>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1301 <getFileCount+0x171>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d8e(%rip),%rcx
cmp    (%rcx),%rax
jne    12bf <getFileCount+0x12f>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x430(%rbp),%rdi
lea    0xda5(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   10a0 <strcat@plt>
lea    -0x430(%rbp),%rdi
call   1090 <getFileCount@plt>
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    12fc <getFileCount+0x16c>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d12(%rip),%rcx
cmp    (%rcx),%rax
je     12f7 <getFileCount+0x167>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12fc <getFileCount+0x16c>
jmp    1301 <getFileCount+0x171>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1203 <getFileCount+0x73>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getFileCount>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
test   %rdi,%rdi
je     1284 <getFileCount+0xe4>
mov    %rdi,%r14
call   1050 <opendir@plt>
test   %rax,%rax
je     12a6 <getFileCount+0x106>
mov    %rax,%r13
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1288 <getFileCount+0xe8>
mov    %rax,%rbx
xor    %ebp,%ebp
mov    0x2ded(%rip),%r12
mov    %rsp,%r15
jmp    120a <getFileCount+0x6a>
add    $0xffffffff,%ebp
add    $0x1,%ebp
mov    %r13,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     128a <getFileCount+0xea>
mov    (%rbx),%rax
cmpb   $0x2e,(%rax)
je     11f0 <getFileCount+0x50>
mov    0x8(%rbx),%rsi
cmp    (%r12),%rsi
jne    1260 <getFileCount+0xc0>
mov    $0x400,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
lea    0xdc0(%rip),%rsi
call   10b0 <strcat@plt>
mov    (%rbx),%rsi
mov    %r15,%rdi
call   10b0 <strcat@plt>
mov    %r15,%rdi
call   10a0 <getFileCount@plt>
add    %eax,%ebp
jmp    11f3 <getFileCount+0x53>
nopl   0x0(%rax)
mov    0x2d79(%rip),%rax
cmp    (%rax),%rsi
je     11f3 <getFileCount+0x53>
lea    0xd8f(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %ebp,%ebp
jmp    1292 <getFileCount+0xf2>
xor    %ebp,%ebp
mov    %r13,%rdi
call   1080 <closedir@plt>
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6d(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

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

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@DT_DIR = global i64 0
@.str.1 = [2 x i8] c"/\00"
@DT_REG = global i64 0
@.str.2 = [24 x i8] c"unknown filetype! (%d)\0A\00"
@str = [40 x i8] c"error opening directory! Doesn't exist?\00"
define i32 @getFileCount(i8* %0) {
%2 = alloca [1024 x i8]
%3 = icmp eq i8* %0, null
br i1 %3, label %50, label %4
4: ; preds = %1
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %12, label %7
7: ; preds = %4
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %47, label %10
10: ; preds = %7
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %15
12: ; preds = %4
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %10, %42
%16 = phi %struct.dirent* [ %8, %10 ], [ %45, %42 ]
%17 = phi i32 [ 0, %10 ], [ %44, %42 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 46
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = add nsw i32 %17, -1
br label %42
24: ; preds = %15
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 1
%26 = load i64, i64* %25
%27 = load i64, i64* @DT_DIR
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %36
29: ; preds = %24
%30 = call i32 @strcpy(i8* %11, i8* %0)
%31 = call i32 @strcat(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%32 = load i8*, i8** %18
%33 = call i32 @strcat(i8* %11, i8* %32)
%34 = call i32 @getFileCount(i8* %11)
%35 = add nsw i32 %34, %17
br label %42
36: ; preds = %24
%37 = load i64, i64* @DT_REG
%38 = icmp eq i64 %26, %37
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i64 %26)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %29, %36, %22
%43 = phi i32 [ %23, %22 ], [ %35, %29 ], [ %17, %36 ]
%44 = add nsw i32 %43, 1
%45 = call %struct.dirent* @readdir(i32* %5)
%46 = icmp eq %struct.dirent* %45, null
br i1 %46, label %47, label %15
47: ; preds = %42, %7
%48 = phi i32 [ 0, %7 ], [ %44, %42 ]
%49 = call i32 @closedir(i32* %5)
br label %50
50: ; preds = %1, %47
%51 = phi i32 [ %48, %47 ], [ 0, %1 ]
ret i32 %51
}