#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;
  int d_type;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
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*/ sprintf(char *, char *, char *, ...);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int print_dir_tree(char *path) {
  printf("path = %s\n");
  DIR *dir;
  struct dirent *entry;
  dir = opendir(path);
  int len;
  if (dir == 0) {
    return -1;
  }
  while ((entry = readdir(dir))) {
    printf("%s\n", entry->d_name);
    if (strcmp(path, "/") == 0)
      printf("/%s D_TYPE = %d\n", entry->d_name, entry->d_type);
    else
      printf("%s/%s D_TYPE = %d\n", path, entry->d_name, entry->d_type);
    if (entry->d_type == DT_DIR) {
      if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
        char *newpath = NULL;
        newpath = (char *)malloc(sizeof(char) *
                                 (strlen(entry->d_name) + strlen(path) + 2));
        if (strcmp(path, "/") == 0)
          sprintf(newpath, "/%s", entry->d_name);
        else
          sprintf(newpath, "%s/%s", path, entry->d_name);
        print_dir_tree(newpath);
        free(newpath);
      }
    }
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<print_dir_tree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11de <print_dir_tree+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1378 <print_dir_tree+0x1d8>
jmp    11e3 <print_dir_tree+0x43>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1368 <print_dir_tree+0x1c8>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    124a <print_dir_tree+0xaa>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    126a <print_dir_tree+0xca>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%ecx
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rax
movslq 0x8(%rax),%rax
mov    0x2d67(%rip),%rcx
cmp    (%rcx),%rax
jne    1363 <print_dir_tree+0x1c3>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd91(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     135e <print_dir_tree+0x1be>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd73(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     135e <print_dir_tree+0x1be>
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xd09(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    132f <print_dir_tree+0x18f>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xd02(%rip),%rsi
mov    $0x0,%al
call   10c0 <sprintf@plt>
jmp    134c <print_dir_tree+0x1ac>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
lea    0xcde(%rip),%rsi
mov    $0x0,%al
call   10c0 <sprintf@plt>
mov    -0x30(%rbp),%rdi
call   10b0 <print_dir_tree@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    1363 <print_dir_tree+0x1c3>
jmp    11e3 <print_dir_tree+0x43>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_dir_tree>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe3b(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1338 <print_dir_tree+0x188>
mov    %rax,%r15
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     132c <print_dir_tree+0x17c>
mov    %rax,%r12
lea    0xe10(%rip),%rbp
mov    0x2dde(%rip),%rbx
lea    0xe18(%rip),%r13
jmp    1254 <print_dir_tree+0xa4>
lea    0xe11(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   10d0 <sprintf@plt>
mov    %rbx,%rbp
mov    0x2dba(%rip),%rbx
mov    %r13,%rdi
call   10c0 <print_dir_tree@plt>
mov    %r13,%rdi
call   1030 <free@plt>
lea    0xde4(%rip),%r13
nopl   (%rax)
mov    %r15,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     132c <print_dir_tree+0x17c>
mov    (%r12),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1090 <strcmp@plt>
mov    (%r12),%rdx
mov    0x8(%r12),%ecx
test   %rax,%rax
je     1290 <print_dir_tree+0xe0>
lea    0xd90(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12a3 <print_dir_tree+0xf3>
nopl   0x0(%rax)
lea    0xd78(%rip),%rdi
mov    %rdx,%rsi
mov    %ecx,%edx
xor    %eax,%eax
call   1070 <printf@plt>
movslq 0x8(%r12),%rax
cmp    %rax,(%rbx)
jne    1240 <print_dir_tree+0x90>
mov    (%r12),%rdi
mov    %r13,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1240 <print_dir_tree+0x90>
mov    (%r12),%rdi
lea    0xd57(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1240 <print_dir_tree+0x90>
mov    (%r12),%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   10b0 <malloc@plt>
mov    %rax,%r13
mov    %r14,%rdi
mov    %rbp,%rbx
mov    %rbp,%rsi
call   1090 <strcmp@plt>
mov    (%r12),%rcx
mov    %r13,%rdi
test   %rax,%rax
jne    120b <print_dir_tree+0x5b>
lea    0xd08(%rip),%rsi
mov    %rcx,%rdx
xor    %eax,%eax
call   10d0 <sprintf@plt>
jmp    121c <print_dir_tree+0x6c>
mov    %r15,%rdi
call   1080 <closedir@plt>
xor    %eax,%eax
jmp    133d <print_dir_tree+0x18d>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8*, i32 }
@.str = [11 x i8] c"path = %s\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [17 x i8] c"/%s D_TYPE = %d\0A\00"
@.str.4 = [19 x i8] c"%s/%s D_TYPE = %d\0A\00"
@DT_DIR = global i64 0
@.str.5 = [2 x i8] c".\00"
@.str.6 = [3 x i8] c"..\00"
@.str.7 = [4 x i8] c"/%s\00"
@.str.8 = [6 x i8] c"%s/%s\00"
define i32 @print_dir_tree(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = load i8*, i8** %3
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %103
14: ; preds = %1
br label %15
15: ; preds = %99, %14
%16 = load i32*, i32** %4
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %5
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %100
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %5
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %3
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %19
%28 = load %struct.dirent*, %struct.dirent** %5
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = load %struct.dirent*, %struct.dirent** %5
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %30, i32 %33)
br label %44
35: ; preds = %19
%36 = load i8*, i8** %3
%37 = load %struct.dirent*, %struct.dirent** %5
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = load %struct.dirent*, %struct.dirent** %5
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %36, i8* %39, i32 %42)
br label %44
44: ; preds = %35, %27
%45 = load %struct.dirent*, %struct.dirent** %5
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = sext i32 %47 to i64
%49 = load i64, i64* @DT_DIR
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %99
51: ; preds = %44
%52 = load %struct.dirent*, %struct.dirent** %5
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = call i64 @strcmp(i8* %54, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %98
57: ; preds = %51
%58 = load %struct.dirent*, %struct.dirent** %5
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i8*, i8** %59
%61 = call i64 @strcmp(i8* %60, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %98
63: ; preds = %57
store i8* null, i8** %7
%64 = load %struct.dirent*, %struct.dirent** %5
%65 = getelementptr inbounds %struct.dirent, %struct.dirent* %64, i32 0, i32 0
%66 = load i8*, i8** %65
%67 = call i32 @strlen(i8* %66)
%68 = load i8*, i8** %3
%69 = call i32 @strlen(i8* %68)
%70 = add nsw i32 %67, %69
%71 = add nsw i32 %70, 2
%72 = sext i32 %71 to i64
%73 = mul i64 1, %72
%74 = trunc i64 %73 to i32
%75 = call i64 @malloc(i32 %74)
%76 = inttoptr i64 %75 to i8*
store i8* %76, i8** %7
%77 = load i8*, i8** %3
%78 = call i64 @strcmp(i8* %77, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%79 = icmp eq i64 %78, 0
br i1 %79, label %80, label %86
80: ; preds = %63
%81 = load i8*, i8** %7
%82 = load %struct.dirent*, %struct.dirent** %5
%83 = getelementptr inbounds %struct.dirent, %struct.dirent* %82, i32 0, i32 0
%84 = load i8*, i8** %83
%85 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %81, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %84)
br label %93
86: ; preds = %63
%87 = load i8*, i8** %7
%88 = load i8*, i8** %3
%89 = load %struct.dirent*, %struct.dirent** %5
%90 = getelementptr inbounds %struct.dirent, %struct.dirent* %89, i32 0, i32 0
%91 = load i8*, i8** %90
%92 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %87, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i8* %88, i8* %91)
br label %93
93: ; preds = %86, %80
%94 = load i8*, i8** %7
%95 = call i32 @print_dir_tree(i8* %94)
%96 = load i8*, i8** %7
%97 = call i32 @free(i8* %96)
br label %98
98: ; preds = %93, %57, %51
br label %99
99: ; preds = %98, %44
br label %15
100: ; preds = %15
%101 = load i32*, i32** %4
%102 = call i32 @closedir(i32* %101)
store i32 0, i32* %2
br label %103
103: ; preds = %100, %13
%104 = load i32, i32* %2
ret i32 %104
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i32 }
@.str = [11 x i8] c"path = %s\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [17 x i8] c"/%s D_TYPE = %d\0A\00"
@.str.4 = [19 x i8] c"%s/%s D_TYPE = %d\0A\00"
@DT_DIR = global i64 0
@.str.5 = [2 x i8] c".\00"
@.str.6 = [3 x i8] c"..\00"
@.str.7 = [4 x i8] c"/%s\00"
@.str.8 = [6 x i8] c"%s/%s\00"
define i32 @print_dir_tree(i8* %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %59, label %5
5: ; preds = %1
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %57, label %8
8: ; preds = %5, %54
%9 = phi %struct.dirent* [ %55, %54 ], [ %6, %5 ]
%10 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%11 = load i8*, i8** %10
%12 = tail call i32 @puts(i8* dereferenceable(1) %11)
%13 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
%15 = load i8*, i8** %10
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 1
%17 = load i32, i32* %16
br i1 %14, label %18, label %20
18: ; preds = %8
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %15, i32 %17)
br label %22
20: ; preds = %8
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %15, i32 %17)
br label %22
22: ; preds = %20, %18
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 1
%24 = load i32, i32* %23
%25 = sext i32 %24 to i64
%26 = load i64, i64* @DT_DIR
%27 = icmp eq i64 %26, %25
br i1 %27, label %28, label %54
28: ; preds = %22
%29 = load i8*, i8** %10
%30 = tail call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %54, label %32
32: ; preds = %28
%33 = load i8*, i8** %10
%34 = tail call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %54, label %36
36: ; preds = %32
%37 = load i8*, i8** %10
%38 = tail call i32 @strlen(i8* %37)
%39 = tail call i32 @strlen(i8* %0)
%40 = add i32 %38, 2
%41 = add i32 %40, %39
%42 = tail call i64 @malloc(i32 %41)
%43 = inttoptr i64 %42 to i8*
%44 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
%46 = load i8*, i8** %10
br i1 %45, label %47, label %49
47: ; preds = %36
%48 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %43, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %46)
br label %51
49: ; preds = %36
%50 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %43, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i8* %0, i8* %46)
br label %51
51: ; preds = %49, %47
%52 = tail call i32 @print_dir_tree(i8* %43)
%53 = tail call i32 @free(i8* %43)
br label %54
54: ; preds = %28, %32, %51, %22
%55 = tail call %struct.dirent* @readdir(i32* %3)
%56 = icmp eq %struct.dirent* %55, null
br i1 %56, label %57, label %8
57: ; preds = %54, %5
%58 = tail call i32 @closedir(i32* %3)
br label %59
59: ; preds = %1, %57
%60 = phi i32 [ 0, %57 ], [ -1, %1 ]
ret i32 %60
}