#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*/ *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int get_file_count(char *root) {
  DIR *dir;
  struct dirent *ptr;
  int total = 0;
  char path[1024];
  dir = opendir(root);
  if (dir == NULL) {
    perror("fail to open dir");
    exit(1);
  }
  errno = 0;
  while ((ptr = readdir(dir)) != NULL) {
    if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
      continue;
    if (ptr->d_type == DT_DIR) {
      sprintf(path, "%s/%s", root, ptr->d_name);
      total += get_file_count(path);
    }
    if (ptr->d_type == DT_REG)
      total++;
  }
  if (errno != 0) {
    perror("fail to read dir");
    exit(1);
  }
  closedir(dir);
  return total;
}


x86 assembly (clang, -O0):
<get_file_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c4 <get_file_count+0x44>
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2e05(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1296 <get_file_count+0x116>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1223 <get_file_count+0xa3>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdfd(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1228 <get_file_count+0xa8>
jmp    11d2 <get_file_count+0x52>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2da1(%rip),%rcx
cmp    (%rcx),%rax
jne    1270 <get_file_count+0xf0>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
lea    0xdbb(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1030 <get_file_count@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d61(%rip),%rcx
cmp    (%rcx),%rax
jne    1291 <get_file_count+0x111>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d2 <get_file_count+0x52>
mov    0x2d33(%rip),%rax
cmpq   $0x0,(%rax)
je     12bd <get_file_count+0x13d>
lea    0xd6c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x1c(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_count>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r14
call   1040 <opendir@plt>
test   %rax,%rax
je     1289 <get_file_count+0x109>
mov    %rax,%rbx
mov    0x2e24(%rip),%rax
movq   $0x0,(%rax)
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     125c <get_file_count+0xdc>
mov    %rax,%r13
xor    %r15d,%r15d
lea    0xe41(%rip),%rbp
lea    0xe39(%rip),%r12
jmp    1200 <get_file_count+0x80>
xor    %ecx,%ecx
mov    0x2dfd(%rip),%rdx
cmp    (%rdx),%rax
sete   %cl
add    %ecx,%r15d
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     125f <get_file_count+0xdf>
mov    0x0(%r13),%rdi
mov    %rbp,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11f0 <get_file_count+0x70>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11f0 <get_file_count+0x70>
mov    0x8(%r13),%rax
mov    0x2dab(%rip),%rcx
cmp    (%rcx),%rax
jne    11da <get_file_count+0x5a>
mov    0x0(%r13),%rcx
mov    %rsp,%rdi
lea    0xdd4(%rip),%rsi
mov    %r14,%rdx
call   1090 <sprintf@plt>
mov    %rsp,%rdi
call   1030 <get_file_count@plt>
add    %eax,%r15d
mov    0x8(%r13),%rax
jmp    11da <get_file_count+0x5a>
xor    %r15d,%r15d
mov    0x2d6a(%rip),%rax
cmpq   $0x0,(%rax)
jne    1292 <get_file_count+0x112>
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %r15d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd70(%rip),%rdi
jmp    1299 <get_file_count+0x119>
lea    0xd81(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@.str = [17 x i8] c"fail to open dir\00"
@errno = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@DT_DIR = global i64 0
@.str.3 = [6 x i8] c"%s/%s\00"
@DT_REG = global i64 0
@.str.4 = [17 x i8] c"fail to read dir\00"
define i32 @get_file_count(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i32
%6 = alloca [1024 x i8]
store i8* %0, i8** %2
store i32 0, i32* %5
%7 = load i8*, i8** %2
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
store i64 0, i64* @errno
br label %15
15: ; preds = %58, %31, %14
%16 = load i32*, i32** %3
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %4
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %59
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %4
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %19
%26 = load %struct.dirent*, %struct.dirent** %4
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25, %19
br label %15
32: ; preds = %25
%33 = load %struct.dirent*, %struct.dirent** %4
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 1
%35 = load i64, i64* %34
%36 = load i64, i64* @DT_DIR
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %49
38: ; preds = %32
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%40 = load i8*, i8** %2
%41 = load %struct.dirent*, %struct.dirent** %4
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = call i32 @sprintf(i8* %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %40, i8* %43)
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%46 = call i32 @get_file_count(i8* %45)
%47 = load i32, i32* %5
%48 = add nsw i32 %47, %46
store i32 %48, i32* %5
br label %49
49: ; preds = %38, %32
%50 = load %struct.dirent*, %struct.dirent** %4
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 1
%52 = load i64, i64* %51
%53 = load i64, i64* @DT_REG
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = load i32, i32* %5
%57 = add nsw i32 %56, 1
store i32 %57, i32* %5
br label %58
58: ; preds = %55, %49
br label %15
59: ; preds = %15
%60 = load i64, i64* @errno
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %59
%66 = load i32*, i32** %3
%67 = call i32 @closedir(i32* %66)
%68 = load i32, i32* %5
ret i32 %68
}

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