#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*/ DIR;

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


x86 assembly (clang, -O0):
<count_filetype_in_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x440(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x438(%rbp)
cmp    $0x0,%rax
jne    11eb <count_filetype_in_dir+0x4b>
movl   $0x0,-0x4(%rbp)
jmp    1309 <count_filetype_in_dir+0x169>
jmp    11f0 <count_filetype_in_dir+0x50>
mov    -0x438(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12f7 <count_filetype_in_dir+0x157>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1222 <count_filetype_in_dir+0x82>
jmp    11f0 <count_filetype_in_dir+0x50>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xdad(%rip),%rdx
call   1080 <snprintf@plt>
lea    -0x430(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%rax
jge    1277 <count_filetype_in_dir+0xd7>
jmp    11f0 <count_filetype_in_dir+0x50>
mov    -0x30(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12a4 <count_filetype_in_dir+0x104>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <count_filetype_in_dir@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    12f2 <count_filetype_in_dir+0x152>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2e,%esi
call   1040 <get_last@plt>
mov    %rax,-0x440(%rbp)
cmp    $0x0,%rax
je     12ed <count_filetype_in_dir+0x14d>
mov    -0x440(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12ed <count_filetype_in_dir+0x14d>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12f2 <count_filetype_in_dir+0x152>
jmp    11f0 <count_filetype_in_dir+0x50>
mov    -0x438(%rbp),%rdi
call   10a0 <closedir@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%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    $0x428,%rsp
mov    %rsi,%rbp
mov    %rdi,%r15
call   1050 <opendir@plt>
test   %rax,%rax
je     11f4 <count_filetype_in_dir+0x54>
mov    %rax,%rbx
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     11f8 <count_filetype_in_dir+0x58>
mov    %rax,%r12
mov    %rbp,0x18(%rsp)
movl   $0x0,0xc(%rsp)
lea    0x20(%rsp),%rbp
lea    0xe13(%rip),%r13
lea    0x10(%rsp),%r14
jmp    1240 <count_filetype_in_dir+0xa0>
xor    %eax,%eax
jmp    120c <count_filetype_in_dir+0x6c>
movl   $0x0,0xc(%rsp)
mov    %rbx,%rdi
call   10a0 <closedir@plt>
mov    0xc(%rsp),%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
mov    0x18(%rsp),%rsi
call   1070 <count_filetype_in_dir@plt>
add    %eax,0xc(%rsp)
nop
mov    %rbx,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     1200 <count_filetype_in_dir+0x60>
mov    (%r12),%rax
cmpb   $0x2e,(%rax)
je     1230 <count_filetype_in_dir+0x90>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
mov    (%r12),%r8
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %r13,%rdx
mov    %r15,%rcx
call   1080 <snprintf@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   10b0 <stat@plt>
test   %rax,%rax
js     1230 <count_filetype_in_dir+0x90>
mov    0x10(%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
jne    121e <count_filetype_in_dir+0x7e>
mov    (%r12),%rdi
mov    $0x2e,%esi
call   1040 <get_last@plt>
test   %rax,%rax
je     1230 <count_filetype_in_dir+0x90>
mov    %rax,%rdi
mov    0x18(%rsp),%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
cmp    $0x1,%eax
adcl   $0x0,0xc(%rsp)
jmp    1230 <count_filetype_in_dir+0x90>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\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 %struct.dirent*
%8 = alloca %struct.stat
%9 = alloca [1024 x i8]
%10 = alloca i32*
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i8* null, i8** %11
%12 = load i8*, i8** %4
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %10
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %73
16: ; preds = %2
br label %17
17: ; preds = %68, %41, %28, %16
%18 = load i32*, i32** %10
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %7
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %69
21: ; preds = %17
%22 = load %struct.dirent*, %struct.dirent** %7
%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 eq i32 %26, 46
br i1 %27, label %28, label %29
28: ; preds = %21
br label %17
29: ; preds = %21
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i32 0, i32 1024)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%33 = load i8*, i8** %4
%34 = load %struct.dirent*, %struct.dirent** %7
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @snprintf(i8* %32, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %33, i8* %36)
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%39 = call i64 @stat(i8* %38, %struct.stat* %8)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %29
br label %17
42: ; preds = %29
%43 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @S_ISDIR(i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %42
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%49 = load i8*, i8** %5
%50 = call i32 @count_filetype_in_dir(i8* %48, i8* %49)
%51 = load i32, i32* %6
%52 = add nsw i32 %51, %50
store i32 %52, i32* %6
br label %68
53: ; preds = %42
%54 = load %struct.dirent*, %struct.dirent** %7
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i32 0, i32 0
%56 = load i8*, i8** %55
%57 = call i8* @get_last(i8* %56, i8 signext 46)
store i8* %57, i8** %11
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %67
59: ; preds = %53
%60 = load i8*, i8** %11
%61 = load i8*, i8** %5
%62 = call i32 @strncmp(i8* %60, i8* %61, i32 8)
%63 = icmp ne i32 %62, 0
br i1 %63, label %67, label %64
64: ; preds = %59
%65 = load i32, i32* %6
%66 = add nsw i32 %65, 1
store i32 %66, i32* %6
br label %67
67: ; preds = %64, %59, %53
br label %68
68: ; preds = %67, %47
br label %17
69: ; preds = %17
%70 = load i32*, i32** %10
%71 = call i32 @closedir(i32* %70)
%72 = load i32, i32* %6
store i32 %72, i32* %3
br label %73
73: ; preds = %69, %15
%74 = load i32, i32* %3
ret i32 %74
}

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