#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 */
scalar_t__ IsADir(char *);
int NAME_MAX;
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int rmdir(char const *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int unlink(char const *);
int _RemoveDir(const char *name) {
  DIR *dir;
  struct dirent *file;
  char fname[NAME_MAX * 2 + 2];
  int err;
  dir = opendir(name);
  if (dir == NULL)
    return -1;
  while ((file = readdir(dir))) {
    ;
    if (!strcmp(file->d_name, "."))
      continue;
    if (!strcmp(file->d_name, ".."))
      continue;
    sprintf(fname, "%s/%s", name, file->d_name);
    if (IsADir(fname)) {
      if ((err = _RemoveDir(fname)) < 0)
        return err;
    } else {
      if (unlink(fname) == -1)
        return -1;
    }
  }
  if ((err = rmdir(name)) < 0 && (err = unlink(name)) < 0)
    return err;
  return 0;
}


x86 assembly (clang, -O0):
<_RemoveDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,%eax
lea    0x2(%rax,%rax,1),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e9 <_RemoveDir+0x69>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    131a <_RemoveDir+0x19a>
jmp    11ee <_RemoveDir+0x6e>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12d0 <_RemoveDir+0x150>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdee(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1226 <_RemoveDir+0xa6>
jmp    11ee <_RemoveDir+0x6e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdcc(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1247 <_RemoveDir+0xc7>
jmp    11ee <_RemoveDir+0x6e>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
lea    0xda6(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x40(%rbp),%rdi
call   1070 <IsADir@plt>
cmp    $0x0,%rax
je     12a1 <_RemoveDir+0x121>
mov    -0x40(%rbp),%rdi
call   1090 <_RemoveDir@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    129c <_RemoveDir+0x11c>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    131a <_RemoveDir+0x19a>
jmp    12cb <_RemoveDir+0x14b>
mov    -0x40(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    12c6 <_RemoveDir+0x146>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    131a <_RemoveDir+0x19a>
jmp    12cb <_RemoveDir+0x14b>
jmp    11ee <_RemoveDir+0x6e>
mov    -0x10(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    130c <_RemoveDir+0x18c>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    130c <_RemoveDir+0x18c>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    131a <_RemoveDir+0x19a>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_RemoveDir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
add    %eax,%eax
add    $0x2,%eax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
call   1040 <opendir@plt>
mov    %rax,%r12
mov    $0xffffffff,%eax
test   %r12,%r12
je     126c <_RemoveDir+0xec>
mov    %r12,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1252 <_RemoveDir+0xd2>
lea    0xe23(%rip),%r13
jmp    11fd <_RemoveDir+0x7d>
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
je     127b <_RemoveDir+0xfb>
xchg   %ax,%ax
mov    %r12,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1252 <_RemoveDir+0xd2>
mov    (%rax),%r14
mov    %r14,%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11f0 <_RemoveDir+0x70>
mov    %r14,%rdi
lea    0xde7(%rip),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11f0 <_RemoveDir+0x70>
mov    %rbx,%rdi
lea    0xdd7(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
call   1070 <IsADir@plt>
mov    %rbx,%rdi
test   %rax,%rax
je     11e0 <_RemoveDir+0x60>
call   1090 <_RemoveDir@plt>
test   %eax,%eax
jns    11f0 <_RemoveDir+0x70>
jmp    126c <_RemoveDir+0xec>
mov    %r15,%rdi
call   1050 <rmdir@plt>
test   %eax,%eax
jns    126a <_RemoveDir+0xea>
mov    %r15,%rdi
call   1030 <unlink@plt>
test   %eax,%eax
js     126c <_RemoveDir+0xec>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    126c <_RemoveDir+0xec>

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@NAME_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @_RemoveDir(i8* %0) {
%2 = load i32, i32* @NAME_MAX
%3 = shl nsw i32 %2, 1
%4 = add nsw i32 %3, 2
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i32* @opendir(i8* %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %41, label %9
9: ; preds = %1
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %34, label %12
12: ; preds = %9, %31
%13 = phi %struct.dirent* [ %32, %31 ], [ %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, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %31, 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.1, i64 0, i64 0))
%20 = icmp eq i32 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %18
%22 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %15)
%23 = call i64 @IsADir(i8* %6)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = call i32 @_RemoveDir(i8* %6)
%27 = icmp slt i32 %26, 0
br i1 %27, label %41, label %31
28: ; preds = %21
%29 = call i32 @unlink(i8* %6)
%30 = icmp eq i32 %29, -1
br i1 %30, label %41, label %31
31: ; preds = %25, %28, %12, %18
%32 = call %struct.dirent* @readdir(i32* %7)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %34, label %12
34: ; preds = %31, %9
%35 = call i32 @rmdir(i8* %0)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = call i32 @unlink(i8* %0)
%39 = icmp slt i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %37, %34
br label %41
41: ; preds = %28, %25, %37, %1, %40
%42 = phi i32 [ 0, %40 ], [ -1, %1 ], [ %38, %37 ], [ -1, %28 ], [ %26, %25 ]
ret i32 %42
}