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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
char *calloc(size_t, int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char const *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rmdir(char const *);
int /*<<< orphan*/ rmtree(char *);
int /*<<< orphan*/ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcmp(char const *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
int /*<<< orphan*/ unlink(char *);
void fspath_rmtree(const char path[]) {
  size_t path_len;
  char *full_path;
  DIR *dir;
  struct stat stat_path, stat_entry;
  struct dirent *entry;
  stat(path, &stat_path);
  if (S_ISDIR(stat_path.st_mode) == 0) {
    fprintf(stderr, "%s: %s\n", "Is not directory", path);
    exit(-1);
  }
  if ((dir = opendir(path)) == NULL) {
    fprintf(stderr, "%s: %s\n", "Can`t open directory", path);
    exit(-1);
  }
  path_len = strlen(path);
  while ((entry = readdir(dir)) != NULL) {
    if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
      continue;
    full_path = calloc(path_len + strlen(entry->d_name) + 1, sizeof(char));
    strcpy(full_path, path);
    strcat(full_path, "/");
    strcat(full_path, entry->d_name);
    stat(full_path, &stat_entry);
    if (S_ISDIR(stat_entry.st_mode) != 0) {
      rmtree(full_path);
      continue;
    }
    unlink(full_path);
  }
  rmdir(path);
  closedir(dir);
}


x86 assembly (clang, -O0):
<fspath_rmtree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10d0 <stat@plt>
mov    -0x28(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    1245 <fspath_rmtree+0x55>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rcx
lea    0xdd1(%rip),%rsi
lea    0xdd2(%rip),%rdx
call   10c0 <fprintf@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1286 <fspath_rmtree+0x96>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rcx
lea    0xd90(%rip),%rsi
lea    0xda2(%rip),%rdx
call   10c0 <fprintf@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
call   10e0 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     137c <fspath_rmtree+0x18c>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xd77(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%eax
je     12e2 <fspath_rmtree+0xf2>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xd5a(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%eax
jne    12e7 <fspath_rmtree+0xf7>
jmp    1293 <fspath_rmtree+0xa3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
call   1070 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   10a0 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xd05(%rip),%rsi
call   1100 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
call   1100 <strcat@plt>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10d0 <stat@plt>
mov    -0x30(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%rax
je     136e <fspath_rmtree+0x17e>
mov    -0x18(%rbp),%rdi
call   10f0 <rmtree@plt>
jmp    1293 <fspath_rmtree+0xa3>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
jmp    1293 <fspath_rmtree+0xa3>
mov    -0x8(%rbp),%rdi
call   1060 <rmdir@plt>
mov    -0x20(%rbp),%rdi
call   1090 <closedir@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fspath_rmtree>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   10d0 <stat@plt>
mov    0x8(%rsp),%edi
call   1080 <S_ISDIR@plt>
test   %rax,%rax
je     1339 <fspath_rmtree+0x149>
mov    %rbx,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1352 <fspath_rmtree+0x162>
mov    %rax,%r14
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     1319 <fspath_rmtree+0x129>
mov    %rax,%rbp
add    $0x1,%r15
lea    0xdcf(%rip),%r12
jmp    1289 <fspath_rmtree+0x99>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   10f0 <rmtree@plt>
mov    %r14,%rdi
call   10e0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1319 <fspath_rmtree+0x129>
mov    0x0(%rbp),%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
je     1275 <fspath_rmtree+0x85>
mov    %rbx,%rdi
lea    0xd88(%rip),%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
je     1275 <fspath_rmtree+0x85>
mov    %rbx,%rdi
call   1070 <strlen@plt>
lea    (%rax,%r15,1),%rdi
mov    $0x1,%esi
call   10a0 <calloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    0x10(%rsp),%rsi
call   1040 <strcpy@plt>
mov    %r13,%rdi
lea    0xd52(%rip),%rsi
call   1100 <strcat@plt>
mov    0x0(%rbp),%rsi
mov    %r13,%rdi
call   1100 <strcat@plt>
mov    %r13,%rdi
mov    %rsp,%rsi
call   10d0 <stat@plt>
mov    (%rsp),%edi
call   1080 <S_ISDIR@plt>
mov    %r13,%rdi
test   %rax,%rax
jne    1270 <fspath_rmtree+0x80>
call   1030 <unlink@plt>
jmp    1275 <fspath_rmtree+0x85>
mov    0x10(%rsp),%rdi
call   1060 <rmdir@plt>
mov    %r14,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <closedir@plt>
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
lea    0xcb7(%rip),%rsi
lea    0xcb8(%rip),%rdx
jmp    1369 <fspath_rmtree+0x179>
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
lea    0xc9e(%rip),%rsi
lea    0xcb0(%rip),%rdx
mov    %rbx,%rcx
call   10c0 <fprintf@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [8 x i8] c"%s: %s\0A\00"
@.str.1 = [17 x i8] c"Is not directory\00"
@.str.2 = [21 x i8] c"Can`t open directory\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [2 x i8] c"/\00"
define void @fspath_rmtree(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.stat
%7 = alloca %struct.stat
%8 = alloca %struct.dirent*
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 @stat(i8* %9, %struct.stat* %6)
%11 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i64 @S_ISDIR(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %2
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %1
%21 = load i8*, i8** %2
%22 = call i32* @opendir(i8* %21)
store i32* %22, i32** %5
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %2
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %20
%30 = load i8*, i8** %2
%31 = call i64 @strlen(i8* %30)
store i64 %31, i64* %3
br label %32
32: ; preds = %77, %74, %48, %29
%33 = load i32*, i32** %5
%34 = call %struct.dirent* @readdir(i32* %33)
store %struct.dirent* %34, %struct.dirent** %8
%35 = icmp ne %struct.dirent* %34, null
br i1 %35, label %36, label %80
36: ; preds = %32
%37 = load %struct.dirent*, %struct.dirent** %8
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 @strcmp(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %36
%43 = load %struct.dirent*, %struct.dirent** %8
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i32 @strcmp(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %42, %36
br label %32
49: ; preds = %42
%50 = load i64, i64* %3
%51 = load %struct.dirent*, %struct.dirent** %8
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 0
%53 = load i8*, i8** %52
%54 = call i64 @strlen(i8* %53)
%55 = add i64 %50, %54
%56 = add i64 %55, 1
%57 = call i8* @calloc(i64 %56, i32 1)
store i8* %57, i8** %4
%58 = load i8*, i8** %4
%59 = load i8*, i8** %2
%60 = call i32 @strcpy(i8* %58, i8* %59)
%61 = load i8*, i8** %4
%62 = call i32 @strcat(i8* %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%63 = load i8*, i8** %4
%64 = load %struct.dirent*, %struct.dirent** %8
%65 = getelementptr inbounds %struct.dirent, %struct.dirent* %64, i32 0, i32 0
%66 = load i8*, i8** %65
%67 = call i32 @strcat(i8* %63, i8* %66)
%68 = load i8*, i8** %4
%69 = call i32 @stat(i8* %68, %struct.stat* %7)
%70 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%71 = load i32, i32* %70
%72 = call i64 @S_ISDIR(i32 %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %49
%75 = load i8*, i8** %4
%76 = call i32 @rmtree(i8* %75)
br label %32
77: ; preds = %49
%78 = load i8*, i8** %4
%79 = call i32 @unlink(i8* %78)
br label %32
80: ; preds = %32
%81 = load i8*, i8** %2
%82 = call i32 @rmdir(i8* %81)
%83 = load i32*, i32** %5
%84 = call i32 @closedir(i32* %83)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [8 x i8] c"%s: %s\0A\00"
@.str.1 = [17 x i8] c"Is not directory\00"
@.str.2 = [21 x i8] c"Can`t open directory\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [2 x i8] c"/\00"
define void @fspath_rmtree(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %2 to i8*
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i32 @stat(i8* %0, %struct.stat* %2)
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i64 @S_ISDIR(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %1
%16 = tail call i32* @opendir(i8* %0)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%21 = tail call i32 @exit(i32 -1)
unreachable
22: ; preds = %15
%23 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%24 = tail call %struct.dirent* @readdir(i32* %16)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %57, label %26
26: ; preds = %22
%27 = add i64 %23, 1
%28 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %29
29: ; preds = %26, %38
%30 = phi %struct.dirent* [ %24, %26 ], [ %39, %38 ]
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i64 0, i32 0
%32 = load i8*, i8** %31
%33 = tail call i32 @strcmp(i8* dereferenceable(1) %32, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%34 = icmp eq i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %29
%36 = tail call i32 @strcmp(i8* dereferenceable(1) %32, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %29, %35, %53, %55
%39 = tail call %struct.dirent* @readdir(i32* %16)
%40 = icmp eq %struct.dirent* %39, null
br i1 %40, label %57, label %29
41: ; preds = %35
%42 = tail call i64 @strlen(i8* dereferenceable(1) %32)
%43 = add i64 %27, %42
%44 = tail call i8* @calloc(i64 %43, i32 1)
%45 = tail call i32 @strcpy(i8* %44, i8* %0)
%46 = tail call i32 @strcat(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%47 = load i8*, i8** %31
%48 = tail call i32 @strcat(i8* %44, i8* %47)
%49 = call i32 @stat(i8* %44, %struct.stat* %3)
%50 = load i32, i32* %28
%51 = tail call i64 @S_ISDIR(i32 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %55, label %53
53: ; preds = %41
%54 = tail call i32 @rmtree(i8* %44)
br label %38
55: ; preds = %41
%56 = tail call i32 @unlink(i8* %44)
br label %38
57: ; preds = %38, %22
%58 = tail call i32 @rmdir(i8* %0)
%59 = tail call i32 @closedir(i32* %16)
ret void
}