#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 */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ endswith(char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rmtempdir(char *, int /*<<< orphan*/);
scalar_t__ runtest(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ startswith(char *, char *);
int runtests(char *dirname, char *ironout) {
  struct dirent *ent;
  DIR *dir;
  int total = 0;
  int fails = 0;
  dir = opendir(dirname);
  if (!dir) {
    printf("failed: %s does not exist\n", dirname);
    return 1;
  }
  while ((ent = readdir(dir))) {
    char *name = ent->d_name;
    rmtempdir(".", 0);
    if (startswith(name, "test-") && !endswith(name, "~")) {
      char path[1024];
      sprintf(path, "%s/%s", dirname, name);
      total++;
      if (runtest(path, ironout)) {
        fails++;
      }
    }
  }
  closedir(dir);
  if (fails)
    printf("\n%d of %d failed\n", fails, total);
  else
    printf("%d succeeded\n", total);
  return fails;
}


x86 assembly (clang, -O0):
<runtests>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e7 <runtests+0x57>
mov    -0x10(%rbp),%rsi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e3 <runtests+0x153>
jmp    11ec <runtests+0x5c>
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12a0 <runtests+0x110>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
lea    0xe06(%rip),%rdi
xor    %esi,%esi
call   1080 <rmtempdir@plt>
mov    -0x38(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1070 <startswith@plt>
cmp    $0x0,%rax
je     129b <runtests+0x10b>
mov    -0x38(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   1030 <endswith@plt>
cmp    $0x0,%eax
jne    129b <runtests+0x10b>
lea    -0x440(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    0xdc0(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
lea    -0x440(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <runtest@plt>
cmp    $0x0,%rax
je     1296 <runtests+0x106>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    129b <runtests+0x10b>
jmp    11ec <runtests+0x5c>
mov    -0x28(%rbp),%rdi
call   1060 <closedir@plt>
cmpl   $0x0,-0x30(%rbp)
je     12cc <runtests+0x13c>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12dd <runtests+0x14d>
mov    -0x2c(%rbp),%esi
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runtests>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,0x18(%rsp)
mov    %rdi,%rbp
call   1040 <opendir@plt>
test   %rax,%rax
je     1286 <runtests+0xf6>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     129e <runtests+0x10e>
mov    %rbp,0x10(%rsp)
xor    %ebp,%ebp
lea    0xe42(%rip),%r13
lea    0xe3d(%rip),%r15
movl   $0x0,0xc(%rsp)
jmp    11fd <runtests+0x6d>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1264 <runtests+0xd4>
mov    (%rax),%r14
mov    %r13,%rdi
xor    %esi,%esi
call   1080 <rmtempdir@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <startswith@plt>
test   %rax,%rax
je     11f0 <runtests+0x60>
mov    %r14,%rdi
lea    0xdff(%rip),%rsi
call   1030 <endswith@plt>
test   %eax,%eax
jne    11f0 <runtests+0x60>
lea    0x20(%rsp),%r12
mov    %r12,%rdi
lea    0xde9(%rip),%rsi
mov    0x10(%rsp),%rdx
mov    %r14,%rcx
call   10b0 <sprintf@plt>
addl   $0x1,0xc(%rsp)
mov    %r12,%rdi
mov    0x18(%rsp),%rsi
call   10a0 <runtest@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
jmp    11f0 <runtests+0x60>
mov    %rbx,%rdi
call   1060 <closedir@plt>
test   %ebp,%ebp
je     12aa <runtests+0x11a>
lea    0xdb4(%rip),%rdi
mov    %ebp,%esi
mov    0xc(%rsp),%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12be <runtests+0x12e>
lea    0xd73(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
jmp    12be <runtests+0x12e>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %esi,%esi
jmp    12ae <runtests+0x11e>
mov    0xc(%rsp),%esi
lea    0xd88(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [27 x i8] c"failed: %s does not exist\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [6 x i8] c"test-\00"
@.str.3 = [2 x i8] c"~\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [18 x i8] c"\0A%d of %d failed\0A\00"
@.str.6 = [14 x i8] c"%d succeeded\0A\00"
define i32 @runtests(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.dirent*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [1024 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %8
store i32 0, i32* %9
%12 = load i8*, i8** %4
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = icmp ne i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %66
19: ; preds = %2
br label %20
20: ; preds = %51, %19
%21 = load i32*, i32** %7
%22 = call %struct.dirent* @readdir(i32* %21)
store %struct.dirent* %22, %struct.dirent** %6
%23 = icmp ne %struct.dirent* %22, null
br i1 %23, label %24, label %52
24: ; preds = %20
%25 = load %struct.dirent*, %struct.dirent** %6
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
store i8* %27, i8** %10
%28 = call i32 @rmtempdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 0)
%29 = load i8*, i8** %10
%30 = call i64 @startswith(i8* %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %51
32: ; preds = %24
%33 = load i8*, i8** %10
%34 = call i32 @endswith(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp ne i32 %34, 0
br i1 %35, label %51, label %36
36: ; preds = %32
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%38 = load i8*, i8** %4
%39 = load i8*, i8** %10
%40 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %38, i8* %39)
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%44 = load i8*, i8** %5
%45 = call i64 @runtest(i8* %43, i8* %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %36
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
br label %50
50: ; preds = %47, %36
br label %51
51: ; preds = %50, %32, %24
br label %20
52: ; preds = %20
%53 = load i32*, i32** %7
%54 = call i32 @closedir(i32* %53)
%55 = load i32, i32* %9
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %52
%58 = load i32, i32* %9
%59 = load i32, i32* %8
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %58, i32 %59)
br label %64
61: ; preds = %52
%62 = load i32, i32* %8
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %62)
br label %64
64: ; preds = %61, %57
%65 = load i32, i32* %9
store i32 %65, i32* %3
br label %66
66: ; preds = %64, %16
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [27 x i8] c"failed: %s does not exist\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [6 x i8] c"test-\00"
@.str.3 = [2 x i8] c"~\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [18 x i8] c"\0A%d of %d failed\0A\00"
@.str.6 = [14 x i8] c"%d succeeded\0A\00"
define i32 @runtests(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %13, label %6
6: ; preds = %2
%7 = call %struct.dirent* @readdir(i32* %4)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = call i32 @closedir(i32* %4)
br label %44
11: ; preds = %6
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %15
13: ; preds = %2
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %47
15: ; preds = %11, %34
%16 = phi %struct.dirent* [ %7, %11 ], [ %37, %34 ]
%17 = phi i32 [ 0, %11 ], [ %36, %34 ]
%18 = phi i32 [ 0, %11 ], [ %35, %34 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = call i32 @rmtempdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 0)
%22 = call i64 @startswith(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %34, label %24
24: ; preds = %15
%25 = call i32 @endswith(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %20)
%29 = add nsw i32 %18, 1
%30 = call i64 @runtest(i8* %12, i8* %1)
%31 = icmp ne i64 %30, 0
%32 = zext i1 %31 to i32
%33 = add nsw i32 %17, %32
br label %34
34: ; preds = %27, %24, %15
%35 = phi i32 [ %18, %24 ], [ %29, %27 ], [ %18, %15 ]
%36 = phi i32 [ %17, %24 ], [ %33, %27 ], [ %17, %15 ]
%37 = call %struct.dirent* @readdir(i32* %4)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %39, label %15
39: ; preds = %34
%40 = call i32 @closedir(i32* %4)
%41 = icmp eq i32 %36, 0
br i1 %41, label %44, label %42
42: ; preds = %39
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %36, i32 %35)
br label %47
44: ; preds = %9, %39
%45 = phi i32 [ 0, %9 ], [ %35, %39 ]
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %42, %44, %13
%48 = phi i32 [ 1, %13 ], [ 0, %44 ], [ %36, %42 ]
ret i32 %48
}