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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stat(char *, struct stat *);
char *xstrdup_printf(char *, char const *, char *);
int recursive_file_count(const char *path) {
  DIR *dir;
  struct dirent *entry;
  struct stat statbuf;
  char *filename;
  int count;
  dir = opendir(path);
  if (!dir)
    return 0;
  count = 0;
  while ((entry = readdir(dir))) {
    if (*entry->d_name == '.')
      continue;
    filename = xstrdup_printf("%s/%s", path, entry->d_name);
    stat(filename, &statbuf);
    if (S_ISDIR(statbuf.st_mode))
      count += recursive_file_count(filename);
    else if (S_ISREG(statbuf.st_mode))
      count++;
    free(filename);
  }
  closedir(dir);
  return count;
}


x86 assembly (clang, -O0):
<recursive_file_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c0 <recursive_file_count+0x30>
movl   $0x0,-0x4(%rbp)
jmp    1281 <recursive_file_count+0xf1>
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1272 <recursive_file_count+0xe2>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11f6 <recursive_file_count+0x66>
jmp    11c7 <recursive_file_count+0x37>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf8(%rip),%rdi
call   1080 <xstrdup_printf@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1090 <stat@plt>
mov    -0x28(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1244 <recursive_file_count+0xb4>
mov    -0x30(%rbp),%rdi
call   1060 <recursive_file_count@plt>
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    1264 <recursive_file_count+0xd4>
mov    -0x28(%rbp),%edi
call   10a0 <S_ISREG@plt>
cmp    $0x0,%rax
je     125f <recursive_file_count+0xcf>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1264 <recursive_file_count+0xd4>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    11c7 <recursive_file_count+0x37>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recursive_file_count>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <opendir@plt>
test   %rax,%rax
je     1240 <recursive_file_count+0xb0>
mov    %rax,%rbx
xor    %r13d,%r13d
lea    0xe47(%rip),%rbp
mov    %rsp,%r15
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1236 <recursive_file_count+0xa6>
mov    (%rax),%rdx
cmpb   $0x2e,(%rdx)
je     11c0 <recursive_file_count+0x30>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1080 <xstrdup_printf@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%r12
mov    %r15,%rsi
call   1090 <stat@plt>
mov    (%rsp),%r15d
mov    %r15d,%edi
call   1050 <S_ISDIR@plt>
test   %rax,%rax
je     120c <recursive_file_count+0x7c>
mov    %rbp,%rdi
call   1060 <recursive_file_count@plt>
jmp    121f <recursive_file_count+0x8f>
mov    %r15d,%edi
call   10a0 <S_ISREG@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
add    %eax,%r13d
mov    %rbp,%rdi
call   1030 <free@plt>
lea    0xdcf(%rip),%rbp
mov    %r12,%r15
jmp    11c0 <recursive_file_count+0x30>
mov    %rbx,%rdi
call   1070 <closedir@plt>
jmp    1243 <recursive_file_count+0xb3>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
define i32 @recursive_file_count(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %36, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %8
8: ; preds = %6, %30
%9 = phi i32 [ 0, %6 ], [ %32, %30 ]
br label %10
10: ; preds = %8, %13
%11 = tail call %struct.dirent* @readdir(i32* %4)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %34, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 46
br i1 %17, label %10, label %18
18: ; preds = %13
%19 = tail call i8* @xstrdup_printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %15)
%20 = call i32 @stat(i8* %19, %struct.stat* %2)
%21 = load i32, i32* %7
%22 = tail call i64 @S_ISDIR(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %18
%25 = tail call i32 @recursive_file_count(i8* %19)
br label %30
26: ; preds = %18
%27 = tail call i64 @S_ISREG(i32 %21)
%28 = icmp ne i64 %27, 0
%29 = zext i1 %28 to i32
br label %30
30: ; preds = %26, %24
%31 = phi i32 [ %25, %24 ], [ %29, %26 ]
%32 = add nsw i32 %31, %9
%33 = tail call i32 @free(i8* %19)
br label %8
34: ; preds = %10
%35 = tail call i32 @closedir(i32* %4)
br label %36
36: ; preds = %1, %34
%37 = phi i32 [ %9, %34 ], [ 0, %1 ]
ret i32 %37
}