#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 */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
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 count_dir(const char *src_path, int *num_folders, int *num_files) {
  struct dirent *ds;
  struct stat st;
  char wpath[1024];
  DIR *dr;
  if (!num_folders || !num_files || (dr = opendir(src_path)) == NULL)
    return -1;
  (*num_files) = 0;
  (*num_folders) = 0;
  while ((ds = readdir(dr))) {
    if (*(ds->d_name) == '.')
      continue;
    memset(wpath, 0, sizeof(wpath));
    snprintf(wpath, sizeof(wpath), "%s/%s", src_path, ds->d_name);
    if (stat(wpath, &st) < 0)
      continue;
    if (!S_ISDIR(st.st_mode))
      (*num_files)++;
    else
      (*num_folders)++;
  }
  closedir(dr);
  return 0;
}


x86 assembly (clang, -O0):
<count_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b7 <count_dir+0x47>
cmpq   $0x0,-0x20(%rbp)
je     11b7 <count_dir+0x47>
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x438(%rbp)
cmp    $0x0,%rax
jne    11c3 <count_dir+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a2 <count_dir+0x132>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x438(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     128f <count_dir+0x11f>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1209 <count_dir+0x99>
jmp    11d7 <count_dir+0x67>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xdc6(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x430(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    125e <count_dir+0xee>
jmp    11d7 <count_dir+0x67>
mov    -0x30(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    127f <count_dir+0x10f>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    128a <count_dir+0x11a>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    11d7 <count_dir+0x67>
mov    -0x438(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    $0xffffffff,%ebp
test   %rsi,%rsi
je     1256 <count_dir+0xe6>
mov    %rdx,%r14
test   %rdx,%rdx
je     1256 <count_dir+0xe6>
mov    %rsi,%r15
mov    %rdi,%r12
call   1030 <opendir@plt>
test   %rax,%rax
je     1256 <count_dir+0xe6>
mov    %rax,%rbx
movl   $0x0,(%r14)
movl   $0x0,(%r15)
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     124c <count_dir+0xdc>
mov    %rax,%rbp
lea    0x10(%rsp),%r13
jmp    11f0 <count_dir+0x80>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     124c <count_dir+0xdc>
mov    0x0(%rbp),%rax
cmpb   $0x2e,(%rax)
je     11e0 <count_dir+0x70>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%r8
mov    %r13,%rdi
mov    $0x400,%esi
lea    0xde5(%rip),%rdx
mov    %r12,%rcx
call   1050 <snprintf@plt>
mov    %r13,%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
js     11e0 <count_dir+0x70>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
mov    %r15,%rax
cmove  %r14,%rax
addl   $0x1,(%rax)
jmp    11e0 <count_dir+0x70>
mov    %rbx,%rdi
call   1070 <closedir@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x418,%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 @count_dir(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca %struct.stat
%10 = alloca [1024 x i8]
%11 = alloca i32*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %21
14: ; preds = %3
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i8*, i8** %5
%19 = call i32* @opendir(i8* %18)
store i32* %19, i32** %11
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %17, %14, %3
store i32 -1, i32* %4
br label %67
22: ; preds = %17
%23 = load i32*, i32** %7
store i32 0, i32* %23
%24 = load i32*, i32** %6
store i32 0, i32* %24
br label %25
25: ; preds = %63, %49, %36, %22
%26 = load i32*, i32** %11
%27 = call %struct.dirent* @readdir(i32* %26)
store %struct.dirent* %27, %struct.dirent** %8
%28 = icmp ne %struct.dirent* %27, null
br i1 %28, label %29, label %64
29: ; preds = %25
%30 = load %struct.dirent*, %struct.dirent** %8
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 46
br i1 %35, label %36, label %37
36: ; preds = %29
br label %25
37: ; preds = %29
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%39 = call i32 @memset(i8* %38, i32 0, i32 1024)
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%41 = load i8*, i8** %5
%42 = load %struct.dirent*, %struct.dirent** %8
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i32 @snprintf(i8* %40, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %41, i8* %44)
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%47 = call i64 @stat(i8* %46, %struct.stat* %9)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %37
br label %25
50: ; preds = %37
%51 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @S_ISDIR(i32 %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %59, label %55
55: ; preds = %50
%56 = load i32*, i32** %7
%57 = load i32, i32* %56
%58 = add nsw i32 %57, 1
store i32 %58, i32* %56
br label %63
59: ; preds = %50
%60 = load i32*, i32** %6
%61 = load i32, i32* %60
%62 = add nsw i32 %61, 1
store i32 %62, i32* %60
br label %63
63: ; preds = %59, %55
br label %25
64: ; preds = %25
%65 = load i32*, i32** %11
%66 = call i32 @closedir(i32* %65)
store i32 0, i32* %4
br label %67
67: ; preds = %64, %21
%68 = load i32, i32* %4
ret i32 %68
}

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