#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 */
scalar_t__ EBADF;
scalar_t__ ENAMETOOLONG;
scalar_t__ ENOENT;
scalar_t__ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
int dirent_test(void) {
  DIR *dirp;
  struct dirent *direntp;
  printf("Test opendir()...");
  dirp = opendir("/");
  if (dirp == NULL) {
    fflush(stdout);
    perror("failed");
    return -1;
  }
  printf("passed\n");
  errno = 0;
  printf("Test readdir()...");
  do {
    direntp = readdir(dirp);
    if (direntp == NULL) {
      if (errno != 0) {
        fflush(stdout);
        perror("failed");
        return -1;
      }
    } else {
      printf("%s...", direntp->d_name);
      fflush(stdout);
    }
  } while (direntp != NULL);
  printf("passed\n");
  printf("Test closedir()...");
  if (closedir(dirp) == 0) {
    printf("passed\n");
  } else {
    fflush(stdout);
    perror("failed");
    return -1;
  }
  printf("Test readdir() (EBADF)...");
  if (readdir(NULL) != NULL) {
    printf("failed\n");
    return -1;
  } else {
    if (errno != EBADF) {
      fflush(stdout);
      perror("should have failed with EBADF");
      return -1;
    }
  }
  printf("passed\n");
  printf("Test closedir() (EBADF)...");
  if (closedir(NULL) == 0) {
    printf("failed\n");
    return -1;
  } else {
    if (errno != EBADF) {
      fflush(stdout);
      perror("should have failed with EBADF");
      return -1;
    }
  }
  printf("passed\n");
  printf("Test opendir() (ENOENT)...");
  if (opendir("/nodir") != NULL) {
    printf("failed\n");
    return -1;
  } else {
    if (errno != ENOENT) {
      fflush(stdout);
      perror("should have failed with ENOENT");
      return -1;
    }
  }
  printf("passed\n");
  printf("Test opendir() (ENOENT)...");
  if (opendir("/dev/no_dev") != NULL) {
    printf("failed\n");
    return -1;
  } else {
    if (errno != ENOENT) {
      fflush(stdout);
      perror("should have failed with ENOENT");
      return -1;
    }
  }
  printf("passed\n");
  printf("Test opendir() (ENAMETOOLONG)...");
  if (opendir("/usr/"
              "libs012345678901234567890123456789012345678901234567890123456789"
              "0123456789") != NULL) {
    printf("failed\n");
    return -1;
  } else {
    if (errno != ENAMETOOLONG) {
      fflush(stdout);
      perror("should have failed with ENAMETOOLONG");
      return -1;
    }
  }
  printf("passed\n");
  return 0;
}


