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

/* Variables and functions */
scalar_t__ EBUSY;
scalar_t__ EMFILE;
scalar_t__ ENOTEMPTY;
scalar_t__ EROFS;
int FILENAME_MAX;
int /*<<< orphan*/ MNT_DETACH;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ debug(char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ exitf(char *, ...);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ rmdir(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
scalar_t__ strcmp(char *, char *);
scalar_t__ umount2(char const *, int /*<<< orphan*/);
scalar_t__ unlink(char *);
void remove_dir(const char *dir) {
  DIR *dp;
  struct dirent *ep;
  int iter = 0;
retry:
  dp = opendir(dir);
  if (dp == NULL) {
    if (errno == EMFILE) {
      exitf("opendir(%s) failed due to NOFILE, exiting");
    }
    exitf("opendir(%s) failed", dir);
  }
  while ((ep = readdir(dp))) {
    if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
      continue;
    char filename[FILENAME_MAX];
    snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
    struct stat st;
    if (lstat(filename, &st))
      exitf("lstat(%s) failed", filename);
    if (S_ISDIR(st.st_mode)) {
      remove_dir(filename);
      continue;
    }
    int i;
    for (i = 0;; i++) {
      debug("unlink(%s)\n", filename);
      if (unlink(filename) == 0)
        break;
      if (errno == EROFS) {
        debug("ignoring EROFS\n");
        break;
      }
      if (errno != EBUSY || i > 100)
        exitf("unlink(%s) failed", filename);
      debug("umount(%s)\n", filename);
      if (umount2(filename, MNT_DETACH))
        exitf("umount(%s) failed", filename);
    }
  }
  closedir(dp);
  int i;
  for (i = 0;; i++) {
    debug("rmdir(%s)\n", dir);
    if (rmdir(dir) == 0)
      break;
    if (i < 100) {
      if (errno == EROFS) {
        debug("ignoring EROFS\n");
        break;
      }
      if (errno == EBUSY) {
        debug("umount(%s)\n", dir);
        if (umount2(dir, MNT_DETACH))
          exitf("umount(%s) failed", dir);
        continue;
      }
      if (errno == ENOTEMPTY) {
        if (iter < 100) {
          iter++;
          goto retry;
        }
      }
    }
    exitf("rmdir(%s) failed", dir);
  }
}


x86 assembly (clang, -O0):
<remove_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1235 <remove_dir+0x65>
mov    0x2dce(%rip),%rax
mov    (%rax),%rax
mov    0x2da4(%rip),%rcx
cmp    (%rcx),%rax
jne    1223 <remove_dir+0x53>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   10a0 <exitf@plt>
mov    -0x8(%rbp),%rsi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   10a0 <exitf@plt>
jmp    123a <remove_dir+0x6a>
mov    -0x10(%rbp),%rdi
call   10e0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1434 <remove_dir+0x264>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xddf(%rip),%rsi
call   10d0 <strcmp@plt>
cmp    $0x0,%rax
je     128b <remove_dir+0xbb>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc1(%rip),%rsi
call   10d0 <strcmp@plt>
cmp    $0x0,%rax
jne    1290 <remove_dir+0xc0>
jmp    123a <remove_dir+0x6a>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
lea    0xd70(%rip),%rdx
call   1090 <snprintf@plt>
mov    -0x50(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10c0 <lstat@plt>
cmp    $0x0,%rax
je     12fe <remove_dir+0x12e>
mov    -0x50(%rbp),%rsi
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   10a0 <exitf@plt>
mov    -0x38(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1325 <remove_dir+0x155>
mov    -0x50(%rbp),%rdi
call   10f0 <remove_dir@plt>
movl   $0x3,-0x3c(%rbp)
jmp    1413 <remove_dir+0x243>
movl   $0x0,-0x40(%rbp)
mov    -0x50(%rbp),%rsi
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   1070 <debug@plt>
mov    -0x50(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jne    1356 <remove_dir+0x186>
jmp    140c <remove_dir+0x23c>
mov    0x2c73(%rip),%rax
mov    (%rax),%rax
mov    0x2c81(%rip),%rcx
cmp    (%rcx),%rax
jne    1383 <remove_dir+0x1b3>
lea    0xcec(%rip),%rdi
mov    $0x0,%al
call   1070 <debug@plt>
jmp    140c <remove_dir+0x23c>
mov    0x2c46(%rip),%rax
mov    (%rax),%rax
mov    0x2c2c(%rip),%rcx
cmp    (%rcx),%rax
jne    13a7 <remove_dir+0x1d7>
cmpl   $0x64,-0x40(%rbp)
jle    13b9 <remove_dir+0x1e9>
mov    -0x50(%rbp),%rsi
lea    0xcc1(%rip),%rdi
mov    $0x0,%al
call   10a0 <exitf@plt>
mov    -0x50(%rbp),%rsi
lea    0xcc1(%rip),%rdi
mov    $0x0,%al
call   1070 <debug@plt>
mov    -0x50(%rbp),%rdi
mov    0x2bf2(%rip),%rax
mov    (%rax),%esi
call   1040 <umount2@plt>
cmp    $0x0,%rax
je     13f9 <remove_dir+0x229>
mov    -0x50(%rbp),%rsi
lea    0xc9f(%rip),%rdi
mov    $0x0,%al
call   10a0 <exitf@plt>
jmp    13fe <remove_dir+0x22e>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    132c <remove_dir+0x15c>
movl   $0x0,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x3c(%rbp),%eax
test   %eax,%eax
je     142f <remove_dir+0x25f>
jmp    142a <remove_dir+0x25a>
jmp    123a <remove_dir+0x6a>
jmp    123a <remove_dir+0x6a>
mov    -0x10(%rbp),%rdi
call   10b0 <closedir@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xc54(%rip),%rdi
mov    $0x0,%al
call   1070 <debug@plt>
mov    -0x8(%rbp),%rdi
call   1060 <rmdir@plt>
cmp    $0x0,%rax
jne    146e <remove_dir+0x29e>
jmp    1560 <remove_dir+0x390>
cmpl   $0x64,-0x44(%rbp)
jge    1540 <remove_dir+0x370>
mov    0x2b51(%rip),%rax
mov    (%rax),%rax
mov    0x2b5f(%rip),%rcx
cmp    (%rcx),%rax
jne    14a5 <remove_dir+0x2d5>
lea    0xbca(%rip),%rdi
mov    $0x0,%al
call   1070 <debug@plt>
jmp    1560 <remove_dir+0x390>
mov    0x2b24(%rip),%rax
mov    (%rax),%rax
mov    0x2b0a(%rip),%rcx
cmp    (%rcx),%rax
jne    1504 <remove_dir+0x334>
mov    -0x8(%rbp),%rsi
lea    0xbbb(%rip),%rdi
mov    $0x0,%al
call   1070 <debug@plt>
mov    -0x8(%rbp),%rdi
mov    0x2aec(%rip),%rax
mov    (%rax),%esi
call   1040 <umount2@plt>
cmp    $0x0,%rax
je     14ff <remove_dir+0x32f>
mov    -0x8(%rbp),%rsi
lea    0xb99(%rip),%rdi
mov    $0x0,%al
call   10a0 <exitf@plt>
jmp    1552 <remove_dir+0x382>
mov    0x2ac5(%rip),%rax
mov    (%rax),%rax
mov    0x2a93(%rip),%rcx
cmp    (%rcx),%rax
jne    153b <remove_dir+0x36b>
cmpl   $0x64,-0x1c(%rbp)
jge    1536 <remove_dir+0x366>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e3 <remove_dir+0x13>
jmp    153b <remove_dir+0x36b>
jmp    1540 <remove_dir+0x370>
mov    -0x8(%rbp),%rsi
lea    0xb63(%rip),%rdi
mov    $0x0,%al
call   10a0 <exitf@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1444 <remove_dir+0x274>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r13
mov    0x2de5(%rip),%r14
lea    0xe4c(%rip),%rbx
movl   $0x0,-0x2c(%rbp)
mov    %r13,%rdi
call   1050 <opendir@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1237 <remove_dir+0x67>
mov    (%r14),%rax
mov    0x2d9d(%rip),%rcx
cmp    (%rcx),%rax
jne    1226 <remove_dir+0x56>
lea    0xde1(%rip),%rdi
xor    %eax,%eax
call   10a0 <exitf@plt>
lea    0xdfd(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   10a0 <exitf@plt>
mov    %r15,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     13f0 <remove_dir+0x220>
mov    %rax,%r12
jmp    1277 <remove_dir+0xa7>
nopl   (%rax)
mov    %r14,%rdi
call   10f0 <remove_dir@plt>
mov    -0x38(%rbp),%rsp
lea    0xddb(%rip),%rbx
mov    %r15,%rdi
call   10e0 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     13f0 <remove_dir+0x220>
mov    (%r12),%rdi
mov    %rbx,%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     1263 <remove_dir+0x93>
mov    (%r12),%rdi
lea    0xdaa(%rip),%rsi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     1263 <remove_dir+0x93>
mov    %rsp,-0x38(%rbp)
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r14
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    (%r12),%r8
mov    %r14,%rdi
lea    0xd74(%rip),%rdx
mov    %r13,%rcx
call   1090 <snprintf@plt>
mov    %r14,%rdi
lea    -0x30(%rbp),%rsi
call   10c0 <lstat@plt>
test   %rax,%rax
je     12f6 <remove_dir+0x126>
lea    0xd5a(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   10a0 <exitf@plt>
mov    -0x30(%rbp),%edi
call   1080 <S_ISDIR@plt>
test   %rax,%rax
mov    0x2cc8(%rip),%r12
jne    1250 <remove_dir+0x80>
lea    0xd42(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <debug@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
je     1258 <remove_dir+0x88>
xor    %ebx,%ebx
jmp    1365 <remove_dir+0x195>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%ebx
lea    0xd0d(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <debug@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
je     1258 <remove_dir+0x88>
mov    (%r12),%rax
mov    0x2c78(%rip),%rcx
cmp    (%rcx),%rax
je     13d4 <remove_dir+0x204>
mov    0x2c44(%rip),%rcx
cmp    (%rcx),%rax
jne    1386 <remove_dir+0x1b6>
cmp    $0x65,%ebx
jb     1397 <remove_dir+0x1c7>
lea    0xce6(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   10a0 <exitf@plt>
lea    0xce7(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <debug@plt>
mov    0x2c19(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <umount2@plt>
test   %rax,%rax
je     1340 <remove_dir+0x170>
lea    0xccc(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   10a0 <exitf@plt>
jmp    1340 <remove_dir+0x170>
lea    0xc88(%rip),%rdi
xor    %eax,%eax
call   1070 <debug@plt>
jmp    1258 <remove_dir+0x88>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   10b0 <closedir@plt>
lea    0xca4(%rip),%r15
mov    %r15,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <debug@plt>
mov    %r13,%rdi
call   1060 <rmdir@plt>
test   %rax,%rax
mov    0x2bb2(%rip),%r14
lea    0xc89(%rip),%r12
je     14fb <remove_dir+0x32b>
xor    %ebx,%ebx
nopl   (%rax)
mov    %r12,%rdi
cmp    $0x63,%ebx
ja     14b0 <remove_dir+0x2e0>
mov    (%r14),%rax
mov    0x2ba6(%rip),%rcx
cmp    (%rcx),%rax
je     14ed <remove_dir+0x31d>
mov    0x2b6e(%rip),%rcx
cmp    (%rcx),%rax
jne    1490 <remove_dir+0x2c0>
lea    0xc27(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <debug@plt>
mov    0x2b59(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1040 <umount2@plt>
lea    0xc11(%rip),%rdi
test   %rax,%rax
jne    14b0 <remove_dir+0x2e0>
jmp    14ba <remove_dir+0x2ea>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    0x2b0e(%rip),%rcx
cmp    (%rcx),%rax
jne    14b0 <remove_dir+0x2e0>
mov    %r12,%rdi
cmpl   $0x63,-0x2c(%rbp)
jbe    14dd <remove_dir+0x30d>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rsi
xor    %eax,%eax
call   10a0 <exitf@plt>
add    $0x1,%ebx
mov    %r15,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <debug@plt>
mov    %r13,%rdi
call   1060 <rmdir@plt>
test   %rax,%rax
jne    1430 <remove_dir+0x260>
jmp    14fb <remove_dir+0x32b>
addl   $0x1,-0x2c(%rbp)
lea    0xb56(%rip),%rbx
jmp    11f9 <remove_dir+0x29>
lea    0xb6f(%rip),%rdi
xor    %eax,%eax
call   1070 <debug@plt>
lea    -0x28(%rbp),%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 }
@errno = global i64 0
@EMFILE = global i64 0
@.str = [42 x i8] c"opendir(%s) failed due to NOFILE, exiting\00"
@.str.1 = [19 x i8] c"opendir(%s) failed\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@FILENAME_MAX = global i32 0
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [17 x i8] c"lstat(%s) failed\00"
@.str.6 = [12 x i8] c"unlink(%s)\0A\00"
@EROFS = global i64 0
@.str.7 = [16 x i8] c"ignoring EROFS\0A\00"
@EBUSY = global i64 0
@.str.8 = [18 x i8] c"unlink(%s) failed\00"
@.str.9 = [12 x i8] c"umount(%s)\0A\00"
@MNT_DETACH = global i32 0
@.str.10 = [18 x i8] c"umount(%s) failed\00"
@.str.11 = [11 x i8] c"rmdir(%s)\0A\00"
@ENOTEMPTY = global i64 0
@.str.12 = [17 x i8] c"rmdir(%s) failed\00"
define void @remove_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
br label %12
12: ; preds = %143, %1
%13 = load i8*, i8** %2
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %3
%15 = load i32*, i32** %3
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %26
17: ; preds = %12
%18 = load i64, i64* @errno
%19 = load i64, i64* @EMFILE
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i8*, ...) @exitf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = load i8*, i8** %2
%25 = call i32 (i8*, ...) @exitf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %23, %12
br label %27
27: ; preds = %101, %98, %43, %26
%28 = load i32*, i32** %3
%29 = call %struct.dirent* @readdir(i32* %28)
store %struct.dirent* %29, %struct.dirent** %4
%30 = icmp ne %struct.dirent* %29, null
br i1 %30, label %31, label %102
31: ; preds = %27
%32 = load %struct.dirent*, %struct.dirent** %4
%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 ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %31
%38 = load %struct.dirent*, %struct.dirent** %4
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i64 @strcmp(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %37, %31
br label %27
44: ; preds = %37
%45 = load i32, i32* @FILENAME_MAX
%46 = zext i32 %45 to i64
%47 = call i8* @llvm.stacksave()
store i8* %47, i8** %6
%48 = alloca i8, i64 %46
store i64 %46, i64* %7
%49 = trunc i64 %46 to i32
%50 = load i8*, i8** %2
%51 = load %struct.dirent*, %struct.dirent** %4
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 0
%53 = load i8*, i8** %52
%54 = call i32 @snprintf(i8* %48, i32 %49, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %50, i8* %53)
%55 = call i64 @lstat(i8* %48, %struct.stat* %8)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %44
%58 = call i32 (i8*, ...) @exitf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %48)
br label %59
59: ; preds = %57, %44
%60 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i64 @S_ISDIR(i32 %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %59
store i32 3, i32* %9
br label %98
65: ; preds = %59
store i32 0, i32* %10
br label %66
66: ; preds = %94, %65
%67 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* %48)
%68 = call i64 @unlink(i8* %48)
%69 = icmp eq i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %66
br label %97
71: ; preds = %66
%72 = load i64, i64* @errno
%73 = load i64, i64* @EROFS
%74 = icmp eq i64 %72, %73
br i1 %74, label %75, label %77
75: ; preds = %71
%76 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %97
77: ; preds = %71
%78 = load i64, i64* @errno
%79 = load i64, i64* @EBUSY
%80 = icmp ne i64 %78, %79
br i1 %80, label %84, label %81
81: ; preds = %77
%82 = load i32, i32* %10
%83 = icmp sgt i32 %82, 100
br i1 %83, label %84, label %86
84: ; preds = %81, %77
%85 = call i32 (i8*, ...) @exitf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* %48)
br label %86
86: ; preds = %84, %81
%87 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i8* %48)
%88 = load i32, i32* @MNT_DETACH
%89 = call i64 @umount2(i8* %48, i32 %88)
%90 = icmp ne i64 %89, 0
br i1 %90, label %91, label %93
91: ; preds = %86
%92 = call i32 (i8*, ...) @exitf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i8* %48)
br label %93
93: ; preds = %91, %86
br label %94
94: ; preds = %93
%95 = load i32, i32* %10
%96 = add nsw i32 %95, 1
store i32 %96, i32* %10
br label %66
97: ; preds = %75, %70
store i32 0, i32* %9
br label %98
98: ; preds = %97, %64
%99 = load i8*, i8** %6
%100 = load i32, i32* %9
switch i32 %100, label %155 [
i32 0, label %101
i32 3, label %27
]
101: ; preds = %98
br label %27
102: ; preds = %27
%103 = load i32*, i32** %3
%104 = call i32 @closedir(i32* %103)
store i32 0, i32* %11
br label %105
105: ; preds = %151, %102
%106 = load i8*, i8** %2
%107 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i8* %106)
%108 = load i8*, i8** %2
%109 = call i64 @rmdir(i8* %108)
%110 = icmp eq i64 %109, 0
br i1 %110, label %111, label %112
111: ; preds = %105
br label %154
112: ; preds = %105
%113 = load i32, i32* %11
%114 = icmp slt i32 %113, 100
br i1 %114, label %115, label %148
115: ; preds = %112
%116 = load i64, i64* @errno
%117 = load i64, i64* @EROFS
%118 = icmp eq i64 %116, %117
br i1 %118, label %119, label %121
119: ; preds = %115
%120 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %154
121: ; preds = %115
%122 = load i64, i64* @errno
%123 = load i64, i64* @EBUSY
%124 = icmp eq i64 %122, %123
br i1 %124, label %125, label %136
125: ; preds = %121
%126 = load i8*, i8** %2
%127 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i8* %126)
%128 = load i8*, i8** %2
%129 = load i32, i32* @MNT_DETACH
%130 = call i64 @umount2(i8* %128, i32 %129)
%131 = icmp ne i64 %130, 0
br i1 %131, label %132, label %135
132: ; preds = %125
%133 = load i8*, i8** %2
%134 = call i32 (i8*, ...) @exitf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i8* %133)
br label %135
135: ; preds = %132, %125
br label %151
136: ; preds = %121
%137 = load i64, i64* @errno
%138 = load i64, i64* @ENOTEMPTY
%139 = icmp eq i64 %137, %138
br i1 %139, label %140, label %147
140: ; preds = %136
%141 = load i32, i32* %5
%142 = icmp slt i32 %141, 100
br i1 %142, label %143, label %146
143: ; preds = %140
%144 = load i32, i32* %5
%145 = add nsw i32 %144, 1
store i32 %145, i32* %5
br label %12
146: ; preds = %140
br label %147
147: ; preds = %146, %136
br label %148
148: ; preds = %147, %112
%149 = load i8*, i8** %2
%150 = call i32 (i8*, ...) @exitf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0), i8* %149)
br label %151
151: ; preds = %148, %135
%152 = load i32, i32* %11
%153 = add nsw i32 %152, 1
store i32 %153, i32* %11
br label %105
154: ; preds = %119, %111
ret void
155: ; preds = %98
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@errno = global i64 0
@EMFILE = global i64 0
@.str = [42 x i8] c"opendir(%s) failed due to NOFILE, exiting\00"
@.str.1 = [19 x i8] c"opendir(%s) failed\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@FILENAME_MAX = global i32 0
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [17 x i8] c"lstat(%s) failed\00"
@.str.6 = [12 x i8] c"unlink(%s)\0A\00"
@EROFS = global i64 0
@.str.7 = [16 x i8] c"ignoring EROFS\0A\00"
@EBUSY = global i64 0
@.str.8 = [18 x i8] c"unlink(%s) failed\00"
@.str.9 = [12 x i8] c"umount(%s)\0A\00"
@MNT_DETACH = global i32 0
@.str.10 = [18 x i8] c"umount(%s) failed\00"
@.str.11 = [11 x i8] c"rmdir(%s)\0A\00"
@ENOTEMPTY = global i64 0
@.str.12 = [17 x i8] c"rmdir(%s) failed\00"
define void @remove_dir(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %5
5: ; preds = %108, %1
%6 = phi i32 [ 0, %1 ], [ %109, %108 ]
%7 = call i32* @opendir(i8* %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %17
9: ; preds = %5
%10 = load i64, i64* @errno
%11 = load i64, i64* @EMFILE
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 (i8*, ...) @exitf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = call i32 (i8*, ...) @exitf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %15, %5
%18 = call %struct.dirent* @readdir(i32* %7)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %80, label %20
20: ; preds = %17, %30
%21 = phi %struct.dirent* [ %31, %30 ], [ %18, %17 ]
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %20
%27 = load i8*, i8** %22
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %20, %26, %79
%31 = call %struct.dirent* @readdir(i32* %7)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %80, label %20
33: ; preds = %26
%34 = load i32, i32* @FILENAME_MAX
%35 = zext i32 %34 to i64
%36 = call i8* @llvm.stacksave()
%37 = alloca i8, i64 %35
%38 = load i8*, i8** %22
%39 = call i32 @snprintf(i8* %37, i32 %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %38)
%40 = call i64 @lstat(i8* %37, %struct.stat* %2)
%41 = icmp eq i64 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %33
%43 = call i32 (i8*, ...) @exitf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %37)
br label %44
44: ; preds = %42, %33
%45 = load i32, i32* %4
%46 = call i64 @S_ISDIR(i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* %37)
%50 = call i64 @unlink(i8* %37)
%51 = icmp eq i64 %50, 0
br i1 %51, label %79, label %53
52: ; preds = %44
br label %79
53: ; preds = %48, %74
%54 = phi i32 [ %75, %74 ], [ 0, %48 ]
%55 = load i64, i64* @errno
%56 = load i64, i64* @EROFS
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %79
60: ; preds = %53
%61 = load i64, i64* @EBUSY
%62 = icmp ne i64 %55, %61
%63 = icmp ugt i32 %54, 100
%64 = select i1 %62, i1 true, i1 %63
br i1 %64, label %65, label %67
65: ; preds = %60
%66 = call i32 (i8*, ...) @exitf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* %37)
br label %67
67: ; preds = %60, %65
%68 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i8* %37)
%69 = load i32, i32* @MNT_DETACH
%70 = call i64 @umount2(i8* %37, i32 %69)
%71 = icmp eq i64 %70, 0
br i1 %71, label %74, label %72
72: ; preds = %67
%73 = call i32 (i8*, ...) @exitf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i8* %37)
br label %74
74: ; preds = %67, %72
%75 = add nuw nsw i32 %54, 1
%76 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* %37)
%77 = call i64 @unlink(i8* %37)
%78 = icmp eq i64 %77, 0
br i1 %78, label %79, label %53
79: ; preds = %74, %48, %58, %52
br label %30
80: ; preds = %30, %17
%81 = call i32 @closedir(i32* %7)
%82 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i8* %0)
%83 = call i64 @rmdir(i8* %0)
%84 = icmp eq i64 %83, 0
br i1 %84, label %118, label %85
85: ; preds = %80
%86 = icmp ult i32 %6, 100
br label %87
87: ; preds = %85, %113
%88 = phi i32 [ 0, %85 ], [ %114, %113 ]
%89 = icmp ult i32 %88, 100
br i1 %89, label %90, label %110
90: ; preds = %87
%91 = load i64, i64* @errno
%92 = load i64, i64* @EROFS
%93 = icmp eq i64 %91, %92
br i1 %93, label %94, label %96
94: ; preds = %90
%95 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %118
96: ; preds = %90
%97 = load i64, i64* @EBUSY
%98 = icmp eq i64 %91, %97
br i1 %98, label %99, label %104
99: ; preds = %96
%100 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i8* %0)
%101 = load i32, i32* @MNT_DETACH
%102 = call i64 @umount2(i8* %0, i32 %101)
%103 = icmp eq i64 %102, 0
br i1 %103, label %113, label %110
104: ; preds = %96
%105 = load i64, i64* @ENOTEMPTY
%106 = icmp eq i64 %91, %105
%107 = select i1 %106, i1 %86, i1 false
br i1 %107, label %108, label %110
108: ; preds = %104
%109 = add nuw nsw i32 %6, 1
br label %5
110: ; preds = %87, %104, %99
%111 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), %99 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0), %104 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0), %87 ]
%112 = call i32 (i8*, ...) @exitf(i8* %111, i8* %0)
br label %113
113: ; preds = %110, %99
%114 = add nuw nsw i32 %88, 1
%115 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i8* %0)
%116 = call i64 @rmdir(i8* %0)
%117 = icmp eq i64 %116, 0
br i1 %117, label %118, label %87
118: ; preds = %80, %113, %94
ret void
}