#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*/ *);
void listDirectory() {
  DIR *dir;
  struct dirent *entry;
  dir = opendir("./");
  if (dir != NULL) {
    while ((entry = readdir(dir))) {
      printf("\t\t%s\n", entry->d_name);
    }
    closedir(dir);
  } else {
    printf("ERROR: Could not open the directory\n");
  }
}


x86 assembly (clang, -O0):
<listDirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a7 <listDirectory+0x67>
jmp    1168 <listDirectory+0x28>
mov    -0x8(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1199 <listDirectory+0x59>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1168 <listDirectory+0x28>
mov    -0x8(%rbp),%rdi
call   1050 <closedir@plt>
jmp    11b5 <listDirectory+0x75>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listDirectory>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11a9 <listDirectory+0x59>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     119a <listDirectory+0x4a>
lea    0xe87(%rip),%r14
nopl   0x0(%rax)
mov    (%rax),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    1180 <listDirectory+0x30>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <closedir@plt>
lea    0xe59(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <puts@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"./\00"
@.str.1 = [6 x i8] c"\09\09%s\0A\00"
@.str.2 = [37 x i8] c"ERROR: Could not open the directory\0A\00"
define void @listDirectory() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %3, i32** %1
%4 = load i32*, i32** %1
%5 = icmp ne i32* %4, null
br i1 %5, label %6, label %19
6: ; preds = %0
br label %7
7: ; preds = %11, %6
%8 = load i32*, i32** %1
%9 = call %struct.dirent* @readdir(i32* %8)
store %struct.dirent* %9, %struct.dirent** %2
%10 = icmp ne %struct.dirent* %9, null
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = load %struct.dirent*, %struct.dirent** %2
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i32 0, i32 0
%14 = load i8*, i8** %13
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %7
16: ; preds = %7
%17 = load i32*, i32** %1
%18 = call i32 @closedir(i32* %17)
br label %21
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"./\00"
@.str.1 = [6 x i8] c"\09\09%s\0A\00"
@str = [36 x i8] c"ERROR: Could not open the directory\00"
define void @listDirectory() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %15, label %3
3: ; preds = %0
%4 = tail call %struct.dirent* @readdir(i32* %1)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %13, label %6
6: ; preds = %3, %6
%7 = phi %struct.dirent* [ %11, %6 ], [ %4, %3 ]
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%9 = load i8*, i8** %8
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = tail call %struct.dirent* @readdir(i32* %1)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %13, label %6
13: ; preds = %6, %3
%14 = tail call i32 @closedir(i32* %1)
br label %17
15: ; preds = %0
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %17
17: ; preds = %15, %13
ret void
}