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

/* Variables and functions */
int /*<<< orphan*/ AT_SYMLINK_NOFOLLOW;
char *basename(char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int dirfd(int /*<<< orphan*/ *);
char *dirname(char const *);
int /*<<< orphan*/ err(int, char *);
int fstatat(int, int /*<<< orphan*/, struct stat *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *strdup(char *);
scalar_t__ validate_old_timelog(int, struct dirent *, char *, struct tm *);
int /*<<< orphan*/ warn(char *, char const *);
int /*<<< orphan*/ warnx(char *, char *);
time_t mtime_old_timelog(const char *file) {
  struct stat sb;
  struct tm tm;
  int dir_fd;
  time_t t;
  struct dirent *dp;
  DIR *dirp;
  char *s, *logfname, *dir;
  t = -1;
  if ((dir = dirname(file)) == NULL) {
    warn("dirname() of '%s'", file);
    return (t);
  }
  if ((s = basename(file)) == NULL) {
    warn("basename() of '%s'", file);
    return (t);
  } else if (s[0] == '/') {
    warnx("Invalid log filename '%s'", s);
    return (t);
  } else if ((logfname = strdup(s)) == NULL)
    err(1, "strdup()");
  if ((dirp = opendir(dir)) == NULL) {
    warn("Cannot open log directory '%s'", dir);
    return (t);
  }
  dir_fd = dirfd(dirp);
  while ((dp = readdir(dirp)) != NULL) {
    if (validate_old_timelog(dir_fd, dp, logfname, &tm) == 0)
      continue;
    if (fstatat(dir_fd, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
      warn("Cannot stat '%s'", file);
      continue;
    }
    if (t < sb.st_mtime)
      t = sb.st_mtime;
  }
  closedir(dirp);
  return (t);
}


x86 assembly (clang, -O0):
<mtime_old_timelog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <dirname@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
jne    1205 <mtime_old_timelog+0x45>
mov    -0x10(%rbp),%rsi
lea    0xe0b(%rip),%rdi
call   1030 <warn@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    136b <mtime_old_timelog+0x1ab>
mov    -0x10(%rbp),%rdi
call   1090 <basename@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    1237 <mtime_old_timelog+0x77>
mov    -0x10(%rbp),%rsi
lea    0xdeb(%rip),%rdi
call   1030 <warn@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    136b <mtime_old_timelog+0x1ab>
mov    -0x40(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1262 <mtime_old_timelog+0xa2>
mov    -0x40(%rbp),%rsi
lea    0xdd3(%rip),%rdi
call   1050 <warnx@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    136b <mtime_old_timelog+0x1ab>
mov    -0x40(%rbp),%rdi
call   10d0 <strdup@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    128a <mtime_old_timelog+0xca>
mov    $0x1,%edi
lea    0xdba(%rip),%rsi
call   10c0 <err@plt>
jmp    128f <mtime_old_timelog+0xcf>
jmp    1294 <mtime_old_timelog+0xd4>
mov    -0x50(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    12c6 <mtime_old_timelog+0x106>
mov    -0x50(%rbp),%rsi
lea    0xd92(%rip),%rdi
call   1030 <warn@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    136b <mtime_old_timelog+0x1ab>
mov    -0x38(%rbp),%rdi
call   1080 <dirfd@plt>
mov    %eax,-0x24(%rbp)
mov    -0x38(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     135c <mtime_old_timelog+0x19c>
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x48(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   10b0 <validate_old_timelog@plt>
cmp    $0x0,%rax
jne    130c <mtime_old_timelog+0x14c>
jmp    12d2 <mtime_old_timelog+0x112>
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
mov    0x2cc4(%rip),%rax
mov    (%rax),%ecx
lea    -0x18(%rbp),%rdx
call   10e0 <fstatat@plt>
cmp    $0xffffffff,%eax
jne    1345 <mtime_old_timelog+0x185>
mov    -0x10(%rbp),%rsi
lea    0xd2c(%rip),%rdi
call   1030 <warn@plt>
jmp    12d2 <mtime_old_timelog+0x112>
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1357 <mtime_old_timelog+0x197>
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    12d2 <mtime_old_timelog+0x112>
mov    -0x38(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mtime_old_timelog>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1060 <dirname@plt>
test   %rax,%rax
je     1201 <mtime_old_timelog+0x41>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1090 <basename@plt>
test   %rax,%rax
je     120a <mtime_old_timelog+0x4a>
cmpb   $0x2f,(%rax)
jne    1231 <mtime_old_timelog+0x71>
lea    0xe2e(%rip),%rdi
mov    %rax,%rsi
call   1050 <warnx@plt>
jmp    1219 <mtime_old_timelog+0x59>
lea    0xdf8(%rip),%rdi
jmp    1211 <mtime_old_timelog+0x51>
lea    0xe01(%rip),%rdi
mov    %rbx,%rsi
call   1030 <warn@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%rdi
call   10d0 <strdup@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1252 <mtime_old_timelog+0x92>
lea    0xdf7(%rip),%rsi
mov    $0x1,%edi
call   10c0 <err@plt>
mov    %rbp,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12f1 <mtime_old_timelog+0x131>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <dirfd@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1300 <mtime_old_timelog+0x140>
mov    %rax,%r13
mov    $0xffffffff,%r12d
jmp    12af <mtime_old_timelog+0xef>
nopl   0x0(%rax)
lea    0xdd0(%rip),%rdi
mov    %rbx,%rsi
call   1030 <warn@plt>
mov    %r15,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1306 <mtime_old_timelog+0x146>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %r14,%rdx
lea    0x10(%rsp),%rcx
call   10b0 <validate_old_timelog@plt>
test   %rax,%rax
je     129f <mtime_old_timelog+0xdf>
mov    0x0(%r13),%esi
mov    0x2d0f(%rip),%rax
mov    (%rax),%ecx
mov    %ebp,%edi
lea    0x8(%rsp),%rdx
call   10e0 <fstatat@plt>
cmp    $0xffffffff,%eax
je     1290 <mtime_old_timelog+0xd0>
mov    0x8(%rsp),%eax
cmp    %eax,%r12d
cmovl  %eax,%r12d
jmp    129f <mtime_old_timelog+0xdf>
lea    0xd50(%rip),%rdi
mov    %rbp,%rsi
jmp    1214 <mtime_old_timelog+0x54>
mov    $0xffffffff,%r12d
mov    %r15,%rdi
call   1070 <closedir@plt>
jmp    121f <mtime_old_timelog+0x5f>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.tm = type { i32 }
%struct.dirent = type { i32 }
@.str = [18 x i8] c"dirname() of '%s'\00"
@.str.1 = [19 x i8] c"basename() of '%s'\00"
@.str.2 = [26 x i8] c"Invalid log filename '%s'\00"
@.str.3 = [9 x i8] c"strdup()\00"
@.str.4 = [31 x i8] c"Cannot open log directory '%s'\00"
@AT_SYMLINK_NOFOLLOW = global i32 0
@.str.5 = [17 x i8] c"Cannot stat '%s'\00"
define i32 @mtime_old_timelog(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca %struct.tm
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.dirent*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %3
store i32 -1, i32* %7
%13 = load i8*, i8** %3
%14 = call i8* @dirname(i8* %13)
store i8* %14, i8** %12
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i32 @warn(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* %7
store i32 %19, i32* %2
br label %92
20: ; preds = %1
%21 = load i8*, i8** %3
%22 = call i8* @basename(i8* %21)
store i8* %22, i8** %10
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = call i32 @warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* %7
store i32 %27, i32* %2
br label %92
28: ; preds = %20
%29 = load i8*, i8** %10
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 47
br i1 %33, label %34, label %38
34: ; preds = %28
%35 = load i8*, i8** %10
%36 = call i32 @warnx(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %7
store i32 %37, i32* %2
br label %92
38: ; preds = %28
%39 = load i8*, i8** %10
%40 = call i8* @strdup(i8* %39)
store i8* %40, i8** %11
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
br label %45
45: ; preds = %44
br label %46
46: ; preds = %45
%47 = load i8*, i8** %12
%48 = call i32* @opendir(i8* %47)
store i32* %48, i32** %9
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i8*, i8** %12
%52 = call i32 @warn(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %51)
%53 = load i32, i32* %7
store i32 %53, i32* %2
br label %92
54: ; preds = %46
%55 = load i32*, i32** %9
%56 = call i32 @dirfd(i32* %55)
store i32 %56, i32* %6
br label %57
57: ; preds = %87, %76, %67, %54
%58 = load i32*, i32** %9
%59 = call %struct.dirent* @readdir(i32* %58)
store %struct.dirent* %59, %struct.dirent** %8
%60 = icmp ne %struct.dirent* %59, null
br i1 %60, label %61, label %88
61: ; preds = %57
%62 = load i32, i32* %6
%63 = load %struct.dirent*, %struct.dirent** %8
%64 = load i8*, i8** %11
%65 = call i64 @validate_old_timelog(i32 %62, %struct.dirent* %63, i8* %64, %struct.tm* %5)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %61
br label %57
68: ; preds = %61
%69 = load i32, i32* %6
%70 = load %struct.dirent*, %struct.dirent** %8
%71 = getelementptr inbounds %struct.dirent, %struct.dirent* %70, i32 0, i32 0
%72 = load i32, i32* %71
%73 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%74 = call i32 @fstatat(i32 %69, i32 %72, %struct.stat* %4, i32 %73)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %79
76: ; preds = %68
%77 = load i8*, i8** %3
%78 = call i32 @warn(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %77)
br label %57
79: ; preds = %68
%80 = load i32, i32* %7
%81 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%82 = load i32, i32* %81
%83 = icmp slt i32 %80, %82
br i1 %83, label %84, label %87
84: ; preds = %79
%85 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%86 = load i32, i32* %85
store i32 %86, i32* %7
br label %87
87: ; preds = %84, %79
br label %57
88: ; preds = %57
%89 = load i32*, i32** %9
%90 = call i32 @closedir(i32* %89)
%91 = load i32, i32* %7
store i32 %91, i32* %2
br label %92
92: ; preds = %88, %50, %34, %24, %16
%93 = load i32, i32* %2
ret i32 %93
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.tm = type { i32 }
%struct.dirent = type { i32 }
@.str = [18 x i8] c"dirname() of '%s'\00"
@.str.1 = [19 x i8] c"basename() of '%s'\00"
@.str.2 = [26 x i8] c"Invalid log filename '%s'\00"
@.str.3 = [9 x i8] c"strdup()\00"
@.str.4 = [31 x i8] c"Cannot open log directory '%s'\00"
@AT_SYMLINK_NOFOLLOW = global i32 0
@.str.5 = [17 x i8] c"Cannot stat '%s'\00"
define i32 @mtime_old_timelog(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca %struct.tm
%4 = bitcast %struct.stat* %2 to i8*
%5 = bitcast %struct.tm* %3 to i8*
%6 = tail call i8* @dirname(i8* %0)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @warn(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %63
10: ; preds = %1
%11 = tail call i8* @basename(i8* %0)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %63
15: ; preds = %10
%16 = load i8, i8* %11
%17 = icmp eq i8 %16, 47
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @warnx(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %11)
br label %63
20: ; preds = %15
%21 = tail call i8* @strdup(i8* %11)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
%26 = tail call i32* @opendir(i8* %6)
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = tail call i32 @warn(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %63
30: ; preds = %25
%31 = tail call i32 @dirfd(i32* %26)
%32 = call %struct.dirent* @readdir(i32* %26)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %60, label %34
34: ; preds = %30
%35 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %36
36: ; preds = %34, %54
%37 = phi %struct.dirent* [ %32, %34 ], [ %58, %54 ]
%38 = phi i32 [ -1, %34 ], [ %57, %54 ]
br label %39
39: ; preds = %36, %51
%40 = phi %struct.dirent* [ %37, %36 ], [ %52, %51 ]
%41 = call i64 @validate_old_timelog(i32 %31, %struct.dirent* %40, i8* %21, %struct.tm* %3)
%42 = icmp eq i64 %41, 0
br i1 %42, label %51, label %43
43: ; preds = %39
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i64 0, i32 0
%45 = load i32, i32* %44
%46 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%47 = call i32 @fstatat(i32 %31, i32 %45, %struct.stat* %2, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %54
49: ; preds = %43
%50 = call i32 @warn(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %51
51: ; preds = %49, %39
%52 = call %struct.dirent* @readdir(i32* %26)
%53 = icmp eq %struct.dirent* %52, null
br i1 %53, label %60, label %39
54: ; preds = %43
%55 = load i32, i32* %35
%56 = icmp slt i32 %38, %55
%57 = select i1 %56, i32 %55, i32 %38
%58 = call %struct.dirent* @readdir(i32* %26)
%59 = icmp eq %struct.dirent* %58, null
br i1 %59, label %60, label %36
60: ; preds = %54, %51, %30
%61 = phi i32 [ -1, %30 ], [ %38, %51 ], [ %57, %54 ]
%62 = call i32 @closedir(i32* %26)
br label %63
63: ; preds = %60, %28, %18, %13, %8
%64 = phi i32 [ -1, %8 ], [ -1, %13 ], [ -1, %18 ], [ -1, %28 ], [ %61, %60 ]
ret i32 %64
}