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

/* Variables and functions */
scalar_t__ S_ISLNK(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int asprintf(char **, char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int fs_delete_file(char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int rmdir(char const *);
int stat(char *, struct stat *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int fs_delete_dir(const char *dir_name) {
  DIR *dir;
  struct stat st;
  struct dirent *ep;
  char *full_path;
  dir = opendir(dir_name);
  if (dir == NULL) {
    return -1;
  }
  while (ep = readdir(dir)) {
    if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) {
      continue;
    }
    if (asprintf(&full_path, "%s/%s", dir_name, ep->d_name) == -1) {
      closedir(dir);
      return -1;
    }
    if (stat(full_path, &st) == -1) {
      free(full_path);
      closedir(dir);
      return -1;
    }
    if (S_ISREG(st.st_mode) != 0) {
      if (fs_delete_file(full_path) == -1) {
        free(full_path);
        closedir(dir);
        return -1;
      }
    } else if (S_ISLNK(st.st_mode) == 0) {
      if (fs_delete_dir(full_path) == -1) {
        free(full_path);
        closedir(dir);
        return -1;
      }
    }
    free(full_path);
  }
  closedir(dir);
  if (rmdir(dir_name) == -1) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fs_delete_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f0 <fs_delete_dir+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <fs_delete_dir+0x1c4>
jmp    11f5 <fs_delete_dir+0x35>
mov    -0x18(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1356 <fs_delete_dir+0x196>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1244 <fs_delete_dir+0x84>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1249 <fs_delete_dir+0x89>
jmp    11f5 <fs_delete_dir+0x35>
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
lea    -0x30(%rbp),%rdi
lea    0xda5(%rip),%rsi
call   10e0 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    1281 <fs_delete_dir+0xc1>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <fs_delete_dir+0x1c4>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0xffffffff,%eax
jne    12b5 <fs_delete_dir+0xf5>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <fs_delete_dir+0x1c4>
mov    -0x20(%rbp),%edi
call   10b0 <S_ISREG@plt>
cmp    $0x0,%rax
je     12fc <fs_delete_dir+0x13c>
mov    -0x30(%rbp),%rdi
call   1040 <fs_delete_file@plt>
cmp    $0xffffffff,%eax
jne    12f7 <fs_delete_dir+0x137>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <fs_delete_dir+0x1c4>
jmp    1348 <fs_delete_dir+0x188>
mov    -0x20(%rbp),%edi
call   1050 <S_ISLNK@plt>
cmp    $0x0,%rax
jne    1343 <fs_delete_dir+0x183>
mov    -0x30(%rbp),%rdi
call   10d0 <fs_delete_dir@plt>
cmp    $0xffffffff,%eax
jne    133e <fs_delete_dir+0x17e>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <fs_delete_dir+0x1c4>
jmp    1343 <fs_delete_dir+0x183>
jmp    1348 <fs_delete_dir+0x188>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    11f5 <fs_delete_dir+0x35>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x10(%rbp),%rdi
call   1070 <rmdir@plt>
cmp    $0xffffffff,%eax
jne    137d <fs_delete_dir+0x1bd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <fs_delete_dir+0x1c4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_delete_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r12
call   1060 <opendir@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     12f7 <fs_delete_dir+0x137>
mov    %rax,%rbx
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12da <fs_delete_dir+0x11a>
mov    %rax,%r13
lea    0xdfe(%rip),%r15
jmp    1233 <fs_delete_dir+0x73>
mov    %ebp,%edi
call   1050 <S_ISLNK@plt>
test   %rax,%rax
je     12b3 <fs_delete_dir+0xf3>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12da <fs_delete_dir+0x11a>
mov    0x0(%r13),%edi
mov    %r15,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     121f <fs_delete_dir+0x5f>
mov    0x0(%r13),%edi
lea    0xdb1(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     121f <fs_delete_dir+0x5f>
mov    0x0(%r13),%ecx
lea    0x8(%rsp),%rdi
lea    0xd9a(%rip),%rsi
mov    %r12,%rdx
call   10e0 <asprintf@plt>
cmp    $0xffffffff,%eax
je     12d0 <fs_delete_dir+0x110>
mov    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   10a0 <stat@plt>
cmp    $0xffffffff,%eax
je     12c6 <fs_delete_dir+0x106>
mov    0x10(%rsp),%ebp
mov    %ebp,%edi
call   10b0 <S_ISREG@plt>
test   %rax,%rax
je     1205 <fs_delete_dir+0x45>
mov    0x8(%rsp),%rdi
call   1040 <fs_delete_file@plt>
cmp    $0xffffffff,%eax
jne    1215 <fs_delete_dir+0x55>
jmp    12c6 <fs_delete_dir+0x106>
mov    0x8(%rsp),%rdi
call   10d0 <fs_delete_dir@plt>
cmp    $0xffffffff,%eax
jne    1215 <fs_delete_dir+0x55>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1080 <closedir@plt>
jmp    12f7 <fs_delete_dir+0x137>
mov    %rbx,%rdi
call   1080 <closedir@plt>
mov    %r12,%rdi
call   1070 <rmdir@plt>
xor    %r14d,%r14d
cmp    $0xffffffff,%eax
sete   %r14b
neg    %r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @fs_delete_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.stat
%6 = alloca %struct.dirent*
%7 = alloca i8*
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 %92
13: ; preds = %1
br label %14
14: ; preds = %81, %30, %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 %84
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %6
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @strcmp(i32 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %18
%25 = load %struct.dirent*, %struct.dirent** %6
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i64 @strcmp(i32 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24, %18
br label %14
31: ; preds = %24
%32 = load i8*, i8** %3
%33 = load %struct.dirent*, %struct.dirent** %6
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %32, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = load i32*, i32** %4
%40 = call i32 @closedir(i32* %39)
store i32 -1, i32* %2
br label %92
41: ; preds = %31
%42 = load i8*, i8** %7
%43 = call i32 @stat(i8* %42, %struct.stat* %5)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = load i8*, i8** %7
%47 = call i32 @free(i8* %46)
%48 = load i32*, i32** %4
%49 = call i32 @closedir(i32* %48)
store i32 -1, i32* %2
br label %92
50: ; preds = %41
%51 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i64 @S_ISREG(i32 %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %65
55: ; preds = %50
%56 = load i8*, i8** %7
%57 = call i32 @fs_delete_file(i8* %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %64
59: ; preds = %55
%60 = load i8*, i8** %7
%61 = call i32 @free(i8* %60)
%62 = load i32*, i32** %4
%63 = call i32 @closedir(i32* %62)
store i32 -1, i32* %2
br label %92
64: ; preds = %55
br label %81
65: ; preds = %50
%66 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i64 @S_ISLNK(i32 %67)
%69 = icmp eq i64 %68, 0
br i1 %69, label %70, label %80
70: ; preds = %65
%71 = load i8*, i8** %7
%72 = call i32 @fs_delete_dir(i8* %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %79
74: ; preds = %70
%75 = load i8*, i8** %7
%76 = call i32 @free(i8* %75)
%77 = load i32*, i32** %4
%78 = call i32 @closedir(i32* %77)
store i32 -1, i32* %2
br label %92
79: ; preds = %70
br label %80
80: ; preds = %79, %65
br label %81
81: ; preds = %80, %64
%82 = load i8*, i8** %7
%83 = call i32 @free(i8* %82)
br label %14
84: ; preds = %14
%85 = load i32*, i32** %4
%86 = call i32 @closedir(i32* %85)
%87 = load i8*, i8** %3
%88 = call i32 @rmdir(i8* %87)
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %91
90: ; preds = %84
store i32 -1, i32* %2
br label %92
91: ; preds = %84
store i32 0, i32* %2
br label %92
92: ; preds = %91, %90, %74, %59, %45, %38, %12
%93 = load i32, i32* %2
ret i32 %93
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @fs_delete_dir(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca i8*
%4 = bitcast %struct.stat* %2 to i8*
%5 = bitcast i8** %3 to i8*
%6 = tail call i32* @opendir(i8* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %71, label %8
8: ; preds = %1
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %66, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %13
13: ; preds = %11, %23
%14 = phi %struct.dirent* [ %9, %11 ], [ %24, %23 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i64 @strcmp(i32 %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %13
%20 = load i32, i32* %15
%21 = call i64 @strcmp(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %13, %19, %63
%24 = call %struct.dirent* @readdir(i32* %6)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %66, label %13
26: ; preds = %19
%27 = load i32, i32* %15
%28 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @closedir(i32* %6)
br label %71
32: ; preds = %26
%33 = load i8*, i8** %3
%34 = call i32 @stat(i8* %33, %struct.stat* %2)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i8*, i8** %3
%38 = call i32 @free(i8* %37)
%39 = call i32 @closedir(i32* %6)
br label %71
40: ; preds = %32
%41 = load i32, i32* %12
%42 = call i64 @S_ISREG(i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %52, label %44
44: ; preds = %40
%45 = load i8*, i8** %3
%46 = call i32 @fs_delete_file(i8* %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %63
48: ; preds = %44
%49 = load i8*, i8** %3
%50 = call i32 @free(i8* %49)
%51 = call i32 @closedir(i32* %6)
br label %71
52: ; preds = %40
%53 = call i64 @S_ISLNK(i32 %41)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %63
55: ; preds = %52
%56 = load i8*, i8** %3
%57 = call i32 @fs_delete_dir(i8* %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %63
59: ; preds = %55
%60 = load i8*, i8** %3
%61 = call i32 @free(i8* %60)
%62 = call i32 @closedir(i32* %6)
br label %71
63: ; preds = %52, %55, %44
%64 = load i8*, i8** %3
%65 = call i32 @free(i8* %64)
br label %23
66: ; preds = %23, %8
%67 = call i32 @closedir(i32* %6)
%68 = call i32 @rmdir(i8* %0)
%69 = icmp eq i32 %68, -1
%70 = sext i1 %69 to i32
br label %71
71: ; preds = %66, %1, %59, %48, %36, %30
%72 = phi i32 [ -1, %30 ], [ -1, %36 ], [ -1, %48 ], [ -1, %59 ], [ -1, %1 ], [ %70, %66 ]
ret i32 %72
}