#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*/ buf;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ myerr(char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
char *strerror(int /*<<< orphan*/);
scalar_t__ unlink(char *);
void clean_dir(char *path) {
  DIR *d;
  struct dirent *e;
  char buf[1024];
  while (1) {
    if (!(d = opendir(path)))
      myerr("cannot open dir %s", path);
    while ((e = readdir(d))) {
      if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
        continue;
      break;
    }
    if (!e)
      break;
    snprintf(buf, sizeof(buf), "%s/%s", path, e->d_name);
    closedir(d);
    if (unlink(buf) < 0)
      myerr("unlink failed: %s", strerror(errno));
  }
  closedir(d);
}


x86 assembly (clang, -O0):
<clean_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11b6 <clean_dir+0x36>
mov    -0x8(%rbp),%rsi
lea    0xe4f(%rip),%rdi
call   1070 <myerr@plt>
jmp    11bb <clean_dir+0x3b>
mov    -0x10(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1214 <clean_dir+0x94>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe34(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
je     120a <clean_dir+0x8a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe17(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    120f <clean_dir+0x8f>
jmp    11bb <clean_dir+0x3b>
jmp    1214 <clean_dir+0x94>
cmpq   $0x0,-0x18(%rbp)
jne    1224 <clean_dir+0xa4>
jmp    1288 <clean_dir+0x108>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xdd4(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <closedir@plt>
lea    -0x420(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    1283 <clean_dir+0x103>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xd9e(%rip),%rdi
call   1070 <myerr@plt>
jmp    118f <clean_dir+0xf>
mov    -0x10(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clean_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r15
lea    0xe79(%rip),%rbx
lea    0xe71(%rip),%r12
mov    %rsp,%rbp
mov    %r15,%rdi
call   1040 <opendir@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11d0 <clean_dir+0x50>
lea    0xe44(%rip),%rdi
mov    %r15,%rsi
call   1070 <myerr@plt>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1253 <clean_dir+0xd3>
mov    (%rax),%r13
mov    %r13,%rdi
mov    %rbx,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     11d0 <clean_dir+0x50>
mov    %r13,%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     11d0 <clean_dir+0x50>
mov    %rbp,%rdi
mov    $0x400,%esi
lea    0xe09(%rip),%rdx
mov    %r15,%rcx
mov    %r13,%r8
call   1050 <snprintf@plt>
mov    %r14,%rdi
call   1060 <closedir@plt>
mov    %rbp,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jns    11a5 <clean_dir+0x25>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdd6(%rip),%rdi
mov    %rax,%rsi
call   1070 <myerr@plt>
jmp    11a5 <clean_dir+0x25>
mov    %r14,%rdi
call   1060 <closedir@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [19 x i8] c"cannot open dir %s\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [18 x i8] c"unlink failed: %s\00"
@errno = global i32 0
define void @clean_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca [1024 x i8]
store i8* %0, i8** %2
br label %6
6: ; preds = %1, %52
%7 = load i8*, i8** %2
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %6
%11 = load i8*, i8** %2
%12 = call i32 @myerr(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %13
13: ; preds = %10, %6
br label %14
14: ; preds = %30, %13
%15 = load i32*, i32** %3
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %4
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %32
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %4
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = load %struct.dirent*, %struct.dirent** %4
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = call i32 @strcmp(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %24, %18
br label %14
31: ; preds = %24
br label %32
32: ; preds = %31, %14
%33 = load %struct.dirent*, %struct.dirent** %4
%34 = icmp ne %struct.dirent* %33, null
br i1 %34, label %36, label %35
35: ; preds = %32
br label %53
36: ; preds = %32
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%38 = load i8*, i8** %2
%39 = load %struct.dirent*, %struct.dirent** %4
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @snprintf(i8* %37, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %38, i8* %41)
%43 = load i32*, i32** %3
%44 = call i32 @closedir(i32* %43)
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%46 = call i64 @unlink(i8* %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %36
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 @myerr(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %48, %36
br label %6
53: ; preds = %35
%54 = load i32*, i32** %3
%55 = call i32 @closedir(i32* %54)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [19 x i8] c"cannot open dir %s\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [18 x i8] c"unlink failed: %s\00"
@errno = global i32 0
define void @clean_dir(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %33, %1
%5 = call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = call i32 @myerr(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %4
%10 = call %struct.dirent* @readdir(i32* %5)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %34, label %12
12: ; preds = %9, %21
%13 = phi %struct.dirent* [ %22, %21 ], [ %10, %9 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = call i32 @strcmp(i8* dereferenceable(1) %15, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = call i32 @strcmp(i8* dereferenceable(1) %15, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18, %12
%22 = call %struct.dirent* @readdir(i32* %5)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %34, label %12
24: ; preds = %18
%25 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %15)
%26 = call i32 @closedir(i32* %5)
%27 = call i64 @unlink(i8* %3)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @myerr(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %29, %24
br label %4
34: ; preds = %9, %21
%35 = call i32 @closedir(i32* %5)
ret void
}