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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ remove(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strerror(scalar_t__);
int remove_directory(char const *name) {
  DIR *directory;
  struct dirent *entry;
  struct stat file_stat;
  int statbit = 0;
  char buffer[1024] = {0};
  directory = opendir(name);
  if (directory == NULL) {
    fprintf(stderr, "Cannot open directory %s\n", name);
    return 0;
  }
  while ((entry = readdir(directory)) != NULL) {
    if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
      continue;
    }
    snprintf(buffer, 1024, "%s/%s", name, entry->d_name);
    stat(buffer, &file_stat);
    if (statbit == -1 && errno != 0) {
      (void)fprintf(stderr, "Error: %s.\n", strerror(errno));
    }
    if (S_ISREG(file_stat.st_mode)) {
      remove(buffer);
    } else if (S_ISDIR(file_stat.st_mode)) {
      remove_directory(buffer);
    }
  }
  closedir(directory);
  remove(name);
  return 1;
}


x86 assembly (clang, -O0):
<remove_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1236 <remove_directory+0x66>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xddb(%rip),%rsi
call   10b0 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1376 <remove_directory+0x1a6>
jmp    123b <remove_directory+0x6b>
mov    -0x18(%rbp),%rdi
call   10e0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     135d <remove_directory+0x18d>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdbb(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     128c <remove_directory+0xbc>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd9d(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    1291 <remove_directory+0xc1>
jmp    123b <remove_directory+0x6b>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xd6e(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x430(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10c0 <stat@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1312 <remove_directory+0x142>
mov    0x2d03(%rip),%rax
cmpq   $0x0,(%rax)
je     1312 <remove_directory+0x142>
mov    0x2cfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x434(%rbp)
mov    0x2ce3(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x434(%rbp),%edi
mov    %rax,%rdx
lea    0xd16(%rip),%rsi
call   10b0 <fprintf@plt>
mov    -0x28(%rbp),%edi
call   10d0 <S_ISREG@plt>
cmp    $0x0,%rax
je     1335 <remove_directory+0x165>
lea    -0x430(%rbp),%rdi
call   1030 <remove@plt>
jmp    1358 <remove_directory+0x188>
mov    -0x28(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1353 <remove_directory+0x183>
lea    -0x430(%rbp),%rdi
call   1040 <remove_directory@plt>
jmp    1358 <remove_directory+0x188>
jmp    123b <remove_directory+0x6b>
mov    -0x18(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x10(%rbp),%rdi
call   1030 <remove@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_directory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r14
lea    0x10(%rsp),%rdi
xor    %ebp,%ebp
mov    $0x400,%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    %r14,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12c6 <remove_directory+0x106>
mov    %rax,%rbx
mov    %rax,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     12af <remove_directory+0xef>
mov    %rax,%r13
lea    0xe05(%rip),%r15
lea    0x10(%rsp),%r12
jmp    1238 <remove_directory+0x78>
nopl   (%rax)
mov    %r12,%rdi
call   1030 <remove@plt>
mov    %rbx,%rdi
call   10e0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12af <remove_directory+0xef>
mov    0x0(%r13),%rdi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1228 <remove_directory+0x68>
mov    0x0(%r13),%rdi
lea    0xdc6(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1228 <remove_directory+0x68>
mov    0x0(%r13),%r8
mov    %r12,%rdi
mov    $0x400,%esi
lea    0xdac(%rip),%rdx
mov    %r14,%rcx
call   1070 <snprintf@plt>
mov    %r12,%rdi
lea    0x8(%rsp),%rsi
call   10c0 <stat@plt>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   10d0 <S_ISREG@plt>
test   %rax,%rax
jne    1220 <remove_directory+0x60>
mov    %ebp,%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
je     1228 <remove_directory+0x68>
mov    %r12,%rdi
call   1040 <remove_directory@plt>
jmp    1228 <remove_directory+0x68>
mov    %rbx,%rdi
call   1090 <closedir@plt>
mov    %r14,%rdi
call   1030 <remove@plt>
mov    $0x1,%ebp
jmp    12de <remove_directory+0x11e>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
lea    0xd2a(%rip),%rsi
mov    %r14,%rdx
call   10b0 <fprintf@plt>
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Cannot open directory %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@errno = global i64 0
@.str.4 = [12 x i8] c"Error: %s.\0A\00"
define i32 @remove_directory(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca [1024 x i8]
store i8* %0, i8** %3
store i32 0, i32* %7
%9 = bitcast [1024 x i8]* %8 to i8*
%10 = load i8*, i8** %3
%11 = call i32* @opendir(i8* %10)
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %3
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 0, i32* %2
br label %78
18: ; preds = %1
br label %19
19: ; preds = %72, %35, %18
%20 = load i32*, i32** %4
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %5
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %73
23: ; preds = %19
%24 = load %struct.dirent*, %struct.dirent** %5
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %35, label %29
29: ; preds = %23
%30 = load %struct.dirent*, %struct.dirent** %5
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29, %23
br label %19
36: ; preds = %29
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%38 = load i8*, i8** %3
%39 = load %struct.dirent*, %struct.dirent** %5
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @snprintf(i8* %37, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %38, i8* %41)
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%44 = call i32 @stat(i8* %43, %struct.stat* %6)
%45 = load i32, i32* %7
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %55
47: ; preds = %36
%48 = load i64, i64* @errno
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i32, i32* @stderr
%52 = load i64, i64* @errno
%53 = call i8* @strerror(i64 %52)
%54 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %50, %47, %36
%56 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i64 @S_ISREG(i32 %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%62 = call i32 @remove(i8* %61)
br label %72
63: ; preds = %55
%64 = getelementptr inbounds %struct.stat, %struct.stat* %6, 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 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%70 = call i32 @remove_directory(i8* %69)
br label %71
71: ; preds = %68, %63
br label %72
72: ; preds = %71, %60
br label %19
73: ; preds = %19
%74 = load i32*, i32** %4
%75 = call i32 @closedir(i32* %74)
%76 = load i8*, i8** %3
%77 = call i32 @remove(i8* %76)
store i32 1, i32* %2
br label %78
78: ; preds = %73, %14
%79 = load i32, i32* %2
ret i32 %79
}

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