#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ ent;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ dirfd(int /*<<< orphan*/ *);
int /*<<< orphan*/ do_test(int /*<<< orphan*/ *, char *, int);
int fstatat(int /*<<< orphan*/, char *, struct stat *, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(struct dirent *, struct dirent *, int);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
char *strstr(char *, char *);
void traverse(const char *dir_path, int depth) {
  DIR *dir = NULL;
  dir = opendir(dir_path);
  while (true) {
    struct dirent ent, *tmp_ent = NULL;
    struct stat st;
    if ((tmp_ent = readdir(dir)) == NULL) {
      break;
    }
    memcpy(&ent, tmp_ent, sizeof(ent));
    if (ent.d_name[0] == '.') {
      continue;
    }
    if (fstatat(dirfd(dir), ent.d_name, &st, 0) == -1) {
      continue;
    }
    if (S_ISDIR(st.st_mode)) {
      char path[PATH_MAX];
      snprintf(path, sizeof(path), "%s/%s", dir_path, ent.d_name);
      traverse(path, depth + 1);
      continue;
    }
    if (strncmp(ent.d_name, "test_", strlen("test_")) == 0) {
      const char *p = strstr(ent.d_name, ".conf");
      if (p != NULL && *(p + strlen(".conf")) == '\0') {
        do_test(dir, ent.d_name, depth);
      }
    }
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<traverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1218 <traverse+0x48>
jmp    1369 <traverse+0x199>
mov    -0x28(%rbp),%rsi
lea    -0x20(%rbp),%rdi
mov    $0x8,%edx
call   10b0 <memcpy@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    123f <traverse+0x6f>
jmp    11f4 <traverse+0x24>
mov    -0x18(%rbp),%rdi
call   10a0 <dirfd@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
xor    %ecx,%ecx
call   10f0 <fstatat@plt>
cmp    $0xffffffff,%eax
jne    1267 <traverse+0x97>
jmp    11f4 <traverse+0x24>
mov    -0x30(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12d6 <traverse+0x106>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xd4a(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x50(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
call   1080 <traverse@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
jmp    11f4 <traverse+0x24>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    0xd21(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%edx
lea    0xd0f(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1364 <traverse+0x194>
mov    -0x20(%rbp),%rdi
lea    0xcfb(%rip),%rsi
call   10e0 <strstr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     135f <traverse+0x18f>
mov    -0x48(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    0xcd8(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x60(%rbp),%rax
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    135f <traverse+0x18f>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10d0 <do_test@plt>
jmp    1364 <traverse+0x194>
jmp    11f4 <traverse+0x24>
mov    -0x18(%rbp),%rdi
call   1090 <closedir@plt>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<traverse>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,-0x48(%rbp)
mov    %rdi,-0x50(%rbp)
call   1040 <opendir@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     1327 <traverse+0x157>
mov    -0x48(%rbp),%rcx
add    $0x1,%ecx
mov    %ecx,-0x3c(%rbp)
lea    -0x30(%rbp),%r13
lea    -0x38(%rbp),%r14
jmp    1271 <traverse+0xa1>
mov    %rsp,%r12
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    -0x30(%rbp),%r8
mov    %rbx,%rdi
lea    0xdbc(%rip),%rdx
mov    -0x50(%rbp),%rcx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
mov    -0x3c(%rbp),%esi
call   1080 <traverse@plt>
mov    %r12,%rsp
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     1327 <traverse+0x157>
mov    %r13,%rdi
mov    %rax,%rsi
mov    $0x8,%edx
call   10b0 <memcpy@plt>
mov    -0x30(%rbp),%rax
cmpb   $0x2e,(%rax)
je     1260 <traverse+0x90>
mov    %r15,%rdi
call   10a0 <dirfd@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edi
mov    %r14,%rdx
xor    %ecx,%ecx
call   10f0 <fstatat@plt>
cmp    $0xffffffff,%eax
je     1260 <traverse+0x90>
mov    -0x38(%rbp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
jne    1216 <traverse+0x46>
mov    -0x30(%rbp),%rbx
lea    0xd43(%rip),%r12
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1260 <traverse+0x90>
mov    -0x30(%rbp),%rdi
lea    0xd24(%rip),%rsi
call   10e0 <strstr@plt>
test   %rax,%rax
je     1260 <traverse+0x90>
mov    %rax,%r12
lea    0xd0c(%rip),%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0x0,(%r12,%rax,1)
jne    1260 <traverse+0x90>
mov    -0x30(%rbp),%rsi
mov    %r15,%rdi
mov    -0x48(%rbp),%rdx
call   10d0 <do_test@plt>
jmp    1260 <traverse+0x90>
mov    %r15,%rdi
call   1090 <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 { i8* }
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [6 x i8] c"test_\00"
@.str.2 = [6 x i8] c".conf\00"
define void @traverse(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.dirent
%7 = alloca %struct.dirent*
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32* null, i32** %5
%12 = load i8*, i8** %3
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %5
br label %14
14: ; preds = %2, %28, %36, %42, %82
store %struct.dirent* null, %struct.dirent** %7
%15 = load i32*, i32** %5
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %7
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %18, label %19
18: ; preds = %14
br label %83
19: ; preds = %14
%20 = load %struct.dirent*, %struct.dirent** %7
%21 = call i32 @memcpy(%struct.dirent* %6, %struct.dirent* %20, i32 8)
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = getelementptr inbounds i8, i8* %23, i64 0
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 46
br i1 %27, label %28, label %29
28: ; preds = %19
br label %14
29: ; preds = %19
%30 = load i32*, i32** %5
%31 = call i32 @dirfd(i32* %30)
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i32 @fstatat(i32 %31, i8* %33, %struct.stat* %8, i32 0)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %29
br label %14
37: ; preds = %29
%38 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i64 @S_ISDIR(i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %55
42: ; preds = %37
%43 = load i32, i32* @PATH_MAX
%44 = zext i32 %43 to i64
%45 = call i8* @llvm.stacksave()
store i8* %45, i8** %9
%46 = alloca i8, i64 %44
store i64 %44, i64* %10
%47 = trunc i64 %44 to i32
%48 = load i8*, i8** %3
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 @snprintf(i8* %46, i32 %47, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %48, i8* %50)
%52 = load i32, i32* %4
%53 = add nsw i32 %52, 1
%54 = load i8*, i8** %9
br label %14
55: ; preds = %37
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%59 = call i64 @strncmp(i8* %57, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %58)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %82
61: ; preds = %55
%62 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i32 0, i32 0
%63 = load i8*, i8** %62
%64 = call i8* @strstr(i8* %63, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i8* %64, i8** %11
%65 = load i8*, i8** %11
%66 = icmp ne i8* %65, null
br i1 %66, label %67, label %81
67: ; preds = %61
%68 = load i8*, i8** %11
%69 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = icmp eq i32 %73, 0
br i1 %74, label %75, label %81
75: ; preds = %67
%76 = load i32*, i32** %5
%77 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i32 0, i32 0
%78 = load i8*, i8** %77
%79 = load i32, i32* %4
%80 = call i32 @do_test(i32* %76, i8* %78, i32 %79)
br label %81
81: ; preds = %75, %67, %61
br label %82
82: ; preds = %81, %55
br label %14
83: ; preds = %18
%84 = load i32*, i32** %5
%85 = call i32 @closedir(i32* %84)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [6 x i8] c"test_\00"
@.str.2 = [6 x i8] c".conf\00"
define void @traverse(i8* %0, i32 %1) {
%3 = alloca %struct.dirent
%4 = alloca %struct.stat
%5 = tail call i32* @opendir(i8* %0)
%6 = bitcast %struct.dirent* %3 to i8*
%7 = bitcast %struct.stat* %4 to i8*
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %57, label %10
10: ; preds = %2
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %3, i64 0, i32 0
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%13 = add nsw i32 %1, 1
br label %14
14: ; preds = %10, %54
%15 = phi %struct.dirent* [ %8, %10 ], [ %55, %54 ]
%16 = call i32 @memcpy(%struct.dirent* %3, %struct.dirent* %15, i32 8)
%17 = load i8*, i8** %11
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 46
br i1 %19, label %54, label %20
20: ; preds = %14
%21 = call i32 @dirfd(i32* %5)
%22 = load i8*, i8** %11
%23 = call i32 @fstatat(i32 %21, i8* %22, %struct.stat* %4, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %54, label %25
25: ; preds = %20
%26 = load i32, i32* %12
%27 = call i64 @S_ISDIR(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %36, label %29
29: ; preds = %25
%30 = load i32, i32* @PATH_MAX
%31 = zext i32 %30 to i64
%32 = call i8* @llvm.stacksave()
%33 = alloca i8, i64 %31
%34 = load i8*, i8** %11
%35 = call i32 @snprintf(i8* %33, i32 %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %34)
br label %54
36: ; preds = %25
%37 = load i8*, i8** %11
%38 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%39 = call i64 @strncmp(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %54
41: ; preds = %36
%42 = load i8*, i8** %11
%43 = call i8* @strstr(i8* dereferenceable(1) %42, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%44 = icmp eq i8* %43, null
br i1 %44, label %54, label %45
45: ; preds = %41
%46 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %43, i64 %47
%49 = load i8, i8* %48
%50 = icmp eq i8 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %45
%52 = load i8*, i8** %11
%53 = call i32 @do_test(i32* %5, i8* %52, i32 %1)
br label %54
54: ; preds = %36, %51, %45, %41, %20, %14, %29
%55 = call %struct.dirent* @readdir(i32* %5)
%56 = icmp eq %struct.dirent* %55, null
br i1 %56, label %57, label %14
57: ; preds = %54, %2
%58 = call i32 @closedir(i32* %5)
ret void
}