#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*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  DIR *dir;
  struct dirent *direntry;
  char *dirname = ".";
  if (argc > 1)
    dirname = argv[1];
  printf("\n **** DISPLAYING DIRECTORY %s ****\n\n", dirname);
  if (!(dir = opendir(dirname))) {
    perror(dirname);
    exit(errno);
  };
  while ((direntry = readdir(dir)))
    printf("%s\n", direntry->d_name);
  closedir(dir);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1197 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe60(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d5 <main+0x75>
mov    -0x28(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11da <main+0x7a>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1209 <main+0xa9>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe29(%rip),%rdi
call   1040 <printf@plt>
jmp    11da <main+0x7a>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     116f <main+0xf>
mov    0x8(%rsi),%r14
jmp    1176 <main+0x16>
lea    0xe8a(%rip),%r14
lea    0xe85(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
jne    11a8 <main+0x48>
mov    %r14,%rdi
call   1070 <perror@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11d8 <main+0x78>
lea    0xe69(%rip),%r14
nop
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
jne    11c0 <main+0x60>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %edi,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [38 x i8] c"\0A **** DISPLAYING DIRECTORY %s ****\0A\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
br label %7
7: ; preds = %4, %2
%8 = phi i8* [ %6, %4 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
%9 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = tail call i32* @opendir(i8* %8)
%11 = icmp eq i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = tail call %struct.dirent* @readdir(i32* %10)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %26, label %19
15: ; preds = %7
%16 = tail call i32 @perror(i8* %8)
%17 = load i32, i32* @errno
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %12, %19
%20 = phi %struct.dirent* [ %24, %19 ], [ %13, %12 ]
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = tail call %struct.dirent* @readdir(i32* %10)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %26, label %19
26: ; preds = %19, %12
%27 = tail call i32 @closedir(i32* %10)
%28 = tail call i32 @exit(i32 0)
unreachable
}