#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*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ pthread_init();
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(char *, int /*<<< orphan*/);
main() {
  struct dirent *file;
  DIR *dot_dir;
  int i, found = 0;
  pthread_init();
  if (dot_dir = opendir(".")) {
    while (file = readdir(dot_dir)) {
      if (!strcmp("test_readdir", file->d_name)) {
        found = 1;
      }
    }
    closedir(dot_dir);
    if (found) {
      printf("test_readdir PASSED\n");
      exit(0);
    } else {
      printf("Couldn't find file test_readdir ERROR\n");
    }
  } else {
    printf("opendir() ERROR\n");
  }
  printf("test_readdir FAILED\n");
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1080 <pthread_init@plt>
lea    0xe6c(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1221 <main+0xb1>
jmp    11ac <main+0x3c>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11ea <main+0x7a>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xe32(%rip),%rdi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11e5 <main+0x75>
movl   $0x1,-0x20(%rbp)
jmp    11ac <main+0x3c>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
cmpl   $0x0,-0x20(%rbp)
je     1210 <main+0xa0>
lea    0xe0b(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
jmp    122d <main+0xbd>
lea    0xe23(%rip),%rdi
call   1040 <printf@plt>
lea    0xe28(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <pthread_init@plt>
lea    0xe7b(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
jne    1198 <main+0x28>
lea    0xe79(%rip),%rdi
jmp    1200 <main+0x90>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    11b2 <main+0x42>
mov    %rbx,%rdi
call   1050 <closedir@plt>
jmp    11f9 <main+0x89>
xor    %ebp,%ebp
lea    0xe47(%rip),%r14
mov    $0x1,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
cmove  %r15d,%ebp
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    11d0 <main+0x60>
mov    %rbx,%rdi
call   1050 <closedir@plt>
test   %ebp,%ebp
jne    121b <main+0xab>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
lea    0xe13(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [13 x i8] c"test_readdir\00"
@.str.2 = [21 x i8] c"test_readdir PASSED\0A\00"
@.str.3 = [39 x i8] c"Couldn't find file test_readdir ERROR\0A\00"
@.str.4 = [17 x i8] c"opendir() ERROR\0A\00"
@.str.5 = [21 x i8] c"test_readdir FAILED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.dirent*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
%6 = call i32 (...) @pthread_init()
%7 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %33
9: ; preds = %0
br label %10
10: ; preds = %21, %9
%11 = load i32*, i32** %3
%12 = call %struct.dirent* @readdir(i32* %11)
store %struct.dirent* %12, %struct.dirent** %2
%13 = icmp ne %struct.dirent* %12, null
br i1 %13, label %14, label %22
14: ; preds = %10
%15 = load %struct.dirent*, %struct.dirent** %2
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @strcmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %14
store i32 1, i32* %5
br label %21
21: ; preds = %20, %14
br label %10
22: ; preds = %10
%23 = load i32*, i32** %3
%24 = call i32 @closedir(i32* %23)
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %22
%31 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30
br label %35
33: ; preds = %0
%34 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %33, %32
%36 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [13 x i8] c"test_readdir\00"
@str = [16 x i8] c"opendir() ERROR\00"
@str.6 = [20 x i8] c"test_readdir FAILED\00"
@str.7 = [38 x i8] c"Couldn't find file test_readdir ERROR\00"
@str.8 = [20 x i8] c"test_readdir PASSED\00"
define i32 @main() {
%1 = tail call i32 (...) @pthread_init()
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %25, label %4
4: ; preds = %0
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @closedir(i32* %2)
br label %25
9: ; preds = %4, %9
%10 = phi %struct.dirent* [ %17, %9 ], [ %5, %4 ]
%11 = phi i32 [ %16, %9 ], [ 0, %4 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i32 @strcmp(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, 0
%16 = select i1 %15, i32 1, i32 %11
%17 = tail call %struct.dirent* @readdir(i32* %2)
%18 = icmp eq %struct.dirent* %17, null
br i1 %18, label %19, label %9
19: ; preds = %9
%20 = icmp eq i32 %16, 0
%21 = tail call i32 @closedir(i32* %2)
br i1 %20, label %25, label %22
22: ; preds = %19
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0))
%24 = tail call i32 @exit(i32 0)
unreachable
25: ; preds = %0, %19, %7
%26 = phi i8* [ getelementptr inbounds ([38 x i8], [38 x i8]* @str.7, i64 0, i64 0), %7 ], [ getelementptr inbounds ([38 x i8], [38 x i8]* @str.7, i64 0, i64 0), %19 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0), %0 ]
%27 = tail call i32 @puts(i8* dereferenceable(1) %26)
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
}