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

/* Variables and functions */
int PATH_MAX;
scalar_t__ _q_unlink(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
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*/ strcmp(char *, char *);
scalar_t__ strncmp(char *, char *, int);
scalar_t__ time(int /*<<< orphan*/ *);
int _upload_clear_base(const char *upload_basepath, int upload_clearold) {
  if (upload_clearold <= 0)
    return -1;
  DIR *dp;
  if ((dp = opendir(upload_basepath)) == NULL)
    return false;
  time_t now = time(NULL);
  int removed = 0;
  struct dirent *dirp;
  while ((dirp = readdir(dp)) != NULL) {
    if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, "..") ||
        strncmp(dirp->d_name, "q_", 2) != 0) {
      continue;
    }
    char filepath[PATH_MAX];
    snprintf(filepath, sizeof(filepath), "%s/%s", upload_basepath,
             dirp->d_name);
    struct stat filestat;
    if (stat(filepath, &filestat) != 0)
      continue;
    if (filestat.st_mtime >= now + upload_clearold)
      continue;
    if (_q_unlink(filepath) == 0)
      removed++;
  }
  closedir(dp);
  return removed;
}


x86 assembly (clang, -O0):
<_upload_clear_base>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11b5 <_upload_clear_base+0x25>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <_upload_clear_base+0x1b1>
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11d8 <_upload_clear_base+0x48>
movl   $0x0,-0x4(%rbp)
jmp    1341 <_upload_clear_base+0x1b1>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1332 <_upload_clear_base+0x1a2>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf0(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
je     125d <_upload_clear_base+0xcd>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xdd3(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
je     125d <_upload_clear_base+0xcd>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xdba(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1262 <_upload_clear_base+0xd2>
jmp    11ec <_upload_clear_base+0x5c>
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x48(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    (%rax),%r8
lea    0xd64(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x60(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     12ca <_upload_clear_base+0x13a>
movl   $0x2,-0x54(%rbp)
jmp    1311 <_upload_clear_base+0x181>
mov    -0x50(%rbp),%rax
mov    -0x28(%rbp),%rcx
movslq -0x14(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jl     12ee <_upload_clear_base+0x15e>
movl   $0x2,-0x54(%rbp)
jmp    1311 <_upload_clear_base+0x181>
mov    -0x60(%rbp),%rdi
call   10b0 <_q_unlink@plt>
cmp    $0x0,%rax
jne    130a <_upload_clear_base+0x17a>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x54(%rbp),%eax
test   %eax,%eax
je     132d <_upload_clear_base+0x19d>
jmp    1328 <_upload_clear_base+0x198>
jmp    11ec <_upload_clear_base+0x5c>
jmp    11ec <_upload_clear_base+0x5c>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_upload_clear_base>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %esi,%esi
jle    11f1 <_upload_clear_base+0x61>
mov    %esi,%r12d
mov    %rdi,%rbx
call   1040 <opendir@plt>
test   %rax,%rax
je     11fb <_upload_clear_base+0x6b>
mov    %rax,%r13
mov    %rbx,-0x38(%rbp)
xor    %ebx,%ebx
xor    %edi,%edi
call   1090 <time@plt>
mov    %rax,%r15
mov    %r13,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12d2 <_upload_clear_base+0x142>
mov    %rax,%r14
mov    %r12d,%eax
add    %rax,%r15
xor    %ebx,%ebx
lea    0xe16(%rip),%r12
mov    %r15,-0x30(%rbp)
jmp    1224 <_upload_clear_base+0x94>
mov    $0xffffffff,%ebx
jmp    12da <_upload_clear_base+0x14a>
xor    %ebx,%ebx
jmp    12da <_upload_clear_base+0x14a>
mov    %r15,%rsp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12d2 <_upload_clear_base+0x142>
mov    (%r14),%r15
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1210 <_upload_clear_base+0x80>
mov    %r15,%rdi
lea    0xdc0(%rip),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1210 <_upload_clear_base+0x80>
mov    %r15,%rdi
lea    0xdb0(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1210 <_upload_clear_base+0x80>
mov    %rsp,%r15
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rcx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,%rsp
mov    (%r14),%r8
mov    %rcx,%r14
mov    %rcx,%rdi
lea    0xd74(%rip),%rdx
mov    -0x38(%rbp),%rcx
call   1050 <snprintf@plt>
mov    %r14,%rdi
lea    -0x40(%rbp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    1202 <_upload_clear_base+0x72>
mov    -0x30(%rbp),%rax
cmp    %rax,-0x40(%rbp)
jge    1202 <_upload_clear_base+0x72>
mov    %r14,%rdi
call   10b0 <_q_unlink@plt>
cmp    $0x1,%rax
adc    $0x0,%ebx
jmp    1202 <_upload_clear_base+0x72>
mov    %r13,%rdi
call   1060 <closedir@plt>
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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