#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*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
scalar_t__ strcmp(char *, char *);
int list_dir(const char *path) {
  struct dirent *d;
  DIR *dir = opendir(path);
  if (dir == NULL) {
    printf("Could not open current directory %s \n", path);
    return 0;
  }
  while ((d = readdir(dir)) != NULL) {
    if ((strcmp(d->d_name, ".") == 0) || (strcmp(d->d_name, "..") == 0)) {
      continue;
    }
    printf("pid=%d type=%d name=%s/%s\n", getpid(), d->d_type, path, d->d_name);
    if (d->d_type == DT_DIR) {
      char dir_path[512] = {0x00};
      sprintf(dir_path, "%s/%s", path, d->d_name);
      if (fork() != 0) {
        list_dir(dir_path);
        exit(0);
      }
    }
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<list_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f5 <list_dir+0x45>
mov    -0x10(%rbp),%rsi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f9 <list_dir+0x149>
jmp    11fa <list_dir+0x4a>
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12f0 <list_dir+0x140>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe08(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     124b <list_dir+0x9b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdea(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1250 <list_dir+0xa0>
jmp    11fa <list_dir+0x4a>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
movslq 0x8(%rax),%rax
mov    0x2d58(%rip),%rcx
cmp    (%rcx),%rax
jne    12eb <list_dir+0x13b>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
lea    0xd87(%rip),%rsi
call   10b0 <sprintf@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
je     12e6 <list_dir+0x136>
lea    -0x220(%rbp),%rdi
call   10a0 <list_dir@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
jmp    12eb <list_dir+0x13b>
jmp    11fa <list_dir+0x4a>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%r14
call   1040 <opendir@plt>
test   %rax,%rax
je     12b6 <list_dir+0x106>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12ac <list_dir+0xfc>
mov    %rax,%r13
lea    0xe37(%rip),%r15
lea    0xe2f(%rip),%r12
mov    %rsp,%rbp
jmp    1214 <list_dir+0x64>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12ac <list_dir+0xfc>
mov    0x0(%r13),%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1200 <list_dir+0x50>
mov    0x0(%r13),%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1200 <list_dir+0x50>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x8(%r13),%edx
mov    0x0(%r13),%r8
lea    0xddd(%rip),%rdi
mov    %eax,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
movslq 0x8(%r13),%rax
mov    0x2d7d(%rip),%rcx
cmp    %rax,(%rcx)
jne    1200 <list_dir+0x50>
mov    $0x200,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x0(%r13),%rcx
mov    %rbp,%rdi
lea    0xdbf(%rip),%rsi
mov    %r14,%rdx
call   10b0 <sprintf@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     1200 <list_dir+0x50>
mov    %rsp,%rdi
call   10a0 <list_dir@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    %rbx,%rdi
call   1070 <closedir@plt>
jmp    12c7 <list_dir+0x117>
lea    0xd43(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8*, i32 }
@.str = [38 x i8] c"Could not open current directory %s \0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [27 x i8] c"pid=%d type=%d name=%s/%s\0A\00"
@DT_DIR = global i64 0
@.str.4 = [6 x i8] c"%s/%s\00"
define i32 @list_dir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca i32*
%6 = alloca [512 x i8]
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %12)
store i32 0, i32* %2
br label %67
14: ; preds = %1
br label %15
15: ; preds = %63, %31, %14
%16 = load i32*, i32** %5
%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 %64
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 i64 @strcmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %31, label %25
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 i64 @strcmp(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25, %19
br label %15
32: ; preds = %25
%33 = call i32 (...) @getpid()
%34 = load %struct.dirent*, %struct.dirent** %4
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = load i8*, i8** %3
%38 = load %struct.dirent*, %struct.dirent** %4
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %33, i32 %36, i8* %37, i8* %40)
%42 = load %struct.dirent*, %struct.dirent** %4
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = sext i32 %44 to i64
%46 = load i64, i64* @DT_DIR
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %63
48: ; preds = %32
%49 = bitcast [512 x i8]* %6 to i8*
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%51 = load i8*, i8** %3
%52 = load %struct.dirent*, %struct.dirent** %4
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = call i32 @sprintf(i8* %50, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %51, i8* %54)
%56 = call i64 (...) @fork()
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %48
%59 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%60 = call i32 @list_dir(i8* %59)
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %48
br label %63
63: ; preds = %62, %32
br label %15
64: ; preds = %15
%65 = load i32*, i32** %5
%66 = call i32 @closedir(i32* %65)
br label %67
67: ; preds = %64, %11
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i32 }
@.str = [38 x i8] c"Could not open current directory %s \0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [27 x i8] c"pid=%d type=%d name=%s/%s\0A\00"
@DT_DIR = global i64 0
@.str.4 = [6 x i8] c"%s/%s\00"
define i32 @list_dir(i8* %0) {
%2 = alloca [512 x i8]
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %10, label %5
5: ; preds = %1
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %44, label %8
8: ; preds = %5
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
br label %12
10: ; preds = %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %46
12: ; preds = %8, %22
%13 = phi %struct.dirent* [ %6, %8 ], [ %23, %22 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %12
%19 = load i8*, i8** %14
%20 = tail call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %25, %43, %12, %18
%23 = tail call %struct.dirent* @readdir(i32* %3)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %44, label %12
25: ; preds = %18
%26 = tail call i32 (...) @getpid()
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 1
%28 = load i32, i32* %27
%29 = load i8*, i8** %14
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %28, i8* %0, i8* %29)
%31 = load i32, i32* %27
%32 = sext i32 %31 to i64
%33 = load i64, i64* @DT_DIR
%34 = icmp eq i64 %33, %32
br i1 %34, label %35, label %22
35: ; preds = %25
%36 = load i8*, i8** %14
%37 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %36)
%38 = tail call i64 (...) @fork()
%39 = icmp eq i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %35
%41 = call i32 @list_dir(i8* %9)
%42 = call i32 @exit(i32 0)
unreachable
43: ; preds = %35
br label %22
44: ; preds = %22, %5
%45 = tail call i32 @closedir(i32* %3)
br label %46
46: ; preds = %44, %10
ret i32 0
}