#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 */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ lstat(char *, struct stat *);
char *malloc(int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
char *strcpy(char *, char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
int dircount(char *current_dir) {
  int count = 0;
  struct dirent *dp;
  struct stat buf;
  DIR *dfd;
  char *current_file;
  current_file = malloc(255);
  if ((dfd = opendir(current_dir)) == NULL) {
    syslog(LOG_ERR, "unable to open spool directory %s, skipping", current_dir);
    return (-1);
  }
  while ((dp = readdir(dfd)) != NULL) {
    current_file = strcpy(current_file, current_dir);
    current_file = strcat(current_file, "/");
    current_file = strcat(current_file, dp->d_name);
    if (lstat(current_file, &buf) < 0) {
      syslog(LOG_ERR, "unable to stat entry %s in spool directory",
             current_file);
      continue;
    } else {
      if (!(S_ISREG(buf.st_mode))) {
        continue;
      }
    }
    if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) {
      continue;
    }
    count++;
  }
  closedir(dfd);
  return (count);
}


x86 assembly (clang, -O0):
<dircount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0xff,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11fd <dircount+0x5d>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe14(%rip),%rsi
call   10b0 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fe <dircount+0x15e>
jmp    1202 <dircount+0x62>
mov    -0x30(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12ef <dircount+0x14f>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xdf7(%rip),%rsi
call   10c0 <strcat@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   10c0 <strcat@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
jge    1287 <dircount+0xe7>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xdb1(%rip),%rsi
call   10b0 <syslog@plt>
jmp    1202 <dircount+0x62>
mov    -0x28(%rbp),%edi
call   1080 <S_ISREG@plt>
cmp    $0x0,%eax
jne    129d <dircount+0xfd>
jmp    1202 <dircount+0x62>
jmp    12a2 <dircount+0x102>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdaa(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12dc <dircount+0x13c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd8c(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12e1 <dircount+0x141>
jmp    1202 <dircount+0x62>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1202 <dircount+0x62>
mov    -0x30(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dircount>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    $0xff,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12ae <dircount+0xfe>
mov    %rax,%r15
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12cd <dircount+0x11d>
mov    %rax,%r13
movl   $0x0,0xc(%rsp)
lea    0x10(%rsp),%rbp
lea    0xe4e(%rip),%r12
jmp    123c <dircount+0x8c>
nopl   0x0(%rax)
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
mov    %rbx,%rdx
call   10c0 <syslog@plt>
mov    %r15,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12d5 <dircount+0x125>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
movw   $0x2f,(%rbx,%rax,1)
mov    0x0(%r13),%rsi
mov    %rbx,%rdi
call   10d0 <strcat@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <lstat@plt>
test   %rax,%rax
js     1210 <dircount+0x60>
mov    0x10(%rsp),%edi
call   1090 <S_ISREG@plt>
test   %eax,%eax
je     1228 <dircount+0x78>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1228 <dircount+0x78>
mov    0x0(%r13),%rdi
lea    0xdbd(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1228 <dircount+0x78>
addl   $0x1,0xc(%rsp)
jmp    1228 <dircount+0x78>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xd42(%rip),%rsi
mov    %r14,%rdx
call   10c0 <syslog@plt>
mov    $0xffffffff,%eax
jmp    12e1 <dircount+0x131>
movl   $0x0,0xc(%rsp)
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    0xc(%rsp),%eax
add    $0x18,%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 }
@LOG_ERR = global i32 0
@.str = [44 x i8] c"unable to open spool directory %s, skipping\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [43 x i8] c"unable to stat entry %s in spool directory\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
define i32 @dircount(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.dirent*
%6 = alloca %struct.stat
%7 = alloca i32*
%8 = alloca i8*
store i8* %0, i8** %3
store i32 0, i32* %4
%9 = call i8* @malloc(i32 255)
store i8* %9, i8** %8
%10 = load i8*, i8** %3
%11 = call i32* @opendir(i8* %10)
store i32* %11, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i32, i32* @LOG_ERR
%15 = load i8*, i8** %3
%16 = call i32 @syslog(i32 %14, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %2
br label %67
17: ; preds = %1
br label %18
18: ; preds = %60, %59, %45, %36, %17
%19 = load i32*, i32** %7
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %5
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %63
22: ; preds = %18
%23 = load i8*, i8** %8
%24 = load i8*, i8** %3
%25 = call i8* @strcpy(i8* %23, i8* %24)
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = call i8* @strcat(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%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* @strcat(i8* %28, i8* %31)
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = call i64 @lstat(i8* %33, %struct.stat* %6)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %22
%37 = load i32, i32* @LOG_ERR
%38 = load i8*, i8** %8
%39 = call i32 @syslog(i32 %37, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %38)
br label %18
40: ; preds = %22
%41 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @S_ISREG(i32 %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %40
br label %18
46: ; preds = %40
br label %47
47: ; preds = %46
%48 = load %struct.dirent*, %struct.dirent** %5
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %59, label %53
53: ; preds = %47
%54 = load %struct.dirent*, %struct.dirent** %5
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i32 0, i32 0
%56 = load i8*, i8** %55
%57 = call i64 @strcmp(i8* %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %53, %47
br label %18
60: ; preds = %53
%61 = load i32, i32* %4
%62 = add nsw i32 %61, 1
store i32 %62, i32* %4
br label %18
63: ; preds = %18
%64 = load i32*, i32** %7
%65 = call i32 @closedir(i32* %64)
%66 = load i32, i32* %4
store i32 %66, i32* %2
br label %67
67: ; preds = %63, %13
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@LOG_ERR = global i32 0
@.str = [44 x i8] c"unable to open spool directory %s, skipping\00"
@.str.2 = [43 x i8] c"unable to stat entry %s in spool directory\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
define i32 @dircount(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call dereferenceable_or_null(255) i8* @malloc(i32 255)
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %12, label %7
7: ; preds = %1
%8 = tail call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %51, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %48
12: ; preds = %1
%13 = load i32, i32* @LOG_ERR
%14 = tail call i32 @syslog(i32 %13, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %54
15: ; preds = %48, %29
%16 = phi %struct.dirent* [ %49, %48 ], [ %30, %29 ]
%17 = tail call i8* @strcpy(i8* dereferenceable(1) %4, i8* dereferenceable(1) %0)
%18 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%19 = getelementptr i8, i8* %4, i64 %18
%20 = bitcast i8* %19 to i16*
store i16 47, i16* %20
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = tail call i8* @strcat(i8* dereferenceable(1) %4, i8* dereferenceable(1) %22)
%24 = call i64 @lstat(i8* %4, %struct.stat* %2)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %15
%27 = load i32, i32* @LOG_ERR
%28 = tail call i32 @syslog(i32 %27, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %29
29: ; preds = %36, %40, %26, %32
%30 = tail call %struct.dirent* @readdir(i32* %5)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %51, label %15
32: ; preds = %15
%33 = load i32, i32* %11
%34 = tail call i32 @S_ISREG(i32 %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %29, label %36
36: ; preds = %32
%37 = load i8*, i8** %21
%38 = tail call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %29, label %40
40: ; preds = %36
%41 = load i8*, i8** %21
%42 = tail call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %29, label %44
44: ; preds = %40
%45 = add nuw nsw i32 %50, 1
%46 = tail call %struct.dirent* @readdir(i32* %5)
%47 = icmp eq %struct.dirent* %46, null
br i1 %47, label %51, label %48
48: ; preds = %10, %44
%49 = phi %struct.dirent* [ %8, %10 ], [ %46, %44 ]
%50 = phi i32 [ 0, %10 ], [ %45, %44 ]
br label %15
51: ; preds = %44, %29, %7
%52 = phi i32 [ 0, %7 ], [ %50, %29 ], [ %45, %44 ]
%53 = tail call i32 @closedir(i32* %5)
br label %54
54: ; preds = %51, %12
%55 = phi i32 [ -1, %12 ], [ %52, %51 ]
ret i32 %55
}