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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ chdir(char *);
double difftime(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int stat(char *, struct stat *);
int main(int argc, char *argv[]) {
  struct stat buf, sb;
  DIR *dirfd;
  struct dirent *dentry;
  char fname[PATH_MAX], cwdir[PATH_MAX];
  double min_diftime = 10.0;
  int rc;
  int count = 0;
  sprintf(cwdir, "%s", ".");
  sprintf(fname, "%s", "dummy");
  getcwd(cwdir, PATH_MAX);
  rc = stat(argv[1], &sb);
  if (rc < 0) {
    printf("Unable to open the directory\n");
    return 1;
  }
  dirfd = opendir(argv[1]);
  if (!dirfd) {
    printf("Unable to open the directory\n");
    return 1;
  }
  chdir(argv[1]);
  while ((dentry = (struct dirent *)readdir(dirfd)) != NULL) {
    if (stat(dentry->d_name, &buf) < 0) {
      continue;
    }
    if (difftime(sb.st_mtime, buf.st_mtime) < min_diftime) {
      sprintf(fname, "%s", dentry->d_name);
      min_diftime = difftime(sb.st_mtime, buf.st_mtime);
    }
  }
  printf("Newest file in the dir %s is %s \n", argv[1], fname);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x38(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x70(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x40(%rbp)
mov    (%rax),%eax
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x68(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x48(%rbp)
movsd  0xe18(%rip),%xmm0
movsd  %xmm0,-0x50(%rbp)
movl   $0x0,-0x58(%rbp)
lea    0xe0d(%rip),%rsi
lea    0xe09(%rip),%rdx
call   10a0 <sprintf@plt>
mov    -0x70(%rbp),%rdi
lea    0xdf6(%rip),%rsi
lea    0xdf4(%rip),%rdx
call   10a0 <sprintf@plt>
mov    -0x68(%rbp),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
call   1070 <getcwd@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    126f <main+0xef>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    135f <main+0x1df>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12ac <main+0x12c>
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    135f <main+0x1df>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <chdir@plt>
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1337 <main+0x1b7>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%eax
jge    12ee <main+0x16e>
jmp    12b9 <main+0x139>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1060 <difftime@plt>
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1332 <main+0x1b2>
mov    -0x70(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rdx
lea    0xceb(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1060 <difftime@plt>
movsd  %xmm0,-0x50(%rbp)
jmp    12b9 <main+0x139>
mov    -0x70(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xce7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r12
mov    %rsp,%r14
mov    0x2e42(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rdx,%r15
sub    %rcx,%r15
mov    %r15,%rsp
neg    %rcx
mov    (%rax),%esi
mov    %rsp,%rax
mov    %rsi,%rbx
add    $0xf,%rbx
and    $0xfffffffffffffff0,%rbx
mov    %rax,%rdi
sub    %rbx,%rdi
mov    %rdi,%rsp
neg    %rbx
movw   $0x2e,(%rax,%rbx,1)
movl   $0x6d6d7564,(%rdx,%rcx,1)
movw   $0x79,0x4(%r15)
call   1090 <getcwd@plt>
mov    0x8(%r12),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <stat@plt>
test   %eax,%eax
js     12b7 <main+0x127>
mov    0x8(%r12),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12b7 <main+0x127>
mov    %rax,%rbx
mov    %r14,-0x50(%rbp)
mov    %r12,-0x48(%rbp)
mov    0x8(%r12),%rdi
call   1060 <chdir@plt>
movsd  0xdbe(%rip),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x38(%rbp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12ca <main+0x13a>
mov    %rax,%r12
mov    (%rax),%rdi
mov    %r13,%rsi
call   10a0 <stat@plt>
test   %eax,%eax
js     1260 <main+0xd0>
mov    -0x38(%rbp),%r14d
mov    -0x2c(%rbp),%edi
mov    %r14d,%esi
call   1080 <difftime@plt>
movsd  -0x40(%rbp),%xmm1
ucomisd %xmm0,%xmm1
jbe    1260 <main+0xd0>
mov    (%r12),%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%edi
mov    %r14d,%esi
call   1080 <difftime@plt>
movsd  %xmm0,-0x40(%rbp)
jmp    1260 <main+0xd0>
lea    0xd72(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
jmp    12e9 <main+0x159>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd35(%rip),%rdi
xor    %ebx,%ebx
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    -0x50(%rbp),%r14
mov    %r14,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [6 x i8] c"dummy\00"
@.str.3 = [30 x i8] c"Unable to open the directory\0A\00"
@.str.4 = [34 x i8] c"Newest file in the dir %s is %s \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca %struct.stat
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
%13 = alloca double
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* @PATH_MAX
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %10
%20 = alloca i8, i64 %18
store i64 %18, i64* %11
%21 = load i32, i32* @PATH_MAX
%22 = zext i32 %21 to i64
%23 = alloca i8, i64 %22
store i64 %22, i64* %12
store double 1.000000e+01, double* %13
store i32 0, i32* %15
%24 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @PATH_MAX
%27 = call i32 @getcwd(i8* %23, i32 %26)
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @stat(i8* %30, %struct.stat* %7)
store i32 %31, i32* %14
%32 = load i32, i32* %14
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %2
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %16
br label %86
36: ; preds = %2
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i32* @opendir(i8* %39)
store i32* %40, i32** %8
%41 = load i32*, i32** %8
%42 = icmp ne i32* %41, null
br i1 %42, label %45, label %43
43: ; preds = %36
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %16
br label %86
45: ; preds = %36
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 @chdir(i8* %48)
br label %50
50: ; preds = %80, %61, %45
%51 = load i32*, i32** %8
%52 = call i64 @readdir(i32* %51)
%53 = inttoptr i64 %52 to %struct.dirent*
store %struct.dirent* %53, %struct.dirent** %9
%54 = icmp ne %struct.dirent* %53, null
br i1 %54, label %55, label %81
55: ; preds = %50
%56 = load %struct.dirent*, %struct.dirent** %9
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @stat(i8* %58, %struct.stat* %6)
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55
br label %50
62: ; preds = %55
%63 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%64 = load i32, i32* %63
%65 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call double @difftime(i32 %64, i32 %66)
%68 = load double, double* %13
%69 = fcmp olt double %67, %68
br i1 %69, label %70, label %80
70: ; preds = %62
%71 = load %struct.dirent*, %struct.dirent** %9
%72 = getelementptr inbounds %struct.dirent, %struct.dirent* %71, i32 0, i32 0
%73 = load i8*, i8** %72
%74 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %73)
%75 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%76 = load i32, i32* %75
%77 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%78 = load i32, i32* %77
%79 = call double @difftime(i32 %76, i32 %78)
store double %79, double* %13
br label %80
80: ; preds = %70, %62
br label %50
81: ; preds = %50
%82 = load i8**, i8*** %5
%83 = getelementptr inbounds i8*, i8** %82, i64 1
%84 = load i8*, i8** %83
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %84, i8* %20)
store i32 0, i32* %3
store i32 1, i32* %16
br label %86
86: ; preds = %81, %43, %34
%87 = load i8*, i8** %10
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str.2 = [6 x i8] c"dummy\00"
@.str.4 = [34 x i8] c"Newest file in the dir %s is %s \0A\00"
@str.5 = [29 x i8] c"Unable to open the directory\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = alloca i8, i64 %12
%14 = bitcast i8* %13 to i16*
store i16 46, i16* %14
%15 = call i32 @getcwd(i8* %13, i32 %11)
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @stat(i8* %17, %struct.stat* %4)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.5, i64 0, i64 0))
br label %59
22: ; preds = %2
%23 = load i8*, i8** %16
%24 = call i32* @opendir(i8* %23)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.5, i64 0, i64 0))
br label %59
28: ; preds = %22
%29 = load i8*, i8** %16
%30 = call i32 @chdir(i8* %29)
%31 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %34
34: ; preds = %50, %28
%35 = phi double [ %55, %50 ], [ 1.000000e+01, %28 ]
br label %36
36: ; preds = %34, %46
br label %37
37: ; preds = %36, %40
%38 = call i64 @readdir(i32* %24)
%39 = icmp eq i64 %38, 0
br i1 %39, label %56, label %40
40: ; preds = %37
%41 = inttoptr i64 %38 to %struct.dirent*
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i64 0, i32 0
%43 = load i8*, i8** %42
%44 = call i32 @stat(i8* %43, %struct.stat* %3)
%45 = icmp slt i32 %44, 0
br i1 %45, label %37, label %46
46: ; preds = %40
%47 = load i32, i32* %33
%48 = call double @difftime(i32 %32, i32 %47)
%49 = fcmp olt double %48, %35
br i1 %49, label %50, label %36
50: ; preds = %46
%51 = inttoptr i64 %38 to %struct.dirent*
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i64 0, i32 0
%53 = load i8*, i8** %52
%54 = call i8* @strcpy(i8* %10, i8* dereferenceable(1) %53)
%55 = call double @difftime(i32 %32, i32 %47)
br label %34
56: ; preds = %37
%57 = load i8*, i8** %16
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %57, i8* %10)
br label %59
59: ; preds = %56, %26, %20
%60 = phi i32 [ 1, %20 ], [ 0, %56 ], [ 1, %26 ]
ret i32 %60
}