#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 const *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("usage: ls directory-name");
    return 1;
  }
  const char *dir_name = argv[1];
  DIR *dp = opendir(dir_name);
  if (dp == NULL) {
    printf("failed to open directory %s", dir_name);
    return 1;
  }
  struct dirent *dir_entry = NULL;
  while ((dir_entry = readdir(dp)) != NULL) {
    printf("%s\n", dir_entry->d_name);
  }
  closedir(dp);
  return 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)
cmpl   $0x2,-0x8(%rbp)
je     117a <main+0x3a>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1205 <main+0xc5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bc <main+0x7c>
mov    -0x18(%rbp),%rsi
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1205 <main+0xc5>
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11f5 <main+0xb5>
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11c4 <main+0x84>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1187 <main+0x37>
mov    0x8(%rsi),%r14
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11a3 <main+0x53>
mov    %rax,%rbx
nopl   (%rax)
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1197 <main+0x47>
mov    (%rax),%rdi
call   1030 <puts@plt>
jmp    1170 <main+0x20>
lea    0xe72(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11b4 <main+0x64>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
jmp    11b9 <main+0x69>
lea    0xe6f(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [25 x i8] c"usage: ls directory-name\00"
@.str.1 = [28 x i8] c"failed to open directory %s\00"
@.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 i8*
%7 = alloca i32*
%8 = alloca %struct.dirent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %37
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = call i32* @opendir(i8* %17)
store i32* %18, i32** %7
%19 = load i32*, i32** %7
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = load i8*, i8** %6
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 1, i32* %3
br label %37
24: ; preds = %13
store %struct.dirent* null, %struct.dirent** %8
br label %25
25: ; preds = %29, %24
%26 = load i32*, i32** %7
%27 = call %struct.dirent* @readdir(i32* %26)
store %struct.dirent* %27, %struct.dirent** %8
%28 = icmp ne %struct.dirent* %27, null
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load %struct.dirent*, %struct.dirent** %8
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %32)
br label %25
34: ; preds = %25
%35 = load i32*, i32** %7
%36 = call i32 @closedir(i32* %35)
store i32 0, i32* %3
br label %37
37: ; preds = %34, %21, %11
%38 = load i32, i32* %3
ret i32 %38
}

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