#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 */
typedef int /*<<< orphan*/ wpath;
struct stat {
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ ext;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ get_extension(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strncmp(char *, char const *, int);
int count_filetype_in_dir(const char *path, const char *extension) {
  int count = 0;
  char ext[64];
  struct dirent *ds;
  struct stat st;
  char wpath[1024];
  DIR *dr;
  if ((dr = opendir(path)) == NULL)
    return 0;
  while ((ds = readdir(dr))) {
    if (*(ds->d_name) != '.') {
      memset(wpath, 0, sizeof(wpath));
      snprintf(wpath, sizeof(wpath), "%s/%s", path, ds->d_name);
      if (stat(wpath, &st) >= 0) {
        if (S_ISDIR(st.st_mode)) {
          count += count_filetype_in_dir(wpath, extension);
        } else {
          memset(ext, 0, sizeof(ext));
          if (get_extension(wpath, ext, sizeof(ext)) < 0)
            printf("cannot get ext\n");
          else if (!strncmp(ext, extension, sizeof(ext)))
            count++;
        }
      }
    }
  }
  closedir(dr);
  return count;
}


x86 assembly (clang, -O0):
<count_filetype_in_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x478(%rbp)
cmp    $0x0,%rax
jne    11f0 <count_filetype_in_dir+0x40>
movl   $0x0,-0x4(%rbp)
jmp    132e <count_filetype_in_dir+0x17e>
jmp    11f5 <count_filetype_in_dir+0x45>
mov    -0x478(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x68(%rbp)
cmp    $0x0,%rax
je     131c <count_filetype_in_dir+0x16c>
mov    -0x68(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     1317 <count_filetype_in_dir+0x167>
lea    -0x470(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
lea    -0x470(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x68(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xdad(%rip),%rdx
call   1080 <snprintf@plt>
lea    -0x470(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%rax
jl     1312 <count_filetype_in_dir+0x162>
mov    -0x70(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     129f <count_filetype_in_dir+0xef>
lea    -0x470(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <count_filetype_in_dir@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    130d <count_filetype_in_dir+0x15d>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1090 <memset@plt>
lea    -0x470(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   10d0 <get_extension@plt>
cmp    $0x0,%rax
jge    12df <count_filetype_in_dir+0x12f>
lea    0xd31(%rip),%rdi
call   1050 <printf@plt>
jmp    1308 <count_filetype_in_dir+0x158>
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x40,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1303 <count_filetype_in_dir+0x153>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1308 <count_filetype_in_dir+0x158>
jmp    130d <count_filetype_in_dir+0x15d>
jmp    1312 <count_filetype_in_dir+0x162>
jmp    1317 <count_filetype_in_dir+0x167>
jmp    11f5 <count_filetype_in_dir+0x45>
mov    -0x478(%rbp),%rdi
call   10a0 <closedir@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_filetype_in_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x458,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
call   1050 <opendir@plt>
test   %rax,%rax
je     12d1 <count_filetype_in_dir+0x121>
mov    %rax,%rbx
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12d6 <count_filetype_in_dir+0x126>
mov    %rax,%r13
xor    %r12d,%r12d
lea    0x50(%rsp),%rbp
jmp    1224 <count_filetype_in_dir+0x74>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1070 <count_filetype_in_dir@plt>
add    %eax,%r12d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12d9 <count_filetype_in_dir+0x129>
mov    0x0(%r13),%rax
cmpb   $0x2e,(%rax)
je     1210 <count_filetype_in_dir+0x60>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
mov    0x0(%r13),%r8
mov    %rbp,%rdi
mov    $0x400,%esi
lea    0xdb1(%rip),%rdx
mov    %r15,%rcx
call   1080 <snprintf@plt>
mov    %rbp,%rdi
lea    0x8(%rsp),%rsi
call   10b0 <stat@plt>
test   %rax,%rax
js     1210 <count_filetype_in_dir+0x60>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
jne    11f6 <count_filetype_in_dir+0x46>
lea    0x10(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1090 <memset@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    $0x40,%edx
call   10d0 <get_extension@plt>
test   %rax,%rax
js     12c0 <count_filetype_in_dir+0x110>
mov    %r13,%rdi
mov    %r14,%rsi
mov    $0x40,%edx
call   1030 <strncmp@plt>
cmp    $0x1,%eax
adc    $0x0,%r12d
jmp    1210 <count_filetype_in_dir+0x60>
lea    0xd3f(%rip),%rdi
call   1040 <puts@plt>
jmp    1210 <count_filetype_in_dir+0x60>
xor    %r12d,%r12d
jmp    12e1 <count_filetype_in_dir+0x131>
xor    %r12d,%r12d
mov    %rbx,%rdi
call   10a0 <closedir@plt>
mov    %r12d,%eax
add    $0x458,%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"
@.str.1 = [16 x i8] c"cannot get ext\0A\00"
define i32 @count_filetype_in_dir(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [64 x i8]
%8 = alloca %struct.dirent*
%9 = alloca %struct.stat
%10 = alloca [1024 x i8]
%11 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
%12 = load i8*, i8** %4
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %11
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %77
16: ; preds = %2
br label %17
17: ; preds = %72, %16
%18 = load i32*, i32** %11
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %8
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %73
21: ; preds = %17
%22 = load %struct.dirent*, %struct.dirent** %8
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 46
br i1 %27, label %28, label %72
28: ; preds = %21
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%30 = call i32 @memset(i8* %29, i32 0, i32 1024)
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%32 = load i8*, i8** %4
%33 = load %struct.dirent*, %struct.dirent** %8
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @snprintf(i8* %31, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %32, i8* %35)
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%38 = call i64 @stat(i8* %37, %struct.stat* %9)
%39 = icmp sge i64 %38, 0
br i1 %39, label %40, label %71
40: ; preds = %28
%41 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @S_ISDIR(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %40
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%47 = load i8*, i8** %5
%48 = call i32 @count_filetype_in_dir(i8* %46, i8* %47)
%49 = load i32, i32* %6
%50 = add nsw i32 %49, %48
store i32 %50, i32* %6
br label %70
51: ; preds = %40
%52 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%53 = call i32 @memset(i8* %52, i32 0, i32 64)
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%55 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%56 = call i64 @get_extension(i8* %54, i8* %55, i32 64)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %51
%59 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %69
60: ; preds = %51
%61 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%62 = load i8*, i8** %5
%63 = call i32 @strncmp(i8* %61, i8* %62, i32 64)
%64 = icmp ne i32 %63, 0
br i1 %64, label %68, label %65
65: ; preds = %60
%66 = load i32, i32* %6
%67 = add nsw i32 %66, 1
store i32 %67, i32* %6
br label %68
68: ; preds = %65, %60
br label %69
69: ; preds = %68, %58
br label %70
70: ; preds = %69, %45
br label %71
71: ; preds = %70, %28
br label %72
72: ; preds = %71, %21
br label %17
73: ; preds = %17
%74 = load i32*, i32** %11
%75 = call i32 @closedir(i32* %74)
%76 = load i32, i32* %6
store i32 %76, i32* %3
br label %77
77: ; preds = %73, %15
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
@str = [15 x i8] c"cannot get ext\00"
define i32 @count_filetype_in_dir(i8* %0, i8* %1) {
%3 = alloca [64 x i8]
%4 = alloca %struct.stat
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.stat* %4 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = tail call i32* @opendir(i8* %0)
%10 = icmp eq i32* %9, null
br i1 %10, label %54, label %11
11: ; preds = %2
%12 = call %struct.dirent* @readdir(i32* %9)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %51, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %16
16: ; preds = %14, %47
%17 = phi %struct.dirent* [ %12, %14 ], [ %49, %47 ]
%18 = phi i32 [ 0, %14 ], [ %48, %47 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 46
br i1 %22, label %47, label %23
23: ; preds = %16
%24 = call i32 @memset(i8* %8, i32 0, i32 1024)
%25 = load i8*, i8** %19
%26 = call i32 @snprintf(i8* %8, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %25)
%27 = call i64 @stat(i8* %8, %struct.stat* %4)
%28 = icmp sgt i64 %27, -1
br i1 %28, label %29, label %47
29: ; preds = %23
%30 = load i32, i32* %15
%31 = call i64 @S_ISDIR(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %29
%34 = call i32 @count_filetype_in_dir(i8* %8, i8* %1)
%35 = add nsw i32 %34, %18
br label %47
36: ; preds = %29
%37 = call i32 @memset(i8* %6, i32 0, i32 64)
%38 = call i64 @get_extension(i8* %8, i8* %6, i32 64)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %47
42: ; preds = %36
%43 = call i32 @strncmp(i8* %6, i8* %1, i32 64)
%44 = icmp eq i32 %43, 0
%45 = zext i1 %44 to i32
%46 = add nsw i32 %18, %45
br label %47
47: ; preds = %42, %23, %40, %33, %16
%48 = phi i32 [ %35, %33 ], [ %18, %40 ], [ %18, %23 ], [ %18, %16 ], [ %46, %42 ]
%49 = call %struct.dirent* @readdir(i32* %9)
%50 = icmp eq %struct.dirent* %49, null
br i1 %50, label %51, label %16
51: ; preds = %47, %11
%52 = phi i32 [ 0, %11 ], [ %48, %47 ]
%53 = call i32 @closedir(i32* %9)
br label %54
54: ; preds = %2, %51
%55 = phi i32 [ %52, %51 ], [ 0, %2 ]
ret i32 %55
}