#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;
scalar_t__ DT_REG;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ err(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
void traverse(char *name, int n) {
  DIR *dir;
  struct dirent *ent;
  chdir(name);
  if (!(dir = opendir(".")))
    err(1, 0, "could not open current dir\n");
  for (;;) {
    int i;
    if (!(ent = readdir(dir)))
      break;
    if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
      continue;
    for (i = 0; i < n; i++)
      printf("|    ");
    if (ent->d_type == DT_DIR) {
      printf("|--- %s\n", ent->d_name);
      traverse(ent->d_name, n + 1);
    } else if (ent->d_type == DT_REG) {
      printf("|--- %s\n", ent->d_name);
    }
  }
  if (strcmp(name, ".") != 0)
    chdir("../");
  closedir(dir);
}


x86 assembly (clang, -O0):
<traverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <chdir@plt>
lea    0xe7e(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11c5 <traverse+0x45>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe40(%rip),%rdx
call   10a0 <err@plt>
jmp    11ca <traverse+0x4a>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11e6 <traverse+0x66>
jmp    12cf <traverse+0x14f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe29(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1220 <traverse+0xa0>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe0b(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1225 <traverse+0xa5>
jmp    11ca <traverse+0x4a>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1254 <traverse+0xd4>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    122c <traverse+0xac>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%rax
jne    1298 <traverse+0x118>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
call   1060 <traverse@plt>
jmp    12ca <traverse+0x14a>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d39(%rip),%rcx
cmp    (%rcx),%rax
jne    12c5 <traverse+0x145>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12ca <traverse+0x14a>
jmp    11ca <traverse+0x4a>
mov    -0x8(%rbp),%rdi
lea    0xd43(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12f5 <traverse+0x175>
lea    0xd3e(%rip),%rdi
call   1040 <chdir@plt>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<traverse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %rdi,0x10(%rsp)
call   1040 <chdir@plt>
lea    0xe7b(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c2 <traverse+0x42>
lea    0xe4a(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   10a0 <err@plt>
mov    %rbx,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1298 <traverse+0x118>
mov    %rax,%r12
lea    0x1(%r15),%eax
mov    %eax,0xc(%rsp)
lea    0xe38(%rip),%rbp
lea    0xe33(%rip),%r14
jmp    1220 <traverse+0xa0>
mov    (%r12),%rsi
lea    0xe2c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r12),%rdi
mov    0xc(%rsp),%esi
call   1060 <traverse@plt>
nopl   (%rax)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     1298 <traverse+0x118>
mov    (%r12),%rdi
mov    %rbp,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1210 <traverse+0x90>
mov    (%r12),%rdi
lea    0xde0(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1210 <traverse+0x90>
test   %r15d,%r15d
jle    1260 <traverse+0xe0>
mov    %r15d,%r13d
xchg   %ax,%ax
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffff,%r13d
jne    1250 <traverse+0xd0>
mov    0x8(%r12),%rax
mov    0x2d6c(%rip),%rcx
cmp    (%rcx),%rax
je     11ee <traverse+0x6e>
mov    0x2d64(%rip),%rcx
cmp    (%rcx),%rax
jne    1210 <traverse+0x90>
mov    (%r12),%rsi
lea    0xd99(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1210 <traverse+0x90>
lea    0xd7e(%rip),%rsi
mov    0x10(%rsp),%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     12ba <traverse+0x13a>
lea    0xd79(%rip),%rdi
call   1040 <chdir@plt>
mov    %rbx,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <closedir@plt>

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

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@.str = [2 x i8] c".\00"
@.str.1 = [28 x i8] c"could not open current dir\0A\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [6 x i8] c"| \00"
@DT_DIR = global i64 0
@.str.4 = [9 x i8] c"|--- %s\0A\00"
@DT_REG = global i64 0
@.str.5 = [4 x i8] c"../\00"
define void @traverse(i8* %0, i32 %1) {
%3 = tail call i32 @chdir(i8* %0)
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @err(i32 1, i32 0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = tail call %struct.dirent* @readdir(i32* %4)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %48, label %11
11: ; preds = %8
%12 = icmp sgt i32 %1, 0
%13 = add nsw i32 %1, 1
br label %14
14: ; preds = %11, %45
%15 = phi %struct.dirent* [ %9, %11 ], [ %46, %45 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = tail call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %45, label %20
20: ; preds = %14
%21 = load i8*, i8** %16
%22 = tail call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %45, label %24
24: ; preds = %20
br i1 %12, label %25, label %30
25: ; preds = %24, %25
%26 = phi i32 [ %28, %25 ], [ 0, %24 ]
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%28 = add nuw nsw i32 %26, 1
%29 = icmp eq i32 %28, %1
br i1 %29, label %30, label %25
30: ; preds = %25, %24
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 1
%32 = load i64, i64* %31
%33 = load i64, i64* @DT_DIR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i8*, i8** %16
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%38 = load i8*, i8** %16
tail call void @traverse(i8* %38, i32 %13)
br label %45
39: ; preds = %30
%40 = load i64, i64* @DT_REG
%41 = icmp eq i64 %32, %40
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i8*, i8** %16
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %35, %42, %39, %14, %20
%46 = tail call %struct.dirent* @readdir(i32* %4)
%47 = icmp eq %struct.dirent* %46, null
br i1 %47, label %48, label %14
48: ; preds = %45, %8
%49 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%50 = icmp eq i64 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %48
%52 = tail call i32 @chdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
br label %53
53: ; preds = %51, %48
%54 = tail call i32 @closedir(i32* %4)
ret void
}