#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char const *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int rmdir(char const *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, char const *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ strcmp(char const *, char *);
size_t strlen(char const *);
int unlink(char *);
int remove_directory(const char *path) {
  struct stat statbuf;
  struct dirent *p;
  DIR *d = opendir(path);
  size_t path_len = strlen(path);
  int r = -1;
  if (d) {
    r = 0;
    while (!r && (p = readdir(d))) {
      int r2 = -1;
      char *buf;
      size_t len;
      if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
        continue;
      len = path_len + strlen(p->d_name) + 2;
      buf = malloc(len);
      if (buf) {
        snprintf(buf, len, "%s/%s", path, p->d_name);
        if (!stat(buf, &statbuf)) {
          if (S_ISDIR(statbuf.st_mode))
            r2 = remove_directory(buf);
          else
            r2 = unlink(buf);
        }
        free(buf);
      }
      r = r2;
    }
    closedir(d);
  }
  if (!r)
    r = rmdir(path);
  return r;
}


x86 assembly (clang, -O0):
<remove_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strlen@plt>
mov    %rax,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     134d <remove_directory+0x17d>
movl   $0x0,-0x2c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x41(%rbp)
jne    1235 <remove_directory+0x65>
mov    -0x20(%rbp),%rdi
call   10e0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1245 <remove_directory+0x75>
jmp    1344 <remove_directory+0x174>
movl   $0xffffffff,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xda7(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%eax
je     1284 <remove_directory+0xb4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xd8a(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%eax
jne    1289 <remove_directory+0xb9>
jmp    120f <remove_directory+0x3f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1080 <strlen@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
add    %rcx,%rax
add    $0x2,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   10f0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1339 <remove_directory+0x169>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
lea    0xd22(%rip),%rdx
call   10a0 <snprintf@plt>
mov    -0x38(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10d0 <stat@plt>
cmp    $0x0,%eax
jne    1330 <remove_directory+0x160>
mov    -0x10(%rbp),%edi
call   1090 <S_ISDIR@plt>
cmp    $0x0,%rax
je     131f <remove_directory+0x14f>
mov    -0x38(%rbp),%rdi
call   1050 <remove_directory@plt>
mov    %eax,-0x30(%rbp)
jmp    132b <remove_directory+0x15b>
mov    -0x38(%rbp),%rdi
call   1040 <unlink@plt>
mov    %eax,-0x30(%rbp)
jmp    1330 <remove_directory+0x160>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    120f <remove_directory+0x3f>
mov    -0x20(%rbp),%rdi
call   10b0 <closedir@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    1363 <remove_directory+0x193>
mov    -0x8(%rbp),%rdi
call   1070 <rmdir@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_directory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
call   1060 <opendir@plt>
test   %rax,%rax
je     12cc <remove_directory+0xfc>
mov    %rax,%r15
mov    %r14,%rdi
call   1080 <strlen@plt>
add    $0x2,%rax
mov    %rax,0x10(%rsp)
lea    0xdf7(%rip),%rbx
jmp    1223 <remove_directory+0x53>
call   1040 <unlink@plt>
mov    %eax,%ebp
mov    %r13,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
jne    12f6 <remove_directory+0x126>
mov    %r15,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     12d3 <remove_directory+0x103>
mov    (%rax),%r12
mov    %r12,%rdi
mov    %rbx,%rsi
call   10c0 <strcmp@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     121b <remove_directory+0x4b>
mov    %r12,%rdi
lea    0xdae(%rip),%rsi
call   10c0 <strcmp@plt>
test   %eax,%eax
je     121b <remove_directory+0x4b>
mov    %r12,%rdi
call   1080 <strlen@plt>
mov    %rax,%rbp
add    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   10f0 <malloc@plt>
test   %rax,%rax
je     12f1 <remove_directory+0x121>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbp,%rsi
lea    0xd7b(%rip),%rdx
mov    %r14,%rcx
mov    %r12,%r8
call   10a0 <snprintf@plt>
mov    %r13,%rdi
lea    0x8(%rsp),%rsi
call   10d0 <stat@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    1213 <remove_directory+0x43>
mov    0x8(%rsp),%edi
call   1090 <S_ISDIR@plt>
mov    %r13,%rdi
test   %rax,%rax
je     120c <remove_directory+0x3c>
call   1050 <remove_directory@plt>
jmp    1211 <remove_directory+0x41>
mov    $0xffffffff,%ebp
jmp    12fe <remove_directory+0x12e>
mov    %r15,%rdi
call   10b0 <closedir@plt>
mov    %r14,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <rmdir@plt>
mov    $0xffffffff,%ebp
mov    %r15,%rdi
call   10b0 <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.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @remove_directory(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca %struct.dirent*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %2
%11 = load i8*, i8** %2
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %5
%13 = load i8*, i8** %2
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %6
store i32 -1, i32* %7
%15 = load i32*, i32** %5
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %83
17: ; preds = %1
store i32 0, i32* %7
br label %18
18: ; preds = %78, %39, %17
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = load i32*, i32** %5
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %4
%24 = icmp ne %struct.dirent* %23, null
br label %25
25: ; preds = %21, %18
%26 = phi i1 [ false, %18 ], [ %24, %21 ]
br i1 %26, label %27, label %80
27: ; preds = %25
store i32 -1, i32* %8
%28 = load %struct.dirent*, %struct.dirent** %4
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i32 @strcmp(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %27
%34 = load %struct.dirent*, %struct.dirent** %4
%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, %27
br label %18
40: ; preds = %33
%41 = load i64, i64* %6
%42 = load %struct.dirent*, %struct.dirent** %4
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i64 @strlen(i8* %44)
%46 = add i64 %41, %45
%47 = add i64 %46, 2
store i64 %47, i64* %10
%48 = load i64, i64* %10
%49 = call i8* @malloc(i64 %48)
store i8* %49, i8** %9
%50 = load i8*, i8** %9
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %78
52: ; preds = %40
%53 = load i8*, i8** %9
%54 = load i64, i64* %10
%55 = load i8*, i8** %2
%56 = load %struct.dirent*, %struct.dirent** %4
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @snprintf(i8* %53, i64 %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %55, i8* %58)
%60 = load i8*, i8** %9
%61 = call i32 @stat(i8* %60, %struct.stat* %3)
%62 = icmp ne i32 %61, 0
br i1 %62, label %75, label %63
63: ; preds = %52
%64 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%65 = load i32, i32* %64
%66 = call i64 @S_ISDIR(i32 %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %63
%69 = load i8*, i8** %9
%70 = call i32 @remove_directory(i8* %69)
store i32 %70, i32* %8
br label %74
71: ; preds = %63
%72 = load i8*, i8** %9
%73 = call i32 @unlink(i8* %72)
store i32 %73, i32* %8
br label %74
74: ; preds = %71, %68
br label %75
75: ; preds = %74, %52
%76 = load i8*, i8** %9
%77 = call i32 @free(i8* %76)
br label %78
78: ; preds = %75, %40
%79 = load i32, i32* %8
store i32 %79, i32* %7
br label %18
80: ; preds = %25
%81 = load i32*, i32** %5
%82 = call i32 @closedir(i32* %81)
br label %83
83: ; preds = %80, %1
%84 = load i32, i32* %7
%85 = icmp ne i32 %84, 0
br i1 %85, label %89, label %86
86: ; preds = %83
%87 = load i8*, i8** %2
%88 = call i32 @rmdir(i8* %87)
store i32 %88, i32* %7
br label %89
89: ; preds = %86, %83
%90 = load i32, i32* %7
ret i32 %90
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @remove_directory(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %50, label %6
6: ; preds = %1
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = add i64 %7, 2
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %10
10: ; preds = %6, %41
%11 = tail call %struct.dirent* @readdir(i32* %4)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %47, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = tail 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 %41, label %18
18: ; preds = %13
%19 = tail 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 %41, label %21
21: ; preds = %18
%22 = tail call i64 @strlen(i8* dereferenceable(1) %15)
%23 = add i64 %8, %22
%24 = tail call i8* @malloc(i64 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %44, label %26
26: ; preds = %21
%27 = tail call i32 @snprintf(i8* %24, i64 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %15)
%28 = call i32 @stat(i8* %24, %struct.stat* %2)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %26
%31 = load i32, i32* %9
%32 = tail call i64 @S_ISDIR(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = tail call i32 @remove_directory(i8* %24)
br label %38
36: ; preds = %30
%37 = tail call i32 @unlink(i8* %24)
br label %38
38: ; preds = %34, %36, %26
%39 = phi i32 [ -1, %26 ], [ %35, %34 ], [ %37, %36 ]
%40 = tail call i32 @free(i8* %24)
br label %41
41: ; preds = %38, %13, %18
%42 = phi i32 [ 0, %18 ], [ 0, %13 ], [ %39, %38 ]
%43 = icmp eq i32 %42, 0
br i1 %43, label %10, label %44
44: ; preds = %21, %41
%45 = phi i32 [ %42, %41 ], [ -1, %21 ]
%46 = tail call i32 @closedir(i32* %4)
br label %50
47: ; preds = %10
%48 = tail call i32 @closedir(i32* %4)
%49 = tail call i32 @rmdir(i8* %0)
br label %50
50: ; preds = %1, %44, %47
%51 = phi i32 [ %45, %44 ], [ %49, %47 ], [ -1, %1 ]
ret i32 %51
}