#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*/ free(char *);
int /*<<< orphan*/ *getcwd(char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  DIR *dp = NULL;
  struct dirent *dirp = NULL;
  char *path = NULL;
  if (argc > 2) {
    printf("Usage: ls directory_name");
    exit(1);
  } else if (argc == 1) {
    path = (char *)malloc(1024);
    if (getcwd(path, 1024) == NULL) {
      printf("getcwd failed!\n");
      exit(1);
    }
  } else {
    path = argv[1];
  }
  if ((dp = opendir(path)) == NULL) {
    printf("can't open %s", path);
    exit(1);
  }
  while ((dirp = readdir(dp)) != NULL) {
    printf("%s\n", dirp->d_name);
  }
  closedir(dp);
  dp = NULL;
  if (argc == 1) {
    free(path);
  }
  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)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11d0 <main+0x50>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x1,-0x8(%rbp)
jne    121d <main+0x9d>
mov    $0x400,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
cmp    $0x0,%rax
jne    1218 <main+0x98>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1229 <main+0xa9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    122e <main+0xae>
mov    -0x28(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1261 <main+0xe1>
mov    -0x28(%rbp),%rsi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1266 <main+0xe6>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1297 <main+0x117>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1266 <main+0xe6>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
movq   $0x0,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    12bb <main+0x13b>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x3,%edi
jge    1223 <main+0x93>
mov    %edi,%ebp
cmp    $0x1,%edi
jne    11d9 <main+0x49>
mov    $0x400,%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x400,%esi
call   1070 <getcwd@plt>
test   %rax,%rax
jne    11dd <main+0x4d>
lea    0xe5d(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x8(%rsi),%r14
mov    %r14,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     123b <main+0xab>
mov    %rax,%rbx
nopl   (%rax)
mov    %rbx,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1207 <main+0x77>
mov    (%rax),%rdi
call   1040 <puts@plt>
jmp    11f0 <main+0x60>
mov    %rbx,%rdi
call   1080 <closedir@plt>
cmp    $0x1,%ebp
jne    121c <main+0x8c>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdd6(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdd7(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [25 x i8] c"Usage: ls directory_name\00"
@.str.1 = [16 x i8] c"getcwd failed!\0A\00"
@.str.2 = [14 x i8] c"can't open %s\00"
@.str.3 = [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 i32* null, i32** %6
store %struct.dirent* null, %struct.dirent** %7
store i8* null, i8** %8
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(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 i32, i32* %4
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %27
17: ; preds = %14
%18 = call i64 @malloc(i32 1024)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = call i32* @getcwd(i8* %20, i32 1024)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
br label %31
27: ; preds = %14
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
store i8* %30, i8** %8
br label %31
31: ; preds = %27, %26
br label %32
32: ; preds = %31
%33 = load i8*, i8** %8
%34 = call i32* @opendir(i8* %33)
store i32* %34, i32** %6
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i8*, i8** %8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %32
br label %41
41: ; preds = %45, %40
%42 = load i32*, i32** %6
%43 = call %struct.dirent* @readdir(i32* %42)
store %struct.dirent* %43, %struct.dirent** %7
%44 = icmp ne %struct.dirent* %43, null
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = load %struct.dirent*, %struct.dirent** %7
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %48)
br label %41
50: ; preds = %41
%51 = load i32*, i32** %6
%52 = call i32 @closedir(i32* %51)
store i32* null, i32** %6
%53 = load i32, i32* %4
%54 = icmp eq i32 %53, 1
br i1 %54, label %55, label %58
55: ; preds = %50
%56 = load i8*, i8** %8
%57 = call i32 @free(i8* %56)
br label %58
58: ; preds = %55, %50
ret i32 0
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [25 x i8] c"Usage: ls directory_name\00"
@.str.2 = [14 x i8] c"can't open %s\00"
@str = [15 x i8] c"getcwd failed!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 2
br i1 %3, label %4, label %7
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))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = icmp eq i32 %0, 1
br i1 %8, label %9, label %17
9: ; preds = %7
%10 = tail call i64 @malloc(i32 1024)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32* @getcwd(i8* %11, i32 1024)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %7
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
br label %20
20: ; preds = %17, %9
%21 = phi i8* [ %11, %9 ], [ %19, %17 ]
%22 = tail call i32* @opendir(i8* %21)
%23 = icmp eq i32* %22, null
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = tail call %struct.dirent* @readdir(i32* %22)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %37, label %30
27: ; preds = %20
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %24, %30
%31 = phi %struct.dirent* [ %35, %30 ], [ %25, %24 ]
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i64 0, i32 0
%33 = load i8*, i8** %32
%34 = tail call i32 @puts(i8* dereferenceable(1) %33)
%35 = tail call %struct.dirent* @readdir(i32* %22)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %37, label %30
37: ; preds = %30, %24
%38 = tail call i32 @closedir(i32* %22)
br i1 %8, label %39, label %41
39: ; preds = %37
%40 = tail call i32 @free(i8* %21)
br label %41
41: ; preds = %39, %37
ret i32 0
}