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

/* Variables and functions */
int MAXPATHLEN;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ ls_dofile(struct stat, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int stat(char *, struct stat *);
int strlen(char *);
void cmd_ls(int argc, char *argv[]) {
  char *path = ".";
  DIR *dirp;
  struct dirent *dp;
  struct stat sb;
  if (argc > 1)
    path = argv[1];
  if (stat(path, &sb) == -1) {
    perror("stat");
    return;
  }
  if (!S_ISDIR(sb.st_mode)) {
    ls_dofile(sb, path);
  } else {
    if ((dirp = opendir(path)) == NULL) {
      perror("opendir");
      return;
    }
    while ((dp = readdir(dirp)) != NULL) {
      char buf[MAXPATHLEN + 1];
      if (strlen(path) + strlen(dp->d_name) + 1 > MAXPATHLEN)
        continue;
      snprintf(buf, MAXPATHLEN, "%s/%s", path, dp->d_name);
      if (stat(buf, &sb) == -1)
        continue;
      ls_dofile(sb, dp->d_name);
    }
  }
}


x86 assembly (clang, -O0):
<cmd_ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe6a(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x1,-0x4(%rbp)
jle    11b0 <cmd_ls+0x30>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11d7 <cmd_ls+0x57>
lea    0xe35(%rip),%rdi
call   1090 <perror@plt>
jmp    132b <cmd_ls+0x1ab>
mov    -0x30(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11f9 <cmd_ls+0x79>
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%edi
call   10a0 <ls_dofile@plt>
jmp    132b <cmd_ls+0x1ab>
mov    -0x18(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1221 <cmd_ls+0xa1>
lea    0xdf0(%rip),%rdi
call   1090 <perror@plt>
jmp    132b <cmd_ls+0x1ab>
jmp    1226 <cmd_ls+0xa6>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1326 <cmd_ls+0x1a6>
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    0x2d53(%rip),%rcx
cmp    (%rcx),%eax
jle    12a9 <cmd_ls+0x129>
movl   $0x2,-0x44(%rbp)
jmp    1305 <cmd_ls+0x185>
mov    -0x50(%rbp),%rdi
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
lea    0xd47(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    12ef <cmd_ls+0x16f>
movl   $0x2,-0x44(%rbp)
jmp    1305 <cmd_ls+0x185>
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
mov    -0x30(%rbp),%edi
call   10a0 <ls_dofile@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x44(%rbp),%eax
test   %eax,%eax
je     1321 <cmd_ls+0x1a1>
jmp    131c <cmd_ls+0x19c>
jmp    1226 <cmd_ls+0xa6>
jmp    1226 <cmd_ls+0xa6>
jmp    132b <cmd_ls+0x1ab>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_ls>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jl     119c <cmd_ls+0x1c>
mov    0x8(%rsi),%r14
jmp    11a3 <cmd_ls+0x23>
lea    0xe5d(%rip),%r14
lea    -0x30(%rbp),%rsi
mov    %r14,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     12b2 <cmd_ls+0x132>
mov    -0x30(%rbp),%ebx
mov    %ebx,%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     129a <cmd_ls+0x11a>
mov    %r14,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12bb <cmd_ls+0x13b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     128b <cmd_ls+0x10b>
mov    %rax,%r13
mov    %rbx,-0x38(%rbp)
jmp    1215 <cmd_ls+0x95>
nopl   0x0(%rax,%rax,1)
mov    -0x40(%rbp),%rsp
mov    -0x38(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     128b <cmd_ls+0x10b>
mov    %rsp,-0x40(%rbp)
mov    0x2dc8(%rip),%rbx
mov    (%rbx),%eax
add    $0x1,%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    0x0(%r13),%rdi
call   1040 <strlen@plt>
add    %r12d,%eax
mov    (%rbx),%esi
cmp    %esi,%eax
jge    1200 <cmd_ls+0x80>
mov    0x0(%r13),%r8
mov    %r15,%rdi
lea    0xdae(%rip),%rdx
mov    %r14,%rcx
call   1060 <snprintf@plt>
mov    %r15,%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     1200 <cmd_ls+0x80>
mov    0x0(%r13),%rsi
mov    -0x30(%rbp),%edi
call   10a0 <ls_dofile@plt>
jmp    1200 <cmd_ls+0x80>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
mov    %r14,%rsi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <ls_dofile@plt>
lea    0xd49(%rip),%rdi
jmp    12c2 <cmd_ls+0x142>
lea    0xd45(%rip),%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <perror@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [5 x i8] c"stat\00"
@.str.2 = [8 x i8] c"opendir\00"
@MAXPATHLEN = global i32 0
@.str.3 = [6 x i8] c"%s/%s\00"
define void @cmd_ls(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %5
%12 = load i32, i32* %3
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8**, i8*** %4
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %5
br label %18
18: ; preds = %14, %2
%19 = load i8*, i8** %5
%20 = call i32 @stat(i8* %19, %struct.stat* %8)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %84
24: ; preds = %18
%25 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @S_ISDIR(i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %24
%30 = load i8*, i8** %5
%31 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @ls_dofile(i32 %32, i8* %30)
br label %84
34: ; preds = %24
%35 = load i8*, i8** %5
%36 = call i32* @opendir(i8* %35)
store i32* %36, i32** %6
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %84
40: ; preds = %34
br label %41
41: ; preds = %82, %79, %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 %83
45: ; preds = %41
%46 = load i32, i32* @MAXPATHLEN
%47 = add nsw i32 %46, 1
%48 = zext i32 %47 to i64
%49 = call i8* @llvm.stacksave()
store i8* %49, i8** %9
%50 = alloca i8, i64 %48
store i64 %48, i64* %10
%51 = load i8*, i8** %5
%52 = call i32 @strlen(i8* %51)
%53 = load %struct.dirent*, %struct.dirent** %7
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = call i32 @strlen(i8* %55)
%57 = add nsw i32 %52, %56
%58 = add nsw i32 %57, 1
%59 = load i32, i32* @MAXPATHLEN
%60 = icmp sgt i32 %58, %59
br i1 %60, label %61, label %62
61: ; preds = %45
store i32 2, i32* %11
br label %79
62: ; preds = %45
%63 = load i32, i32* @MAXPATHLEN
%64 = load i8*, i8** %5
%65 = load %struct.dirent*, %struct.dirent** %7
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i8*, i8** %66
%68 = call i32 @snprintf(i8* %50, i32 %63, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %64, i8* %67)
%69 = call i32 @stat(i8* %50, %struct.stat* %8)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %72
71: ; preds = %62
store i32 2, i32* %11
br label %79
72: ; preds = %62
%73 = load %struct.dirent*, %struct.dirent** %7
%74 = getelementptr inbounds %struct.dirent, %struct.dirent* %73, i32 0, i32 0
%75 = load i8*, i8** %74
%76 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%77 = load i32, i32* %76
%78 = call i32 @ls_dofile(i32 %77, i8* %75)
store i32 0, i32* %11
br label %79
79: ; preds = %72, %71, %61
%80 = load i8*, i8** %9
%81 = load i32, i32* %11
switch i32 %81, label %85 [
i32 0, label %82
i32 2, label %41
]
82: ; preds = %79
br label %41
83: ; preds = %41
br label %84
84: ; preds = %22, %38, %83, %29
ret void
85: ; preds = %79
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [5 x i8] c"stat\00"
@.str.2 = [8 x i8] c"opendir\00"
@MAXPATHLEN = global i32 0
@.str.3 = [6 x i8] c"%s/%s\00"
define void @cmd_ls(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %6, %2
%10 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
%11 = call i32 @stat(i8* %10, %struct.stat* %3)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %56
15: ; preds = %9
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @S_ISDIR(i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @ls_dofile(i32 %17, i8* %10)
br label %56
22: ; preds = %15
%23 = tail call i32* @opendir(i8* %10)
%24 = icmp eq i32* %23, null
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = call %struct.dirent* @readdir(i32* %23)
%27 = icmp eq %struct.dirent* %26, null
br i1 %27, label %56, label %30
28: ; preds = %22
%29 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %56
30: ; preds = %25, %53
%31 = phi %struct.dirent* [ %54, %53 ], [ %26, %25 ]
%32 = load i32, i32* @MAXPATHLEN
%33 = add nsw i32 %32, 1
%34 = zext i32 %33 to i64
%35 = call i8* @llvm.stacksave()
%36 = alloca i8, i64 %34
%37 = call i32 @strlen(i8* %10)
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i64 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 %40, %37
%42 = load i32, i32* @MAXPATHLEN
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %53
44: ; preds = %30
%45 = load i8*, i8** %38
%46 = call i32 @snprintf(i8* %36, i32 %42, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %10, i8* %45)
%47 = call i32 @stat(i8* %36, %struct.stat* %3)
%48 = icmp eq i32 %47, -1
br i1 %48, label %53, label %49
49: ; preds = %44
%50 = load i8*, i8** %38
%51 = load i32, i32* %16
%52 = call i32 @ls_dofile(i32 %51, i8* %50)
br label %53
53: ; preds = %44, %30, %49
%54 = call %struct.dirent* @readdir(i32* %23)
%55 = icmp eq %struct.dirent* %54, null
br i1 %55, label %56, label %30
56: ; preds = %53, %25, %20, %28, %13
ret void
}