#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;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int readdir_r(int /*<<< orphan*/ *, struct dirent *, struct dirent **);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int count_files(char *path) {
  int sum = 0;
  DIR *dir;
  dir = opendir(path);
  if (!dir) {
    perror("opendir");
    exit(EXIT_FAILURE);
  }
  for (;;) {
    struct dirent entry;
    struct dirent *result;
    char *name;
    char sub_path[256];
    int error = readdir_r(dir, &entry, &result);
    if (error != 0) {
      perror("readdir");
      exit(EXIT_FAILURE);
    }
    if (result == NULL)
      break;
    name = result->d_name;
    if ((strcmp(name, ".") == 0) || (strcmp(name, "..") == 0))
      continue;
    sum++;
    if (result->d_type == DT_DIR) {
      sprintf(sub_path, "%s/%s", path, name);
      sum += count_files(sub_path);
    }
  }
  closedir(dir);
  return sum;
}


x86 assembly (clang, -O0):
<count_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <count_files+0x48>
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    11cd <count_files+0x4d>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1030 <readdir_r@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
je     120b <count_files+0x8b>
lea    0xe10(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x30(%rbp)
jne    121b <count_files+0x9b>
jmp    12b2 <count_files+0x132>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xde0(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     125a <count_files+0xda>
mov    -0x38(%rbp),%rdi
lea    0xdc5(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    125f <count_files+0xdf>
jmp    11cd <count_files+0x4d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d69(%rip),%rcx
cmp    (%rcx),%rax
jne    12ad <count_files+0x12d>
lea    -0x140(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    0xd7d(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x140(%rbp),%rdi
call   1070 <count_files@plt>
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11cd <count_files+0x4d>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0xc(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
mov    %rdi,0x18(%rsp)
call   1040 <opendir@plt>
test   %rax,%rax
je     12bd <count_files+0x13d>
mov    %rax,%r12
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1030 <readdir_r@plt>
test   %eax,%eax
jne    12a3 <count_files+0x123>
xor    %r13d,%r13d
lea    0xe46(%rip),%rbx
lea    0x20(%rsp),%rbp
lea    0x8(%rsp),%r15
jmp    11f6 <count_files+0x76>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1030 <readdir_r@plt>
test   %eax,%eax
jne    12a3 <count_files+0x123>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     1286 <count_files+0x106>
mov    (%rax),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11e0 <count_files+0x60>
mov    %r14,%rdi
lea    0xdef(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11e0 <count_files+0x60>
add    $0x1,%r13d
mov    0x8(%rsp),%rax
mov    0x8(%rax),%rax
mov    0x2da1(%rip),%rcx
cmp    (%rcx),%rax
jne    11e0 <count_files+0x60>
mov    %r13d,0x14(%rsp)
mov    %r12,%r13
mov    %r15,%r12
lea    0x30(%rsp),%r15
mov    %r15,%rdi
lea    0xdb5(%rip),%rsi
mov    0x18(%rsp),%rdx
mov    %r14,%rcx
call   1090 <sprintf@plt>
mov    %r15,%rdi
mov    %r12,%r15
mov    %r13,%r12
mov    0x14(%rsp),%r13d
call   1070 <count_files@plt>
add    %eax,%r13d
jmp    11e0 <count_files+0x60>
mov    %r12,%rdi
call   1050 <closedir@plt>
mov    %r13d,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd3c(%rip),%rdi
jmp    12aa <count_files+0x12a>

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

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@.str = [8 x i8] c"opendir\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [8 x i8] c"readdir\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@DT_DIR = global i64 0
@.str.4 = [6 x i8] c"%s/%s\00"
define i32 @count_files(i8* %0) {
%2 = alloca %struct.dirent
%3 = alloca %struct.dirent*
%4 = alloca [256 x i8]
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %13, label %7
7: ; preds = %1
%8 = bitcast %struct.dirent* %2 to i8*
%9 = bitcast %struct.dirent** %3 to i8*
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%11 = call i32 @readdir_r(i32* %5, %struct.dirent* %2, %struct.dirent** %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %21, label %17
13: ; preds = %1
%14 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %44, %7
%18 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %7, %44
%22 = phi i32 [ %45, %44 ], [ 0, %7 ]
%23 = load %struct.dirent*, %struct.dirent** %3
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %48, label %25
25: ; preds = %21
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i64 0, i32 0
%27 = load i8*, i8** %26
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %44, label %30
30: ; preds = %25
%31 = call i64 @strcmp(i8* %27, 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 %44, label %33
33: ; preds = %30
%34 = add nsw i32 %22, 1
%35 = load %struct.dirent*, %struct.dirent** %3
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i64 0, i32 1
%37 = load i64, i64* %36
%38 = load i64, i64* @DT_DIR
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %44
40: ; preds = %33
%41 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %27)
%42 = call i32 @count_files(i8* %10)
%43 = add nsw i32 %42, %34
br label %44
44: ; preds = %33, %40, %25, %30
%45 = phi i32 [ %22, %30 ], [ %22, %25 ], [ %43, %40 ], [ %34, %33 ]
%46 = call i32 @readdir_r(i32* %5, %struct.dirent* %2, %struct.dirent** %3)
%47 = icmp eq i32 %46, 0
br i1 %47, label %21, label %17
48: ; preds = %21
%49 = call i32 @closedir(i32* %5)
ret i32 %22
}