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

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ PATH_MAX;
int S_IFDIR;
int S_IFMT;
scalar_t__ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ e(int);
scalar_t__ errno;
scalar_t__ lstat(char const *, struct stat *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ pathncat(char *, int, char const *, int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ rmdir(char const *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
scalar_t__ unlink(char const *);
void cleanup(const char *path) {
  DIR *dir;
  struct dirent *dirent;
  char pathsub[PATH_MAX + 1];
  struct stat statbuf;
  if (lstat(path, &statbuf) < 0) {
    if (errno != ENOENT)
      (e(292));
    return;
  }
  if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
    if (unlink(path) < 0)
      (e(299));
    return;
  }
  if (!(dir = opendir(path))) {
    (e(306));
    return;
  }
  while (dirent = readdir(dir)) {
    if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
      continue;
    if (!pathncat(pathsub, sizeof(pathsub), path, dirent->d_name)) {
      (e(319));
      continue;
    }
    cleanup(pathsub);
  }
  if (closedir(dir) < 0)
    (e(326));
  if (rmdir(path) < 0)
    (e(329));
}


x86 assembly (clang, -O0):
<cleanup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <lstat@plt>
cmp    $0x0,%rax
jge    1226 <cleanup+0x86>
mov    0x2dd3(%rip),%rax
mov    (%rax),%rax
mov    0x2db9(%rip),%rcx
cmp    (%rcx),%rax
je     121a <cleanup+0x7a>
mov    $0x124,%edi
call   10a0 <e@plt>
movl   $0x1,-0x34(%rbp)
jmp    1370 <cleanup+0x1d0>
mov    -0x30(%rbp),%eax
mov    0x2da8(%rip),%rcx
and    (%rcx),%eax
mov    0x2d7f(%rip),%rcx
cmp    (%rcx),%eax
je     126a <cleanup+0xca>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    125e <cleanup+0xbe>
mov    $0x12b,%edi
call   10a0 <e@plt>
movl   $0x1,-0x34(%rbp)
jmp    1370 <cleanup+0x1d0>
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1297 <cleanup+0xf7>
mov    $0x132,%edi
call   10a0 <e@plt>
movl   $0x1,-0x34(%rbp)
jmp    1370 <cleanup+0x1d0>
jmp    129c <cleanup+0xfc>
mov    -0x10(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     132f <cleanup+0x18f>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xd41(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12eb <cleanup+0x14b>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xd24(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12f0 <cleanup+0x150>
jmp    129c <cleanup+0xfc>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
call   10c0 <pathncat@plt>
cmp    $0x0,%eax
jne    1321 <cleanup+0x181>
mov    $0x13f,%edi
call   10a0 <e@plt>
jmp    129c <cleanup+0xfc>
mov    -0x40(%rbp),%rdi
call   10b0 <cleanup@plt>
jmp    129c <cleanup+0xfc>
mov    -0x10(%rbp),%rdi
call   1060 <closedir@plt>
cmp    $0x0,%rax
jge    134c <cleanup+0x1ac>
mov    $0x146,%edi
call   10a0 <e@plt>
mov    -0x8(%rbp),%rdi
call   1050 <rmdir@plt>
cmp    $0x0,%rax
jge    1369 <cleanup+0x1c9>
mov    $0x149,%edi
call   10a0 <e@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cleanup>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e38(%rip),%rax
mov    (%rax),%r15d
add    $0x1,%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x30(%rbp),%rsi
call   1070 <lstat@plt>
test   %rax,%rax
js     1294 <cleanup+0xf4>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
and    -0x30(%rbp),%eax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%eax
jne    12b1 <cleanup+0x111>
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1302 <cleanup+0x162>
mov    %rax,%r12
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12c5 <cleanup+0x125>
mov    %rax,%r13
jmp    1248 <cleanup+0xa8>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10b0 <cleanup@plt>
mov    %r12,%rdi
call   1090 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12c5 <cleanup+0x125>
mov    0x0(%r13),%edi
lea    0xdae(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1238 <cleanup+0x98>
mov    0x0(%r13),%edi
lea    0xd98(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1238 <cleanup+0x98>
mov    0x0(%r13),%ecx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rdx
call   10c0 <pathncat@plt>
test   %eax,%eax
jne    1230 <cleanup+0x90>
mov    $0x13f,%edi
call   10a0 <e@plt>
jmp    1238 <cleanup+0x98>
mov    0x2d35(%rip),%rax
mov    (%rax),%rax
mov    0x2d1b(%rip),%rcx
cmp    (%rcx),%rax
je     12f3 <cleanup+0x153>
mov    $0x124,%edi
jmp    1307 <cleanup+0x167>
mov    %r14,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jns    12f3 <cleanup+0x153>
mov    $0x12b,%edi
jmp    1307 <cleanup+0x167>
mov    %r12,%rdi
call   1060 <closedir@plt>
test   %rax,%rax
jns    12dc <cleanup+0x13c>
mov    $0x146,%edi
call   10a0 <e@plt>
mov    %r14,%rdi
call   1050 <rmdir@plt>
test   %rax,%rax
jns    12f3 <cleanup+0x153>
mov    $0x149,%edi
call   10a0 <e@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x132,%edi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <e@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
define void @cleanup(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.stat
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i32, i32* @PATH_MAX
%10 = add nsw i32 %9, 1
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %5
%13 = alloca i8, i64 %11
store i64 %11, i64* %6
%14 = load i8*, i8** %2
%15 = call i64 @lstat(i8* %14, %struct.stat* %7)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %1
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @e(i32 292)
br label %23
23: ; preds = %21, %17
store i32 1, i32* %8
br label %86
24: ; preds = %1
%25 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load i32, i32* @S_IFMT
%28 = and i32 %26, %27
%29 = load i32, i32* @S_IFDIR
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %38
31: ; preds = %24
%32 = load i8*, i8** %2
%33 = call i64 @unlink(i8* %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @e(i32 299)
br label %37
37: ; preds = %35, %31
store i32 1, i32* %8
br label %86
38: ; preds = %24
%39 = load i8*, i8** %2
%40 = call i32* @opendir(i8* %39)
store i32* %40, i32** %3
%41 = icmp ne i32* %40, null
br i1 %41, label %44, label %42
42: ; preds = %38
%43 = call i32 @e(i32 306)
store i32 1, i32* %8
br label %86
44: ; preds = %38
br label %45
45: ; preds = %72, %70, %61, %44
%46 = load i32*, i32** %3
%47 = call %struct.dirent* @readdir(i32* %46)
store %struct.dirent* %47, %struct.dirent** %4
%48 = icmp ne %struct.dirent* %47, null
br i1 %48, label %49, label %73
49: ; preds = %45
%50 = load %struct.dirent*, %struct.dirent** %4
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i64 @strcmp(i32 %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%54 = icmp eq i64 %53, 0
br i1 %54, label %61, label %55
55: ; preds = %49
%56 = load %struct.dirent*, %struct.dirent** %4
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i64 @strcmp(i32 %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55, %49
br label %45
62: ; preds = %55
%63 = trunc i64 %11 to i32
%64 = load i8*, i8** %2
%65 = load %struct.dirent*, %struct.dirent** %4
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i32 @pathncat(i8* %13, i32 %63, i8* %64, i32 %67)
%69 = icmp ne i32 %68, 0
br i1 %69, label %72, label %70
70: ; preds = %62
%71 = call i32 @e(i32 319)
br label %45
72: ; preds = %62
br label %45
73: ; preds = %45
%74 = load i32*, i32** %3
%75 = call i64 @closedir(i32* %74)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %73
%78 = call i32 @e(i32 326)
br label %79
79: ; preds = %77, %73
%80 = load i8*, i8** %2
%81 = call i64 @rmdir(i8* %80)
%82 = icmp slt i64 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %79
%84 = call i32 @e(i32 329)
br label %85
85: ; preds = %83, %79
store i32 0, i32* %8
br label %86
86: ; preds = %85, %42, %37, %23
%87 = load i8*, i8** %5
%88 = load i32, i32* %8
switch i32 %88, label %90 [
i32 0, label %89
i32 1, label %89
]
89: ; preds = %86, %86
ret void
90: ; preds = %86
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
define void @cleanup(i8* %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @PATH_MAX
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.stat* %2 to i8*
%8 = call i64 @lstat(i8* %0, %struct.stat* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %66, label %14
14: ; preds = %10
%15 = tail call i32 @e(i32 292)
br label %66
16: ; preds = %1
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @S_IFMT
%20 = and i32 %19, %18
%21 = load i32, i32* @S_IFDIR
%22 = icmp eq i32 %20, %21
br i1 %22, label %28, label %23
23: ; preds = %16
%24 = tail call i64 @unlink(i8* %0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %66
26: ; preds = %23
%27 = tail call i32 @e(i32 299)
br label %66
28: ; preds = %16
%29 = tail call i32* @opendir(i8* %0)
%30 = icmp eq i32* %29, null
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = call %struct.dirent* @readdir(i32* %29)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %56, label %36
34: ; preds = %28
%35 = tail call i32 @e(i32 306)
br label %66
36: ; preds = %31, %46
%37 = phi %struct.dirent* [ %47, %46 ], [ %32, %31 ]
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i64 0, i32 0
%39 = load i32, i32* %38
%40 = call i64 @strcmp(i32 %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %36
%43 = load i32, i32* %38
%44 = call i64 @strcmp(i32 %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %36, %42, %53, %55
%47 = call %struct.dirent* @readdir(i32* %29)
%48 = icmp eq %struct.dirent* %47, null
br i1 %48, label %56, label %36
49: ; preds = %42
%50 = load i32, i32* %38
%51 = call i32 @pathncat(i8* %6, i32 %4, i8* %0, i32 %50)
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @e(i32 319)
br label %46
55: ; preds = %49
br label %46
56: ; preds = %46, %31
%57 = call i64 @closedir(i32* %29)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 @e(i32 326)
br label %61
61: ; preds = %59, %56
%62 = call i64 @rmdir(i8* %0)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 @e(i32 329)
br label %66
66: ; preds = %61, %64, %23, %26, %10, %14, %34
ret void
}