#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, const char *argv[]) {
  DIR *dp;
  struct dirent *dirp;
  if (argc != 2) {
    fprintf(stderr, "Usage: ls directory_name");
    exit(1);
  }
  if ((dp = opendir(argv[1])) == NULL) {
    fprintf(stderr, "can't open %s", argv[1]);
  }
  while ((dirp = readdir(dp)) != NULL) {
    printf("%s \n", dirp->d_name);
  }
  closedir(dp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a1 <main+0x41>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11db <main+0x7b>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    11e0 <main+0x80>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     120f <main+0xaf>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe22(%rip),%rdi
call   1040 <printf@plt>
jmp    11e0 <main+0x80>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11da <main+0x7a>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1198 <main+0x38>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xe88(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11c8 <main+0x68>
lea    0xe7b(%rip),%r14
nopl   0x0(%rax)
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
jne    11b0 <main+0x50>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [25 x i8] c"Usage: ls directory_name\00"
@.str.1 = [14 x i8] c"can't open %s\00"
@.str.2 = [5 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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32* @opendir(i8* %17)
store i32* %18, i32** %6
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %26
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %20, %14
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 ([5 x i8], [5 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)
ret i32 0
}

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