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

/* Variables and functions */
scalar_t__ DT_DIR;
scalar_t__ DT_LNK;
scalar_t__ DT_REG;
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ process_file(char *, char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
void traverse_directory(const char *path) {
  struct dirent *entry;
  DIR *dp;
  dp = opendir(path);
  if (!dp)
    return;
  while ((entry = readdir(dp))) {
    if (entry->d_type == DT_DIR) {
      if (entry->d_name[0] != '.') {
        char new_path[PATH_MAX];
        snprintf(new_path, PATH_MAX, "%s%s/", path, entry->d_name);
        traverse_directory(new_path);
      }
    } else if (entry->d_type == DT_REG || entry->d_type == DT_LNK) {
      process_file(entry->d_name, path);
    }
  }
  closedir(dp);
}


x86 assembly (clang, -O0):
<traverse_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1189 <traverse_directory+0x29>
jmp    1284 <traverse_directory+0x124>
jmp    118e <traverse_directory+0x2e>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     127b <traverse_directory+0x11b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
jne    1232 <traverse_directory+0xd2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     122d <traverse_directory+0xcd>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r8
lea    0xde8(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1080 <traverse_directory@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
jmp    1276 <traverse_directory+0x116>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2d98(%rip),%rcx
cmp    (%rcx),%rax
je     1260 <traverse_directory+0x100>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%rax
jne    1271 <traverse_directory+0x111>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <process_file@plt>
jmp    1276 <traverse_directory+0x116>
jmp    118e <traverse_directory+0x2e>
mov    -0x18(%rbp),%rdi
call   1060 <closedir@plt>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<traverse_directory>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,-0x30(%rbp)
call   1030 <opendir@plt>
test   %rax,%rax
je     123f <traverse_directory+0xdf>
mov    %rax,%r12
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1237 <traverse_directory+0xd7>
mov    0x2e35(%rip),%r14
mov    0x2e36(%rip),%r13
jmp    11ca <traverse_directory+0x6a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x8(%rax),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <process_file@plt>
mov    %r12,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1237 <traverse_directory+0xd7>
mov    (%rax),%rcx
cmp    (%r14),%rcx
jne    1220 <traverse_directory+0xc0>
mov    0x8(%rax),%rcx
cmpb   $0x2e,(%rcx)
je     11bd <traverse_directory+0x5d>
mov    %rsp,%r15
mov    0x2e0b(%rip),%rdx
mov    (%rdx),%ecx
mov    %rsp,%rbx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    (%rdx),%esi
mov    0x8(%rax),%r8
mov    %rbx,%rdi
lea    0xdf8(%rip),%rdx
mov    -0x30(%rbp),%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1080 <traverse_directory@plt>
mov    %r15,%rsp
jmp    11bd <traverse_directory+0x5d>
xchg   %ax,%ax
cmp    0x0(%r13),%rcx
je     11b0 <traverse_directory+0x50>
mov    0x2d9b(%rip),%rdx
cmp    (%rdx),%rcx
jne    11bd <traverse_directory+0x5d>
jmp    11b0 <traverse_directory+0x50>
mov    %r12,%rdi
call   1060 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@DT_DIR = global i64 0
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s%s/\00"
@DT_REG = global i64 0
@DT_LNK = global i64 0
define void @traverse_directory(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.dirent*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @opendir(i8* %7)
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
br label %67
12: ; preds = %1
br label %13
13: ; preds = %63, %12
%14 = load i32*, i32** %4
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %3
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %64
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %3
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* @DT_DIR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %44
23: ; preds = %17
%24 = load %struct.dirent*, %struct.dirent** %3
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 1
%26 = load i8*, i8** %25
%27 = getelementptr inbounds i8, i8* %26, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 46
br i1 %30, label %31, label %43
31: ; preds = %23
%32 = load i32, i32* @PATH_MAX
%33 = zext i32 %32 to i64
%34 = call i8* @llvm.stacksave()
store i8* %34, i8** %5
%35 = alloca i8, i64 %33
store i64 %33, i64* %6
%36 = load i32, i32* @PATH_MAX
%37 = load i8*, i8** %2
%38 = load %struct.dirent*, %struct.dirent** %3
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 1
%40 = load i8*, i8** %39
%41 = call i32 @snprintf(i8* %35, i32 %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %37, i8* %40)
%42 = load i8*, i8** %5
br label %43
43: ; preds = %31, %23
br label %63
44: ; preds = %17
%45 = load %struct.dirent*, %struct.dirent** %3
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i64, i64* %46
%48 = load i64, i64* @DT_REG
%49 = icmp eq i64 %47, %48
br i1 %49, label %56, label %50
50: ; preds = %44
%51 = load %struct.dirent*, %struct.dirent** %3
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 0
%53 = load i64, i64* %52
%54 = load i64, i64* @DT_LNK
%55 = icmp eq i64 %53, %54
br i1 %55, label %56, label %62
56: ; preds = %50, %44
%57 = load %struct.dirent*, %struct.dirent** %3
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 1
%59 = load i8*, i8** %58
%60 = load i8*, i8** %2
%61 = call i32 @process_file(i8* %59, i8* %60)
br label %62
62: ; preds = %56, %50
br label %63
63: ; preds = %62, %43
br label %13
64: ; preds = %13
%65 = load i32*, i32** %4
%66 = call i32 @closedir(i32* %65)
br label %67
67: ; preds = %64, %11
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@DT_DIR = global i64 0
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s%s/\00"
@DT_REG = global i64 0
@DT_LNK = global i64 0
define void @traverse_directory(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %41, label %4
4: ; preds = %1
%5 = call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %39, label %7
7: ; preds = %4, %36
%8 = phi %struct.dirent* [ %37, %36 ], [ %5, %4 ]
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @DT_DIR
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %26
13: ; preds = %7
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 1
%15 = load i8*, i8** %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 46
br i1 %17, label %36, label %18
18: ; preds = %13
%19 = load i32, i32* @PATH_MAX
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
%22 = alloca i8, i64 %20
%23 = load i32, i32* @PATH_MAX
%24 = load i8*, i8** %14
%25 = call i32 @snprintf(i8* %22, i32 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %24)
br label %36
26: ; preds = %7
%27 = load i64, i64* @DT_REG
%28 = icmp eq i64 %10, %27
%29 = load i64, i64* @DT_LNK
%30 = icmp eq i64 %10, %29
%31 = select i1 %28, i1 true, i1 %30
br i1 %31, label %32, label %36
32: ; preds = %26
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 1
%34 = load i8*, i8** %33
%35 = call i32 @process_file(i8* %34, i8* %0)
br label %36
36: ; preds = %26, %32, %13, %18
%37 = call %struct.dirent* @readdir(i32* %2)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %39, label %7
39: ; preds = %36, %4
%40 = call i32 @closedir(i32* %2)
br label %41
41: ; preds = %1, %39
ret void
}