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

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int do_ls(char *pathname) {
  DIR *directory;
  struct dirent *dir;
  int ret;
  char *slash = "/";
  char *symbol;
  if (pathname == NULL)
    pathname = ".";
  directory = opendir(pathname);
  if (!directory)
    return -1;
  while ((dir = readdir(directory)) != NULL) {
    if (*dir->d_name == '.') {
      continue;
    }
    symbol = dir->d_type == DT_DIR ? slash : "";
    printf("%s%ls  ", symbol, dir->d_name);
  }
  printf("\n");
  closedir(directory);
  return 0;
}


x86 assembly (clang, -O0):
<do_ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xead(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <do_ls+0x2d>
lea    0xe99(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1191 <do_ls+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    123e <do_ls+0xfe>
jmp    1196 <do_ls+0x56>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1220 <do_ls+0xe0>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11c5 <do_ls+0x85>
jmp    1196 <do_ls+0x56>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
jne    11ea <do_ls+0xaa>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11fa <do_ls+0xba>
lea    0xe1c(%rip),%rax
mov    %rax,-0x40(%rbp)
jmp    11fa <do_ls+0xba>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1196 <do_ls+0x56>
lea    0xde5(%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    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_ls>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rdi,%rdi
lea    0xe9f(%rip),%rax
cmove  %rax,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11e6 <do_ls+0x96>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11d0 <do_ls+0x80>
mov    0x2e58(%rip),%r15
lea    0xe71(%rip),%r12
lea    0xe75(%rip),%r13
lea    0xe67(%rip),%r14
jmp    11b7 <do_ls+0x67>
nop
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11d0 <do_ls+0x80>
mov    (%rax),%rdx
cmpb   $0x2e,(%rdx)
je     11aa <do_ls+0x5a>
mov    0x8(%rax),%rax
mov    %r12,%rsi
cmp    (%r15),%rax
je     11a0 <do_ls+0x50>
mov    %r13,%rsi
jmp    11a0 <do_ls+0x50>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1060 <closedir@plt>
xor    %eax,%eax
jmp    11eb <do_ls+0x9b>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c".\00"
@DT_DIR = global i64 0
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [8 x i8] c"%s%ls \00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @do_ls(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %3
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %7
%9 = load i8*, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %12
12: ; preds = %11, %1
%13 = load i8*, i8** %3
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %4
%15 = load i32*, i32** %4
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %12
store i32 -1, i32* %2
br label %51
18: ; preds = %12
br label %19
19: ; preds = %40, %30, %18
%20 = load i32*, i32** %4
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %5
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %47
23: ; preds = %19
%24 = load %struct.dirent*, %struct.dirent** %5
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 46
br i1 %29, label %30, label %31
30: ; preds = %23
br label %19
31: ; preds = %23
%32 = load %struct.dirent*, %struct.dirent** %5
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 1
%34 = load i64, i64* %33
%35 = load i64, i64* @DT_DIR
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = load i8*, i8** %7
br label %40
39: ; preds = %31
br label %40
40: ; preds = %39, %37
%41 = phi i8* [ %38, %37 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), %39 ]
store i8* %41, i8** %8
%42 = load i8*, i8** %8
%43 = load %struct.dirent*, %struct.dirent** %5
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %42, i8* %45)
br label %19
47: ; preds = %19
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%49 = load i32*, i32** %4
%50 = call i32 @closedir(i32* %49)
store i32 0, i32* %2
br label %51
51: ; preds = %47, %17
%52 = load i32, i32* %2
ret i32 %52
}

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