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

/* Variables and functions */
#define S_IFDIR 129
int S_IFMT;
#define S_IFREG 128
int /*<<< orphan*/ dirs_rmdir(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int unlink(char *);
void dirs_rm(char *path) {
  if (!path) {
    fprintf(stderr, "rm needs a filepath!\n");
    return;
  }
  struct stat sb;
  if (stat(path, &sb) == -1) {
    perror("stat");
    return;
  }
  switch (sb.st_mode & S_IFMT) {
  case S_IFDIR:
    dirs_rmdir(path);
    break;
  case S_IFREG:
    if (unlink(path) == -1) {
      fprintf(stderr, "could not remove %s\n", path);
    }
    break;
  default:
    fprintf(stderr, "not a directory or file\n");
    break;
  }
}


x86 assembly (clang, -O0):
<dirs_rm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1183 <dirs_rm+0x33>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
lea    0xe89(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1235 <dirs_rm+0xe5>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    11aa <dirs_rm+0x5a>
lea    0xe76(%rip),%rdi
call   1070 <perror@plt>
jmp    1235 <dirs_rm+0xe5>
mov    -0x10(%rbp),%eax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
mov    %eax,-0x14(%rbp)
sub    $0x80,%eax
je     11ec <dirs_rm+0x9c>
jmp    11cb <dirs_rm+0x7b>
mov    -0x14(%rbp),%eax
sub    $0x81,%eax
jne    121e <dirs_rm+0xce>
jmp    11de <dirs_rm+0x8e>
mov    -0x8(%rbp),%rdi
call   1060 <dirs_rmdir@plt>
jmp    1235 <dirs_rm+0xe5>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    1219 <dirs_rm+0xc9>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1235 <dirs_rm+0xe5>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirs_rm>:
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     1198 <dirs_rm+0x48>
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     11f8 <dirs_rm+0xa8>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
cmp    $0x80,%eax
je     11aa <dirs_rm+0x5a>
cmp    $0x81,%eax
jne    11bd <dirs_rm+0x6d>
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1060 <dirs_rmdir@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
jmp    11cd <dirs_rm+0x7d>
mov    %rbx,%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
je     11d9 <dirs_rm+0x89>
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
jmp    1040 <fprintf@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
jmp    1040 <fprintf@plt>
lea    0xe17(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1070 <perror@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"rm needs a filepath!\0A\00"
@.str.1 = [5 x i8] c"stat\00"
@S_IFMT = global i32 0
@.str.2 = [21 x i8] c"could not remove %s\0A\00"
@.str.3 = [25 x i8] c"not a directory or file\0A\00"
define void @dirs_rm(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = icmp ne i8* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %35
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 @stat(i8* %10, %struct.stat* %3)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %35
15: ; preds = %9
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @S_IFMT
%19 = and i32 %17, %18
switch i32 %19, label %32 [
i32 129, label %20
i32 128, label %23
]
20: ; preds = %15
%21 = load i8*, i8** %2
%22 = call i32 @dirs_rmdir(i8* %21)
br label %35
23: ; preds = %15
%24 = load i8*, i8** %2
%25 = call i32 @unlink(i8* %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %2
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %27, %23
br label %35
32: ; preds = %15
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %6, %13, %32, %31, %20
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"rm needs a filepath!\0A\00"
@.str.1 = [5 x i8] c"stat\00"
@S_IFMT = global i32 0
@.str.2 = [21 x i8] c"could not remove %s\0A\00"
@.str.3 = [25 x i8] c"not a directory or file\0A\00"
define void @dirs_rm(i8* %0) {
%2 = alloca %struct.stat
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %30
7: ; preds = %1
%8 = bitcast %struct.stat* %2 to i8*
%9 = call i32 @stat(i8* %0, %struct.stat* %2)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %29
13: ; preds = %7
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @S_IFMT
%17 = and i32 %16, %15
switch i32 %17, label %26 [
i32 129, label %18
i32 128, label %20
]
18: ; preds = %13
%19 = tail call i32 @dirs_rmdir(i8* %0)
br label %29
20: ; preds = %13
%21 = tail call i32 @unlink(i8* %0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %29
26: ; preds = %13
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %18, %26, %23, %20, %11
br label %30
30: ; preds = %29, %4
ret void
}