#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 scalar_t__ time_t;
struct stat {
  scalar_t__ st_mtime;
};
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ NAME_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
char *format(char *, int /*<<< orphan*/);
int /*<<< orphan*/ get_tag(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewinddir(int /*<<< orphan*/ *);
int /*<<< orphan*/ starts_with(int /*<<< orphan*/, char *);
int stat(char const *, struct stat *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ unlink_by_tag(int /*<<< orphan*/);
int /*<<< orphan*/ warning(char *, char const *);
void unlink_old_reports(void) {
  DIR *d;
  struct dirent *e;
  struct stat st;
  const char *path;
  char oldest[NAME_MAX + 1];
  time_t oldest_mtime;
  size_t count;
  d = opendir("/var/crash_reports");
  if (d == NULL) {
    warning("%s: opendir", "/var/crash_reports");
    return;
  }
  for (;;) {
    count = 0;
    while ((e = readdir(d)) != NULL) {
      if (!starts_with(e->d_name, "crash_report"))
        continue;
      path = format("/var/crash_reports"
                    "/%s",
                    e->d_name);
      if (stat(path, &st) == -1) {
        if (errno != ENOENT)
          warning("%s: stat", path);
        continue;
      }
      if (count++ == 0 || st.st_mtime < oldest_mtime) {
        strcpy(oldest, e->d_name);
        oldest_mtime = st.st_mtime;
      }
    }
    rewinddir(d);
    if (count > 10) {
      if (!unlink_by_tag(get_tag(oldest)))
        break;
    } else
      break;
  }
  closedir(d);
}


x86 assembly (clang, -O0):
<unlink_old_reports>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
lea    0xe12(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1221 <unlink_old_reports+0x71>
lea    0xe0a(%rip),%rdi
lea    0xdf0(%rip),%rsi
call   1040 <warning@plt>
movl   $0x1,-0x44(%rbp)
jmp    1354 <unlink_old_reports+0x1a4>
jmp    1226 <unlink_old_reports+0x76>
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1303 <unlink_old_reports+0x153>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
call   10d0 <starts_with@plt>
cmp    $0x0,%eax
jne    1265 <unlink_old_reports+0xb5>
jmp    122e <unlink_old_reports+0x7e>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xdba(%rip),%rdi
call   10b0 <format@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
jne    12c0 <unlink_old_reports+0x110>
mov    0x2d48(%rip),%rax
mov    (%rax),%rax
mov    0x2d2e(%rip),%rcx
cmp    (%rcx),%rax
je     12bb <unlink_old_reports+0x10b>
mov    -0x20(%rbp),%rsi
lea    0xd8c(%rip),%rdi
call   1040 <warning@plt>
jmp    122e <unlink_old_reports+0x7e>
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x40(%rbp)
cmp    $0x0,%rax
je     12e7 <unlink_old_reports+0x137>
mov    -0x18(%rbp),%rax
cmp    -0x38(%rbp),%rax
jge    12fe <unlink_old_reports+0x14e>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    122e <unlink_old_reports+0x7e>
mov    -0x8(%rbp),%rdi
call   1070 <rewinddir@plt>
cmpq   $0xa,-0x40(%rbp)
jbe    133a <unlink_old_reports+0x18a>
mov    -0x50(%rbp),%rdi
call   10a0 <get_tag@plt>
mov    %eax,%edi
call   1060 <unlink_by_tag@plt>
cmp    $0x0,%eax
jne    1335 <unlink_old_reports+0x185>
jmp    1344 <unlink_old_reports+0x194>
jmp    133f <unlink_old_reports+0x18f>
jmp    1344 <unlink_old_reports+0x194>
jmp    1226 <unlink_old_reports+0x76>
mov    -0x8(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlink_old_reports>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsp,%r14
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
lea    0xe14(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1309 <unlink_old_reports+0x159>
mov    %rax,%rbx
mov    %r14,-0x40(%rbp)
lea    0xe17(%rip),%r15
lea    -0x50(%rbp),%r13
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     131e <unlink_old_reports+0x16e>
mov    %rax,%r14
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    1264 <unlink_old_reports+0xb4>
mov    (%r14),%esi
mov    -0x38(%rbp),%rdi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    %r12,-0x48(%rbp)
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12e0 <unlink_old_reports+0x130>
mov    (%r14),%edi
mov    %r15,%rsi
call   10d0 <starts_with@plt>
test   %eax,%eax
je     1250 <unlink_old_reports+0xa0>
mov    (%r14),%esi
lea    0xdaf(%rip),%rdi
call   10b0 <format@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r13,%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
jne    12bc <unlink_old_reports+0x10c>
mov    0x2d44(%rip),%rax
mov    (%rax),%rax
mov    0x2d2a(%rip),%rcx
cmp    (%rcx),%rax
je     1250 <unlink_old_reports+0xa0>
lea    0xd90(%rip),%rdi
mov    %r12,%rsi
call   1040 <warning@plt>
jmp    1250 <unlink_old_reports+0xa0>
mov    -0x50(%rbp),%r12
mov    -0x30(%rbp),%rax
test   %rax,%rax
je     122c <unlink_old_reports+0x7c>
cmp    -0x48(%rbp),%r12
jge    1240 <unlink_old_reports+0x90>
jmp    122c <unlink_old_reports+0x7c>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1070 <rewinddir@plt>
cmpq   $0xb,-0x30(%rbp)
jb     1326 <unlink_old_reports+0x176>
mov    -0x38(%rbp),%rdi
call   10a0 <get_tag@plt>
mov    %eax,%edi
call   1060 <unlink_by_tag@plt>
test   %eax,%eax
jne    1210 <unlink_old_reports+0x60>
jmp    1326 <unlink_old_reports+0x176>
lea    0xd03(%rip),%rdi
lea    0xce9(%rip),%rsi
call   1040 <warning@plt>
jmp    1332 <unlink_old_reports+0x182>
mov    %rbx,%rdi
call   1070 <rewinddir@plt>
mov    %rbx,%rdi
call   1080 <closedir@plt>
mov    -0x40(%rbp),%r14
mov    %r14,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i64 }
@NAME_MAX = global i32 0
@.str = [19 x i8] c"/var/crash_reports\00"
@.str.1 = [12 x i8] c"%s: opendir\00"
@.str.2 = [13 x i8] c"crash_report\00"
@.str.3 = [22 x i8] c"/var/crash_reports/%s\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.4 = [9 x i8] c"%s: stat\00"
define void @unlink_old_reports() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = load i32, i32* @NAME_MAX
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = call i32* @opendir(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %1
%16 = load i32*, i32** %1
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @warning(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %9
br label %82
20: ; preds = %0
br label %21
21: ; preds = %78, %20
store i64 0, i64* %8
br label %22
22: ; preds = %65, %48, %32, %21
%23 = load i32*, i32** %1
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %2
%25 = icmp ne %struct.dirent* %24, null
br i1 %25, label %26, label %66
26: ; preds = %22
%27 = load %struct.dirent*, %struct.dirent** %2
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @starts_with(i32 %29, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %26
br label %22
33: ; preds = %26
%34 = load %struct.dirent*, %struct.dirent** %2
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i8* @format(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %36)
store i8* %37, i8** %4
%38 = load i8*, i8** %4
%39 = call i32 @stat(i8* %38, %struct.stat* %3)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %49
41: ; preds = %33
%42 = load i64, i64* @errno
%43 = load i64, i64* @ENOENT
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i8*, i8** %4
%47 = call i32 @warning(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %45, %41
br label %22
49: ; preds = %33
%50 = load i64, i64* %8
%51 = add i64 %50, 1
store i64 %51, i64* %8
%52 = icmp eq i64 %50, 0
br i1 %52, label %58, label %53
53: ; preds = %49
%54 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%55 = load i64, i64* %54
%56 = load i64, i64* %7
%57 = icmp slt i64 %55, %56
br i1 %57, label %58, label %65
58: ; preds = %53, %49
%59 = load %struct.dirent*, %struct.dirent** %2
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i32 @strcpy(i8* %14, i32 %61)
%63 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%64 = load i64, i64* %63
store i64 %64, i64* %7
br label %65
65: ; preds = %58, %53
br label %22
66: ; preds = %22
%67 = load i32*, i32** %1
%68 = call i32 @rewinddir(i32* %67)
%69 = load i64, i64* %8
%70 = icmp ugt i64 %69, 10
br i1 %70, label %71, label %77
71: ; preds = %66
%72 = call i32 @get_tag(i8* %14)
%73 = call i32 @unlink_by_tag(i32 %72)
%74 = icmp ne i32 %73, 0
br i1 %74, label %76, label %75
75: ; preds = %71
br label %79
76: ; preds = %71
br label %78
77: ; preds = %66
br label %79
78: ; preds = %76
br label %21
79: ; preds = %77, %75
%80 = load i32*, i32** %1
%81 = call i32 @closedir(i32* %80)
store i32 0, i32* %9
br label %82
82: ; preds = %79, %18
%83 = load i8*, i8** %5
%84 = load i32, i32* %9
switch i32 %84, label %86 [
i32 0, label %85
i32 1, label %85
]
85: ; preds = %82, %82
ret void
86: ; preds = %82
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i64 }
%struct.dirent = type { i32 }
@NAME_MAX = global i32 0
@.str = [19 x i8] c"/var/crash_reports\00"
@.str.1 = [12 x i8] c"%s: opendir\00"
@.str.2 = [13 x i8] c"crash_report\00"
@.str.3 = [22 x i8] c"/var/crash_reports/%s\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.4 = [9 x i8] c"%s: stat\00"
define void @unlink_old_reports() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @NAME_MAX
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = tail call i32* @opendir(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
br label %14
12: ; preds = %0
%13 = tail call i32 @warning(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %68
14: ; preds = %10, %62
%15 = phi i64 [ %58, %62 ], [ undef, %10 ]
%16 = call %struct.dirent* @readdir(i32* %8)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @rewinddir(i32* %8)
br label %66
20: ; preds = %14, %53
%21 = phi %struct.dirent* [ %55, %53 ], [ %16, %14 ]
%22 = phi i64 [ %45, %53 ], [ 0, %14 ]
%23 = phi i64 [ %54, %53 ], [ %15, %14 ]
br label %24
24: ; preds = %20, %41
%25 = phi %struct.dirent* [ %21, %20 ], [ %42, %41 ]
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @starts_with(i32 %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i32 %28, 0
br i1 %29, label %41, label %30
30: ; preds = %24
%31 = load i32, i32* %26
%32 = call i8* @format(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = call i32 @stat(i8* %32, %struct.stat* %1)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %44
35: ; preds = %30
%36 = load i64, i64* @errno
%37 = load i64, i64* @ENOENT
%38 = icmp eq i64 %36, %37
br i1 %38, label %41, label %39
39: ; preds = %35
%40 = call i32 @warning(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %32)
br label %41
41: ; preds = %35, %39, %24
%42 = call %struct.dirent* @readdir(i32* %8)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %57, label %24
44: ; preds = %30
%45 = add i64 %22, 1
%46 = icmp eq i64 %22, 0
%47 = load i64, i64* %11
%48 = icmp slt i64 %47, %23
%49 = select i1 %46, i1 true, i1 %48
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = load i32, i32* %26
%52 = call i32 @strcpy(i8* %7, i32 %51)
br label %53
53: ; preds = %44, %50
%54 = phi i64 [ %47, %50 ], [ %23, %44 ]
%55 = call %struct.dirent* @readdir(i32* %8)
%56 = icmp eq %struct.dirent* %55, null
br i1 %56, label %57, label %20
57: ; preds = %53, %41
%58 = phi i64 [ %23, %41 ], [ %54, %53 ]
%59 = phi i64 [ %22, %41 ], [ %45, %53 ]
%60 = call i32 @rewinddir(i32* %8)
%61 = icmp ugt i64 %59, 10
br i1 %61, label %62, label %66
62: ; preds = %57
%63 = call i32 @get_tag(i8* %7)
%64 = call i32 @unlink_by_tag(i32 %63)
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %14
66: ; preds = %57, %62, %18
%67 = call i32 @closedir(i32* %8)
br label %68
68: ; preds = %66, %12
ret void
}