#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;
};

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, int);
int recursiveDeleteContents(char const *);
int rmdir(char const *);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int unlink(char const *);
int recursiveDelete(const char *path) {
  int ret;
  struct stat stBuf;
  ret = stat(path, &stBuf);
  if (ret != 0) {
    ret = -errno;
    fprintf(stderr,
            "recursiveDelete(%s): stat failed with "
            "error %d\n",
            path, ret);
    return ret;
  }
  if (S_ISDIR(stBuf.st_mode)) {
    ret = recursiveDeleteContents(path);
    if (ret)
      return ret;
    ret = rmdir(path);
    if (ret) {
      ret = errno;
      fprintf(stderr, "recursiveDelete(%s): rmdir failed with error %d\n", path,
              ret);
      return ret;
    }
  } else {
    ret = unlink(path);
    if (ret) {
      ret = -errno;
      fprintf(stderr,
              "recursiveDelete(%s): unlink failed with "
              "error %d\n",
              path, ret);
      return ret;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<recursiveDelete>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11bb <recursiveDelete+0x5b>
xor    %eax,%eax
mov    0x2e49(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe55(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1293 <recursiveDelete+0x133>
mov    -0x18(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     123c <recursiveDelete+0xdc>
mov    -0x10(%rbp),%rdi
call   1040 <recursiveDeleteContents@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11ee <recursiveDelete+0x8e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1293 <recursiveDelete+0x133>
mov    -0x10(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1237 <recursiveDelete+0xd7>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe09(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1293 <recursiveDelete+0x133>
jmp    128c <recursiveDelete+0x12c>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1287 <recursiveDelete+0x127>
xor    %eax,%eax
mov    0x2d7d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xdea(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1293 <recursiveDelete+0x133>
jmp    128c <recursiveDelete+0x12c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recursiveDelete>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1080 <stat@plt>
test   %eax,%eax
je     11a0 <recursiveDelete+0x40>
mov    0x2e5f(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1070 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
mov    %rbx,%rdi
test   %rax,%rax
je     11e2 <recursiveDelete+0x82>
call   1040 <recursiveDeleteContents@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1197 <recursiveDelete+0x37>
mov    %rbx,%rdi
call   1050 <rmdir@plt>
test   %eax,%eax
je     1208 <recursiveDelete+0xa8>
mov    0x2e0a(%rip),%rax
mov    (%rax),%ebp
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
jmp    118d <recursiveDelete+0x2d>
call   1030 <unlink@plt>
test   %eax,%eax
je     1208 <recursiveDelete+0xa8>
mov    0x2de6(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
jmp    118d <recursiveDelete+0x2d>
xor    %ebp,%ebp
jmp    1197 <recursiveDelete+0x37>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@stderr = global i32 0
@.str = [48 x i8] c"recursiveDelete(%s): stat failed with error %d\0A\00"
@.str.1 = [49 x i8] c"recursiveDelete(%s): rmdir failed with error %d\0A\00"
@.str.2 = [50 x i8] c"recursiveDelete(%s): unlink failed with error %d\0A\00"
define i32 @recursiveDelete(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @stat(i8* %6, %struct.stat* %5)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
store i32 %12, i32* %4
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = load i32, i32* %4
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15)
%17 = load i32, i32* %4
store i32 %17, i32* %2
br label %58
18: ; preds = %1
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @S_ISDIR(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %43
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = call i32 @recursiveDeleteContents(i8* %24)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = load i32, i32* %4
store i32 %29, i32* %2
br label %58
30: ; preds = %23
%31 = load i8*, i8** %3
%32 = call i32 @rmdir(i8* %31)
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %30
%36 = load i32, i32* @errno
store i32 %36, i32* %4
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %3
%39 = load i32, i32* %4
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %38, i32 %39)
%41 = load i32, i32* %4
store i32 %41, i32* %2
br label %58
42: ; preds = %30
br label %57
43: ; preds = %18
%44 = load i8*, i8** %3
%45 = call i32 @unlink(i8* %44)
store i32 %45, i32* %4
%46 = load i32, i32* %4
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %56
48: ; preds = %43
%49 = load i32, i32* @errno
%50 = sub nsw i32 0, %49
store i32 %50, i32* %4
%51 = load i32, i32* @stderr
%52 = load i8*, i8** %3
%53 = load i32, i32* %4
%54 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %52, i32 %53)
%55 = load i32, i32* %4
store i32 %55, i32* %2
br label %58
56: ; preds = %43
br label %57
57: ; preds = %56, %42
store i32 0, i32* %2
br label %58
58: ; preds = %57, %48, %35, %28, %10
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@stderr = global i32 0
@.str = [48 x i8] c"recursiveDelete(%s): stat failed with error %d\0A\00"
@.str.1 = [49 x i8] c"recursiveDelete(%s): rmdir failed with error %d\0A\00"
@.str.2 = [50 x i8] c"recursiveDelete(%s): unlink failed with error %d\0A\00"
define i32 @recursiveDelete(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = sub nsw i32 0, %7
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %8)
br label %34
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i64 @S_ISDIR(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %11
%17 = tail call i32 @recursiveDeleteContents(i8* %0)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %34
19: ; preds = %16
%20 = tail call i32 @rmdir(i8* %0)
%21 = icmp eq i32 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %19
%23 = load i32, i32* @errno
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %23)
br label %34
26: ; preds = %11
%27 = tail call i32 @unlink(i8* %0)
%28 = icmp eq i32 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = load i32, i32* @errno
%31 = sub nsw i32 0, %30
%32 = load i32, i32* @stderr
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %31)
br label %34
34: ; preds = %19, %26, %16, %29, %22, %6
%35 = phi i32 [ %8, %6 ], [ %23, %22 ], [ %31, %29 ], [ %17, %16 ], [ 0, %26 ], [ 0, %19 ]
ret i32 %35
}