#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__ EISDIR;
scalar_t__ ENOENT;
int /*<<< orphan*/ assert_perror(scalar_t__);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
char *concat(char const *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int rmdir(char const *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int unlink(char *);
void rm_rf(const char *directory) {
  DIR *stream = opendir(directory);
  for (struct dirent *entry = readdir(stream); entry != NULL;
       entry = readdir(stream)) {
    if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
      continue;
    char *full = concat(directory, entry->d_name);
    int err = unlink(full);
    if (err < 0) {
      if (errno == EISDIR)
        rm_rf(full);
      else
        assert_perror(errno);
    }
    free(full);
  }
  closedir(stream);
  int err = rmdir(directory);
  if (err < 0 && errno != ENOENT)
    assert_perror(errno);
}


x86 assembly (clang, -O0):
<rm_rf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     128e <rm_rf+0xee>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     1209 <rm_rf+0x69>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    120e <rm_rf+0x6e>
jmp    127c <rm_rf+0xdc>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1080 <concat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <unlink@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1273 <rm_rf+0xd3>
mov    0x2d9a(%rip),%rax
mov    (%rax),%rax
mov    0x2da0(%rip),%rcx
cmp    (%rcx),%rax
jne    125f <rm_rf+0xbf>
mov    -0x20(%rbp),%rdi
call   10c0 <rm_rf@plt>
jmp    126e <rm_rf+0xce>
mov    0x2d72(%rip),%rax
mov    (%rax),%rdi
call   1060 <assert_perror@plt>
jmp    1273 <rm_rf+0xd3>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x18(%rbp)
jmp    11c6 <rm_rf+0x26>
mov    -0x10(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x8(%rbp),%rdi
call   1070 <rmdir@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    12d6 <rm_rf+0x136>
mov    0x2d24(%rip),%rax
mov    (%rax),%rax
mov    0x2d0a(%rip),%rcx
cmp    (%rcx),%rax
je     12d6 <rm_rf+0x136>
mov    0x2d0a(%rip),%rax
mov    (%rax),%rdi
call   1060 <assert_perror@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rm_rf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <opendir@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1259 <rm_rf+0xb9>
mov    %rax,%rbx
lea    0xe30(%rip),%r15
lea    0xe28(%rip),%r12
mov    0x2e09(%rip),%rbp
jmp    1208 <rm_rf+0x68>
call   1060 <assert_perror@plt>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   10b0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1259 <rm_rf+0xb9>
mov    (%rbx),%edi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     11f8 <rm_rf+0x58>
mov    (%rbx),%edi
mov    %r12,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     11f8 <rm_rf+0x58>
mov    (%rbx),%esi
mov    %r14,%rdi
call   1080 <concat@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <unlink@plt>
test   %eax,%eax
jns    11f0 <rm_rf+0x50>
mov    0x2d92(%rip),%rax
mov    (%rax),%rdi
cmp    0x0(%rbp),%rdi
jne    11e1 <rm_rf+0x41>
mov    %rbx,%rdi
call   10c0 <rm_rf@plt>
jmp    11f0 <rm_rf+0x50>
mov    %r13,%rdi
call   1090 <closedir@plt>
mov    %r14,%rdi
call   1070 <rmdir@plt>
test   %eax,%eax
jns    1283 <rm_rf+0xe3>
mov    0x2d64(%rip),%rax
mov    (%rax),%rdi
mov    0x2d4a(%rip),%rax
cmp    (%rax),%rdi
jne    1292 <rm_rf+0xf2>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <assert_perror@plt>

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

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