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

/* Variables and functions */
int closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ tidy_item(int *, int /*<<< orphan*/, int /*<<< orphan*/,
                             char *, int);
void tidy_dir(int *removed_p, time_t then, char *path) {
  int ret;
  char perror_str[80 + 1];
  struct dirent *dirent;
  DIR *dir;
  dir = opendir(path);
  if (NULL == dir) {
    snprintf(perror_str, (size_t)80, "Can't open dir %s - ignoring\n", path);
    perror(perror_str);
    return;
  }
  int path_len = (int)strlen(path);
  while (NULL != (dirent = readdir(dir))) {
    tidy_item(removed_p, then, dirent->d_name, path, path_len);
  }
  ret = closedir(dir);
  if (ret != 0) {
    snprintf(perror_str, (size_t)80, "Failed to close dir %s - ignoring\n",
             path);
    perror(perror_str);
  }
}


x86 assembly (clang, -O0):
<tidy_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x80(%rbp)
xor    %eax,%eax
cmp    -0x80(%rbp),%rax
jne    11c6 <tidy_dir+0x56>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x50,%esi
lea    0xe4d(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x70(%rbp),%rdi
call   1090 <perror@plt>
jmp    124a <tidy_dir+0xda>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x84(%rbp)
mov    -0x80(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,%rcx
mov    %rcx,-0x78(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     1212 <tidy_dir+0xa2>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x78(%rbp),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%rcx
mov    -0x84(%rbp),%r8d
call   1050 <tidy_item@plt>
jmp    11d5 <tidy_dir+0x65>
mov    -0x80(%rbp),%rdi
call   1070 <closedir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     124a <tidy_dir+0xda>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x50,%esi
lea    0xde2(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x70(%rbp),%rdi
call   1090 <perror@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tidy_dir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x60,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%r12
mov    %rdx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11db <tidy_dir+0x6b>
mov    %rax,%rbp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11c2 <tidy_dir+0x52>
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
mov    %ebx,%r8d
call   1050 <tidy_item@plt>
jmp    11a0 <tidy_dir+0x30>
mov    %rbp,%rdi
call   1070 <closedir@plt>
test   %eax,%eax
jne    11e4 <tidy_dir+0x74>
add    $0x60,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1e(%rip),%rdx
jmp    11eb <tidy_dir+0x7b>
lea    0xe33(%rip),%rdx
mov    %rsp,%rbx
mov    $0x50,%esi
mov    %rbx,%rdi
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1090 <perror@plt>
jmp    11ce <tidy_dir+0x5e>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [30 x i8] c"Can't open dir %s - ignoring\0A\00"
@.str.1 = [35 x i8] c"Failed to close dir %s - ignoring\0A\00"
define void @tidy_dir(i32* %0, i32 %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [81 x i8]
%9 = alloca %struct.dirent*
%10 = alloca i32*
%11 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%12 = load i8*, i8** %6
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %10
%14 = load i32*, i32** %10
%15 = icmp eq i32* null, %14
br i1 %15, label %16, label %22
16: ; preds = %3
%17 = getelementptr inbounds [81 x i8], [81 x i8]* %8, i64 0, i64 0
%18 = load i8*, i8** %6
%19 = call i32 @snprintf(i8* %17, i64 80, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = getelementptr inbounds [81 x i8], [81 x i8]* %8, i64 0, i64 0
%21 = call i32 @perror(i8* %20)
br label %50
22: ; preds = %3
%23 = load i8*, i8** %6
%24 = call i64 @strlen(i8* %23)
%25 = trunc i64 %24 to i32
store i32 %25, i32* %11
br label %26
26: ; preds = %30, %22
%27 = load i32*, i32** %10
%28 = call %struct.dirent* @readdir(i32* %27)
store %struct.dirent* %28, %struct.dirent** %9
%29 = icmp ne %struct.dirent* null, %28
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = load i32*, i32** %4
%32 = load i32, i32* %5
%33 = load %struct.dirent*, %struct.dirent** %9
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i8*, i8** %6
%37 = load i32, i32* %11
%38 = call i32 @tidy_item(i32* %31, i32 %32, i32 %35, i8* %36, i32 %37)
br label %26
39: ; preds = %26
%40 = load i32*, i32** %10
%41 = call i32 @closedir(i32* %40)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %39
%45 = getelementptr inbounds [81 x i8], [81 x i8]* %8, i64 0, i64 0
%46 = load i8*, i8** %6
%47 = call i32 @snprintf(i8* %45, i64 80, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %46)
%48 = getelementptr inbounds [81 x i8], [81 x i8]* %8, i64 0, i64 0
%49 = call i32 @perror(i8* %48)
br label %50
50: ; preds = %16, %44, %39
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [30 x i8] c"Can't open dir %s - ignoring\0A\00"
@.str.1 = [35 x i8] c"Failed to close dir %s - ignoring\0A\00"
define void @tidy_dir(i32* %0, i32 %1, i8* %2) {
%4 = alloca [81 x i8]
%5 = getelementptr inbounds [81 x i8], [81 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @opendir(i8* %2)
%7 = icmp eq i32* %6, null
br i1 %7, label %23, label %8
8: ; preds = %3
%9 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%10 = trunc i64 %9 to i32
%11 = tail call %struct.dirent* @readdir(i32* %6)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %20, label %13
13: ; preds = %8, %13
%14 = phi %struct.dirent* [ %18, %13 ], [ %11, %8 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i32 @tidy_item(i32* %0, i32 %1, i32 %16, i8* %2, i32 %10)
%18 = tail call %struct.dirent* @readdir(i32* %6)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %20, label %13
20: ; preds = %13, %8
%21 = tail call i32 @closedir(i32* %6)
%22 = icmp eq i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %20, %3
%24 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), %20 ]
%25 = call i32 @snprintf(i8* %5, i64 80, i8* %24, i8* %2)
%26 = call i32 @perror(i8* %5)
br label %27
27: ; preds = %23, %20
ret void
}