#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 */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void compute_tree(char *current_file, int lev) {
  int i;
  for (i = 0; i < lev; ++i) {
    printf("|---");
  }
  printf("%s\n", current_file);
  struct stat st;
  stat(current_file, &st);
  if (S_ISDIR(st.st_mode) == 0) {
    return;
  }
  DIR *pd = opendir(current_file);
  struct dirent *pde;
  while ((pde = readdir(pd)) != NULL) {
    if (strcmp(pde->d_name, ".") == 0) {
      continue;
    }
    if (strcmp(pde->d_name, "..") == 0) {
      continue;
    }
    char *new_dir = (char *)malloc(1000);
    strcpy(new_dir, current_file);
    strcat(new_dir, "/");
    strcat(new_dir, pde->d_name);
    compute_tree(new_dir, lev + 1);
    free(new_dir);
  }
  if (pd != NULL) {
    closedir(pd);
  }
}


x86 assembly (clang, -O0):
<compute_tree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11fe <compute_tree+0x3e>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11d6 <compute_tree+0x16>
mov    -0x8(%rbp),%rsi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10b0 <stat@plt>
mov    -0x18(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    1234 <compute_tree+0x74>
jmp    1308 <compute_tree+0x148>
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12f4 <compute_tree+0x134>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xda4(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    127a <compute_tree+0xba>
jmp    1241 <compute_tree+0x81>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xd81(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    129c <compute_tree+0xdc>
jmp    1241 <compute_tree+0x81>
mov    $0x3e8,%edi
call   10d0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
lea    0xd4a(%rip),%rsi
call   10e0 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
call   10e0 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
call   1080 <compute_tree@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    1241 <compute_tree+0x81>
cmpq   $0x0,-0x20(%rbp)
je     1308 <compute_tree+0x148>
mov    -0x20(%rbp),%rdi
call   1090 <closedir@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_tree>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
test   %esi,%esi
jle    11ff <compute_tree+0x2f>
lea    0xe14(%rip),%rbx
mov    %r14d,%ebp
nop
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0xffffffff,%ebp
jne    11f0 <compute_tree+0x20>
mov    %r15,%rdi
call   1050 <puts@plt>
mov    %rsp,%rsi
mov    %r15,%rdi
call   10c0 <stat@plt>
mov    (%rsp),%edi
call   1080 <S_ISDIR@plt>
test   %rax,%rax
je     12e9 <compute_tree+0x119>
mov    %r15,%rdi
call   1060 <opendir@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   10d0 <readdir@plt>
test   %rax,%rax
je     12ce <compute_tree+0xfe>
mov    %rax,%r13
add    $0x1,%r14d
lea    0xdb9(%rip),%rbx
jmp    1260 <compute_tree+0x90>
nop
mov    %rbp,%rdi
call   10d0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12ce <compute_tree+0xfe>
mov    0x0(%r13),%rdi
mov    %rbx,%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     1250 <compute_tree+0x80>
mov    0x0(%r13),%rdi
lea    0xd89(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     1250 <compute_tree+0x80>
mov    $0x3e8,%edi
call   10e0 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %r12,%rdi
lea    0xd60(%rip),%rsi
call   10f0 <strcat@plt>
mov    0x0(%r13),%rsi
mov    %r12,%rdi
call   10f0 <strcat@plt>
mov    %r12,%rdi
mov    %r14d,%esi
call   1090 <compute_tree@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1250 <compute_tree+0x80>
test   %rbp,%rbp
je     12e9 <compute_tree+0x119>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <closedir@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [5 x i8] c"|---\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [2 x i8] c"/\00"
define void @compute_tree(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %10
10: ; preds = %16, %2
%11 = load i32, i32* %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
store i32 %18, i32* %5
br label %10
19: ; preds = %10
%20 = load i8*, i8** %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %3
%23 = call i32 @stat(i8* %22, %struct.stat* %6)
%24 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @S_ISDIR(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %19
br label %74
29: ; preds = %19
%30 = load i8*, i8** %3
%31 = call i32* @opendir(i8* %30)
store i32* %31, i32** %7
br label %32
32: ; preds = %50, %49, %42, %29
%33 = load i32*, i32** %7
%34 = call %struct.dirent* @readdir(i32* %33)
store %struct.dirent* %34, %struct.dirent** %8
%35 = icmp ne %struct.dirent* %34, null
br i1 %35, label %36, label %68
36: ; preds = %32
%37 = load %struct.dirent*, %struct.dirent** %8
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
br label %32
43: ; preds = %36
%44 = load %struct.dirent*, %struct.dirent** %8
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %43
br label %32
50: ; preds = %43
%51 = call i64 @malloc(i32 1000)
%52 = inttoptr i64 %51 to i8*
store i8* %52, i8** %9
%53 = load i8*, i8** %9
%54 = load i8*, i8** %3
%55 = call i32 @strcpy(i8* %53, i8* %54)
%56 = load i8*, i8** %9
%57 = call i32 @strcat(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%58 = load i8*, i8** %9
%59 = load %struct.dirent*, %struct.dirent** %8
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i32 @strcat(i8* %58, i8* %61)
%63 = load i8*, i8** %9
%64 = load i32, i32* %4
%65 = add nsw i32 %64, 1
%66 = load i8*, i8** %9
%67 = call i32 @free(i8* %66)
br label %32
68: ; preds = %32
%69 = load i32*, i32** %7
%70 = icmp ne i32* %69, null
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load i32*, i32** %7
%73 = call i32 @closedir(i32* %72)
br label %74
74: ; preds = %28, %71, %68
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [5 x i8] c"|---\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [2 x i8] c"/\00"
define void @compute_tree(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %10
5: ; preds = %2, %5
%6 = phi i32 [ %8, %5 ], [ 0, %2 ]
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = add nuw nsw i32 %6, 1
%9 = icmp eq i32 %8, %1
br i1 %9, label %10, label %5
10: ; preds = %5, %2
%11 = tail call i32 @puts(i8* dereferenceable(1) %0)
%12 = bitcast %struct.stat* %3 to i8*
%13 = call i32 @stat(i8* %0, %struct.stat* %3)
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i64 @S_ISDIR(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %49, label %18
18: ; preds = %10
%19 = tail call i32* @opendir(i8* %0)
%20 = tail call %struct.dirent* @readdir(i32* %19)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %45, label %22
22: ; preds = %18
%23 = add nsw i32 %1, 1
br label %24
24: ; preds = %22, %42
%25 = phi %struct.dirent* [ %20, %22 ], [ %43, %42 ]
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%27 = load i8*, i8** %26
%28 = tail call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %42, label %30
30: ; preds = %24
%31 = load i8*, i8** %26
%32 = tail call i64 @strcmp(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %30
%35 = tail call i64 @malloc(i32 1000)
%36 = inttoptr i64 %35 to i8*
%37 = tail call i32 @strcpy(i8* %36, i8* %0)
%38 = tail call i32 @strcat(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%39 = load i8*, i8** %26
%40 = tail call i32 @strcat(i8* %36, i8* %39)
tail call void @compute_tree(i8* %36, i32 %23)
%41 = tail call i32 @free(i8* %36)
br label %42
42: ; preds = %34, %24, %30
%43 = tail call %struct.dirent* @readdir(i32* %19)
%44 = icmp eq %struct.dirent* %43, null
br i1 %44, label %45, label %24
45: ; preds = %42, %18
%46 = icmp eq i32* %19, null
br i1 %46, label %49, label %47
47: ; preds = %45
%48 = tail call i32 @closedir(i32* %19)
br label %49
49: ; preds = %45, %47, %10
ret void
}