#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*/ *);
int main(int argc, char *argv[]) {
  DIR *dp;
  struct dirent *dirp;
  if (argc != 2) {
    printf("Usage: ls <directory_name>\n");
    return 1;
  }
  if ((dp = opendir(argv[1])) == NULL) {
    printf("can't open %s\n", argv[1]);
    return 1;
  }
  while ((dirp = readdir(dp)) != NULL)
    printf("%s\n", dirp->d_name);
  printf("\n");
  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     117a <main+0x3a>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    120b <main+0xcb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b7 <main+0x77>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    120b <main+0xcb>
jmp    11bc <main+0x7c>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11ed <main+0xad>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11bc <main+0x7c>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1197 <main+0x37>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     11bb <main+0x5b>
mov    %rax,%rbx
nopl   (%rax)
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11a5 <main+0x45>
mov    (%rax),%rdi
call   1040 <puts@plt>
jmp    1180 <main+0x20>
lea    0xe71(%rip),%rdi
call   1040 <puts@plt>
jmp    11cd <main+0x6d>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
jmp    11d2 <main+0x72>
mov    0x8(%r14),%rsi
lea    0xe3a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str.1 = [15 x i8] c"can't open %s\0A\00"
@str = [27 x i8] c"Usage: ls <directory_name>\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 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %27
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 %24, label %17
14: ; preds = %6
%15 = load i8*, i8** %7
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %27
17: ; preds = %11, %17
%18 = phi %struct.dirent* [ %22, %17 ], [ %12, %11 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = tail call i32 @puts(i8* dereferenceable(1) %20)
%22 = tail call %struct.dirent* @readdir(i32* %9)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %24, label %17
24: ; preds = %17, %11
%25 = tail call i32 @putchar(i32 10)
%26 = tail call i32 @closedir(i32* %9)
br label %27
27: ; preds = %24, %14, %4
%28 = phi i32 [ 1, %4 ], [ 1, %14 ], [ 0, %24 ]
ret i32 %28
}