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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ dir_sync(char *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ unlink(char *);
int remove_old_log_file(void) {
  DIR *d;
  struct dirent *namelist;
  char filename[PATH_MAX];
  d = opendir("/var/log/ppc64-diag/"
              "diag_disk");
  if (!d)
    return -errno;
  while ((namelist = readdir(d)) != NULL) {
    if (namelist->d_name[0] == '.')
      continue;
    snprintf(filename, sizeof(filename) - 1, "%s/%s",
             "/var/log/ppc64-diag/"
             "diag_disk",
             namelist->d_name);
    if (unlink(filename) < 0) {
      fprintf(stderr, "\nUnable to remove old log file[%s]. continuing.\n\n",
              filename);
    }
  }
  closedir(d);
  dir_sync("/var/log/ppc64-diag/"
           "diag_disk");
  return 0;
}


x86 assembly (clang, -O0):
<remove_old_log_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    0xe51(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11dd <remove_old_log_file+0x6d>
xor    %eax,%eax
mov    0x2e04(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    128d <remove_old_log_file+0x11d>
jmp    11e2 <remove_old_log_file+0x72>
mov    -0x10(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     126a <remove_old_log_file+0xfa>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1211 <remove_old_log_file+0xa1>
jmp    11e2 <remove_old_log_file+0x72>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
lea    0xdf1(%rip),%rdx
lea    0xdcc(%rip),%rcx
call   1060 <snprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    1265 <remove_old_log_file+0xf5>
mov    -0x38(%rbp),%rdx
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
call   1080 <fprintf@plt>
jmp    11e2 <remove_old_log_file+0x72>
mov    -0x10(%rbp),%rdi
call   1070 <closedir@plt>
lea    0xd86(%rip),%rdi
call   1040 <dir_sync@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_old_log_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e6b(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe60(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1246 <remove_old_log_file+0xd6>
mov    %rax,%r15
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     122e <remove_old_log_file+0xbe>
add    $0xffffffff,%r14d
lea    0xe55(%rip),%r12
lea    0xe30(%rip),%r13
jmp    11ed <remove_old_log_file+0x7d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     122e <remove_old_log_file+0xbe>
mov    (%rax),%r8
cmpb   $0x2e,(%r8)
je     11e0 <remove_old_log_file+0x70>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r12,%rdx
mov    %r13,%rcx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jns    11e0 <remove_old_log_file+0x70>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
jmp    11e0 <remove_old_log_file+0x70>
mov    %r15,%rdi
call   1070 <closedir@plt>
lea    0xdc3(%rip),%rdi
call   1040 <dir_sync@plt>
xor    %eax,%eax
jmp    1251 <remove_old_log_file+0xe1>
mov    0x2d83(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%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* }
@PATH_MAX = global i32 0
@.str = [30 x i8] c"/var/log/ppc64-diag/diag_disk\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.2 = [50 x i8] c"\0AUnable to remove old log file[%s]. continuing.\0A\0A\00"
define i32 @remove_old_log_file() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %4
%10 = alloca i8, i64 %8
store i64 %8, i64* %5
%11 = call i32* @opendir(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %2
%12 = load i32*, i32** %2
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %0
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
store i32 %16, i32* %1
store i32 1, i32* %6
br label %48
17: ; preds = %0
br label %18
18: ; preds = %43, %30, %17
%19 = load i32*, i32** %2
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %3
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %44
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %3
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 46
br i1 %29, label %30, label %31
30: ; preds = %22
br label %18
31: ; preds = %22
%32 = sub i64 %8, 1
%33 = trunc i64 %32 to i32
%34 = load %struct.dirent*, %struct.dirent** %3
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @snprintf(i8* %10, i32 %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %36)
%38 = call i64 @unlink(i8* %10)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %31
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %43
43: ; preds = %40, %31
br label %18
44: ; preds = %18
%45 = load i32*, i32** %2
%46 = call i32 @closedir(i32* %45)
%47 = call i32 @dir_sync(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
store i32 1, i32* %6
br label %48
48: ; preds = %44, %14
%49 = load i8*, i8** %4
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [30 x i8] c"/var/log/ppc64-diag/diag_disk\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.2 = [50 x i8] c"\0AUnable to remove old log file[%s]. continuing.\0A\0A\00"
define i32 @remove_old_log_file() {
%1 = load i32, i32* @PATH_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %11, label %6
6: ; preds = %0
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %30, label %9
9: ; preds = %6
%10 = add i32 %1, -1
br label %14
11: ; preds = %0
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
br label %33
14: ; preds = %9, %27
%15 = phi %struct.dirent* [ %7, %9 ], [ %28, %27 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 46
br i1 %19, label %27, label %20
20: ; preds = %14
%21 = call i32 @snprintf(i8* %3, i32 %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %17)
%22 = call i64 @unlink(i8* %3)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %3)
br label %27
27: ; preds = %20, %24, %14
%28 = call %struct.dirent* @readdir(i32* %4)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %30, label %14
30: ; preds = %27, %6
%31 = call i32 @closedir(i32* %4)
%32 = call i32 @dir_sync(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %30, %11
%34 = phi i32 [ 0, %30 ], [ %13, %11 ]
ret i32 %34
}