#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;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
char *build_total_path(char *, char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ compare_match_string(char *, char *);
scalar_t__ directory_match(char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_file(char *);
int /*<<< orphan*/ printf(char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ sub_directory_or_file(char *);
void search_match_file(char *path, char *match_string, int *max_results,
                       char *father_directory, int *current_result) {
  DIR *path_directory;
  struct dirent *directory_file;
  char *total_path = build_total_path(father_directory, path);
  path_directory = opendir(total_path);
  if (path_directory != NULL) {
    while ((directory_file = readdir(path_directory)) != NULL) {
      if (*max_results <= *current_result) {
        return;
      }
      if (sub_directory_or_file(directory_file->d_name)) {
        char *total_file_name;
        total_file_name = build_total_path(total_path, directory_file->d_name);
        if (directory_match(total_file_name)) {
          search_match_file(directory_file->d_name, match_string, max_results,
                            total_path, current_result);
        } else if (sub_directory_or_file(directory_file->d_name)) {
          if (compare_match_string(match_string, directory_file->d_name)) {
            (*current_result)++;
            printf("%d", *current_result);
            print_file(total_file_name);
          }
        }
        free(total_file_name);
      }
    }
    closedir(path_directory);
  } else {
    perror("");
  }
  free(total_path);
}


x86 assembly (clang, -O0):
<search_match_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <build_total_path@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1312 <search_match_file+0x152>
jmp    120a <search_match_file+0x4a>
mov    -0x30(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1304 <search_match_file+0x144>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%eax
jg     1238 <search_match_file+0x78>
jmp    1327 <search_match_file+0x167>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
call   1090 <sub_directory_or_file@plt>
cmp    $0x0,%rax
je     12ff <search_match_file+0x13f>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
call   1070 <build_total_path@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   10a0 <directory_match@plt>
cmp    $0x0,%rax
je     1296 <search_match_file+0xd6>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1040 <search_match_file@plt>
jmp    12f6 <search_match_file+0x136>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
call   1090 <sub_directory_or_file@plt>
cmp    $0x0,%rax
je     12f1 <search_match_file+0x131>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
call   1050 <compare_match_string@plt>
cmp    $0x0,%rax
je     12ec <search_match_file+0x12c>
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
lea    0xd22(%rip),%rdi
call   1080 <printf@plt>
mov    -0x48(%rbp),%rdi
call   10d0 <print_file@plt>
jmp    12f1 <search_match_file+0x131>
jmp    12f6 <search_match_file+0x136>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
jmp    120a <search_match_file+0x4a>
mov    -0x30(%rbp),%rdi
call   10b0 <closedir@plt>
jmp    131e <search_match_file+0x15e>
lea    0xce9(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_match_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%rbx
mov    %rdx,%r15
mov    %rsi,(%rsp)
mov    %rdi,%rsi
mov    %rcx,%rdi
call   1070 <build_total_path@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     12dc <search_match_file+0x11c>
mov    %rax,%r13
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12af <search_match_file+0xef>
mov    %rax,%rbp
jmp    123a <search_match_file+0x7a>
nopl   (%rax)
mov    (%rsp),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rbx,%r8
call   1040 <search_match_file@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   10c0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12af <search_match_file+0xef>
mov    (%r15),%eax
cmp    (%rbx),%eax
jle    12cd <search_match_file+0x10d>
mov    0x0(%rbp),%rdi
call   1090 <sub_directory_or_file@plt>
test   %rax,%rax
je     122a <search_match_file+0x6a>
mov    0x0(%rbp),%rsi
mov    %r14,%rdi
call   1070 <build_total_path@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   10a0 <directory_match@plt>
mov    0x0(%rbp),%rdi
test   %rax,%rax
jne    1210 <search_match_file+0x50>
call   1090 <sub_directory_or_file@plt>
test   %rax,%rax
je     1222 <search_match_file+0x62>
mov    0x0(%rbp),%rsi
mov    (%rsp),%rdi
call   1050 <compare_match_string@plt>
test   %rax,%rax
je     1222 <search_match_file+0x62>
mov    (%rbx),%esi
add    $0x1,%esi
mov    %esi,(%rbx)
lea    0xd63(%rip),%rdi
call   1080 <printf@plt>
mov    %r12,%rdi
call   10d0 <print_file@plt>
jmp    1222 <search_match_file+0x62>
mov    %r13,%rdi
call   10b0 <closedir@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd1f(%rip),%rdi
call   10e0 <perror@plt>
jmp    12b7 <search_match_file+0xf7>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"%d\00"
@.str.1 = [1 x i8] zeroinitializer
define void @search_match_file(i8* %0, i8* %1, i32* %2, i8* %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca %struct.dirent*
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32* %2, i32** %8
store i8* %3, i8** %9
store i32* %4, i32** %10
%15 = load i8*, i8** %9
%16 = load i8*, i8** %6
%17 = call i8* @build_total_path(i8* %15, i8* %16)
store i8* %17, i8** %13
%18 = load i8*, i8** %13
%19 = call i32* @opendir(i8* %18)
store i32* %19, i32** %11
%20 = load i32*, i32** %11
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %88
22: ; preds = %5
br label %23
23: ; preds = %84, %22
%24 = load i32*, i32** %11
%25 = call %struct.dirent* @readdir(i32* %24)
store %struct.dirent* %25, %struct.dirent** %12
%26 = icmp ne %struct.dirent* %25, null
br i1 %26, label %27, label %85
27: ; preds = %23
%28 = load i32*, i32** %8
%29 = load i32, i32* %28
%30 = load i32*, i32** %10
%31 = load i32, i32* %30
%32 = icmp sle i32 %29, %31
br i1 %32, label %33, label %34
33: ; preds = %27
br label %93
34: ; preds = %27
%35 = load %struct.dirent*, %struct.dirent** %12
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i64 @sub_directory_or_file(i8* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %84
40: ; preds = %34
%41 = load i8*, i8** %13
%42 = load %struct.dirent*, %struct.dirent** %12
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i8* @build_total_path(i8* %41, i8* %44)
store i8* %45, i8** %14
%46 = load i8*, i8** %14
%47 = call i64 @directory_match(i8* %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %40
%50 = load %struct.dirent*, %struct.dirent** %12
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = load i8*, i8** %7
%54 = load i32*, i32** %8
%55 = load i8*, i8** %13
%56 = load i32*, i32** %10
br label %81
57: ; preds = %40
%58 = load %struct.dirent*, %struct.dirent** %12
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i8*, i8** %59
%61 = call i64 @sub_directory_or_file(i8* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %80
63: ; preds = %57
%64 = load i8*, i8** %7
%65 = load %struct.dirent*, %struct.dirent** %12
%66 = getelementptr inbounds %struct.dirent, %struct.dirent* %65, i32 0, i32 0
%67 = load i8*, i8** %66
%68 = call i64 @compare_match_string(i8* %64, i8* %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %79
70: ; preds = %63
%71 = load i32*, i32** %10
%72 = load i32, i32* %71
%73 = add nsw i32 %72, 1
store i32 %73, i32* %71
%74 = load i32*, i32** %10
%75 = load i32, i32* %74
%76 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %75)
%77 = load i8*, i8** %14
%78 = call i32 @print_file(i8* %77)
br label %79
79: ; preds = %70, %63
br label %80
80: ; preds = %79, %57
br label %81
81: ; preds = %80, %49
%82 = load i8*, i8** %14
%83 = call i32 @free(i8* %82)
br label %84
84: ; preds = %81, %34
br label %23
85: ; preds = %23
%86 = load i32*, i32** %11
%87 = call i32 @closedir(i32* %86)
br label %90
88: ; preds = %5
%89 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %90
90: ; preds = %88, %85
%91 = load i8*, i8** %13
%92 = call i32 @free(i8* %91)
br label %93
93: ; preds = %90, %33
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"%d\00"
@.str.1 = [1 x i8] zeroinitializer
define void @search_match_file(i8* %0, i8* %1, i32* nocapture %2, i8* %3, i32* nocapture %4) {
%6 = tail call i8* @build_total_path(i8* %3, i8* %0)
%7 = tail call i32* @opendir(i8* %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %48, label %9
9: ; preds = %5
%10 = tail call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %46, label %12
12: ; preds = %9, %43
%13 = phi %struct.dirent* [ %44, %43 ], [ %10, %9 ]
%14 = load i32, i32* %2
%15 = load i32, i32* %4
%16 = icmp sgt i32 %14, %15
br i1 %16, label %17, label %52
17: ; preds = %12
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = tail call i64 @sub_directory_or_file(i8* %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %43, label %22
22: ; preds = %17
%23 = load i8*, i8** %18
%24 = tail call i8* @build_total_path(i8* %6, i8* %23)
%25 = tail call i64 @directory_match(i8* %24)
%26 = icmp eq i64 %25, 0
%27 = load i8*, i8** %18
br i1 %26, label %29, label %28
28: ; preds = %22
tail call void @search_match_file(i8* %27, i8* %1, i32* %2, i8* %6, i32* %4)
br label %41
29: ; preds = %22
%30 = tail call i64 @sub_directory_or_file(i8* %27)
%31 = icmp eq i64 %30, 0
br i1 %31, label %41, label %32
32: ; preds = %29
%33 = load i8*, i8** %18
%34 = tail call i64 @compare_match_string(i8* %1, i8* %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %32
%37 = load i32, i32* %4
%38 = add nsw i32 %37, 1
store i32 %38, i32* %4
%39 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %38)
%40 = tail call i32 @print_file(i8* %24)
br label %41
41: ; preds = %29, %36, %32, %28
%42 = tail call i32 @free(i8* %24)
br label %43
43: ; preds = %41, %17
%44 = tail call %struct.dirent* @readdir(i32* %7)
%45 = icmp eq %struct.dirent* %44, null
br i1 %45, label %46, label %12
46: ; preds = %43, %9
%47 = tail call i32 @closedir(i32* %7)
br label %50
48: ; preds = %5
%49 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %46
%51 = tail call i32 @free(i8* %6)
br label %52
52: ; preds = %12, %50
ret void
}