#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 stat {
  int st_mode;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int S_IFDIR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
scalar_t__ kl_streq(char *, char *);
char *kl_strerror(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int vfs_lstat(char *, struct stat *);
int /*<<< orphan*/ *vfs_opendir(char *);
void cmd_ls(char *cmd, char *args) {
  if (*args == '\0') {
    printf("Usage: ls <path>\n");
    return;
  }
  DIR *dir = vfs_opendir(args);
  if (!dir) {
    printf("Cannot open %s: %s\n", args, kl_strerror(errno));
    return;
  }
  struct dirent *node;
  struct stat stbuf;
  char filename[1024];
  while ((node = readdir(dir))) {
    if (kl_streq(node->d_name, ".") || kl_streq(node->d_name, "..")) {
      continue;
    }
    snprintf(filename, 1024, "%s/%s", args, node->d_name);
    if (vfs_lstat(filename, &stbuf) == -1) {
      printf("Cannot stat %s: %s\n", args, kl_strerror(errno));
      break;
    }
    if (stbuf.st_mode & S_IFDIR) {
      printf("%s/\n", node->d_name);
    } else {
      printf("%s\n", node->d_name);
    }
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<cmd_ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11b6 <cmd_ls+0x36>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1323 <cmd_ls+0x1a3>
mov    -0x10(%rbp),%rdi
call   1070 <vfs_opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1204 <cmd_ls+0x84>
mov    -0x10(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   10a0 <kl_strerror@plt>
mov    -0x438(%rbp),%rsi
mov    %rax,%rdx
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1323 <cmd_ls+0x1a3>
jmp    1209 <cmd_ls+0x89>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     131a <cmd_ls+0x19a>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf9(%rip),%rsi
call   1030 <kl_streq@plt>
cmp    $0x0,%rax
jne    125a <cmd_ls+0xda>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xddb(%rip),%rsi
call   1030 <kl_streq@plt>
cmp    $0x0,%rax
je     125f <cmd_ls+0xdf>
jmp    1209 <cmd_ls+0x89>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xdac(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x430(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1090 <vfs_lstat@plt>
cmp    $0xffffffff,%eax
jne    12d1 <cmd_ls+0x151>
mov    -0x10(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   10a0 <kl_strerror@plt>
mov    -0x440(%rbp),%rsi
mov    %rax,%rdx
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    131a <cmd_ls+0x19a>
mov    -0x28(%rbp),%eax
mov    0x2cf5(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1300 <cmd_ls+0x180>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1315 <cmd_ls+0x195>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1209 <cmd_ls+0x89>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_ls>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
cmpb   $0x0,(%rsi)
je     1287 <cmd_ls+0xf7>
mov    %rsi,%r14
mov    %rsi,%rdi
call   1080 <vfs_opendir@plt>
test   %rax,%rax
je     12a4 <cmd_ls+0x114>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12f9 <cmd_ls+0x169>
mov    %rax,%r13
lea    0xe39(%rip),%r15
lea    0xe31(%rip),%r12
lea    0x10(%rsp),%rbp
jmp    1214 <cmd_ls+0x84>
lea    0xe40(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12f9 <cmd_ls+0x169>
mov    0x0(%r13),%rdi
mov    %r15,%rsi
call   1040 <kl_streq@plt>
test   %rax,%rax
jne    1200 <cmd_ls+0x70>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1040 <kl_streq@plt>
test   %rax,%rax
jne    1200 <cmd_ls+0x70>
mov    0x0(%r13),%r8
mov    %rbp,%rdi
mov    $0x400,%esi
lea    0xdce(%rip),%rdx
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    %rbp,%rdi
lea    0x8(%rsp),%rsi
call   10a0 <vfs_lstat@plt>
cmp    $0xffffffff,%eax
je     12d7 <cmd_ls+0x147>
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
mov    0x0(%r13),%rsi
test   %eax,0x8(%rsp)
jne    11ea <cmd_ls+0x5a>
mov    %rsi,%rdi
call   1030 <puts@plt>
jmp    1200 <cmd_ls+0x70>
lea    0xda8(%rip),%rdi
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   10b0 <kl_strerror@plt>
lea    0xd47(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   10b0 <kl_strerror@plt>
lea    0xd31(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <closedir@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [18 x i8] c"Usage: ls <path>\0A\00"
@.str.1 = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [20 x i8] c"Cannot stat %s: %s\0A\00"
@S_IFDIR = global i32 0
@.str.6 = [5 x i8] c"%s/\0A\00"
@.str.7 = [4 x i8] c"%s\0A\00"
define void @cmd_ls(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca %struct.stat
%8 = alloca [1024 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %78
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32* @vfs_opendir(i8* %16)
store i32* %17, i32** %5
%18 = load i32*, i32** %5
%19 = icmp ne i32* %18, null
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = load i32, i32* @errno
%23 = call i8* @kl_strerror(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* %23)
br label %78
25: ; preds = %15
br label %26
26: ; preds = %74, %42, %25
%27 = load i32*, i32** %5
%28 = call %struct.dirent* @readdir(i32* %27)
store %struct.dirent* %28, %struct.dirent** %6
%29 = icmp ne %struct.dirent* %28, null
br i1 %29, label %30, label %75
30: ; preds = %26
%31 = load %struct.dirent*, %struct.dirent** %6
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i64 @kl_streq(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%35 = icmp ne i64 %34, 0
br i1 %35, label %42, label %36
36: ; preds = %30
%37 = load %struct.dirent*, %struct.dirent** %6
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i64 @kl_streq(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36, %30
br label %26
43: ; preds = %36
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%45 = load i8*, i8** %4
%46 = load %struct.dirent*, %struct.dirent** %6
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i32 @snprintf(i8* %44, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %45, i8* %48)
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%51 = call i32 @vfs_lstat(i8* %50, %struct.stat* %7)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %58
53: ; preds = %43
%54 = load i8*, i8** %4
%55 = load i32, i32* @errno
%56 = call i8* @kl_strerror(i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %54, i8* %56)
br label %75
58: ; preds = %43
%59 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%60 = load i32, i32* %59
%61 = load i32, i32* @S_IFDIR
%62 = and i32 %60, %61
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %58
%65 = load %struct.dirent*, %struct.dirent** %6
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i8*, i8** %66
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* %67)
br label %74
69: ; preds = %58
%70 = load %struct.dirent*, %struct.dirent** %6
%71 = getelementptr inbounds %struct.dirent, %struct.dirent* %70, i32 0, i32 0
%72 = load i8*, i8** %71
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %72)
br label %74
74: ; preds = %69, %64
br label %26
75: ; preds = %53, %26
%76 = load i32*, i32** %5
%77 = call i32 @closedir(i32* %76)
br label %78
78: ; preds = %75, %20, %13
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str.1 = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [20 x i8] c"Cannot stat %s: %s\0A\00"
@S_IFDIR = global i32 0
@.str.6 = [5 x i8] c"%s/\0A\00"
@str = [17 x i8] c"Usage: ls <path>\00"
define void @cmd_ls(i8* nocapture readnone %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca [1024 x i8]
%5 = load i8, i8* %1
%6 = icmp eq i8 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %57
9: ; preds = %2
%10 = tail call i32* @vfs_opendir(i8* %1)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = tail call i8* @kl_strerror(i32 %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %14)
br label %57
16: ; preds = %9
%17 = bitcast %struct.stat* %3 to i8*
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%19 = call %struct.dirent* @readdir(i32* %10)
%20 = icmp eq %struct.dirent* %19, null
br i1 %20, label %55, label %21
21: ; preds = %16
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %23
23: ; preds = %21, %33
%24 = phi %struct.dirent* [ %19, %21 ], [ %34, %33 ]
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i64 0, i32 0
%26 = load i8*, i8** %25
%27 = call i64 @kl_streq(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i8*, i8** %25
%31 = call i64 @kl_streq(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %51, %53, %23, %29
%34 = call %struct.dirent* @readdir(i32* %10)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %55, label %23
36: ; preds = %29
%37 = load i8*, i8** %25
%38 = call i32 @snprintf(i8* %18, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %1, i8* %37)
%39 = call i32 @vfs_lstat(i8* %18, %struct.stat* %3)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = load i32, i32* @errno
%43 = call i8* @kl_strerror(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %1, i8* %43)
br label %55
45: ; preds = %36
%46 = load i32, i32* %22
%47 = load i32, i32* @S_IFDIR
%48 = and i32 %47, %46
%49 = icmp eq i32 %48, 0
%50 = load i8*, i8** %25
br i1 %49, label %53, label %51
51: ; preds = %45
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* %50)
br label %33
53: ; preds = %45
%54 = call i32 @puts(i8* dereferenceable(1) %50)
br label %33
55: ; preds = %33, %16, %41
%56 = call i32 @closedir(i32* %10)
br label %57
57: ; preds = %12, %55, %7
ret void
}