#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 */
int DT_DIR;
int /*<<< orphan*/ assert(char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ free(char *);
int livecoding_watch_directory(char const *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ startswith(char const *, char *);
char *strjoin(char const *, char *, char const *, int /*<<< orphan*/ *);
int livecoding_watch_directory_recursively(const char *path) {
  DIR *dir;
  struct dirent *entry;
  int r;
  assert(path);
  dir = opendir(path);
  if (!dir) {
    return -errno;
  }
  r = livecoding_watch_directory(path);
  if (r < 0) {
    closedir(dir);
    return r;
  }
  while ((entry = readdir(dir))) {
    char *new_path;
    const char *d_name = entry->d_name;
    if (!(entry->d_type & DT_DIR)) {
      continue;
    }
    if (startswith(d_name, ".")) {
      continue;
    }
    new_path = strjoin(path, "/", d_name, NULL);
    if (!new_path) {
      continue;
    }
    livecoding_watch_directory_recursively(new_path);
    free(new_path);
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<livecoding_watch_directory_recursively>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d0 <livecoding_watch_directory_recursively+0x40>
xor    %eax,%eax
mov    0x2e12(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12b1 <livecoding_watch_directory_recursively+0x121>
mov    -0x10(%rbp),%rdi
call   10b0 <livecoding_watch_directory@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11fa <livecoding_watch_directory_recursively+0x6a>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b1 <livecoding_watch_directory_recursively+0x121>
jmp    11ff <livecoding_watch_directory_recursively+0x6f>
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12a1 <livecoding_watch_directory_recursively+0x111>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%eax
mov    0x2db1(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    123f <livecoding_watch_directory_recursively+0xaf>
jmp    11ff <livecoding_watch_directory_recursively+0x6f>
mov    -0x38(%rbp),%rdi
lea    0xdb6(%rip),%rsi
call   1070 <startswith@plt>
cmp    $0x0,%rax
je     125e <livecoding_watch_directory_recursively+0xce>
jmp    11ff <livecoding_watch_directory_recursively+0x6f>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xd95(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <strjoin@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    128a <livecoding_watch_directory_recursively+0xfa>
jmp    11ff <livecoding_watch_directory_recursively+0x6f>
mov    -0x30(%rbp),%rdi
call   1090 <livecoding_watch_directory_recursively@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    11ff <livecoding_watch_directory_recursively+0x6f>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<livecoding_watch_directory_recursively>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1080 <assert@plt>
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     124f <livecoding_watch_directory_recursively+0xbf>
mov    %rax,%rbx
mov    %r14,%rdi
call   10b0 <livecoding_watch_directory@plt>
test   %eax,%eax
js     125c <livecoding_watch_directory_recursively+0xcc>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1243 <livecoding_watch_directory_recursively+0xb3>
mov    0x2e05(%rip),%r13
lea    0xe1e(%rip),%r15
lea    0xe19(%rip),%r12
jmp    11fd <livecoding_watch_directory_recursively+0x6d>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1243 <livecoding_watch_directory_recursively+0xb3>
mov    0x0(%r13),%ecx
test   %ecx,0x8(%rax)
je     11f0 <livecoding_watch_directory_recursively+0x60>
mov    (%rax),%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <startswith@plt>
test   %rax,%rax
jne    11f0 <livecoding_watch_directory_recursively+0x60>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1060 <strjoin@plt>
test   %rax,%rax
je     11f0 <livecoding_watch_directory_recursively+0x60>
mov    %rax,%rbp
mov    %rax,%rdi
call   1090 <livecoding_watch_directory_recursively@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    11f0 <livecoding_watch_directory_recursively+0x60>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %ebp,%ebp
jmp    1266 <livecoding_watch_directory_recursively+0xd6>
mov    0x2d82(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    1266 <livecoding_watch_directory_recursively+0xd6>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %ebp,%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 }
@errno = global i32 0
@DT_DIR = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i32 @livecoding_watch_directory_recursively(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @assert(i8* %9)
%11 = load i8*, i8** %3
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %1
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
store i32 %17, i32* %2
br label %63
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = call i32 @livecoding_watch_directory(i8* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32*, i32** %4
%25 = call i32 @closedir(i32* %24)
%26 = load i32, i32* %6
store i32 %26, i32* %2
br label %63
27: ; preds = %18
br label %28
28: ; preds = %55, %54, %47, %42, %27
%29 = load i32*, i32** %4
%30 = call %struct.dirent* @readdir(i32* %29)
store %struct.dirent* %30, %struct.dirent** %5
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %32, label %60
32: ; preds = %28
%33 = load %struct.dirent*, %struct.dirent** %5
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
store i8* %35, i8** %8
%36 = load %struct.dirent*, %struct.dirent** %5
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = load i32, i32* @DT_DIR
%40 = and i32 %38, %39
%41 = icmp ne i32 %40, 0
br i1 %41, label %43, label %42
42: ; preds = %32
br label %28
43: ; preds = %32
%44 = load i8*, i8** %8
%45 = call i64 @startswith(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %43
br label %28
48: ; preds = %43
%49 = load i8*, i8** %3
%50 = load i8*, i8** %8
%51 = call i8* @strjoin(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %50, i32* null)
store i8* %51, i8** %7
%52 = load i8*, i8** %7
%53 = icmp ne i8* %52, null
br i1 %53, label %55, label %54
54: ; preds = %48
br label %28
55: ; preds = %48
%56 = load i8*, i8** %7
%57 = call i32 @livecoding_watch_directory_recursively(i8* %56)
%58 = load i8*, i8** %7
%59 = call i32 @free(i8* %58)
br label %28
60: ; preds = %28
%61 = load i32*, i32** %4
%62 = call i32 @closedir(i32* %61)
store i32 0, i32* %2
br label %63
63: ; preds = %60, %23, %15
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i32 }
@errno = global i32 0
@DT_DIR = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i32 @livecoding_watch_directory_recursively(i8* %0) {
%2 = tail call i32 @assert(i8* %0)
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @errno
%7 = sub nsw i32 0, %6
br label %39
8: ; preds = %1
%9 = tail call i32 @livecoding_watch_directory(i8* %0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call %struct.dirent* @readdir(i32* %3)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %37, label %16
14: ; preds = %8
%15 = tail call i32 @closedir(i32* %3)
br label %39
16: ; preds = %11, %34
%17 = phi %struct.dirent* [ %35, %34 ], [ %12, %11 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 1
%21 = load i32, i32* %20
%22 = load i32, i32* @DT_DIR
%23 = and i32 %22, %21
%24 = icmp eq i32 %23, 0
br i1 %24, label %34, label %25
25: ; preds = %16
%26 = tail call i64 @startswith(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = tail call i8* @strjoin(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32* null)
%30 = icmp eq i8* %29, null
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = tail call i32 @livecoding_watch_directory_recursively(i8* %29)
%33 = tail call i32 @free(i8* %29)
br label %34
34: ; preds = %28, %25, %16, %31
%35 = tail call %struct.dirent* @readdir(i32* %3)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %37, label %16
37: ; preds = %34, %11
%38 = tail call i32 @closedir(i32* %3)
br label %39
39: ; preds = %37, %14, %5
%40 = phi i32 [ %9, %14 ], [ 0, %37 ], [ %7, %5 ]
ret i32 %40
}