#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;
  int d_type;
};

/* Variables and functions */
int /*<<< orphan*/ alphasort;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ join_strings(char **, int, char *, char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ rmdir(char *);
int scandir(char *, struct dirent ***, int /*<<< orphan*/ *,
            int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ unlink(char *);
int delete_directory(char *path) {
  struct dirent **eps;
  int i, n;
  char *new_dir, *current_file;
  n = scandir(path, &eps, NULL, alphasort);
  for (i = 0; i < n; i++) {
    if (strcmp(eps[i]->d_name, ".") == 0)
      continue;
    if (strcmp(eps[i]->d_name, "..") == 0)
      continue;
    join_strings(&current_file, 2, path, eps[i]->d_name);
    if (eps[i]->d_type == 4) {
      join_strings(&new_dir, 2, current_file, "/");
      delete_directory(new_dir);
      free(new_dir);
      rmdir(new_dir);
    } else
      unlink(current_file);
  }
  if (rmdir(path) != 0) {
    printf("[Error  ]\t Can't delete directory %s\n", path);
  }
}


x86 assembly (clang, -O0):
<delete_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%ecx
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <scandir@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    129c <delete_directory+0x11c>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%rdi
lea    0xe2f(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    11e6 <delete_directory+0x66>
jmp    128e <delete_directory+0x10e>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%rdi
lea    0xe04(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1210 <delete_directory+0x90>
jmp    128e <delete_directory+0x10e>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%rcx
lea    -0x30(%rbp),%rdi
mov    $0x2,%esi
call   10a0 <join_strings@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
cmpl   $0x4,0x8(%rax)
jne    1280 <delete_directory+0x100>
mov    -0x30(%rbp),%rdx
lea    -0x28(%rbp),%rdi
mov    $0x2,%esi
lea    0xda8(%rip),%rcx
call   10a0 <join_strings@plt>
mov    -0x28(%rbp),%rdi
call   1090 <delete_directory@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1050 <rmdir@plt>
jmp    1289 <delete_directory+0x109>
mov    -0x30(%rbp),%rdi
call   1040 <unlink@plt>
jmp    128e <delete_directory+0x10e>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b0 <delete_directory+0x30>
mov    -0x10(%rbp),%rdi
call   1050 <rmdir@plt>
cmp    $0x0,%rax
je     12bf <delete_directory+0x13f>
mov    -0x10(%rbp),%rsi
lea    0xd4b(%rip),%rdi
call   1060 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<delete_directory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e48(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rsi
xor    %edx,%edx
call   1070 <scandir@plt>
test   %eax,%eax
jle    1274 <delete_directory+0xf4>
mov    %eax,%r13d
xor    %ebp,%ebp
lea    0xe49(%rip),%rbx
lea    0xe41(%rip),%r15
lea    0x10(%rsp),%r12
jmp    11dd <delete_directory+0x5d>
mov    %rdx,%rdi
call   1040 <unlink@plt>
xchg   %ax,%ax
add    $0x1,%rbp
cmp    %rbp,%r13
je     1274 <delete_directory+0xf4>
mov    (%rsp),%rax
mov    (%rax,%rbp,8),%rax
mov    (%rax),%rdi
mov    %rbx,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11d0 <delete_directory+0x50>
mov    (%rsp),%rax
mov    (%rax,%rbp,8),%rax
mov    (%rax),%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11d0 <delete_directory+0x50>
mov    (%rsp),%rax
mov    (%rax,%rbp,8),%rax
mov    (%rax),%rcx
mov    %r12,%rdi
mov    $0x2,%esi
mov    %r14,%rdx
call   10a0 <join_strings@plt>
mov    (%rsp),%rax
mov    (%rax,%rbp,8),%rax
cmpl   $0x4,0x8(%rax)
mov    0x10(%rsp),%rdx
jne    11c6 <delete_directory+0x46>
lea    0x8(%rsp),%rdi
mov    $0x2,%esi
lea    0xdb7(%rip),%rcx
call   10a0 <join_strings@plt>
mov    0x8(%rsp),%rdi
call   1090 <delete_directory@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1050 <rmdir@plt>
jmp    11d0 <delete_directory+0x50>
mov    %r14,%rdi
call   1050 <rmdir@plt>
test   %rax,%rax
je     1290 <delete_directory+0x110>
lea    0xd7d(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8*, i32 }
@alphasort = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [38 x i8] c"[Error ]\09 Can't delete directory %s\0A\00"
define i32 @delete_directory(i8* %0) {
%2 = alloca %struct.dirent**
%3 = alloca i8*
%4 = alloca i8*
%5 = bitcast %struct.dirent*** %2 to i8*
%6 = bitcast i8** %3 to i8*
%7 = bitcast i8** %4 to i8*
%8 = load i32, i32* @alphasort
%9 = call i32 @scandir(i8* %0, %struct.dirent*** %2, i32* null, i32 %8)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %57
11: ; preds = %1
%12 = zext i32 %9 to i64
br label %13
13: ; preds = %11, %54
%14 = phi i64 [ 0, %11 ], [ %55, %54 ]
%15 = load %struct.dirent**, %struct.dirent*** %2
%16 = getelementptr inbounds %struct.dirent*, %struct.dirent** %15, i64 %14
%17 = load %struct.dirent*, %struct.dirent** %16
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %54, label %22
22: ; preds = %13
%23 = load %struct.dirent**, %struct.dirent*** %2
%24 = getelementptr inbounds %struct.dirent*, %struct.dirent** %23, i64 %14
%25 = load %struct.dirent*, %struct.dirent** %24
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%27 = load i8*, i8** %26
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %54, label %30
30: ; preds = %22
%31 = load %struct.dirent**, %struct.dirent*** %2
%32 = getelementptr inbounds %struct.dirent*, %struct.dirent** %31, i64 %14
%33 = load %struct.dirent*, %struct.dirent** %32
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i64 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @join_strings(i8** %4, i32 2, i8* %0, i8* %35)
%37 = load %struct.dirent**, %struct.dirent*** %2
%38 = getelementptr inbounds %struct.dirent*, %struct.dirent** %37, i64 %14
%39 = load %struct.dirent*, %struct.dirent** %38
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i64 0, i32 1
%41 = load i32, i32* %40
%42 = icmp eq i32 %41, 4
%43 = load i8*, i8** %4
br i1 %42, label %44, label %52
44: ; preds = %30
%45 = call i32 @join_strings(i8** %3, i32 2, i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%46 = load i8*, i8** %3
%47 = call i32 @delete_directory(i8* %46)
%48 = load i8*, i8** %3
%49 = call i32 @free(i8* %48)
%50 = load i8*, i8** %3
%51 = call i64 @rmdir(i8* %50)
br label %54
52: ; preds = %30
%53 = call i32 @unlink(i8* %43)
br label %54
54: ; preds = %44, %52, %22, %13
%55 = add nuw nsw i64 %14, 1
%56 = icmp eq i64 %55, %12
br i1 %56, label %57, label %13
57: ; preds = %54, %1
%58 = call i64 @rmdir(i8* %0)
%59 = icmp eq i64 %58, 0
br i1 %59, label %62, label %60
60: ; preds = %57
%61 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %62
62: ; preds = %60, %57
ret i32 undef
}