#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;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *strstr(char *, char *);
int _DC_nuof_messages(char *box, char *name) {
  DIR *d;
  struct dirent *de;
  int nuof = 0;
  char *s;
  if (!box)
    return (0);
  if ((d = opendir(box)) == NULL)
    return (0);
  s = malloc(100 + (name ? strlen(name) : strlen("message")));
  strcpy(s, name ? name : "message");
  strcat(s, ".");
  while ((de = readdir(d)) != NULL) {
    char *found = strstr(de->d_name, s);
    if (found == de->d_name)
      nuof++;
  }
  if (d)
    closedir(d);
  free(s);
  return (nuof);
}


x86 assembly (clang, -O0):
<_DC_nuof_messages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11be <_DC_nuof_messages+0x2e>
movl   $0x0,-0x4(%rbp)
jmp    12d9 <_DC_nuof_messages+0x149>
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11e1 <_DC_nuof_messages+0x51>
movl   $0x0,-0x4(%rbp)
jmp    12d9 <_DC_nuof_messages+0x149>
cmpq   $0x0,-0x18(%rbp)
je     11fe <_DC_nuof_messages+0x6e>
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x48(%rbp)
jmp    120e <_DC_nuof_messages+0x7e>
lea    0xdfb(%rip),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
add    $0x64,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     123f <_DC_nuof_messages+0xaf>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    124f <_DC_nuof_messages+0xbf>
lea    0xdba(%rip),%rax
mov    %rax,-0x58(%rbp)
jmp    124f <_DC_nuof_messages+0xbf>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x38(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12b6 <_DC_nuof_messages+0x126>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
call   10b0 <strstr@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%rax
jne    12b1 <_DC_nuof_messages+0x121>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    126c <_DC_nuof_messages+0xdc>
cmpq   $0x0,-0x20(%rbp)
je     12ca <_DC_nuof_messages+0x13a>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_DC_nuof_messages>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %ebp,%ebp
test   %rdi,%rdi
je     124c <_DC_nuof_messages+0xbc>
mov    %rsi,%r15
call   1060 <opendir@plt>
test   %rax,%rax
je     124c <_DC_nuof_messages+0xbc>
mov    %rax,%r14
test   %r15,%r15
je     11cd <_DC_nuof_messages+0x3d>
mov    %r15,%rdi
call   1070 <strlen@plt>
mov    %rax,%rdi
add    $0x64,%rdi
jmp    11d9 <_DC_nuof_messages+0x49>
mov    $0x6b,%edi
lea    0xe27(%rip),%r15
call   10a0 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <strcpy@plt>
lea    0xe15(%rip),%rsi
mov    %r12,%rdi
call   10b0 <strcat@plt>
mov    %r14,%rdi
call   1090 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     123c <_DC_nuof_messages+0xac>
nopw   0x0(%rax,%rax,1)
mov    (%rax),%rbx
mov    %r12,%rdi
call   1070 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
call   1040 <strncmp@plt>
cmp    $0x1,%eax
adc    $0x0,%ebp
mov    %r14,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
jne    1210 <_DC_nuof_messages+0x80>
mov    %r14,%rdi
call   1080 <closedir@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [8 x i8] c"message\00"
@.str.1 = [2 x i8] c".\00"
define i32 @_DC_nuof_messages(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %8
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i32 0, i32* %3
br label %71
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32* @opendir(i8* %15)
store i32* %16, i32** %6
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 0, i32* %3
br label %71
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i8*, i8** %5
%24 = call i64 @strlen(i8* %23)
br label %27
25: ; preds = %19
%26 = call i64 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = phi i64 [ %24, %22 ], [ %26, %25 ]
%29 = add nsw i64 100, %28
%30 = call i8* @malloc(i64 %29)
store i8* %30, i8** %9
%31 = load i8*, i8** %9
%32 = load i8*, i8** %5
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = load i8*, i8** %5
br label %37
36: ; preds = %27
br label %37
37: ; preds = %36, %34
%38 = phi i8* [ %35, %34 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %36 ]
%39 = call i32 @strcpy(i8* %31, i8* %38)
%40 = load i8*, i8** %9
%41 = call i32 @strcat(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %60, %37
%43 = load i32*, i32** %6
%44 = call %struct.dirent* @readdir(i32* %43)
store %struct.dirent* %44, %struct.dirent** %7
%45 = icmp ne %struct.dirent* %44, null
br i1 %45, label %46, label %61
46: ; preds = %42
%47 = load %struct.dirent*, %struct.dirent** %7
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = load i8*, i8** %9
%51 = call i8* @strstr(i8* %49, i8* %50)
store i8* %51, i8** %10
%52 = load i8*, i8** %10
%53 = load %struct.dirent*, %struct.dirent** %7
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = icmp eq i8* %52, %55
br i1 %56, label %57, label %60
57: ; preds = %46
%58 = load i32, i32* %8
%59 = add nsw i32 %58, 1
store i32 %59, i32* %8
br label %60
60: ; preds = %57, %46
br label %42
61: ; preds = %42
%62 = load i32*, i32** %6
%63 = icmp ne i32* %62, null
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = load i32*, i32** %6
%66 = call i32 @closedir(i32* %65)
br label %67
67: ; preds = %64, %61
%68 = load i8*, i8** %9
%69 = call i32 @free(i8* %68)
%70 = load i32, i32* %8
store i32 %70, i32* %3
br label %71
71: ; preds = %67, %18, %13
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [8 x i8] c"message\00"
@.str.1 = [2 x i8] c".\00"
define i32 @_DC_nuof_messages(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %36, label %4
4: ; preds = %2
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %36, label %7
7: ; preds = %4
%8 = icmp eq i8* %1, null
br i1 %8, label %12, label %9
9: ; preds = %7
%10 = tail call i64 @strlen(i8* %1)
%11 = add i64 %10, 100
br label %12
12: ; preds = %7, %9
%13 = phi i8* [ %1, %9 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %7 ]
%14 = phi i64 [ %11, %9 ], [ 107, %7 ]
%15 = tail call i8* @malloc(i64 %14)
%16 = tail call i32 @strcpy(i8* %15, i8* %13)
%17 = tail call i32 @strcat(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call %struct.dirent* @readdir(i32* %5)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %32, label %20
20: ; preds = %12, %20
%21 = phi %struct.dirent* [ %30, %20 ], [ %18, %12 ]
%22 = phi i32 [ %29, %20 ], [ 0, %12 ]
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = tail call i64 @strlen(i8* dereferenceable(1) %15)
%26 = tail call i32 @strncmp(i8* %24, i8* %15, i64 %25)
%27 = icmp eq i32 %26, 0
%28 = zext i1 %27 to i32
%29 = add nuw nsw i32 %22, %28
%30 = tail call %struct.dirent* @readdir(i32* %5)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %32, label %20
32: ; preds = %20, %12
%33 = phi i32 [ 0, %12 ], [ %29, %20 ]
%34 = tail call i32 @closedir(i32* %5)
%35 = tail call i32 @free(i8* %15)
br label %36
36: ; preds = %4, %2, %32
%37 = phi i32 [ %33, %32 ], [ 0, %2 ], [ 0, %4 ]
ret i32 %37
}