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

/* Variables and functions */
void LOG_io(char *, int /*<<< orphan*/);
int /*<<< orphan*/ del_n_log(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
void rm_r(const char *dn) {
  DIR *dir = opendir(dn);
  if (!dir)
    return LOG_io("rm_r: opendir: %s\n", strerror(errno));
  struct dirent *ent;
  int l0 = strlen(dn);
  char buf[l0 + 1026];
  memcpy(buf, dn, l0);
  buf[l0++] = '/';
  buf[1024] = buf[1025] = 0;
  while ((ent = readdir(dir))) {
    const char *s = ent->d_name;
    if (*s == '.' && (!s[1] || (s[1] == '.' && !s[2])))
      continue;
    strncpy(buf + l0, s, 1024);
    del_n_log(buf, 0, 0);
  }
  buf[l0] = 0;
  del_n_log(buf, 1, 0);
}


x86 assembly (clang, -O0):
<rm_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c5 <rm_r+0x45>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%esi
lea    0xe45(%rip),%rdi
call   1070 <LOG_io@plt>
jmp    12da <rm_r+0x15a>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x402,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x40(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
movb   $0x2f,(%rax,%rcx,1)
movb   $0x0,0x401(%rax)
movb   $0x0,0x400(%rax)
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12bb <rm_r+0x13b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1290 <rm_r+0x110>
mov    -0x38(%rbp),%rax
cmpb   $0x0,0x1(%rax)
je     128b <rm_r+0x10b>
mov    -0x38(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2e,%eax
jne    1290 <rm_r+0x110>
mov    -0x38(%rbp),%rax
cmpb   $0x0,0x2(%rax)
jne    1290 <rm_r+0x110>
jmp    122c <rm_r+0xac>
mov    -0x40(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   10a0 <del_n_log@plt>
jmp    122c <rm_r+0xac>
mov    -0x40(%rbp),%rdi
movslq -0x1c(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    $0x1,%esi
xor    %edx,%edx
call   10a0 <del_n_log@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rm_r>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
call   1040 <opendir@plt>
test   %rax,%rax
je     1279 <rm_r+0xf9>
mov    %rax,%r14
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
mov    %rsp,-0x38(%rbp)
lea    0x402(%r12),%r13d
mov    %rsp,%rbx
mov    %rbx,-0x30(%rbp)
add    $0xf,%r13
and    $0xfffffffffffffff0,%r13
sub    %r13,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
movslq %r12d,%r12
movb   $0x2f,(%rbx,%r12,1)
add    $0x1,%r12
neg    %r13
mov    -0x30(%rbp),%rax
movw   $0x0,0x400(%rax,%r13,1)
mov    %r14,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1252 <rm_r+0xd2>
lea    (%rbx,%r12,1),%r15
jmp    1236 <rm_r+0xb6>
nop
mov    %r15,%rdi
mov    $0x400,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <del_n_log@plt>
mov    %r14,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1252 <rm_r+0xd2>
mov    (%rax),%rsi
cmpb   $0x2e,(%rsi)
jne    1210 <rm_r+0x90>
movzbl 0x1(%rsi),%eax
test   %al,%al
je     1229 <rm_r+0xa9>
cmp    $0x2e,%al
jne    1210 <rm_r+0x90>
cmpb   $0x0,0x2(%rsi)
jne    1210 <rm_r+0x90>
jmp    1229 <rm_r+0xa9>
movb   $0x0,(%rbx,%r12,1)
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   10a0 <del_n_log@plt>
mov    -0x38(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd72(%rip),%rdi
mov    %eax,%esi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <LOG_io@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [19 x i8] c"rm_r: opendir: %s\0A\00"
@errno = global i32 0
define void @rm_r(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = icmp ne i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = load i32, i32* @errno
%15 = call i32 @strerror(i32 %14)
br label %75
16: ; preds = %1
%17 = load i8*, i8** %2
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 1026
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %6
%23 = alloca i8, i64 %21
store i64 %21, i64* %7
%24 = load i8*, i8** %2
%25 = load i32, i32* %5
%26 = call i32 @memcpy(i8* %23, i8* %24, i32 %25)
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
store i32 %28, i32* %5
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %23, i64 %29
store i8 47, i8* %30
%31 = getelementptr inbounds i8, i8* %23, i64 1025
store i8 0, i8* %31
%32 = getelementptr inbounds i8, i8* %23, i64 1024
store i8 0, i8* %32
br label %33
33: ; preds = %62, %61, %16
%34 = load i32*, i32** %3
%35 = call %struct.dirent* @readdir(i32* %34)
store %struct.dirent* %35, %struct.dirent** %4
%36 = icmp ne %struct.dirent* %35, null
br i1 %36, label %37, label %69
37: ; preds = %33
%38 = load %struct.dirent*, %struct.dirent** %4
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
store i8* %40, i8** %8
%41 = load i8*, i8** %8
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 46
br i1 %44, label %45, label %62
45: ; preds = %37
%46 = load i8*, i8** %8
%47 = getelementptr inbounds i8, i8* %46, i64 1
%48 = load i8, i8* %47
%49 = icmp ne i8 %48, 0
br i1 %49, label %50, label %61
50: ; preds = %45
%51 = load i8*, i8** %8
%52 = getelementptr inbounds i8, i8* %51, i64 1
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 46
br i1 %55, label %56, label %62
56: ; preds = %50
%57 = load i8*, i8** %8
%58 = getelementptr inbounds i8, i8* %57, i64 2
%59 = load i8, i8* %58
%60 = icmp ne i8 %59, 0
br i1 %60, label %62, label %61
61: ; preds = %56, %45
br label %33
62: ; preds = %56, %50, %37
%63 = load i32, i32* %5
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %23, i64 %64
%66 = load i8*, i8** %8
%67 = call i32 @strncpy(i8* %65, i8* %66, i32 1024)
%68 = call i32 @del_n_log(i8* %23, i32 0, i32 0)
br label %33
69: ; preds = %33
%70 = load i32, i32* %5
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %23, i64 %71
store i8 0, i8* %72
%73 = call i32 @del_n_log(i8* %23, i32 1, i32 0)
%74 = load i8*, i8** %6
br label %75
75: ; preds = %69, %13
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [19 x i8] c"rm_r: opendir: %s\0A\00"
@errno = global i32 0
define void @rm_r(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @errno
%6 = tail call i32 @strerror(i32 %5)
tail call void @LOG_io(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %46
7: ; preds = %1
%8 = tail call i32 @strlen(i8* %0)
%9 = add nsw i32 %8, 1026
%10 = zext i32 %9 to i64
%11 = tail call i8* @llvm.stacksave()
%12 = alloca i8, i64 %10
%13 = call i32 @memcpy(i8* %12, i8* %0, i32 %8)
%14 = add nsw i32 %8, 1
%15 = sext i32 %8 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
store i8 47, i8* %16
%17 = getelementptr inbounds i8, i8* %12, i64 1025
store i8 0, i8* %17
%18 = getelementptr inbounds i8, i8* %12, i64 1024
store i8 0, i8* %18
%19 = call %struct.dirent* @readdir(i32* %2)
%20 = icmp eq %struct.dirent* %19, null
%21 = sext i32 %14 to i64
br i1 %20, label %43, label %22
22: ; preds = %7
%23 = getelementptr inbounds i8, i8* %12, i64 %21
br label %24
24: ; preds = %22, %40
%25 = phi %struct.dirent* [ %19, %22 ], [ %41, %40 ]
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%27 = load i8*, i8** %26
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 46
br i1 %29, label %30, label %37
30: ; preds = %24
%31 = getelementptr inbounds i8, i8* %27, i64 1
%32 = load i8, i8* %31
switch i8 %32, label %37 [
i8 0, label %40
i8 46, label %33
]
33: ; preds = %30
%34 = getelementptr inbounds i8, i8* %27, i64 2
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %30, %33, %24
%38 = call i32 @strncpy(i8* %23, i8* %27, i32 1024)
%39 = call i32 @del_n_log(i8* %12, i32 0, i32 0)
br label %40
40: ; preds = %33, %30, %37
%41 = call %struct.dirent* @readdir(i32* %2)
%42 = icmp eq %struct.dirent* %41, null
br i1 %42, label %43, label %24
43: ; preds = %40, %7
%44 = getelementptr inbounds i8, i8* %12, i64 %21
store i8 0, i8* %44
%45 = call i32 @del_n_log(i8* %12, i32 1, i32 0)
br label %46
46: ; preds = %43, %4
ret void
}