#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*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
void listDir(char *dir) {
  DIR *dp;
  struct dirent *ep;
  dp = opendir(dir);
  if (dp != NULL) {
    while (ep = readdir(dp))
      puts(ep->d_name);
    (void)closedir(dp);
  } else
    perror("Couldn't open the directory!\n");
}


x86 assembly (clang, -O0):
<listDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ae <listDir+0x5e>
jmp    1179 <listDir+0x29>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11a0 <listDir+0x50>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1030 <puts@plt>
jmp    1179 <listDir+0x29>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
jmp    11ba <listDir+0x6a>
lea    0xe4b(%rip),%rdi
call   1070 <perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listDir>:
push   %rbx
call   1040 <opendir@plt>
test   %rax,%rax
je     117f <listDir+0x2f>
mov    %rax,%rbx
xchg   %ax,%ax
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     1176 <listDir+0x26>
mov    (%rax),%edi
call   1030 <puts@plt>
jmp    1160 <listDir+0x10>
mov    %rbx,%rdi
pop    %rbx
jmp    1050 <closedir@plt>
lea    0xe7a(%rip),%rdi
pop    %rbx
jmp    1070 <perror@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [30 x i8] c"Couldn't open the directory!\0A\00"
define void @listDir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32* @opendir(i8* %5)
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %22
9: ; preds = %1
br label %10
10: ; preds = %14, %9
%11 = load i32*, i32** %3
%12 = call %struct.dirent* @readdir(i32* %11)
store %struct.dirent* %12, %struct.dirent** %4
%13 = icmp ne %struct.dirent* %12, null
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = load %struct.dirent*, %struct.dirent** %4
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @puts(i32 %17)
br label %10
19: ; preds = %10
%20 = load i32*, i32** %3
%21 = call i32 @closedir(i32* %20)
br label %24
22: ; preds = %1
%23 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [30 x i8] c"Couldn't open the directory!\0A\00"
define void @listDir(i8* nocapture %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %16, label %4
4: ; preds = %1
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %14, label %7
7: ; preds = %4, %7
%8 = phi %struct.dirent* [ %12, %7 ], [ %5, %4 ]
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @puts(i32 %10)
%12 = tail call %struct.dirent* @readdir(i32* %2)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %14, label %7
14: ; preds = %7, %4
%15 = tail call i32 @closedir(i32* %2)
br label %18
16: ; preds = %1
%17 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
ret void
}