#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 */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rmdir(char *);
char *str_concat(char *, char *);
char *str_dup(char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ unlink(char *);
void remove_dir(char *path) {
  DIR *d;
  struct dirent *e;
  char *file;
  if ((d = opendir(path)) == NULL) {
    return;
  }
  while ((e = readdir(d)) != NULL) {
    if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) {
      file = str_concat(str_concat(str_dup(path), "/"), e->d_name);
      unlink(file);
      free(file);
    }
  }
  closedir(d);
  rmdir(path);
}


x86 assembly (clang, -O0):
<remove_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11b8 <remove_dir+0x28>
jmp    1262 <remove_dir+0xd2>
jmp    11bd <remove_dir+0x2d>
mov    -0x10(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1250 <remove_dir+0xc0>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1f(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     124b <remove_dir+0xbb>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe01(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     124b <remove_dir+0xbb>
mov    -0x8(%rbp),%rdi
call   10a0 <str_dup@plt>
mov    %rax,%rdi
lea    0xde2(%rip),%rsi
call   10b0 <str_concat@plt>
mov    %rax,%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   10b0 <str_concat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    11bd <remove_dir+0x2d>
mov    -0x10(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x8(%rbp),%rdi
call   1060 <rmdir@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <opendir@plt>
test   %rax,%rax
je     1264 <remove_dir+0xd4>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1246 <remove_dir+0xb6>
mov    %rax,%rbp
lea    0xe37(%rip),%r15
lea    0xe2f(%rip),%r12
lea    0xe2b(%rip),%r13
jmp    11f0 <remove_dir+0x60>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1246 <remove_dir+0xb6>
mov    0x0(%rbp),%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11e0 <remove_dir+0x50>
mov    0x0(%rbp),%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     11e0 <remove_dir+0x50>
mov    %r14,%rdi
call   10a0 <str_dup@plt>
mov    %rax,%rdi
mov    %r13,%rsi
call   10b0 <str_concat@plt>
mov    0x0(%rbp),%rsi
mov    %rax,%rdi
call   10b0 <str_concat@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <unlink@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    11e0 <remove_dir+0x50>
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <rmdir@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c"/\00"
define void @remove_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @opendir(i8* %6)
store i32* %7, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
br label %45
10: ; preds = %1
br label %11
11: ; preds = %39, %10
%12 = load i32*, i32** %3
%13 = call %struct.dirent* @readdir(i32* %12)
store %struct.dirent* %13, %struct.dirent** %4
%14 = icmp ne %struct.dirent* %13, null
br i1 %14, label %15, label %40
15: ; preds = %11
%16 = load %struct.dirent*, %struct.dirent** %4
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i32 0, i32 0
%18 = load i8*, i8** %17
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %39
21: ; preds = %15
%22 = load %struct.dirent*, %struct.dirent** %4
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %39
27: ; preds = %21
%28 = load i8*, i8** %2
%29 = call i8* @str_dup(i8* %28)
%30 = call i8* @str_concat(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%31 = load %struct.dirent*, %struct.dirent** %4
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i8* @str_concat(i8* %30, i8* %33)
store i8* %34, i8** %5
%35 = load i8*, i8** %5
%36 = call i32 @unlink(i8* %35)
%37 = load i8*, i8** %5
%38 = call i32 @free(i8* %37)
br label %39
39: ; preds = %27, %21, %15
br label %11
40: ; preds = %11
%41 = load i32*, i32** %3
%42 = call i32 @closedir(i32* %41)
%43 = load i8*, i8** %2
%44 = call i32 @rmdir(i8* %43)
br label %45
45: ; preds = %40, %9
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c"/\00"
define void @remove_dir(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %30, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %27, label %7
7: ; preds = %4, %24
%8 = phi %struct.dirent* [ %25, %24 ], [ %5, %4 ]
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%10 = load i8*, i8** %9
%11 = tail call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %24, label %13
13: ; preds = %7
%14 = load i8*, i8** %9
%15 = tail call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %13
%18 = tail call i8* @str_dup(i8* %0)
%19 = tail call i8* @str_concat(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = load i8*, i8** %9
%21 = tail call i8* @str_concat(i8* %19, i8* %20)
%22 = tail call i32 @unlink(i8* %21)
%23 = tail call i32 @free(i8* %21)
br label %24
24: ; preds = %17, %13, %7
%25 = tail call %struct.dirent* @readdir(i32* %2)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %27, label %7
27: ; preds = %24, %4
%28 = tail call i32 @closedir(i32* %2)
%29 = tail call i32 @rmdir(i8* %0)
br label %30
30: ; preds = %1, %27
ret void
}