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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
void dirs_ls(char *path) {
  DIR *dir_p;
  struct dirent *entry_p;
  char *cur_path = malloc(PATH_MAX * sizeof(char));
  cur_path[0] = '.';
  cur_path[1] = '/';
  cur_path[2] = '\0';
  if (!path)
    path = ".";
  strcat(cur_path, path);
  dir_p = opendir(cur_path);
  if (dir_p != NULL) {
    while ((entry_p = readdir(dir_p))) {
      printf("%hu %u %s", entry_p->d_reclen, entry_p->d_type, entry_p->d_name);
      if (entry_p->d_type == 4)
        printf("/");
      printf("\n");
    }
    closedir(dir_p);
  } else {
    fprintf(stderr, "Could not open directory.\n");
  }
  free(cur_path);
}


x86 assembly (clang, -O0):
<dirs_ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e5d(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x2e,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x2f,0x1(%rax)
mov    -0x20(%rbp),%rax
movb   $0x0,0x2(%rax)
cmpq   $0x0,-0x8(%rbp)
jne    11d2 <dirs_ls+0x52>
lea    0xe32(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10a0 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1271 <dirs_ls+0xf1>
jmp    11fc <dirs_ls+0x7c>
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1263 <dirs_ls+0xe3>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%ecx
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
cmpl   $0x4,(%rax)
jne    1250 <dirs_ls+0xd0>
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11fc <dirs_ls+0x7c>
mov    -0x10(%rbp),%rdi
call   1060 <closedir@plt>
jmp    1286 <dirs_ls+0x106>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirs_ls>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
movw   $0x2f2e,(%rax)
movb   $0x0,0x2(%rax)
test   %rbx,%rbx
lea    0xe41(%rip),%rsi
cmovne %rbx,%rsi
mov    %rax,%rdi
call   10b0 <strcat@plt>
mov    %r14,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1247 <dirs_ls+0xb7>
mov    %rax,%r12
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     123d <dirs_ls+0xad>
mov    %rax,%rbx
lea    0xe10(%rip),%r15
jmp    121a <dirs_ls+0x8a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r12,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     123d <dirs_ls+0xad>
mov    0x8(%rbx),%esi
mov    (%rbx),%edx
mov    0x4(%rbx),%ecx
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
cmpl   $0x4,(%rbx)
jne    1200 <dirs_ls+0x70>
mov    $0x2f,%edi
call   1040 <putchar@plt>
jmp    1200 <dirs_ls+0x70>
mov    %r12,%rdi
call   1070 <closedir@plt>
jmp    125c <dirs_ls+0xcc>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
call   1080 <fprintf@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
%struct.dirent = type { i32, i32, i32 }
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [10 x i8] c"%hu %u %s\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [2 x i8] c"\0A\00"
@stderr = global i32 0
@.str.4 = [27 x i8] c"Could not open directory.\0A\00"
define void @dirs_ls(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i32, i32* @PATH_MAX
%7 = sext i32 %6 to i64
%8 = mul i64 %7, 1
%9 = trunc i64 %8 to i32
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = getelementptr inbounds i8, i8* %11, i64 0
store i8 46, i8* %12
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i64 1
store i8 47, i8* %14
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i64 2
store i8 0, i8* %16
%17 = load i8*, i8** %2
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2
br label %20
20: ; preds = %19, %1
%21 = load i8*, i8** %5
%22 = load i8*, i8** %2
%23 = call i32 @strcat(i8* %21, i8* %22)
%24 = load i8*, i8** %5
%25 = call i32* @opendir(i8* %24)
store i32* %25, i32** %3
%26 = load i32*, i32** %3
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %55
28: ; preds = %20
br label %29
29: ; preds = %50, %28
%30 = load i32*, i32** %3
%31 = call %struct.dirent* @readdir(i32* %30)
store %struct.dirent* %31, %struct.dirent** %4
%32 = icmp ne %struct.dirent* %31, null
br i1 %32, label %33, label %52
33: ; preds = %29
%34 = load %struct.dirent*, %struct.dirent** %4
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 2
%36 = load i32, i32* %35
%37 = load %struct.dirent*, %struct.dirent** %4
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = load %struct.dirent*, %struct.dirent** %4
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 %39, i32 %42)
%44 = load %struct.dirent*, %struct.dirent** %4
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = icmp eq i32 %46, 4
br i1 %47, label %48, label %50
48: ; preds = %33
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %33
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %29
52: ; preds = %29
%53 = load i32*, i32** %3
%54 = call i32 @closedir(i32* %53)
br label %58
55: ; preds = %20
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %58
58: ; preds = %55, %52
%59 = load i8*, i8** %5
%60 = call i32 @free(i8* %59)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i32, i32, i32 }
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [10 x i8] c"%hu %u %s\00"
@stderr = global i32 0
@.str.4 = [27 x i8] c"Could not open directory.\0A\00"
define void @dirs_ls(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = tail call i8* @malloc(i32 %2)
store i8 46, i8* %3
%4 = getelementptr inbounds i8, i8* %3, i64 1
store i8 47, i8* %4
%5 = getelementptr inbounds i8, i8* %3, i64 2
store i8 0, i8* %5
%6 = icmp eq i8* %0, null
%7 = select i1 %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %0
%8 = tail call i32 @strcat(i8* %3, i8* %7)
%9 = tail call i32* @opendir(i8* %3)
%10 = icmp eq i32* %9, null
br i1 %10, label %33, label %11
11: ; preds = %1
%12 = tail call %struct.dirent* @readdir(i32* %9)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %31, label %14
14: ; preds = %11, %27
%15 = phi %struct.dirent* [ %29, %27 ], [ %12, %11 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 2
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 1
%21 = load i32, i32* %20
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %19, i32 %21)
%23 = load i32, i32* %18
%24 = icmp eq i32 %23, 4
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = tail call i32 @putchar(i32 47)
br label %27
27: ; preds = %25, %14
%28 = tail call i32 @putchar(i32 10)
%29 = tail call %struct.dirent* @readdir(i32* %9)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %31, label %14
31: ; preds = %27, %11
%32 = tail call i32 @closedir(i32* %9)
br label %36
33: ; preds = %1
%34 = load i32, i32* @stderr
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %33, %31
%37 = tail call i32 @free(i8* %3)
ret void
}