#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 /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int strlen(char const *);
void listdir(const char *arg) {
  DIR *dirp;
  struct dirent *dp;
  char fname[1024];
  if ((dirp = opendir(arg)) == NULL) {
    fprintf(stderr, "listdir failed on %s\n", arg);
    perror("couldn't open directory");
    return;
  }
  do {
    errno = 0;
    if ((dp = readdir(dirp)) != NULL && '.' != *dp->d_name) {
      if ((strlen(arg) + strlen(dp->d_name) + 1) < 1024) {
        struct stat s;
        sprintf(fname, "%s/%s", arg, dp->d_name);
        stat(fname, &s);
        (void)printf("%s %ld\n", dp->d_name, s.st_mtime);
      } else {
        fprintf(stderr, "filename %s/%s too long\n", arg, dp->d_name);
        return;
      }
    }
  } while (dp != NULL);
  if (errno != 0)
    perror("error reading directory");
  (void)closedir(dirp);
  return;
}


x86 assembly (clang, -O0):
<listdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11e2 <listdir+0x52>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xe3e(%rip),%rdi
call   10a0 <perror@plt>
jmp    1307 <listdir+0x177>
jmp    11e7 <listdir+0x57>
mov    0x2dea(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12d1 <listdir+0x141>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%ecx
mov    $0x2e,%eax
cmp    %ecx,%eax
je     12d1 <listdir+0x141>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x42c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cmp    $0x400,%eax
jge    12a5 <listdir+0x115>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
lea    0xdbf(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
lea    -0x428(%rbp),%rsi
call   1080 <stat@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x428(%rbp),%edx
lea    0xd99(%rip),%rdi
call   1050 <printf@plt>
jmp    12cc <listdir+0x13c>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
lea    0xd7c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1307 <listdir+0x177>
jmp    12d1 <listdir+0x141>
jmp    12d6 <listdir+0x146>
cmpq   $0x0,-0x18(%rbp)
jne    11e7 <listdir+0x57>
mov    0x2cf0(%rip),%rax
cmpq   $0x0,(%rax)
je     12fe <listdir+0x16e>
lea    0xd5c(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listdir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     12a0 <listdir+0x110>
mov    %rax,%rbx
mov    0x2e1c(%rip),%rbp
movq   $0x0,0x0(%rbp)
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1280 <listdir+0xf0>
mov    %rax,%r15
lea    0x10(%rsp),%r12
jmp    122d <listdir+0x9d>
nop
mov    (%r15),%rcx
mov    %r12,%rdi
lea    0xe41(%rip),%rsi
mov    %r14,%rdx
call   10b0 <sprintf@plt>
mov    %r12,%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
mov    (%r15),%rsi
mov    0x8(%rsp),%edx
lea    0xe24(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,0x0(%rbp)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1280 <listdir+0xf0>
mov    (%r15),%rax
cmpb   $0x2e,(%rax)
je     1215 <listdir+0x85>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
mov    (%r15),%rdi
call   1040 <strlen@plt>
add    %r13d,%eax
cmp    $0x3fe,%eax
jle    11e0 <listdir+0x50>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%rcx
lea    0xdd7(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fprintf@plt>
cmpq   $0x0,0x0(%rbp)
jne    12d7 <listdir+0x147>
mov    %rbx,%rdi
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <closedir@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    0xd50(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xd55(%rip),%rdi
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <perror@plt>
lea    0xd77(%rip),%rdi
call   10a0 <perror@plt>
jmp    1287 <listdir+0xf7>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"listdir failed on %s\0A\00"
@.str.1 = [24 x i8] c"couldn't open directory\00"
@errno = global i64 0
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [8 x i8] c"%s %ld\0A\00"
@.str.4 = [25 x i8] c"filename %s/%s too long\0A\00"
@.str.5 = [24 x i8] c"error reading directory\00"
define void @listdir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca [1024 x i8]
%6 = alloca %struct.stat
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %3
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %2
%13 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %72
15: ; preds = %1
br label %16
16: ; preds = %61, %15
store i64 0, i64* @errno
%17 = load i32*, i32** %3
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %4
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %60
20: ; preds = %16
%21 = load %struct.dirent*, %struct.dirent** %4
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 46, %25
br i1 %26, label %27, label %60
27: ; preds = %20
%28 = load i8*, i8** %2
%29 = call i32 @strlen(i8* %28)
%30 = load %struct.dirent*, %struct.dirent** %4
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 @strlen(i8* %32)
%34 = add nsw i32 %29, %33
%35 = add nsw i32 %34, 1
%36 = icmp slt i32 %35, 1024
br i1 %36, label %37, label %52
37: ; preds = %27
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%39 = load i8*, i8** %2
%40 = load %struct.dirent*, %struct.dirent** %4
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %39, i8* %42)
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%45 = call i32 @stat(i8* %44, %struct.stat* %6)
%46 = load %struct.dirent*, %struct.dirent** %4
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %48, i32 %50)
br label %59
52: ; preds = %27
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %2
%55 = load %struct.dirent*, %struct.dirent** %4
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %54, i8* %57)
br label %72
59: ; preds = %37
br label %60
60: ; preds = %59, %20, %16
br label %61
61: ; preds = %60
%62 = load %struct.dirent*, %struct.dirent** %4
%63 = icmp ne %struct.dirent* %62, null
br i1 %63, label %16, label %64
64: ; preds = %61
%65 = load i64, i64* @errno
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %64
%68 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %69
69: ; preds = %67, %64
%70 = load i32*, i32** %3
%71 = call i32 @closedir(i32* %70)
br label %72
72: ; preds = %69, %52, %10
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [22 x i8] c"listdir failed on %s\0A\00"
@.str.1 = [24 x i8] c"couldn't open directory\00"
@errno = global i64 0
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [8 x i8] c"%s %ld\0A\00"
@.str.4 = [25 x i8] c"filename %s/%s too long\0A\00"
@.str.5 = [24 x i8] c"error reading directory\00"
define void @listdir(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %13, label %7
7: ; preds = %1
store i64 0, i64* @errno
%8 = tail call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %43, label %10
10: ; preds = %7
%11 = bitcast %struct.stat* %3 to i8*
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %17
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
%16 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %50
17: ; preds = %10, %40
%18 = phi %struct.dirent* [ %8, %10 ], [ %41, %40 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 46
br i1 %22, label %40, label %23
23: ; preds = %17
%24 = tail call i32 @strlen(i8* %0)
%25 = load i8*, i8** %19
%26 = tail call i32 @strlen(i8* %25)
%27 = add nsw i32 %26, %24
%28 = icmp slt i32 %27, 1023
br i1 %28, label %29, label %36
29: ; preds = %23
%30 = load i8*, i8** %19
%31 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %30)
%32 = call i32 @stat(i8* %4, %struct.stat* %3)
%33 = load i8*, i8** %19
%34 = load i32, i32* %12
%35 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %33, i32 %34)
br label %40
36: ; preds = %23
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %19
%39 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %38)
br label %50
40: ; preds = %17, %29
store i64 0, i64* @errno
%41 = tail call %struct.dirent* @readdir(i32* %5)
%42 = icmp eq %struct.dirent* %41, null
br i1 %42, label %43, label %17
43: ; preds = %40, %7
%44 = load i64, i64* @errno
%45 = icmp eq i64 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %48
48: ; preds = %46, %43
%49 = tail call i32 @closedir(i32* %5)
br label %50
50: ; preds = %48, %36, %13
ret void
}