#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*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ dirfd(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ rmdir(char *);
scalar_t__ strcmp(char *, char *);
scalar_t__ unlinkat(int /*<<< orphan*/, char *, int /*<<< orphan*/);
void wipe_directory(char *path) {
  DIR *d = opendir(path);
  if (d == NULL) {
    printf("opendir(%s): %m\n", path);
    return;
  }
  struct dirent *dent;
  while ((dent = readdir(d)) != NULL) {
    if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
      continue;
    if (unlinkat(dirfd(d), dent->d_name, 0))
      printf("unable to unlink %s/%s: %m\n", path, dent->d_name);
  }
  closedir(d);
  if (rmdir(path))
    printf("unable to unlink %s: %m\n", path);
}


x86 assembly (clang, -O0):
<wipe_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bb <wipe_directory+0x3b>
mov    -0x8(%rbp),%rsi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1285 <wipe_directory+0x105>
jmp    11c0 <wipe_directory+0x40>
mov    -0x10(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1257 <wipe_directory+0xd7>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe2d(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1211 <wipe_directory+0x91>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe0f(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1216 <wipe_directory+0x96>
jmp    11c0 <wipe_directory+0x40>
mov    -0x10(%rbp),%rdi
call   1090 <dirfd@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
xor    %edx,%edx
call   1030 <unlinkat@plt>
cmp    $0x0,%rax
je     1252 <wipe_directory+0xd2>
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    11c0 <wipe_directory+0x40>
mov    -0x10(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x8(%rbp),%rdi
call   1050 <rmdir@plt>
cmp    $0x0,%rax
je     1285 <wipe_directory+0x105>
mov    -0x8(%rbp),%rsi
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wipe_directory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <opendir@plt>
test   %rax,%rax
je     124d <wipe_directory+0xcd>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     122f <wipe_directory+0xaf>
mov    %rax,%rbp
lea    0xe58(%rip),%r15
lea    0xe50(%rip),%r12
lea    0xe4c(%rip),%r13
jmp    11e0 <wipe_directory+0x60>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     122f <wipe_directory+0xaf>
mov    0x0(%rbp),%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11d0 <wipe_directory+0x50>
mov    0x0(%rbp),%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11d0 <wipe_directory+0x50>
mov    %rbx,%rdi
call   1090 <dirfd@plt>
mov    0x0(%rbp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1030 <unlinkat@plt>
test   %rax,%rax
je     11d0 <wipe_directory+0x50>
mov    0x0(%rbp),%rdx
mov    %r13,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11d0 <wipe_directory+0x50>
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    %r14,%rdi
call   1050 <rmdir@plt>
test   %rax,%rax
je     126c <wipe_directory+0xec>
lea    0xde5(%rip),%rdi
jmp    1254 <wipe_directory+0xd4>
lea    0xdac(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [17 x i8] c"opendir(%s): %m\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [28 x i8] c"unable to unlink %s/%s: %m\0A\00"
@.str.4 = [25 x i8] c"unable to unlink %s: %m\0A\00"
define void @wipe_directory(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32* @opendir(i8* %5)
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %54
12: ; preds = %1
br label %13
13: ; preds = %44, %29, %12
%14 = load i32*, i32** %3
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %4
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %45
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %4
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %17
%24 = load %struct.dirent*, %struct.dirent** %4
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %23, %17
br label %13
30: ; preds = %23
%31 = load i32*, i32** %3
%32 = call i32 @dirfd(i32* %31)
%33 = load %struct.dirent*, %struct.dirent** %4
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i64 @unlinkat(i32 %32, i8* %35, i32 0)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %30
%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 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %39, i8* %42)
br label %44
44: ; preds = %38, %30
br label %13
45: ; preds = %13
%46 = load i32*, i32** %3
%47 = call i32 @closedir(i32* %46)
%48 = load i8*, i8** %2
%49 = call i64 @rmdir(i8* %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %45
%52 = load i8*, i8** %2
%53 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %9, %51, %45
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [17 x i8] c"opendir(%s): %m\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [28 x i8] c"unable to unlink %s/%s: %m\0A\00"
@.str.4 = [25 x i8] c"unable to unlink %s: %m\0A\00"
define void @wipe_directory(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %32, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %28, label %7
7: ; preds = %4, %17
%8 = phi %struct.dirent* [ %18, %17 ], [ %5, %4 ]
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%10 = load i8*, i8** %9
%11 = tail call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %7
%14 = load i8*, i8** %9
%15 = tail call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %20, %25, %7, %13
%18 = tail call %struct.dirent* @readdir(i32* %2)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %28, label %7
20: ; preds = %13
%21 = tail call i32 @dirfd(i32* %2)
%22 = load i8*, i8** %9
%23 = tail call i64 @unlinkat(i32 %21, i8* %22, i32 0)
%24 = icmp eq i64 %23, 0
br i1 %24, label %17, label %25
25: ; preds = %20
%26 = load i8*, i8** %9
%27 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %26)
br label %17
28: ; preds = %17, %4
%29 = tail call i32 @closedir(i32* %2)
%30 = tail call i64 @rmdir(i8* %0)
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %28, %1
%33 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), %28 ]
%34 = tail call i32 (i8*, i8*, ...) @printf(i8* %33, i8* %0)
br label %35
35: ; preds = %32, %28
ret void
}