#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 {
  scalar_t__ st_dev;
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ path;
typedef scalar_t__ dev_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ eprintf(char *);
scalar_t__ lstat(char const *, struct stat *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ rmdir(char *);
scalar_t__ strcmp(char *, char *);
int strlcat(char *, char *, int);
int strlcpy(char *, char const *, int);
int strlen(char *);
scalar_t__ unlink(char *);
int /*<<< orphan*/ weprintf(char *, char *);
void delete_content(const char *dir, dev_t curdevice) {
  char path[PATH_MAX];
  DIR *d;
  struct stat st;
  struct dirent *dent;
  if (lstat(dir, &st) < 0 || st.st_dev != curdevice)
    return;
  if (!(d = opendir(dir)))
    return;
  while ((dent = readdir(d))) {
    if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
      continue;
    if (strlcpy(path, dir, sizeof(path)) >= sizeof(path))
      eprintf("path too long\n");
    if (path[strlen(path) - 1] != '/')
      if (strlcat(path, "/", sizeof(path)) >= sizeof(path))
        eprintf("path too long\n");
    if (strlcat(path, dent->d_name, sizeof(path)) >= sizeof(path))
      eprintf("path too long\n");
    if (lstat(path, &st) < 0)
      weprintf("lstat %s:", path);
    if (S_ISDIR(st.st_mode)) {
      delete_content(path, curdevice);
      if (rmdir(path) < 0)
        weprintf("rmdir %s:", path);
    } else {
      if (unlink(path) < 0)
        weprintf("unlink %s:", path);
    }
  }
  closedir(d);
}


x86 assembly (clang, -O0):
<delete_content>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0x0,%rax
jl     1245 <delete_content+0x65>
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1251 <delete_content+0x71>
movl   $0x1,-0x44(%rbp)
jmp    1431 <delete_content+0x251>
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1274 <delete_content+0x94>
movl   $0x1,-0x44(%rbp)
jmp    1431 <delete_content+0x251>
jmp    1279 <delete_content+0x99>
mov    -0x28(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     1421 <delete_content+0x241>
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
lea    0xd63(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
je     12ca <delete_content+0xea>
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
lea    0xd45(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
jne    12cf <delete_content+0xef>
jmp    1279 <delete_content+0x99>
mov    -0x58(%rbp),%rax
mov    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    %eax,%edx
call   10e0 <strlcpy@plt>
mov    -0x58(%rbp),%rcx
cltq
cmp    %rcx,%rax
jb     12fd <delete_content+0x11d>
lea    0xd0b(%rip),%rdi
call   10f0 <eprintf@plt>
mov    -0x50(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1355 <delete_content+0x175>
mov    -0x58(%rbp),%rax
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
lea    0xce2(%rip),%rsi
call   1040 <strlcat@plt>
mov    -0x58(%rbp),%rcx
cltq
cmp    %rcx,%rax
jb     1350 <delete_content+0x170>
lea    0xcb8(%rip),%rdi
call   10f0 <eprintf@plt>
jmp    1355 <delete_content+0x175>
mov    -0x58(%rbp),%rax
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rcx
mov    (%rcx),%rsi
mov    %eax,%edx
call   1040 <strlcat@plt>
mov    -0x58(%rbp),%rcx
cltq
cmp    %rcx,%rax
jb     1386 <delete_content+0x1a6>
lea    0xc82(%rip),%rdi
call   10f0 <eprintf@plt>
mov    -0x50(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <lstat@plt>
cmp    $0x0,%rax
jge    13ad <delete_content+0x1cd>
mov    -0x50(%rbp),%rsi
lea    0xc6c(%rip),%rdi
call   10d0 <weprintf@plt>
mov    -0x30(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%rax
je     13f4 <delete_content+0x214>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1100 <delete_content@plt>
mov    -0x50(%rbp),%rdi
call   1060 <rmdir@plt>
cmp    $0x0,%rax
jge    13ef <delete_content+0x20f>
mov    -0x50(%rbp),%rsi
lea    0xc34(%rip),%rdi
call   10d0 <weprintf@plt>
jmp    141c <delete_content+0x23c>
mov    -0x50(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    1417 <delete_content+0x237>
mov    -0x50(%rbp),%rsi
lea    0xc16(%rip),%rdi
call   10d0 <weprintf@plt>
jmp    141c <delete_content+0x23c>
jmp    1279 <delete_content+0x99>
mov    -0x28(%rbp),%rdi
call   1090 <closedir@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<delete_content>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,-0x30(%rbp)
mov    %rdi,%r15
mov    0x2df1(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x40(%rbp),%rsi
call   10a0 <lstat@plt>
test   %rax,%rax
js     1388 <delete_content+0x1a8>
mov    -0x30(%rbp),%rax
cmp    %rax,-0x40(%rbp)
jne    1388 <delete_content+0x1a8>
mov    %r15,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1388 <delete_content+0x1a8>
mov    %rax,%r13
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     1380 <delete_content+0x1a0>
mov    %rax,%r14
jmp    1274 <delete_content+0x94>
xchg   %ax,%ax
mov    %r13,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1380 <delete_content+0x1a0>
mov    (%r14),%rdi
lea    0xd83(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     1260 <delete_content+0x80>
mov    (%r14),%rdi
lea    0xd6e(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     1260 <delete_content+0x80>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   10e0 <strlcpy@plt>
cltq
cmp    %r12,%rax
jb     12bd <delete_content+0xdd>
lea    0xd4b(%rip),%rdi
call   10f0 <eprintf@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
je     12f3 <delete_content+0x113>
mov    %rbx,%rdi
lea    0xd3a(%rip),%rsi
mov    %r12d,%edx
call   1040 <strlcat@plt>
cltq
cmp    %r12,%rax
jb     12f3 <delete_content+0x113>
lea    0xd15(%rip),%rdi
call   10f0 <eprintf@plt>
mov    (%r14),%rsi
mov    %rbx,%rdi
mov    %r12d,%edx
call   1040 <strlcat@plt>
cltq
cmp    %r12,%rax
jb     1314 <delete_content+0x134>
lea    0xcf4(%rip),%rdi
call   10f0 <eprintf@plt>
mov    %rbx,%rdi
lea    -0x40(%rbp),%rsi
call   10a0 <lstat@plt>
test   %rax,%rax
jns    1334 <delete_content+0x154>
lea    0xce8(%rip),%rdi
mov    %rbx,%rsi
call   10d0 <weprintf@plt>
mov    -0x38(%rbp),%edi
call   1080 <S_ISDIR@plt>
mov    %rbx,%rdi
test   %rax,%rax
je     135e <delete_content+0x17e>
mov    -0x30(%rbp),%rsi
call   1100 <delete_content@plt>
mov    %rbx,%rdi
call   1060 <rmdir@plt>
lea    0xcc2(%rip),%rdi
jmp    136a <delete_content+0x18a>
call   1030 <unlink@plt>
lea    0xcbe(%rip),%rdi
test   %rax,%rax
jns    1260 <delete_content+0x80>
mov    %rbx,%rsi
call   10d0 <weprintf@plt>
jmp    1260 <delete_content+0x80>
mov    %r13,%rdi
call   1090 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [15 x i8] c"path too long\0A\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [10 x i8] c"lstat %s:\00"
@.str.5 = [10 x i8] c"rmdir %s:\00"
@.str.6 = [11 x i8] c"unlink %s:\00"
define void @delete_content(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = alloca %struct.dirent*
%10 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = load i8*, i8** %3
%16 = call i64 @lstat(i8* %15, %struct.stat* %8)
%17 = icmp slt i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %2
%19 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* %4
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %18, %2
store i32 1, i32* %10
br label %108
24: ; preds = %18
%25 = load i8*, i8** %3
%26 = call i32* @opendir(i8* %25)
store i32* %26, i32** %7
%27 = icmp ne i32* %26, null
br i1 %27, label %29, label %28
28: ; preds = %24
store i32 1, i32* %10
br label %108
29: ; preds = %24
br label %30
30: ; preds = %104, %46, %29
%31 = load i32*, i32** %7
%32 = call %struct.dirent* @readdir(i32* %31)
store %struct.dirent* %32, %struct.dirent** %9
%33 = icmp ne %struct.dirent* %32, null
br i1 %33, label %34, label %105
34: ; preds = %30
%35 = load %struct.dirent*, %struct.dirent** %9
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %46, label %40
40: ; preds = %34
%41 = load %struct.dirent*, %struct.dirent** %9
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = call i64 @strcmp(i8* %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 %47
46: ; preds = %40, %34
br label %30
47: ; preds = %40
%48 = load i8*, i8** %3
%49 = trunc i64 %12 to i32
%50 = call i32 @strlcpy(i8* %14, i8* %48, i32 %49)
%51 = sext i32 %50 to i64
%52 = icmp uge i64 %51, %12
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %55
55: ; preds = %53, %47
%56 = call i32 @strlen(i8* %14)
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %14, i64 %58
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp ne i32 %61, 47
br i1 %62, label %63, label %71
63: ; preds = %55
%64 = trunc i64 %12 to i32
%65 = call i32 @strlcat(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %64)
%66 = sext i32 %65 to i64
%67 = icmp uge i64 %66, %12
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = call i32 @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %70
70: ; preds = %68, %63
br label %71
71: ; preds = %70, %55
%72 = load %struct.dirent*, %struct.dirent** %9
%73 = getelementptr inbounds %struct.dirent, %struct.dirent* %72, i32 0, i32 0
%74 = load i8*, i8** %73
%75 = trunc i64 %12 to i32
%76 = call i32 @strlcat(i8* %14, i8* %74, i32 %75)
%77 = sext i32 %76 to i64
%78 = icmp uge i64 %77, %12
br i1 %78, label %79, label %81
79: ; preds = %71
%80 = call i32 @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %81
81: ; preds = %79, %71
%82 = call i64 @lstat(i8* %14, %struct.stat* %8)
%83 = icmp slt i64 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %81
%85 = call i32 @weprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %14)
br label %86
86: ; preds = %84, %81
%87 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%88 = load i32, i32* %87
%89 = call i64 @S_ISDIR(i32 %88)
%90 = icmp ne i64 %89, 0
br i1 %90, label %91, label %98
91: ; preds = %86
%92 = load i64, i64* %4
%93 = call i64 @rmdir(i8* %14)
%94 = icmp slt i64 %93, 0
br i1 %94, label %95, label %97
95: ; preds = %91
%96 = call i32 @weprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %14)
br label %97
97: ; preds = %95, %91
br label %104
98: ; preds = %86
%99 = call i64 @unlink(i8* %14)
%100 = icmp slt i64 %99, 0
br i1 %100, label %101, label %103
101: ; preds = %98
%102 = call i32 @weprintf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %14)
br label %103
103: ; preds = %101, %98
br label %104
104: ; preds = %103, %97
br label %30
105: ; preds = %30
%106 = load i32*, i32** %7
%107 = call i32 @closedir(i32* %106)
store i32 0, i32* %10
br label %108
108: ; preds = %105, %28, %23
%109 = load i8*, i8** %5
%110 = load i32, i32* %10
switch i32 %110, label %112 [
i32 0, label %111
i32 1, label %111
]
111: ; preds = %108, %108
ret void
112: ; preds = %108
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [15 x i8] c"path too long\0A\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [10 x i8] c"lstat %s:\00"
@.str.5 = [10 x i8] c"rmdir %s:\00"
@.str.6 = [11 x i8] c"unlink %s:\00"
define void @delete_content(i8* %0, i64 %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.stat* %3 to i8*
%8 = call i64 @lstat(i8* %0, %struct.stat* %3)
%9 = icmp sgt i64 %8, -1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i64, i64* %10
%12 = icmp eq i64 %11, %1
%13 = select i1 %9, i1 %12, i1 false
br i1 %13, label %14, label %81
14: ; preds = %2
%15 = tail call i32* @opendir(i8* %0)
%16 = icmp eq i32* %15, null
br i1 %16, label %81, label %17
17: ; preds = %14
%18 = call %struct.dirent* @readdir(i32* %15)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %79, label %20
20: ; preds = %17
%21 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
br label %22
22: ; preds = %20, %35
%23 = phi %struct.dirent* [ %18, %20 ], [ %36, %35 ]
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i64 0, i32 0
%25 = load i8*, i8** %24
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %22
%29 = load i8*, i8** %24
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %38
32: ; preds = %76, %73
%33 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), %73 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), %76 ]
%34 = call i32 @weprintf(i8* %33, i8* %6)
br label %35
35: ; preds = %32, %73, %76, %22, %28
%36 = call %struct.dirent* @readdir(i32* %15)
%37 = icmp eq %struct.dirent* %36, null
br i1 %37, label %79, label %22
38: ; preds = %28
%39 = call i32 @strlcpy(i8* %6, i8* %0, i32 %4)
%40 = sext i32 %39 to i64
%41 = icmp ult i64 %40, %5
br i1 %41, label %44, label %42
42: ; preds = %38
%43 = call i32 @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
%45 = call i32 @strlen(i8* %6)
%46 = add nsw i32 %45, -1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %6, i64 %47
%49 = load i8, i8* %48
%50 = icmp eq i8 %49, 47
br i1 %50, label %57, label %51
51: ; preds = %44
%52 = call i32 @strlcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %4)
%53 = sext i32 %52 to i64
%54 = icmp ult i64 %53, %5
br i1 %54, label %57, label %55
55: ; preds = %51
%56 = call i32 @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %57
57: ; preds = %51, %55, %44
%58 = load i8*, i8** %24
%59 = call i32 @strlcat(i8* %6, i8* %58, i32 %4)
%60 = sext i32 %59 to i64
%61 = icmp ult i64 %60, %5
br i1 %61, label %64, label %62
62: ; preds = %57
%63 = call i32 @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %64
64: ; preds = %62, %57
%65 = call i64 @lstat(i8* %6, %struct.stat* %3)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %64
%68 = call i32 @weprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %69
69: ; preds = %67, %64
%70 = load i32, i32* %21
%71 = call i64 @S_ISDIR(i32 %70)
%72 = icmp eq i64 %71, 0
br i1 %72, label %76, label %73
73: ; preds = %69
%74 = call i64 @rmdir(i8* %6)
%75 = icmp slt i64 %74, 0
br i1 %75, label %32, label %35
76: ; preds = %69
%77 = call i64 @unlink(i8* %6)
%78 = icmp slt i64 %77, 0
br i1 %78, label %32, label %35
79: ; preds = %35, %17
%80 = call i32 @closedir(i32* %15)
br label %81
81: ; preds = %14, %2, %79
ret void
}