#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*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int main() {
  struct dirent *pDirent;
  struct dirent *qDirent;
  DIR *pDir, *qDir;
  pDir = opendir("testdir");
  if (pDir == NULL) {
    printf("opendir 1 failed for directory testdir\n");
  } else {
    printf("files in directory testdir\n");
    while ((pDirent = readdir(pDir)) != NULL) {
      printf("[%s]\n", pDirent->d_name);
    }
    printf("end of files\n");
    closedir(pDir);
  }
  qDir = opendir("testdir");
  if (qDir == NULL) {
    printf("opendir 2 failed for directory testdir\n");
  } else {
    printf("files in directory testdir\n");
    while ((qDirent = readdir(qDir)) != NULL) {
      printf("[%s]\n", qDirent->d_name);
    }
    printf("end of files\n");
    closedir(qDir);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117d <main+0x3d>
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11d3 <main+0x93>
lea    0xeac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11bc <main+0x7c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe9c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    118b <main+0x4b>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
lea    0xe26(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1201 <main+0xc1>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1257 <main+0x117>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1240 <main+0x100>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120f <main+0xcf>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xebe(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11c0 <main+0x70>
mov    %rax,%rbx
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11aa <main+0x5a>
lea    0xe78(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    (%rax),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    1190 <main+0x40>
lea    0xe70(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <closedir@plt>
jmp    11cc <main+0x7c>
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
lea    0xe46(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1230 <main+0xe0>
mov    %rax,%rbx
lea    0xe1f(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     121a <main+0xca>
lea    0xe00(%rip),%r14
mov    (%rax),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    1200 <main+0xb0>
lea    0xe00(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <closedir@plt>
jmp    123c <main+0xec>
lea    0xdf7(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [8 x i8] c"testdir\00"
@.str.1 = [40 x i8] c"opendir 1 failed for directory testdir\0A\00"
@.str.2 = [28 x i8] c"files in directory testdir\0A\00"
@.str.3 = [6 x i8] c"[%s]\0A\00"
@.str.4 = [14 x i8] c"end of files\0A\00"
@.str.5 = [40 x i8] c"opendir 2 failed for directory testdir\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.dirent*
%3 = alloca %struct.dirent*
%4 = alloca i32*
%5 = alloca i32*
store i32 0, i32* %1
%6 = call i32* @opendir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %26
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %17, %11
%14 = load i32*, i32** %4
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %2
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %2
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %20)
br label %13
22: ; preds = %13
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%24 = load i32*, i32** %4
%25 = call i32 @closedir(i32* %24)
br label %26
26: ; preds = %22, %9
%27 = call i32* @opendir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32* %27, i32** %5
%28 = load i32*, i32** %5
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
br label %47
32: ; preds = %26
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %38, %32
%35 = load i32*, i32** %5
%36 = call %struct.dirent* @readdir(i32* %35)
store %struct.dirent* %36, %struct.dirent** %3
%37 = icmp ne %struct.dirent* %36, null
br i1 %37, label %38, label %43
38: ; preds = %34
%39 = load %struct.dirent*, %struct.dirent** %3
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %41)
br label %34
43: ; preds = %34
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%45 = load i32*, i32** %5
%46 = call i32 @closedir(i32* %45)
br label %47
47: ; preds = %43, %30
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [8 x i8] c"testdir\00"
@.str.3 = [6 x i8] c"[%s]\0A\00"
@str.7 = [27 x i8] c"files in directory testdir\00"
@str.8 = [13 x i8] c"end of files\00"
@str.9 = [39 x i8] c"opendir 2 failed for directory testdir\00"
@str.10 = [39 x i8] c"opendir 1 failed for directory testdir\00"
define i32 @main() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.10, i64 0, i64 0))
br label %19
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
%7 = tail call %struct.dirent* @readdir(i32* %1)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %16, label %9
9: ; preds = %5, %9
%10 = phi %struct.dirent* [ %14, %9 ], [ %7, %5 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%14 = tail call %struct.dirent* @readdir(i32* %1)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %16, label %9
16: ; preds = %9, %5
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%18 = tail call i32 @closedir(i32* %1)
br label %19
19: ; preds = %16, %3
%20 = tail call i32* @opendir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.9, i64 0, i64 0))
br label %38
24: ; preds = %19
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
%26 = tail call %struct.dirent* @readdir(i32* %20)
%27 = icmp eq %struct.dirent* %26, null
br i1 %27, label %35, label %28
28: ; preds = %24, %28
%29 = phi %struct.dirent* [ %33, %28 ], [ %26, %24 ]
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i64 0, i32 0
%31 = load i8*, i8** %30
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %31)
%33 = tail call %struct.dirent* @readdir(i32* %20)
%34 = icmp eq %struct.dirent* %33, null
br i1 %34, label %35, label %28
35: ; preds = %28, %24
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%37 = tail call i32 @closedir(i32* %20)
br label %38
38: ; preds = %35, %22
ret i32 0
}