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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int removeFile(char *, int, int, int);
char *stpcpy(char *, char const *);
scalar_t__ strcmp(char const *, char *);
size_t strlen(char const *);
int /*<<< orphan*/ warn(char *, char const *);
bool removeRecursively(const char *dirname, bool force, bool prompt) {
  DIR *dir = opendir(dirname);
  if (!dir) {
    warn("fopendir: '%s'", dirname);
    return false;
  }
  size_t dirnameLength = strlen(dirname);
  bool result = true;
  errno = 0;
  struct dirent *dirent = readdir(dir);
  while (dirent) {
    if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0) {
      dirent = readdir(dir);
      continue;
    }
    char *name = malloc(dirnameLength + strlen(dirent->d_name) + 2);
    if (!name)
      err(1, "malloc");
    stpcpy(stpcpy(stpcpy(name, dirname), "/"), dirent->d_name);
    result &= removeFile(name, force, prompt, true);
    free(name);
    errno = 0;
    dirent = readdir(dir);
  }
  if (errno) {
    warn("readdir: '%s'", dirname);
    closedir(dir);
    return false;
  }
  closedir(dir);
  return result;
}


x86 assembly (clang, -O0):
<removeRecursively>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f6 <removeRecursively+0x46>
mov    -0x10(%rbp),%rsi
lea    0xe1b(%rip),%rdi
call   1040 <warn@plt>
movl   $0x0,-0x4(%rbp)
jmp    1373 <removeRecursively+0x1c3>
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    0x2dcf(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     132e <removeRecursively+0x17e>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xdd2(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     126a <removeRecursively+0xba>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xdb4(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    127c <removeRecursively+0xcc>
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x38(%rbp)
jmp    1225 <removeRecursively+0x75>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
call   1070 <strlen@plt>
mov    -0x48(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12c0 <removeRecursively+0x110>
mov    $0x1,%edi
lea    0xd57(%rip),%rsi
call   10b0 <err@plt>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <stpcpy@plt>
mov    %rax,%rdi
lea    0xd42(%rip),%rsi
call   1060 <stpcpy@plt>
mov    %rax,%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
call   1060 <stpcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    $0x1,%ecx
call   10d0 <removeFile@plt>
and    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    0x2ccb(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x38(%rbp)
jmp    1225 <removeRecursively+0x75>
mov    0x2cab(%rip),%rax
cmpq   $0x0,(%rax)
je     1364 <removeRecursively+0x1b4>
mov    -0x10(%rbp),%rsi
lea    0xcd1(%rip),%rdi
call   1040 <warn@plt>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x0,-0x4(%rbp)
jmp    1373 <removeRecursively+0x1c3>
mov    -0x20(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<removeRecursively>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%r14
call   1060 <opendir@plt>
test   %rax,%rax
je     1238 <removeRecursively+0x78>
mov    %rax,%rbx
mov    %r14,%rdi
call   1080 <strlen@plt>
mov    %rax,%rbp
mov    0x2dea(%rip),%rax
movq   $0x0,(%rax)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1312 <removeRecursively+0x152>
mov    %rax,%r13
mov    %r12d,(%rsp)
mov    %r15d,0x4(%rsp)
add    $0x2,%rbp
mov    %rbp,0x10(%rsp)
mov    $0x1,%ebp
lea    0xde1(%rip),%r15
lea    0xdd9(%rip),%r12
jmp    12b0 <removeRecursively+0xf0>
lea    0xdc1(%rip),%rdi
mov    %r14,%rsi
call   1050 <warn@plt>
xor    %ebp,%ebp
jmp    1347 <removeRecursively+0x187>
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <stpcpy@plt>
movw   $0x2f,(%rax)
lea    0x1(%rax),%rdi
mov    0x0(%r13),%rsi
call   1040 <strcpy@plt>
mov    0x8(%rsp),%r13
mov    %r13,%rdi
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    $0x1,%ecx
call   10e0 <removeFile@plt>
and    %eax,%ebp
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x2d4b(%rip),%rax
movq   $0x0,(%rax)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10b0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1317 <removeRecursively+0x157>
mov    0x0(%r13),%rdi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     12a0 <removeRecursively+0xe0>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     12a0 <removeRecursively+0xe0>
mov    0x0(%r13),%rdi
call   1080 <strlen@plt>
mov    0x10(%rsp),%rcx
lea    (%rax,%rcx,1),%rdi
call   10d0 <malloc@plt>
test   %rax,%rax
mov    %rax,0x8(%rsp)
jne    124e <removeRecursively+0x8e>
mov    $0x1,%edi
lea    0xd0f(%rip),%rsi
call   10c0 <err@plt>
mov    0x8(%rsp),%rax
jmp    124e <removeRecursively+0x8e>
mov    $0x1,%ebp
mov    0x2cc2(%rip),%rax
cmpq   $0x0,(%rax)
je     133f <removeRecursively+0x17f>
lea    0xcee(%rip),%rdi
mov    %r14,%rsi
call   1050 <warn@plt>
mov    %rbx,%rdi
call   1090 <closedir@plt>
xor    %ebp,%ebp
jmp    1347 <removeRecursively+0x187>
mov    %rbx,%rdi
call   1090 <closedir@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [15 x i8] c"fopendir: '%s'\00"
@errno = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [14 x i8] c"readdir: '%s'\00"
define i32 @removeRecursively(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
%11 = alloca %struct.dirent*
%12 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%13 = load i8*, i8** %5
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %8
%15 = load i32*, i32** %8
%16 = icmp ne i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = call i32 @warn(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 0, i32* %4
br label %87
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = call i64 @strlen(i8* %21)
store i64 %22, i64* %9
store i32 1, i32* %10
store i64 0, i64* @errno
%23 = load i32*, i32** %8
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %11
br label %25
25: ; preds = %56, %40, %20
%26 = load %struct.dirent*, %struct.dirent** %11
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %75
28: ; preds = %25
%29 = load %struct.dirent*, %struct.dirent** %11
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = call i64 @strcmp(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %28
%35 = load %struct.dirent*, %struct.dirent** %11
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %34, %28
%41 = load i32*, i32** %8
%42 = call %struct.dirent* @readdir(i32* %41)
store %struct.dirent* %42, %struct.dirent** %11
br label %25
43: ; preds = %34
%44 = load i64, i64* %9
%45 = load %struct.dirent*, %struct.dirent** %11
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i64 @strlen(i8* %47)
%49 = add i64 %44, %48
%50 = add i64 %49, 2
%51 = call i8* @malloc(i64 %50)
store i8* %51, i8** %12
%52 = load i8*, i8** %12
%53 = icmp ne i8* %52, null
br i1 %53, label %56, label %54
54: ; preds = %43
%55 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %54, %43
%57 = load i8*, i8** %12
%58 = load i8*, i8** %5
%59 = call i8* @stpcpy(i8* %57, i8* %58)
%60 = call i8* @stpcpy(i8* %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%61 = load %struct.dirent*, %struct.dirent** %11
%62 = getelementptr inbounds %struct.dirent, %struct.dirent* %61, i32 0, i32 0
%63 = load i8*, i8** %62
%64 = call i8* @stpcpy(i8* %60, i8* %63)
%65 = load i8*, i8** %12
%66 = load i32, i32* %6
%67 = load i32, i32* %7
%68 = call i32 @removeFile(i8* %65, i32 %66, i32 %67, i32 1)
%69 = load i32, i32* %10
%70 = and i32 %69, %68
store i32 %70, i32* %10
%71 = load i8*, i8** %12
%72 = call i32 @free(i8* %71)
store i64 0, i64* @errno
%73 = load i32*, i32** %8
%74 = call %struct.dirent* @readdir(i32* %73)
store %struct.dirent* %74, %struct.dirent** %11
br label %25
75: ; preds = %25
%76 = load i64, i64* @errno
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %83
78: ; preds = %75
%79 = load i8*, i8** %5
%80 = call i32 @warn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %79)
%81 = load i32*, i32** %8
%82 = call i32 @closedir(i32* %81)
store i32 0, i32* %4
br label %87
83: ; preds = %75
%84 = load i32*, i32** %8
%85 = call i32 @closedir(i32* %84)
%86 = load i32, i32* %10
store i32 %86, i32* %4
br label %87
87: ; preds = %83, %78, %17
%88 = load i32, i32* %4
ret i32 %88
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [15 x i8] c"fopendir: '%s'\00"
@errno = global i64 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.5 = [14 x i8] c"readdir: '%s'\00"
define i32 @removeRecursively(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @warn(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %58
8: ; preds = %3
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
store i64 0, i64* @errno
%10 = tail call %struct.dirent* @readdir(i32* %4)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %49, label %12
12: ; preds = %8
%13 = add i64 %9, 2
br label %14
14: ; preds = %12, %38
%15 = phi %struct.dirent* [ %10, %12 ], [ %47, %38 ]
%16 = phi i32 [ 1, %12 ], [ %45, %38 ]
br label %17
17: ; preds = %14, %27
%18 = phi %struct.dirent* [ %15, %14 ], [ %28, %27 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = tail 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 %27, label %23
23: ; preds = %17
%24 = load i8*, i8** %19
%25 = tail call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23, %17
%28 = tail call %struct.dirent* @readdir(i32* %4)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %49, label %17
30: ; preds = %23
%31 = load i8*, i8** %19
%32 = tail call i64 @strlen(i8* dereferenceable(1) %31)
%33 = add i64 %13, %32
%34 = tail call i8* @malloc(i64 %33)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %30
%39 = tail call i8* @stpcpy(i8* %34, i8* %0)
%40 = getelementptr i8, i8* %39, i64 1
%41 = bitcast i8* %39 to i16*
store i16 47, i16* %41
%42 = load i8*, i8** %19
%43 = tail call i8* @strcpy(i8* dereferenceable(1) %40, i8* dereferenceable(1) %42)
%44 = tail call i32 @removeFile(i8* %34, i32 %1, i32 %2, i32 1)
%45 = and i32 %44, %16
%46 = tail call i32 @free(i8* %34)
store i64 0, i64* @errno
%47 = tail call %struct.dirent* @readdir(i32* %4)
%48 = icmp eq %struct.dirent* %47, null
br i1 %48, label %49, label %14
49: ; preds = %38, %27, %8
%50 = phi i32 [ 1, %8 ], [ %16, %27 ], [ %45, %38 ]
%51 = load i64, i64* @errno
%52 = icmp eq i64 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %49
%54 = tail call i32 @warn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%55 = tail call i32 @closedir(i32* %4)
br label %58
56: ; preds = %49
%57 = tail call i32 @closedir(i32* %4)
br label %58
58: ; preds = %53, %56, %6
%59 = phi i32 [ 0, %6 ], [ 0, %53 ], [ %50, %56 ]
ret i32 %59
}