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

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int apkg_rmdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *, char *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ strcmp(char *, char *);
size_t strlen(char *);
int unlink(char *);
int apkg_rmdir_content(char *path) {
  DIR *d = opendir(path);
  size_t path_len = strlen(path);
  int r = -1;
  if (d) {
    struct dirent *p;
    r = 0;
    while (!r && (p = readdir(d))) {
      int r2 = -1;
      char *buf;
      size_t len;
      if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
        continue;
      len = path_len + strlen(p->d_name) + 2;
      buf = malloc(len);
      if (buf) {
        struct stat statbuf;
        snprintf(buf, len, "%s/%s", path, p->d_name);
        if (!stat(buf, &statbuf)) {
          if (S_ISDIR(statbuf.st_mode))
            r2 = apkg_rmdir(buf);
          else {
            r2 = unlink(buf);
          }
        }
        free(buf);
      }
      r = r2;
    }
    closedir(d);
  }
  return r;
}


x86 assembly (clang, -O0):
<apkg_rmdir_content>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     133d <apkg_rmdir_content+0x17d>
movl   $0x0,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x49(%rbp)
jne    1225 <apkg_rmdir_content+0x65>
mov    -0x10(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    1235 <apkg_rmdir_content+0x75>
jmp    1334 <apkg_rmdir_content+0x174>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdb7(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
je     1274 <apkg_rmdir_content+0xb4>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd9a(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    1279 <apkg_rmdir_content+0xb9>
jmp    11ff <apkg_rmdir_content+0x3f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
add    %rcx,%rax
add    $0x2,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   10d0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1329 <apkg_rmdir_content+0x169>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
lea    0xd32(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x38(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%eax
jne    1320 <apkg_rmdir_content+0x160>
mov    -0x48(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     130f <apkg_rmdir_content+0x14f>
mov    -0x38(%rbp),%rdi
call   10e0 <apkg_rmdir@plt>
mov    %eax,-0x2c(%rbp)
jmp    131b <apkg_rmdir_content+0x15b>
mov    -0x38(%rbp),%rdi
call   1040 <unlink@plt>
mov    %eax,-0x2c(%rbp)
jmp    1320 <apkg_rmdir_content+0x160>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11ff <apkg_rmdir_content+0x3f>
mov    -0x10(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x1c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<apkg_rmdir_content>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
call   1050 <opendir@plt>
test   %rax,%rax
je     12bc <apkg_rmdir_content+0xfc>
mov    %rax,%r15
mov    %r14,%rdi
call   1060 <strlen@plt>
add    $0x2,%rax
mov    %rax,0x10(%rsp)
lea    0xe07(%rip),%rbx
jmp    1213 <apkg_rmdir_content+0x53>
call   1040 <unlink@plt>
mov    %eax,%ebp
mov    %r13,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
jne    12cc <apkg_rmdir_content+0x10c>
mov    %r15,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12c3 <apkg_rmdir_content+0x103>
mov    (%rax),%r12
mov    %r12,%rdi
mov    %rbx,%rsi
call   10a0 <strcmp@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     120b <apkg_rmdir_content+0x4b>
mov    %r12,%rdi
lea    0xdbe(%rip),%rsi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     120b <apkg_rmdir_content+0x4b>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbp
add    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   10d0 <malloc@plt>
test   %rax,%rax
je     12c7 <apkg_rmdir_content+0x107>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbp,%rsi
lea    0xd8b(%rip),%rdx
mov    %r14,%rcx
mov    %r12,%r8
call   1080 <snprintf@plt>
mov    %r13,%rdi
lea    0x8(%rsp),%rsi
call   10b0 <stat@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    1203 <apkg_rmdir_content+0x43>
mov    0x8(%rsp),%edi
call   1070 <S_ISDIR@plt>
mov    %r13,%rdi
test   %rax,%rax
je     11fc <apkg_rmdir_content+0x3c>
call   10e0 <apkg_rmdir@plt>
jmp    1201 <apkg_rmdir_content+0x41>
mov    $0xffffffff,%ebp
jmp    12d4 <apkg_rmdir_content+0x114>
xor    %ebp,%ebp
jmp    12cc <apkg_rmdir_content+0x10c>
mov    $0xffffffff,%ebp
mov    %r15,%rdi
call   1090 <closedir@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = 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 @apkg_rmdir_content(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i32
%6 = alloca %struct.dirent*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.stat
store i8* %0, i8** %2
%11 = load i8*, i8** %2
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %3
%13 = load i8*, i8** %2
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %4
store i32 -1, i32* %5
%15 = load i32*, i32** %3
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %83
17: ; preds = %1
store i32 0, i32* %5
br label %18
18: ; preds = %78, %39, %17
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = load i32*, i32** %3
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %6
%24 = icmp ne %struct.dirent* %23, null
br label %25
25: ; preds = %21, %18
%26 = phi i1 [ false, %18 ], [ %24, %21 ]
br i1 %26, label %27, label %80
27: ; preds = %25
store i32 -1, i32* %7
%28 = load %struct.dirent*, %struct.dirent** %6
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i32 @strcmp(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %27
%34 = load %struct.dirent*, %struct.dirent** %6
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @strcmp(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %33, %27
br label %18
40: ; preds = %33
%41 = load i64, i64* %4
%42 = load %struct.dirent*, %struct.dirent** %6
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i64 @strlen(i8* %44)
%46 = add i64 %41, %45
%47 = add i64 %46, 2
store i64 %47, i64* %9
%48 = load i64, i64* %9
%49 = call i8* @malloc(i64 %48)
store i8* %49, i8** %8
%50 = load i8*, i8** %8
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %78
52: ; preds = %40
%53 = load i8*, i8** %8
%54 = load i64, i64* %9
%55 = load i8*, i8** %2
%56 = load %struct.dirent*, %struct.dirent** %6
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @snprintf(i8* %53, i64 %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %55, i8* %58)
%60 = load i8*, i8** %8
%61 = call i32 @stat(i8* %60, %struct.stat* %10)
%62 = icmp ne i32 %61, 0
br i1 %62, label %75, label %63
63: ; preds = %52
%64 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%65 = load i32, i32* %64
%66 = call i64 @S_ISDIR(i32 %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %63
%69 = load i8*, i8** %8
%70 = call i32 @apkg_rmdir(i8* %69)
store i32 %70, i32* %7
br label %74
71: ; preds = %63
%72 = load i8*, i8** %8
%73 = call i32 @unlink(i8* %72)
store i32 %73, i32* %7
br label %74
74: ; preds = %71, %68
br label %75
75: ; preds = %74, %52
%76 = load i8*, i8** %8
%77 = call i32 @free(i8* %76)
br label %78
78: ; preds = %75, %40
%79 = load i32, i32* %7
store i32 %79, i32* %5
br label %18
80: ; preds = %25
%81 = load i32*, i32** %3
%82 = call i32 @closedir(i32* %81)
br label %83
83: ; preds = %80, %1
%84 = load i32, i32* %5
ret i32 %84
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @apkg_rmdir_content(i8* %0) {
%2 = alloca %struct.stat
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %47, label %5
5: ; preds = %1
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = add i64 %6, 2
%8 = bitcast %struct.stat* %2 to i8*
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %10
10: ; preds = %5, %41
%11 = tail call %struct.dirent* @readdir(i32* %3)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %44, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = tail call i32 @strcmp(i8* dereferenceable(1) %15, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %41, label %18
18: ; preds = %13
%19 = tail call i32 @strcmp(i8* dereferenceable(1) %15, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32 %19, 0
br i1 %20, label %41, label %21
21: ; preds = %18
%22 = tail call i64 @strlen(i8* dereferenceable(1) %15)
%23 = add i64 %7, %22
%24 = tail call i8* @malloc(i64 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %44, label %26
26: ; preds = %21
%27 = tail call i32 @snprintf(i8* %24, i64 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %15)
%28 = call i32 @stat(i8* %24, %struct.stat* %2)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %26
%31 = load i32, i32* %9
%32 = tail call i64 @S_ISDIR(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = tail call i32 @apkg_rmdir(i8* %24)
br label %38
36: ; preds = %30
%37 = tail call i32 @unlink(i8* %24)
br label %38
38: ; preds = %34, %36, %26
%39 = phi i32 [ -1, %26 ], [ %35, %34 ], [ %37, %36 ]
%40 = tail call i32 @free(i8* %24)
br label %41
41: ; preds = %38, %13, %18
%42 = phi i32 [ 0, %18 ], [ 0, %13 ], [ %39, %38 ]
%43 = icmp eq i32 %42, 0
br i1 %43, label %10, label %44
44: ; preds = %21, %41, %10
%45 = phi i32 [ %42, %41 ], [ 0, %10 ], [ -1, %21 ]
%46 = tail call i32 @closedir(i32* %3)
br label %47
47: ; preds = %44, %1
%48 = phi i32 [ %45, %44 ], [ -1, %1 ]
ret i32 %48
}