#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 /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ v_printf(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ vv_printf(char *, ...);
int clear_directory(char *dirname) {
  struct dirent *diriter;
  char fn[4096];
  DIR *dir;
  vv_printf("dirname %s\n", dirname);
  dir = opendir(dirname);
  if (dir == NULL) {
    v_printf("FAIL: dir is NULL %d %s\n", errno, strerror(errno));
    return -1;
  }
  diriter = readdir(dir);
  if (diriter == NULL) {
    v_printf("FAIL: diriter is NULL %d %s\n", errno, strerror(errno));
  }
  while (diriter != NULL) {
    struct stat stbuf;
    vv_printf("d_name %s\n", diriter->d_name);
    if (!strcmp(diriter->d_name, ".") || !strcmp(diriter->d_name, "..")) {
      vv_printf("ignoring . and ..\n");
      diriter = readdir(dir);
      continue;
    }
    snprintf(fn, 4096, "%s/%s", dirname, diriter->d_name);
    if (S_ISDIR(stbuf.st_mode)) {
      vv_printf("directory: %s\n", fn);
      clear_directory(fn);
    } else {
      remove(fn);
    }
    diriter = readdir(dir);
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<clear_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1080 <vv_printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jne    122c <clear_directory+0x7c>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1034(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x1034(%rbp),%esi
mov    %eax,%edx
lea    0xdf1(%rip),%rdi
call   10b0 <v_printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <clear_directory+0x1da>
mov    -0x1028(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1278 <clear_directory+0xc8>
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1038(%rbp)
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x1038(%rbp),%esi
mov    %eax,%edx
lea    0xdb2(%rip),%rdi
call   10b0 <v_printf@plt>
jmp    127d <clear_directory+0xcd>
cmpq   $0x0,-0x18(%rbp)
je     1377 <clear_directory+0x1c7>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1080 <vv_printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xda3(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
je     12d5 <clear_directory+0x125>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xd86(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    12f8 <clear_directory+0x148>
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1080 <vv_printf@plt>
mov    -0x1028(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
jmp    127d <clear_directory+0xcd>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
mov    $0x1000,%esi
lea    0xd4d(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x1030(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1356 <clear_directory+0x1a6>
lea    -0x1020(%rbp),%rsi
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1080 <vv_printf@plt>
lea    -0x1020(%rbp),%rdi
call   10d0 <clear_directory@plt>
jmp    1362 <clear_directory+0x1b2>
lea    -0x1020(%rbp),%rdi
call   1030 <remove@plt>
mov    -0x1028(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x18(%rbp)
jmp    127d <clear_directory+0xcd>
mov    -0x1028(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clear_directory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%r14
lea    0xe35(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <vv_printf@plt>
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12b5 <clear_directory+0x105>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12dc <clear_directory+0x12c>
mov    %rax,%rbp
lea    0xe3e(%rip),%r15
lea    0xe43(%rip),%r12
mov    %rsp,%r13
jmp    1232 <clear_directory+0x82>
lea    0xe39(%rip),%rdi
xor    %eax,%eax
call   1080 <vv_printf@plt>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12fc <clear_directory+0x14c>
mov    0x0(%rbp),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1080 <vv_printf@plt>
mov    0x0(%rbp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     1210 <clear_directory+0x60>
mov    %rbp,%rdi
lea    0xdf0(%rip),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     1210 <clear_directory+0x60>
mov    %r13,%rdi
mov    $0x1000,%esi
lea    0xdee(%rip),%rdx
mov    %r14,%rcx
mov    %rbp,%r8
call   1060 <snprintf@plt>
call   1050 <S_ISDIR@plt>
test   %rax,%rax
je     12a8 <clear_directory+0xf8>
lea    0xdd8(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1080 <vv_printf@plt>
mov    %r13,%rdi
call   10d0 <clear_directory@plt>
jmp    121e <clear_directory+0x6e>
mov    %r13,%rdi
call   1030 <remove@plt>
jmp    121e <clear_directory+0x6e>
mov    0x2d24(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10c0 <strerror@plt>
lea    0xd40(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   10b0 <v_printf@plt>
mov    $0xffffffff,%eax
jmp    1306 <clear_directory+0x156>
mov    0x2cfd(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10c0 <strerror@plt>
lea    0xd32(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   10b0 <v_printf@plt>
mov    %rbx,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
add    $0x1008,%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 = [12 x i8] c"dirname %s\0A\00"
@.str.1 = [25 x i8] c"FAIL: dir is NULL %d %s\0A\00"
@errno = global i32 0
@.str.2 = [29 x i8] c"FAIL: diriter is NULL %d %s\0A\00"
@.str.3 = [11 x i8] c"d_name %s\0A\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [3 x i8] c"..\00"
@.str.6 = [19 x i8] c"ignoring . and ..\0A\00"
@.str.7 = [6 x i8] c"%s/%s\00"
@.str.8 = [15 x i8] c"directory: %s\0A\00"
define i32 @clear_directory(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca [4096 x i8]
%6 = alloca i32*
%7 = alloca %struct.stat
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 (i8*, ...) @vv_printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %3
%11 = call i32* @opendir(i8* %10)
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = load i32, i32* @errno
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 @v_printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %17)
store i32 -1, i32* %2
br label %78
19: ; preds = %1
%20 = load i32*, i32** %6
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %4
%22 = load %struct.dirent*, %struct.dirent** %4
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i32, i32* @errno
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 @v_printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %27)
br label %29
29: ; preds = %24, %19
br label %30
30: ; preds = %72, %49, %29
%31 = load %struct.dirent*, %struct.dirent** %4
%32 = icmp ne %struct.dirent* %31, null
br i1 %32, label %33, label %75
33: ; preds = %30
%34 = load %struct.dirent*, %struct.dirent** %4
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 (i8*, ...) @vv_printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %36)
%38 = load %struct.dirent*, %struct.dirent** %4
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i32 @strcmp(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %33
%44 = load %struct.dirent*, %struct.dirent** %4
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = call i32 @strcmp(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%48 = icmp ne i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %43, %33
%50 = call i32 (i8*, ...) @vv_printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%51 = load i32*, i32** %6
%52 = call %struct.dirent* @readdir(i32* %51)
store %struct.dirent* %52, %struct.dirent** %4
br label %30
53: ; preds = %43
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%55 = load i8*, i8** %3
%56 = load %struct.dirent*, %struct.dirent** %4
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @snprintf(i8* %54, i32 4096, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %55, i8* %58)
%60 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i64 @S_ISDIR(i32 %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %53
%65 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%66 = call i32 (i8*, ...) @vv_printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %65)
%67 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%68 = call i32 @clear_directory(i8* %67)
br label %72
69: ; preds = %53
%70 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%71 = call i32 @remove(i8* %70)
br label %72
72: ; preds = %69, %64
%73 = load i32*, i32** %6
%74 = call %struct.dirent* @readdir(i32* %73)
store %struct.dirent* %74, %struct.dirent** %4
br label %30
75: ; preds = %30
%76 = load i32*, i32** %6
%77 = call i32 @closedir(i32* %76)
store i32 0, i32* %2
br label %78
78: ; preds = %75, %14
%79 = load i32, i32* %2
ret i32 %79
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [12 x i8] c"dirname %s\0A\00"
@.str.1 = [25 x i8] c"FAIL: dir is NULL %d %s\0A\00"
@errno = global i32 0
@.str.2 = [29 x i8] c"FAIL: diriter is NULL %d %s\0A\00"
@.str.3 = [11 x i8] c"d_name %s\0A\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [3 x i8] c"..\00"
@.str.6 = [19 x i8] c"ignoring . and ..\0A\00"
@.str.7 = [6 x i8] c"%s/%s\00"
@.str.8 = [15 x i8] c"directory: %s\0A\00"
define i32 @clear_directory(i8* %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (i8*, ...) @vv_printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @v_printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %9)
br label %45
11: ; preds = %1
%12 = tail call %struct.dirent* @readdir(i32* %5)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 @v_printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %16)
br label %43
18: ; preds = %11, %40
%19 = phi %struct.dirent* [ %41, %40 ], [ %12, %11 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @vv_printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %20
%24 = call i32 @strcmp(i8* dereferenceable(1) %23, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %18
%27 = call i32 @strcmp(i8* dereferenceable(1) %23, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26, %18
%30 = call i32 (i8*, ...) @vv_printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %40
31: ; preds = %26
%32 = call i32 @snprintf(i8* %3, i32 4096, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* %23)
%33 = call i64 @S_ISDIR(i32 undef)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = call i32 (i8*, ...) @vv_printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %3)
%37 = call i32 @clear_directory(i8* %3)
br label %40
38: ; preds = %31
%39 = call i32 @remove(i8* %3)
br label %40
40: ; preds = %35, %38, %29
%41 = call %struct.dirent* @readdir(i32* %5)
%42 = icmp eq %struct.dirent* %41, null
br i1 %42, label %43, label %18
43: ; preds = %40, %14
%44 = call i32 @closedir(i32* %5)
br label %45
45: ; preds = %43, %7
%46 = phi i32 [ -1, %7 ], [ 0, %43 ]
ret i32 %46
}