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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int rmdir(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int unlink(char *);
int rmrf(char *path) {
  DIR *d = opendir(path);
  if (d == NULL)
    return -1;
  char file[1024];
  struct dirent *de;
  while ((de = readdir(d))) {
    if (de->d_name[0] == '.')
      continue;
    snprintf(file, sizeof(file), "%s/%s", path, de->d_name);
    int rc = unlink(file);
    if (rc == -1) {
      closedir(d);
      return -1;
    }
  }
  closedir(d);
  return rmdir(path);
}


x86 assembly (clang, -O0):
<rmrf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1193 <rmrf+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <rmrf+0xe1>
jmp    1198 <rmrf+0x38>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
je     122c <rmrf+0xcc>
mov    -0x428(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11cd <rmrf+0x6d>
jmp    1198 <rmrf+0x38>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x428(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xe12(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x420(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0xffffffff,-0x42c(%rbp)
jne    1227 <rmrf+0xc7>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <rmrf+0xe1>
jmp    1198 <rmrf+0x38>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x10(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rmrf>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r14
call   1040 <opendir@plt>
test   %rax,%rax
je     11de <rmrf+0x7e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11e5 <rmrf+0x85>
lea    0xe6e(%rip),%r15
mov    %rsp,%r12
jmp    11ad <rmrf+0x4d>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11e5 <rmrf+0x85>
mov    (%rax),%r8
cmpb   $0x2e,(%r8)
je     11a0 <rmrf+0x40>
mov    %r12,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    %r12,%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    11a0 <rmrf+0x40>
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    $0xffffffff,%eax
jmp    11f5 <rmrf+0x95>
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    %r14,%rdi
call   1050 <rmdir@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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