x86 assembly (clang, -O0):
<dirent_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe95(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b7 <dirent_test+0x57>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    0xe6e(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e14(%rip),%rax
movq   $0x0,(%rax)
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1235 <dirent_test+0xd5>
mov    0x2de0(%rip),%rax
cmpq   $0x0,(%rax)
je     1230 <dirent_test+0xd0>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
jmp    1258 <dirent_test+0xf8>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
jmp    125d <dirent_test+0xfd>
cmpq   $0x0,-0x18(%rbp)
jne    11e1 <dirent_test+0x81>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
cmp    $0x0,%rax
jne    12aa <dirent_test+0x14a>
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12d0 <dirent_test+0x170>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    0xd55(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <readdir@plt>
cmp    $0x0,%rax
je     130b <dirent_test+0x1ab>
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
mov    0x2cce(%rip),%rax
mov    (%rax),%rax
mov    0x2cb4(%rip),%rcx
cmp    (%rcx),%rax
je     134b <dirent_test+0x1eb>
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    0xd36(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
jmp    1350 <dirent_test+0x1f0>
lea    0xcc4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <closedir@plt>
cmp    $0x0,%rax
jne    1399 <dirent_test+0x239>
lea    0xce2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
mov    0x2c40(%rip),%rax
mov    (%rax),%rax
mov    0x2c26(%rip),%rcx
cmp    (%rcx),%rax
je     13d9 <dirent_test+0x279>
mov    0x2c1e(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    0xca8(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
jmp    13de <dirent_test+0x27e>
lea    0xc36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xcb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xcc3(%rip),%rdi
call   1030 <opendir@plt>
cmp    $0x0,%rax
je     142a <dirent_test+0x2ca>
lea    0xc51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
mov    0x2baf(%rip),%rax
mov    (%rax),%rax
mov    0x2b7d(%rip),%rcx
cmp    (%rcx),%rax
je     146a <dirent_test+0x30a>
mov    0x2b8d(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    0xc72(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
jmp    146f <dirent_test+0x30f>
lea    0xba5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xc25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xc58(%rip),%rdi
call   1030 <opendir@plt>
cmp    $0x0,%rax
je     14bb <dirent_test+0x35b>
lea    0xbc0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
mov    0x2b1e(%rip),%rax
mov    (%rax),%rax
mov    0x2aec(%rip),%rcx
cmp    (%rcx),%rax
je     14fb <dirent_test+0x39b>
mov    0x2afc(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    0xbe1(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
jmp    1500 <dirent_test+0x3a0>
lea    0xb14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xbe1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xbf4(%rip),%rdi
call   1030 <opendir@plt>
cmp    $0x0,%rax
je     154c <dirent_test+0x3ec>
lea    0xb2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
mov    0x2a8d(%rip),%rax
mov    (%rax),%rax
mov    0x2a6b(%rip),%rcx
cmp    (%rcx),%rax
je     158c <dirent_test+0x42c>
mov    0x2a6b(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    0xbec(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15a6 <dirent_test+0x446>
jmp    1591 <dirent_test+0x431>
lea    0xa83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirent_test>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe81(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe85(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     138f <dirent_test+0x21f>
mov    %rax,%rbx
lea    0xfd0(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e2f(%rip),%r15
movq   $0x0,(%r15)
lea    0xe55(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1213 <dirent_test+0xa3>
lea    0xe4c(%rip),%r14
mov    0x2df7(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rax),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r12),%edi
call   1080 <fflush@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    11f0 <dirent_test+0x80>
cmpq   $0x0,(%r15)
jne    138f <dirent_test+0x21f>
lea    0xf51(%rip),%rdi
call   1030 <puts@plt>
lea    0xdfc(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <closedir@plt>
test   %rax,%rax
jne    138f <dirent_test+0x21f>
lea    0xf26(%rip),%rdi
call   1030 <puts@plt>
lea    0xde4(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1070 <readdir@plt>
test   %rax,%rax
je     127f <dirent_test+0x10f>
lea    0xf07(%rip),%rdi
call   1030 <puts@plt>
jmp    13a9 <dirent_test+0x239>
mov    (%r15),%rax
mov    0x2d47(%rip),%rbx
cmp    (%rbx),%rax
jne    13ba <dirent_test+0x24a>
lea    0xedc(%rip),%rdi
call   1030 <puts@plt>
lea    0xdd2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <closedir@plt>
test   %rax,%rax
je     126e <dirent_test+0xfe>
mov    (%r15),%rax
cmp    (%rbx),%rax
jne    13ba <dirent_test+0x24a>
lea    0xeaa(%rip),%rdi
call   1030 <puts@plt>
lea    0xdbb(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdc8(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
jne    126e <dirent_test+0xfe>
mov    (%r15),%rax
mov    0x2cbb(%rip),%rbx
cmp    (%rbx),%rax
jne    13d1 <dirent_test+0x261>
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
lea    0xd79(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdac(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
jne    126e <dirent_test+0xfe>
mov    (%r15),%rax
cmp    (%rbx),%rax
jne    13d1 <dirent_test+0x261>
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
lea    0xd8b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd9e(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
jne    126e <dirent_test+0xfe>
mov    (%r15),%rax
mov    0x2c4e(%rip),%rcx
cmp    (%rcx),%rax
jne    13e8 <dirent_test+0x278>
lea    0xdef(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    13ae <dirent_test+0x23e>
mov    0x2c42(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
lea    0xdd8(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2c17(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
lea    0xc8a(%rip),%rdi
jmp    13a4 <dirent_test+0x234>
mov    0x2c00(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
lea    0xcce(%rip),%rdi
jmp    13a4 <dirent_test+0x234>
mov    0x2be9(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
lea    0xd53(%rip),%rdi
jmp    13a4 <dirent_test+0x234>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [18 x i8] c"Test opendir()...\00"
@.str.1 = [2 x i8] c"/\00"
@stdout = global i32 0
@.str.2 = [7 x i8] c"failed\00"
@.str.3 = [8 x i8] c"passed\0A\00"
@errno = global i64 0
@.str.4 = [18 x i8] c"Test readdir()...\00"
@.str.5 = [6 x i8] c"%s...\00"
@.str.6 = [19 x i8] c"Test closedir()...\00"
@.str.7 = [26 x i8] c"Test readdir() (EBADF)...\00"
@.str.8 = [8 x i8] c"failed\0A\00"
@EBADF = global i64 0
@.str.9 = [30 x i8] c"should have failed with EBADF\00"
@.str.10 = [27 x i8] c"Test closedir() (EBADF)...\00"
@.str.11 = [27 x i8] c"Test opendir() (ENOENT)...\00"
@.str.12 = [7 x i8] c"/nodir\00"
@ENOENT = global i64 0
@.str.13 = [31 x i8] c"should have failed with ENOENT\00"
@.str.14 = [12 x i8] c"/dev/no_dev\00"
@.str.15 = [33 x i8] c"Test opendir() (ENAMETOOLONG)...\00"
@.str.16 = [80 x i8] c"/usr/libs0123456789012345678901234567890123456789012345678901234567890123456789\00"
@ENAMETOOLONG = global i64 0
@.str.17 = [37 x i8] c"should have failed with ENAMETOOLONG\00"
define i32 @dirent_test() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%5 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32, i32* @stdout
%10 = call i32 @fflush(i32 %9)
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i64 0, i64* @errno
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %15
15: ; preds = %36, %12
%16 = load i32*, i32** %2
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %3
%18 = load %struct.dirent*, %struct.dirent** %3
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %20, label %28
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* @stdout
%25 = call i32 @fflush(i32 %24)
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
27: ; preds = %20
br label %35
28: ; preds = %15
%29 = load %struct.dirent*, %struct.dirent** %3
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @stdout
%34 = call i32 @fflush(i32 %33)
br label %35
35: ; preds = %28, %27
br label %36
36: ; preds = %35
%37 = load %struct.dirent*, %struct.dirent** %3
%38 = icmp ne %struct.dirent* %37, null
br i1 %38, label %15, label %39
39: ; preds = %36
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%42 = load i32*, i32** %2
%43 = call i64 @closedir(i32* %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %51
47: ; preds = %39
%48 = load i32, i32* @stdout
%49 = call i32 @fflush(i32 %48)
%50 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
51: ; preds = %45
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
%53 = call %struct.dirent* @readdir(i32* null)
%54 = icmp ne %struct.dirent* %53, null
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
57: ; preds = %51
%58 = load i64, i64* @errno
%59 = load i64, i64* @EBADF
%60 = icmp ne i64 %58, %59
br i1 %60, label %61, label %65
61: ; preds = %57
%62 = load i32, i32* @stdout
%63 = call i32 @fflush(i32 %62)
%64 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
65: ; preds = %57
br label %66
66: ; preds = %65
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
%69 = call i64 @closedir(i32* null)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %66
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
73: ; preds = %66
%74 = load i64, i64* @errno
%75 = load i64, i64* @EBADF
%76 = icmp ne i64 %74, %75
br i1 %76, label %77, label %81
77: ; preds = %73
%78 = load i32, i32* @stdout
%79 = call i32 @fflush(i32 %78)
%80 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
81: ; preds = %73
br label %82
82: ; preds = %81
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0))
%85 = call i32* @opendir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.12, i64 0, i64 0))
%86 = icmp ne i32* %85, null
br i1 %86, label %87, label %89
87: ; preds = %82
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
89: ; preds = %82
%90 = load i64, i64* @errno
%91 = load i64, i64* @ENOENT
%92 = icmp ne i64 %90, %91
br i1 %92, label %93, label %97
93: ; preds = %89
%94 = load i32, i32* @stdout
%95 = call i32 @fflush(i32 %94)
%96 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.13, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
97: ; preds = %89
br label %98
98: ; preds = %97
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%100 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0))
%101 = call i32* @opendir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.14, i64 0, i64 0))
%102 = icmp ne i32* %101, null
br i1 %102, label %103, label %105
103: ; preds = %98
%104 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
105: ; preds = %98
%106 = load i64, i64* @errno
%107 = load i64, i64* @ENOENT
%108 = icmp ne i64 %106, %107
br i1 %108, label %109, label %113
109: ; preds = %105
%110 = load i32, i32* @stdout
%111 = call i32 @fflush(i32 %110)
%112 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.13, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
113: ; preds = %105
br label %114
114: ; preds = %113
%115 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%116 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.15, i64 0, i64 0))
%117 = call i32* @opendir(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.16, i64 0, i64 0))
%118 = icmp ne i32* %117, null
br i1 %118, label %119, label %121
119: ; preds = %114
%120 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
121: ; preds = %114
%122 = load i64, i64* @errno
%123 = load i64, i64* @ENAMETOOLONG
%124 = icmp ne i64 %122, %123
br i1 %124, label %125, label %129
125: ; preds = %121
%126 = load i32, i32* @stdout
%127 = call i32 @fflush(i32 %126)
%128 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.17, i64 0, i64 0))
store i32 -1, i32* %1
br label %132
129: ; preds = %121
br label %130
130: ; preds = %129
%131 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %132
132: ; preds = %130, %125, %119, %109, %103, %93, %87, %77, %71, %61, %55, %47, %23, %8
%133 = load i32, i32* %1
ret i32 %133
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [18 x i8] c"Test opendir()...\00"
@.str.1 = [2 x i8] c"/\00"
@stdout = global i32 0
@errno = global i64 0
@.str.4 = [18 x i8] c"Test readdir()...\00"
@.str.5 = [6 x i8] c"%s...\00"
@.str.6 = [19 x i8] c"Test closedir()...\00"
@.str.7 = [26 x i8] c"Test readdir() (EBADF)...\00"
@EBADF = global i64 0
@.str.9 = [30 x i8] c"should have failed with EBADF\00"
@.str.10 = [27 x i8] c"Test closedir() (EBADF)...\00"
@.str.11 = [27 x i8] c"Test opendir() (ENOENT)...\00"
@.str.12 = [7 x i8] c"/nodir\00"
@ENOENT = global i64 0
@.str.13 = [31 x i8] c"should have failed with ENOENT\00"
@.str.14 = [12 x i8] c"/dev/no_dev\00"
@.str.15 = [33 x i8] c"Test opendir() (ENAMETOOLONG)...\00"
@.str.16 = [80 x i8] c"/usr/libs0123456789012345678901234567890123456789012345678901234567890123456789\00"
@ENAMETOOLONG = global i64 0
@.str.17 = [37 x i8] c"should have failed with ENAMETOOLONG\00"
@str.24 = [7 x i8] c"passed\00"
@str.29 = [7 x i8] c"failed\00"
define i32 @dirent_test() {
%1 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = load i32, i32* @stdout
%6 = tail call i32 @fflush(i32 %5)
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @str.29, i64 0, i64 0))
br label %115
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.24, i64 0, i64 0))
store i64 0, i64* @errno
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%11 = tail call %struct.dirent* @readdir(i32* %2)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %13, label %20
13: ; preds = %20, %8
%14 = load i64, i64* @errno
%15 = icmp eq i64 %14, 0
br i1 %15, label %29, label %16
16: ; preds = %13
%17 = load i32, i32* @stdout
%18 = tail call i32 @fflush(i32 %17)
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @str.29, i64 0, i64 0))
br label %115
20: ; preds = %8, %20
%21 = phi %struct.dirent* [ %27, %20 ], [ %11, %8 ]
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @stdout
%26 = tail call i32 @fflush(i32 %25)
%27 = tail call %struct.dirent* @readdir(i32* %2)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %13, label %20
29: ; preds = %13
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.24, i64 0, i64 0))
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%32 = tail call i64 @closedir(i32* %2)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.24, i64 0, i64 0))
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
%37 = tail call %struct.dirent* @readdir(i32* null)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %45, label %43
39: ; preds = %29
%40 = load i32, i32* @stdout
%41 = tail call i32 @fflush(i32 %40)
%42 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @str.29, i64 0, i64 0))
br label %115
43: ; preds = %34
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.29, i64 0, i64 0))
br label %115
45: ; preds = %34
%46 = load i64, i64* @errno
%47 = load i64, i64* @EBADF
%48 = icmp eq i64 %46, %47
br i1 %48, label %53, label %49
49: ; preds = %45
%50 = load i32, i32* @stdout
%51 = tail call i32 @fflush(i32 %50)
%52 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0))
br label %115
53: ; preds = %45
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.24, i64 0, i64 0))
%55 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
%56 = tail call i64 @closedir(i32* null)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.29, i64 0, i64 0))
br label %115
60: ; preds = %53
%61 = load i64, i64* @errno
%62 = load i64, i64* @EBADF
%63 = icmp eq i64 %61, %62
br i1 %63, label %68, label %64
64: ; preds = %60
%65 = load i32, i32* @stdout
%66 = tail call i32 @fflush(i32 %65)
%67 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.9, i64 0, i64 0))
br label %115
68: ; preds = %60
%69 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.24, i64 0, i64 0))
%70 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0))
%71 = tail call i32* @opendir(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.12, i64 0, i64 0))
%72 = icmp eq i32* %71, null
br i1 %72, label %75, label %73
73: ; preds = %68
%74 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.29, i64 0, i64 0))
br label %115
75: ; preds = %68
%76 = load i64, i64* @errno
%77 = load i64, i64* @ENOENT
%78 = icmp eq i64 %76, %77
br i1 %78, label %83, label %79
79: ; preds = %75
%80 = load i32, i32* @stdout
%81 = tail call i32 @fflush(i32 %80)
%82 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.13, i64 0, i64 0))
br label %115
83: ; preds = %75
%84 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.24, i64 0, i64 0))
%85 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0))
%86 = tail call i32* @opendir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.14, i64 0, i64 0))
%87 = icmp eq i32* %86, null
br i1 %87, label %90, label %88
88: ; preds = %83
%89 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.29, i64 0, i64 0))
br label %115
90: ; preds = %83
%91 = load i64, i64* @errno
%92 = load i64, i64* @ENOENT
%93 = icmp eq i64 %91, %92
br i1 %93, label %98, label %94
94: ; preds = %90
%95 = load i32, i32* @stdout
%96 = tail call i32 @fflush(i32 %95)
%97 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.13, i64 0, i64 0))
br label %115
98: ; preds = %90
%99 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.24, i64 0, i64 0))
%100 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.15, i64 0, i64 0))
%101 = tail call i32* @opendir(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.16, i64 0, i64 0))
%102 = icmp eq i32* %101, null
br i1 %102, label %105, label %103
103: ; preds = %98
%104 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.29, i64 0, i64 0))
br label %115
105: ; preds = %98
%106 = load i64, i64* @errno
%107 = load i64, i64* @ENAMETOOLONG
%108 = icmp eq i64 %106, %107
br i1 %108, label %113, label %109
109: ; preds = %105
%110 = load i32, i32* @stdout
%111 = tail call i32 @fflush(i32 %110)
%112 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.17, i64 0, i64 0))
br label %115
113: ; preds = %105
%114 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.24, i64 0, i64 0))
br label %115
115: ; preds = %113, %109, %103, %94, %88, %79, %73, %64, %58, %49, %43, %39, %16, %4
%116 = phi i32 [ -1, %4 ], [ -1, %16 ], [ -1, %43 ], [ -1, %49 ], [ -1, %58 ], [ -1, %64 ], [ -1, %73 ], [ -1, %79 ], [ -1, %88 ], [ -1, %94 ], [ -1, %103 ], [ -1, %109 ], [ 0, %113 ], [ -1, %39 ]
ret i32 %116
}