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

/* Variables and functions */
int /*<<< orphan*/ cleanup();
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ create_files();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ *opendir(char *);
int os2_delete(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, int);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ rmdir(char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(int /*<<< orphan*/, char *);
char *strerror(int /*<<< orphan*/);
int main(void) {
  int total_deleted = 0;
  DIR *d;
  struct dirent *de;
  cleanup();
  create_files();
  d = opendir("test.dir");
  de = readdir(d);
  strcmp(de->d_name, ".") == 0 ||
      (fprintf(stderr, "Failed for %s - %s\n", "match .", strerror(errno)),
       exit(1), 1);
  de = readdir(d);
  strcmp(de->d_name, "..") == 0 ||
      (fprintf(stderr, "Failed for %s - %s\n", "match ..", strerror(errno)),
       exit(1), 1);
  while (1) {
    int n = os2_delete(d);
    if (n == 0)
      break;
    total_deleted += n;
  }
  closedir(d);
  printf("Deleted %d files of %d\n", total_deleted, 700);
  rmdir("test.dir") == 0 ||
      (fprintf(stderr, "Failed for %s - %s\n", "rmdir", strerror(errno)),
       exit(1), 1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   10c0 <cleanup@plt>
mov    $0x0,%al
call   1060 <create_files@plt>
lea    0xe15(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     125a <main+0x9a>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rcx
lea    0xdc5(%rip),%rsi
lea    0xdd2(%rip),%rdx
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xdb7(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     12c0 <main+0x100>
mov    0x2d56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rcx
lea    0xd5f(%rip),%rsi
lea    0xd74(%rip),%rdx
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1080 <os2_delete@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    12db <main+0x11b>
jmp    12e9 <main+0x129>
mov    -0x1c(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    12c0 <main+0x100>
mov    -0x10(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x8(%rbp),%esi
lea    0xd32(%rip),%rdi
mov    $0x2bc,%edx
call   1050 <printf@plt>
lea    0xcf3(%rip),%rdi
call   1040 <rmdir@plt>
cmp    $0x0,%rax
je     1359 <main+0x199>
mov    0x2cbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rcx
lea    0xcc6(%rip),%rsi
lea    0xcfc(%rip),%rdx
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <cleanup@plt>
xor    %eax,%eax
call   1060 <create_files@plt>
lea    0xe28(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10b0 <readdir@plt>
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1263 <main+0xa3>
mov    %rbx,%rdi
call   10b0 <readdir@plt>
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    128a <main+0xca>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <os2_delete@plt>
add    %eax,%ebp
test   %eax,%eax
jne    1220 <main+0x60>
mov    %rbx,%rdi
call   1070 <closedir@plt>
lea    0xdf1(%rip),%rdi
mov    %ebp,%esi
mov    $0x2bc,%edx
call   1050 <printf@plt>
lea    0xdb0(%rip),%rdi
call   1040 <rmdir@plt>
test   %rax,%rax
jne    12b1 <main+0xf1>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d76(%rip),%rax
mov    (%rax),%ebx
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd88(%rip),%rsi
lea    0xd95(%rip),%rdx
jmp    12d6 <main+0x116>
mov    0x2d4f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd61(%rip),%rsi
lea    0xd76(%rip),%rdx
jmp    12d6 <main+0x116>
mov    0x2d28(%rip),%rax
mov    (%rax),%ebx
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd3a(%rip),%rsi
lea    0xd70(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"test.dir\00"
@.str.1 = [2 x i8] c".\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"Failed for %s - %s\0A\00"
@.str.3 = [8 x i8] c"match .\00"
@errno = global i32 0
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [9 x i8] c"match ..\00"
@.str.6 = [24 x i8] c"Deleted %d files of %d\0A\00"
@.str.7 = [6 x i8] c"rmdir\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%6 = call i32 (...) @cleanup()
%7 = call i32 (...) @create_files()
%8 = call i32* @opendir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = call %struct.dirent* @readdir(i32* %9)
store %struct.dirent* %10, %struct.dirent** %4
%11 = load %struct.dirent*, %struct.dirent** %4
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i64 @strcmp(i32 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %0
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; No predecessors!
br label %23
23: ; preds = %22, %0
%24 = phi i1 [ true, %0 ], [ true, %22 ]
%25 = zext i1 %24 to i32
%26 = load i32*, i32** %3
%27 = call %struct.dirent* @readdir(i32* %26)
store %struct.dirent* %27, %struct.dirent** %4
%28 = load %struct.dirent*, %struct.dirent** %4
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @strcmp(i32 %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %23
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; No predecessors!
br label %40
40: ; preds = %39, %23
%41 = phi i1 [ true, %23 ], [ true, %39 ]
%42 = zext i1 %41 to i32
br label %43
43: ; preds = %40, %49
%44 = load i32*, i32** %3
%45 = call i32 @os2_delete(i32* %44)
store i32 %45, i32* %5
%46 = load i32, i32* %5
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
br label %53
49: ; preds = %43
%50 = load i32, i32* %5
%51 = load i32, i32* %2
%52 = add nsw i32 %51, %50
store i32 %52, i32* %2
br label %43
53: ; preds = %48
%54 = load i32*, i32** %3
%55 = call i32 @closedir(i32* %54)
%56 = load i32, i32* %2
%57 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %56, i32 700)
%58 = call i64 @rmdir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%59 = icmp eq i64 %58, 0
br i1 %59, label %67, label %60
60: ; preds = %53
%61 = load i32, i32* @stderr
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %63)
%65 = call i32 @exit(i32 1)
unreachable
66: ; No predecessors!
br label %67
67: ; preds = %66, %53
%68 = phi i1 [ true, %53 ], [ true, %66 ]
%69 = zext i1 %68 to i32
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"test.dir\00"
@.str.1 = [2 x i8] c".\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"Failed for %s - %s\0A\00"
@.str.3 = [8 x i8] c"match .\00"
@errno = global i32 0
@.str.4 = [3 x i8] c"..\00"
@.str.5 = [9 x i8] c"match ..\00"
@.str.6 = [24 x i8] c"Deleted %d files of %d\0A\00"
@.str.7 = [6 x i8] c"rmdir\00"
define i32 @main() {
%1 = tail call i32 (...) @cleanup()
%2 = tail call i32 (...) @create_files()
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%4 = tail call %struct.dirent* @readdir(i32* %3)
%5 = getelementptr inbounds %struct.dirent, %struct.dirent* %4, i64 0, i32 0
%6 = load i32, i32* %5
%7 = tail call i64 @strcmp(i32 %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = tail call %struct.dirent* @readdir(i32* %3)
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i64 @strcmp(i32 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %24)
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %15, %27
%28 = phi i32 [ %31, %27 ], [ 0, %15 ]
%29 = tail call i32 @os2_delete(i32* %3)
%30 = icmp eq i32 %29, 0
%31 = add nsw i32 %29, %28
br i1 %30, label %32, label %27
32: ; preds = %27
%33 = tail call i32 @closedir(i32* %3)
%34 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %31, i32 700)
%35 = tail call i64 @rmdir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = tail call i8* @strerror(i32 %39)
%41 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %40)
%42 = tail call i32 @exit(i32 1)
unreachable
43: ; preds = %32
ret i32 0
}