#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*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *join_path(char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int /*<<< orphan*/ process_file(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void process_directory(char *path, char *url) {
  DIR *dir = opendir(path);
  if (!dir) {
    fprintf(stderr, "error processing directory '%s'", path);
    perror(NULL);
    exit(1);
  }
  struct dirent *entry;
  while ((entry = readdir(dir))) {
    if (entry->d_name[0] == '.')
      continue;
    char *child_path = join_path(path, entry->d_name),
         *child_url = join_path(url, entry->d_name);
    if (entry->d_type == DT_REG)
      process_file(child_path, child_url);
    else if (entry->d_type == DT_DIR)
      process_directory(child_path, child_url);
    free(child_url);
    free(child_path);
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<process_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f4 <process_directory+0x54>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe24(%rip),%rsi
call   1080 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    11f9 <process_directory+0x59>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12bb <process_directory+0x11b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1228 <process_directory+0x88>
jmp    11f9 <process_directory+0x59>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   1060 <join_path@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   1060 <join_path@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d81(%rip),%rcx
cmp    (%rcx),%rax
jne    127a <process_directory+0xda>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <process_file@plt>
jmp    12a4 <process_directory+0x104>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d47(%rip),%rcx
cmp    (%rcx),%rax
jne    129f <process_directory+0xff>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10a0 <process_directory@plt>
jmp    12a4 <process_directory+0x104>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    11f9 <process_directory+0x59>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_directory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <opendir@plt>
test   %rax,%rax
je     126b <process_directory+0xcb>
mov    %rax,%r12
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1255 <process_directory+0xb5>
mov    %rax,%rbx
jmp    120b <process_directory+0x6b>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbp,%rsi
call   1050 <process_file@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1255 <process_directory+0xb5>
mov    (%rbx),%rsi
cmpb   $0x2e,(%rsi)
je     11fb <process_directory+0x5b>
mov    %r15,%rdi
call   1060 <join_path@plt>
mov    %rax,%r13
mov    (%rbx),%rsi
mov    %r14,%rdi
call   1060 <join_path@plt>
mov    %rax,%rbp
mov    0x8(%rbx),%rax
mov    0x2da9(%rip),%rcx
cmp    (%rcx),%rax
je     11e0 <process_directory+0x40>
mov    0x2d8d(%rip),%rcx
cmp    (%rcx),%rax
jne    11eb <process_directory+0x4b>
mov    %r13,%rdi
mov    %rbp,%rsi
call   10a0 <process_directory@plt>
jmp    11eb <process_directory+0x4b>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <closedir@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
mov    %r15,%rdx
call   1080 <fprintf@plt>
xor    %edi,%edi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8*, i64 }
@stderr = global i32 0
@.str = [32 x i8] c"error processing directory '%s'\00"
@DT_REG = global i64 0
@DT_DIR = global i64 0
define void @process_directory(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %19, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %3
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @perror(i32* null)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
br label %20
20: ; preds = %63, %32, %19
%21 = load i32*, i32** %5
%22 = call %struct.dirent* @readdir(i32* %21)
store %struct.dirent* %22, %struct.dirent** %6
%23 = icmp ne %struct.dirent* %22, null
br i1 %23, label %24, label %68
24: ; preds = %20
%25 = load %struct.dirent*, %struct.dirent** %6
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 46
br i1 %31, label %32, label %33
32: ; preds = %24
br label %20
33: ; preds = %24
%34 = load i8*, i8** %3
%35 = load %struct.dirent*, %struct.dirent** %6
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i8* @join_path(i8* %34, i8* %37)
store i8* %38, i8** %7
%39 = load i8*, i8** %4
%40 = load %struct.dirent*, %struct.dirent** %6
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = call i8* @join_path(i8* %39, i8* %42)
store i8* %43, i8** %8
%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_REG
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %53
49: ; preds = %33
%50 = load i8*, i8** %7
%51 = load i8*, i8** %8
%52 = call i32 @process_file(i8* %50, i8* %51)
br label %63
53: ; preds = %33
%54 = load %struct.dirent*, %struct.dirent** %6
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i32 0, i32 1
%56 = load i64, i64* %55
%57 = load i64, i64* @DT_DIR
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = load i8*, i8** %7
%61 = load i8*, i8** %8
br label %62
62: ; preds = %59, %53
br label %63
63: ; preds = %62, %49
%64 = load i8*, i8** %8
%65 = call i32 @free(i8* %64)
%66 = load i8*, i8** %7
%67 = call i32 @free(i8* %66)
br label %20
68: ; preds = %20
%69 = load i32*, i32** %5
%70 = call i32 @closedir(i32* %69)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8*, i64 }
@stderr = global i32 0
@.str = [32 x i8] c"error processing directory '%s'\00"
@DT_REG = global i64 0
@DT_DIR = global i64 0
define void @process_directory(i8* %0, i8* %1) {
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %39, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = tail call i32 @perror(i32* null)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %5, %36
%14 = phi %struct.dirent* [ %37, %36 ], [ %6, %5 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 46
br i1 %18, label %36, label %19
19: ; preds = %13
%20 = tail call i8* @join_path(i8* %0, i8* %16)
%21 = load i8*, i8** %15
%22 = tail call i8* @join_path(i8* %1, i8* %21)
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 1
%24 = load i64, i64* %23
%25 = load i64, i64* @DT_REG
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = tail call i32 @process_file(i8* %20, i8* %22)
br label %33
29: ; preds = %19
%30 = load i64, i64* @DT_DIR
%31 = icmp eq i64 %24, %30
br i1 %31, label %32, label %33
32: ; preds = %29
tail call void @process_directory(i8* %20, i8* %22)
br label %33
33: ; preds = %29, %32, %27
%34 = tail call i32 @free(i8* %22)
%35 = tail call i32 @free(i8* %20)
br label %36
36: ; preds = %33, %13
%37 = tail call %struct.dirent* @readdir(i32* %3)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %39, label %13
39: ; preds = %36, %5
%40 = tail call i32 @closedir(i32* %3)
ret void
}