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

/* Variables and functions */
scalar_t__ ENOTDIR;
char *catfile(char *, int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ rmdir(char *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
char *strdup(char *);
char *strerror(scalar_t__);
int /*<<< orphan*/ unlink(char *);
int rmrf(char *path) {
  struct dirent *d;
  DIR *dir = opendir(path);
  if (dir == NULL) {
    if (errno == ENOTDIR) {
      unlink(path);
      free(path);
    }
    return 0;
  }
  while ((d = readdir(dir)) != NULL) {
    if (strcmp(d->d_name, ".") == 0)
      continue;
    if (strcmp(d->d_name, "..") == 0)
      continue;
    char *p = catfile(path, d->d_name);
    rmrf(strdup(p));
    break;
  }
  closedir(dir);
  if (rmdir(path) != 0)
    printf("Can't cleanup directory '%s': %s", path, strerror(errno));
  free(path);
  return 0;
}


x86 assembly (clang, -O0):
<rmrf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121c <rmrf+0x5c>
mov    0x2ded(%rip),%rax
mov    (%rax),%rax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%rax
jne    1210 <rmrf+0x50>
mov    -0x10(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f4 <rmrf+0x134>
jmp    1221 <rmrf+0x61>
mov    -0x20(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     129e <rmrf+0xde>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    1259 <rmrf+0x99>
jmp    1221 <rmrf+0x61>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xd9a(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    127a <rmrf+0xba>
jmp    1221 <rmrf+0x61>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   10c0 <catfile@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   10d0 <strdup@plt>
mov    %rax,%rdi
call   1050 <rmrf@plt>
mov    -0x20(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x10(%rbp),%rdi
call   1070 <rmdir@plt>
cmp    $0x0,%rax
je     12e4 <rmrf+0x124>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d0f(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xd24(%rip),%rdi
call   1080 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rmrf>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
call   1060 <opendir@plt>
test   %rax,%rax
je     1280 <rmrf+0xc0>
mov    %rax,%r15
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1248 <rmrf+0x88>
mov    %rax,%rbx
lea    0xe0d(%rip),%r12
lea    0xe05(%rip),%r13
jmp    1210 <rmrf+0x50>
nopl   (%rax)
mov    %r15,%rdi
call   10b0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1248 <rmrf+0x88>
mov    (%rbx),%edi
mov    %r12,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1200 <rmrf+0x40>
mov    (%rbx),%edi
mov    %r13,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1200 <rmrf+0x40>
mov    (%rbx),%esi
mov    %r14,%rdi
call   10c0 <catfile@plt>
mov    %rax,%rdi
call   10d0 <strdup@plt>
mov    %rax,%rdi
call   1050 <rmrf@plt>
mov    %r15,%rdi
call   1090 <closedir@plt>
mov    %r14,%rdi
call   1070 <rmdir@plt>
test   %rax,%rax
je     129e <rmrf+0xde>
mov    0x2d74(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
lea    0xd90(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1080 <printf@plt>
jmp    129e <rmrf+0xde>
mov    0x2d51(%rip),%rax
mov    (%rax),%rax
mov    0x2d67(%rip),%rcx
cmp    (%rcx),%rax
jne    12a6 <rmrf+0xe6>
mov    %r14,%rdi
call   1040 <unlink@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@errno = global i64 0
@ENOTDIR = global i64 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [33 x i8] c"Can't cleanup directory '%s': %s\00"
define i32 @rmrf(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca i32*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %21
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOTDIR
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i8*, i8** %3
%17 = call i32 @unlink(i8* %16)
%18 = load i8*, i8** %3
%19 = call i32 @free(i8* %18)
br label %20
20: ; preds = %15, %11
store i32 0, i32* %2
br label %63
21: ; preds = %1
br label %22
22: ; preds = %39, %32, %21
%23 = load i32*, i32** %5
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %4
%25 = icmp ne %struct.dirent* %24, null
br i1 %25, label %26, label %49
26: ; preds = %22
%27 = load %struct.dirent*, %struct.dirent** %4
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i64 @strcmp(i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
br label %22
33: ; preds = %26
%34 = load %struct.dirent*, %struct.dirent** %4
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @strcmp(i32 %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33
br label %22
40: ; preds = %33
%41 = load i8*, i8** %3
%42 = load %struct.dirent*, %struct.dirent** %4
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i8* @catfile(i8* %41, i32 %44)
store i8* %45, i8** %6
%46 = load i8*, i8** %6
%47 = call i8* @strdup(i8* %46)
%48 = call i32 @rmrf(i8* %47)
br label %49
49: ; preds = %40, %22
%50 = load i32*, i32** %5
%51 = call i32 @closedir(i32* %50)
%52 = load i8*, i8** %3
%53 = call i64 @rmdir(i8* %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %49
%56 = load i8*, i8** %3
%57 = load i64, i64* @errno
%58 = call i8* @strerror(i64 %57)
%59 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %56, i8* %58)
br label %60
60: ; preds = %55, %49
%61 = load i8*, i8** %3
%62 = call i32 @free(i8* %61)
store i32 0, i32* %2
br label %63
63: ; preds = %60, %20
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@errno = global i64 0
@ENOTDIR = global i64 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [33 x i8] c"Can't cleanup directory '%s': %s\00"
define i32 @rmrf(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %31, label %13
7: ; preds = %1
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOTDIR
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %41
11: ; preds = %7
%12 = tail call i32 @unlink(i8* %0)
br label %39
13: ; preds = %4, %19
%14 = phi %struct.dirent* [ %20, %19 ], [ %5, %4 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i64 @strcmp(i32 %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13, %22
%20 = tail call %struct.dirent* @readdir(i32* %2)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %31, label %13
22: ; preds = %13
%23 = load i32, i32* %15
%24 = tail call i64 @strcmp(i32 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %19, label %26
26: ; preds = %22
%27 = load i32, i32* %15
%28 = tail call i8* @catfile(i8* %0, i32 %27)
%29 = tail call i8* @strdup(i8* %28)
%30 = tail call i32 @rmrf(i8* %29)
br label %31
31: ; preds = %19, %4, %26
%32 = tail call i32 @closedir(i32* %2)
%33 = tail call i64 @rmdir(i8* %0)
%34 = icmp eq i64 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i64, i64* @errno
%37 = tail call i8* @strerror(i64 %36)
%38 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %37)
br label %39
39: ; preds = %31, %35, %11
%40 = tail call i32 @free(i8* %0)
br label %41
41: ; preds = %39, %7
ret i32 0
}