#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 PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ lstat(char *, struct stat *);
int /*<<< orphan*/ *opendir(char const *const);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int snprintf(char *, int, char *, char const *const, char *);
int /*<<< orphan*/ strcmp(char *, char *);
void ls(const char *const path) {
  DIR *pDir;
  struct dirent *pFile;
  struct stat st;
  char pPathBuffer[PATH_MAX];
  int pathLen;
  pDir = opendir(path);
  while ((pFile = readdir(pDir)) != NULL) {
    if (!strcmp(pFile->d_name, ".") || !strcmp(pFile->d_name, "..")) {
      continue;
    }
    pathLen = snprintf(pPathBuffer, PATH_MAX, "%s/%s", path, pFile->d_name);
    if (pathLen >= PATH_MAX) {
      printf("Long path name skipped: %s/%s", path, pFile->d_name);
      continue;
    }
    printf("%s", pPathBuffer);
    lstat(pPathBuffer, &st);
    if (S_ISDIR(st.st_mode)) {
      printf("/\n");
      ls(pPathBuffer);
    } else {
      printf("\n");
    }
  }
  closedir(pDir);
}


x86 assembly (clang, -O0):
<ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12e0 <ls+0x150>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe07(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
je     1224 <ls+0x94>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdea(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    1229 <ls+0x99>
jmp    11d5 <ls+0x45>
mov    -0x40(%rbp),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
lea    0xdd3(%rip),%rdx
call   1060 <snprintf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%eax
jl     1280 <ls+0xf0>
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11d5 <ls+0x45>
mov    -0x40(%rbp),%rsi
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <lstat@plt>
mov    -0x20(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12cd <ls+0x13d>
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1070 <ls@plt>
jmp    12db <ls+0x14b>
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11d5 <ls+0x45>
mov    -0x10(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    %rsp,-0x40(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
call   1050 <opendir@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   10d0 <readdir@plt>
test   %rax,%rax
je     12f2 <ls+0x142>
mov    %rax,%r13
lea    0xdf8(%rip),%rbx
mov    %r15,-0x30(%rbp)
jmp    1246 <ls+0x96>
mov    0x0(%r13),%rdx
lea    0xde9(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%r14
mov    -0x30(%rbp),%r15
lea    0xdcf(%rip),%rbx
mov    %r15,%rdi
call   10d0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12f2 <ls+0x142>
mov    0x0(%r13),%r12
mov    %r12,%rdi
mov    %rbx,%rsi
call   10c0 <strcmp@plt>
test   %eax,%eax
je     1232 <ls+0x82>
mov    %r12,%rdi
lea    0xd9d(%rip),%rsi
call   10c0 <strcmp@plt>
test   %eax,%eax
je     1232 <ls+0x82>
mov    0x2d7d(%rip),%rax
mov    %r14,%rbx
mov    %rax,%r14
mov    (%rax),%esi
mov    -0x48(%rbp),%r15
mov    %r15,%rdi
lea    0xd92(%rip),%rdx
mov    %rbx,%rcx
mov    %r12,%r8
call   1080 <snprintf@plt>
cmp    (%r14),%eax
jge    120f <ls+0x5f>
mov    %rbx,%r14
lea    0xd77(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
lea    -0x38(%rbp),%rsi
call   10b0 <lstat@plt>
mov    -0x38(%rbp),%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     12e3 <ls+0x133>
lea    0xd50(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1090 <ls@plt>
jmp    1227 <ls+0x77>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1227 <ls+0x77>
mov    %r15,%rdi
call   10a0 <closedir@plt>
mov    -0x40(%rbp),%rsp
lea    -0x28(%rbp),%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 }
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [30 x i8] c"Long path name skipped: %s/%s\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [3 x i8] c"/\0A\00"
@.str.6 = [2 x i8] c"\0A\00"
define void @ls(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
%13 = load i8*, i8** %2
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %3
br label %15
15: ; preds = %59, %42, %31, %1
%16 = load i32*, i32** %3
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %4
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %60
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %4
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @strcmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %19
%26 = load %struct.dirent*, %struct.dirent** %4
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i32 @strcmp(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp ne i32 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %25, %19
br label %15
32: ; preds = %25
%33 = load i32, i32* @PATH_MAX
%34 = load i8*, i8** %2
%35 = load %struct.dirent*, %struct.dirent** %4
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i32 @snprintf(i8* %12, i32 %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %34, i8* %37)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = load i32, i32* @PATH_MAX
%41 = icmp sge i32 %39, %40
br i1 %41, label %42, label %48
42: ; preds = %32
%43 = load i8*, i8** %2
%44 = load %struct.dirent*, %struct.dirent** %4
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %43, i8* %46)
br label %15
48: ; preds = %32
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %12)
%50 = call i32 @lstat(i8* %12, %struct.stat* %5)
%51 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i64 @S_ISDIR(i32 %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %48
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %59
57: ; preds = %48
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %59
59: ; preds = %57, %55
br label %15
60: ; preds = %15
%61 = load i32*, i32** %3
%62 = call i32 @closedir(i32* %61)
%63 = load i8*, i8** %6
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [30 x i8] c"Long path name skipped: %s/%s\00"
@.str.4 = [3 x i8] c"%s\00"
@str = [2 x i8] c"/\00"
define void @ls(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = tail call i32* @opendir(i8* %0)
%9 = call %struct.dirent* @readdir(i32* %8)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %43, label %11
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %13
13: ; preds = %11, %22
%14 = phi %struct.dirent* [ %9, %11 ], [ %23, %22 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @strcmp(i8* dereferenceable(1) %16, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %13
%20 = call i32 @strcmp(i8* dereferenceable(1) %16, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %39, %41, %13, %19, %30
%23 = call %struct.dirent* @readdir(i32* %8)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %43, label %13
25: ; preds = %19
%26 = load i32, i32* @PATH_MAX
%27 = call i32 @snprintf(i8* %7, i32 %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %16)
%28 = load i32, i32* @PATH_MAX
%29 = icmp slt i32 %27, %28
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = load i8*, i8** %15
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %31)
br label %22
33: ; preds = %25
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %7)
%35 = call i32 @lstat(i8* %7, %struct.stat* %2)
%36 = load i32, i32* %12
%37 = call i64 @S_ISDIR(i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %33
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
br label %22
41: ; preds = %33
%42 = call i32 @putchar(i32 10)
br label %22
43: ; preds = %22, %1
%44 = call i32 @closedir(i32* %8)
ret void
}