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

/* Variables and functions */
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ stat(int /*<<< orphan*/, struct stat *);
scalar_t__ unlink(int /*<<< orphan*/);
void *rmFile(void *pDir) {
  DIR *dp = (DIR *)pDir;
  struct dirent *dirp = NULL;
  struct stat buf;
  while ((dirp = readdir(dp)) != NULL) {
    if (stat(dirp->d_name, &buf) < 0) {
      perror("stat failed");
      continue;
    }
    if (S_ISREG(buf.st_mode)) {
      if (unlink(dirp->d_name) < 0)
        perror("remove file failed");
    }
  }
  return ((void *)0);
}


x86 assembly (clang, -O0):
<rmFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11ea <rmFile+0x9a>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    11ad <rmFile+0x5d>
lea    0xe5d(%rip),%rdi
call   1070 <perror@plt>
jmp    116c <rmFile+0x1c>
mov    -0x20(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     11e5 <rmFile+0x95>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    11e0 <rmFile+0x90>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
jmp    11e5 <rmFile+0x95>
jmp    116c <rmFile+0x1c>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rmFile>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
call   1060 <readdir@plt>
test   %rax,%rax
je     11d1 <rmFile+0x81>
mov    %rax,%rbx
lea    0x8(%rsp),%r14
lea    0xe87(%rip),%r12
lea    0xe8c(%rip),%r13
jmp    11a0 <rmFile+0x50>
call   1070 <perror@plt>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1060 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d1 <rmFile+0x81>
mov    (%rbx),%edi
mov    %r14,%rsi
call   1040 <stat@plt>
mov    %r12,%rdi
test   %rax,%rax
js     1182 <rmFile+0x32>
mov    0x8(%rsp),%edi
call   1050 <S_ISREG@plt>
test   %rax,%rax
je     1190 <rmFile+0x40>
mov    (%rbx),%edi
call   1030 <unlink@plt>
mov    %r13,%rdi
test   %rax,%rax
jns    1190 <rmFile+0x40>
jmp    1182 <rmFile+0x32>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i32 }
@.str = [12 x i8] c"stat failed\00"
@.str.1 = [19 x i8] c"remove file failed\00"
define i8* @rmFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca %struct.stat
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = bitcast i8* %6 to i32*
store i32* %7, i32** %3
store %struct.dirent* null, %struct.dirent** %4
br label %8
8: ; preds = %34, %18, %1
%9 = load i32*, i32** %3
%10 = call %struct.dirent* @readdir(i32* %9)
store %struct.dirent* %10, %struct.dirent** %4
%11 = icmp ne %struct.dirent* %10, null
br i1 %11, label %12, label %35
12: ; preds = %8
%13 = load %struct.dirent*, %struct.dirent** %4
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @stat(i32 %15, %struct.stat* %5)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %8
20: ; preds = %12
%21 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i64 @S_ISREG(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %20
%26 = load %struct.dirent*, %struct.dirent** %4
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i64 @unlink(i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %25
br label %34
34: ; preds = %33, %20
br label %8
35: ; preds = %8
ret i8* null
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [12 x i8] c"stat failed\00"
@.str.1 = [19 x i8] c"remove file failed\00"
define noalias i8* @rmFile(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast i8* %0 to i32*
%4 = bitcast %struct.stat* %2 to i8*
%5 = call %struct.dirent* @readdir(i32* %3)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %29, label %7
7: ; preds = %1
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %9
9: ; preds = %7, %18
%10 = phi %struct.dirent* [ %5, %7 ], [ %19, %18 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i64 @stat(i32 %12, %struct.stat* %2)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %9, %25
%16 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), %25 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %9 ]
%17 = call i32 @perror(i8* %16)
br label %18
18: ; preds = %15, %21, %25
%19 = call %struct.dirent* @readdir(i32* %3)
%20 = icmp eq %struct.dirent* %19, null
br i1 %20, label %29, label %9
21: ; preds = %9
%22 = load i32, i32* %8
%23 = call i64 @S_ISREG(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %18, label %25
25: ; preds = %21
%26 = load i32, i32* %11
%27 = call i64 @unlink(i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %15, label %18
29: ; preds = %18, %1
ret i8* null
}