#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*/ DIR;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
char *calloc(int, int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
void search(char *dir, char *file, int max_findings, char **res,
            int recursive_flag) {
  DIR *d;
  int len;
  static int num_found;
  struct dirent *item;
  struct stat path_stat;
  d = opendir(dir);
  char path[128];
  strcpy(path, dir);
  len = strlen(path);
  path[len] = '/';
  path[len + 1] = 0;
  if (!recursive_flag)
    num_found = 0;
  if (max_findings <= num_found)
    return;
  if (d) {
    while ((item = readdir(d)) != NULL) {
      strcat(path, item->d_name);
      stat(path, &path_stat);
      if (S_ISREG(path_stat.st_mode)) {
        if (!strcmp(item->d_name, file)) {
          res[num_found] = calloc(strlen(path) + 1, sizeof(char));
          strcpy(res[num_found], path);
          num_found++;
          if (max_findings <= num_found)
            return;
        }
      }
      if (S_ISDIR(path_stat.st_mode) && item->d_name[0] != '.') {
        search(path, file, max_findings, res, 1);
        if (max_findings <= num_found)
          return;
      }
      path[len + 1] = 0;
    }
    closedir(d);
  } else {
    printf("Error listing directory %s\n", dir);
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<search>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
lea    -0xd0(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0xd0(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x34(%rbp)
movslq -0x34(%rbp),%rax
movb   $0x2f,-0xd0(%rbp,%rax,1)
2f
mov    -0x34(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0x0,-0xd0(%rbp,%rax,1)
cmpl   $0x0,-0x24(%rbp)
jne    125a <search+0x7a>
movl   $0x0,0x2e3a(%rip)
mov    -0x14(%rbp),%eax
cmp    0x2e31(%rip),%eax
jg     126e <search+0x8e>
jmp    13f0 <search+0x210>
cmpq   $0x0,-0x30(%rbp)
je     13d6 <search+0x1f6>
jmp    127e <search+0x9e>
mov    -0x30(%rbp),%rdi
call   10e0 <readdir@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     13c8 <search+0x1e8>
lea    -0xd0(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
call   10f0 <strcat@plt>
lea    -0xd0(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   10c0 <stat@plt>
mov    -0x48(%rbp),%edi
call   10d0 <S_ISREG@plt>
cmp    $0x0,%rax
je     1358 <search+0x178>
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%eax
jne    1353 <search+0x173>
lea    -0xd0(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
mov    $0x1,%esi
call   10a0 <calloc@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq 0x2d88(%rip),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
movslq 0x2d79(%rip),%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0xd0(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2d63(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2d5a(%rip)
mov    -0x14(%rbp),%eax
cmp    0x2d51(%rip),%eax
jg     134e <search+0x16e>
jmp    13f0 <search+0x210>
jmp    1353 <search+0x173>
jmp    1358 <search+0x178>
mov    -0x48(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     13b3 <search+0x1d3>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     13b3 <search+0x1d3>
lea    -0xd0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%r8d
call   1080 <search@plt>
mov    -0x14(%rbp),%eax
cmp    0x2cf1(%rip),%eax
jg     13ae <search+0x1ce>
jmp    13f0 <search+0x210>
jmp    13b3 <search+0x1d3>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0x0,-0xd0(%rbp,%rax,1)
jmp    127e <search+0x9e>
mov    -0x30(%rbp),%rdi
call   1090 <closedir@plt>
jmp    13f0 <search+0x210>
mov    -0x8(%rbp),%rsi
lea    0xc1f(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %r8d,%r14d
mov    %rcx,0x10(%rsp)
mov    %edx,%r12d
mov    %rsi,0x18(%rsp)
mov    %rdi,%r13
call   1040 <opendir@plt>
mov    %rax,%rbx
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
movslq %eax,%r15
movw   $0x2f,0x20(%rsp,%r15,1)
test   %r14d,%r14d
je     1244 <search+0x64>
mov    0x2e5a(%rip),%eax
cmp    %r12d,%eax
jl     1259 <search+0x79>
jmp    1368 <search+0x188>
movl   $0x0,0x2e46(%rip)
xor    %eax,%eax
cmp    %r12d,%eax
jge    1368 <search+0x188>
test   %rbx,%rbx
je     137a <search+0x19a>
mov    %rbx,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     1360 <search+0x180>
mov    %rax,%r14
lea    0x20(%rsp),%rbp
jmp    129a <search+0xba>
nopl   (%rax)
movb   $0x0,0x21(%rsp,%r15,1)
mov    %rbx,%rdi
call   10e0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1360 <search+0x180>
mov    (%r14),%rsi
mov    %rbp,%rdi
call   10f0 <strcat@plt>
mov    %rbp,%rdi
lea    0x8(%rsp),%rsi
call   10c0 <stat@plt>
mov    0x8(%rsp),%r13d
mov    %r13d,%edi
call   10d0 <S_ISREG@plt>
test   %rax,%rax
je     1319 <search+0x139>
mov    (%r14),%rdi
mov    0x18(%rsp),%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
jne    1319 <search+0x139>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
mov    $0x1,%esi
call   10a0 <calloc@plt>
movslq 0x2da3(%rip),%rcx
mov    0x10(%rsp),%rdx
mov    %rax,(%rdx,%rcx,8)
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    0x2d89(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2d80(%rip)
cmp    %r12d,%eax
jge    1368 <search+0x188>
mov    %r13d,%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     1280 <search+0xa0>
mov    (%r14),%rax
cmpb   $0x2e,(%rax)
je     1280 <search+0xa0>
mov    %rbp,%rdi
mov    0x18(%rsp),%rsi
mov    %r12d,%edx
mov    0x10(%rsp),%rcx
mov    $0x1,%r8d
call   1080 <search@plt>
cmp    %r12d,0x2d3c(%rip)
jl     1280 <search+0xa0>
jmp    1368 <search+0x188>
mov    %rbx,%rdi
call   1090 <closedir@plt>
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc7f(%rip),%rdi
mov    %r13,%rsi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@search.num_found = internal global i32 0
@.str = [28 x i8] c"Error listing directory %s\0A\00"
define void @search(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 i32
%13 = alloca %struct.dirent*
%14 = alloca %struct.stat
%15 = alloca [128 x 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
%16 = load i8*, i8** %6
%17 = call i32* @opendir(i8* %16)
store i32* %17, i32** %11
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 0
%19 = load i8*, i8** %6
%20 = call i32 @strcpy(i8* %18, i8* %19)
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 0
%22 = call i32 @strlen(i8* %21)
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 %24
store i8 47, i8* %25
%26 = load i32, i32* %12
%27 = add nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 %28
store i8 0, i8* %29
%30 = load i32, i32* %10
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %5
store i32 0, i32* @search.num_found
br label %33
33: ; preds = %32, %5
%34 = load i32, i32* %8
%35 = load i32, i32* @search.num_found
%36 = icmp sle i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
br label %124
38: ; preds = %33
%39 = load i32*, i32** %11
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %120
41: ; preds = %38
br label %42
42: ; preds = %112, %41
%43 = load i32*, i32** %11
%44 = call %struct.dirent* @readdir(i32* %43)
store %struct.dirent* %44, %struct.dirent** %13
%45 = icmp ne %struct.dirent* %44, null
br i1 %45, label %46, label %117
46: ; preds = %42
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 0
%48 = load %struct.dirent*, %struct.dirent** %13
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 @strcat(i8* %47, i8* %50)
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 0
%53 = call i32 @stat(i8* %52, %struct.stat* %14)
%54 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i64 @S_ISREG(i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %89
58: ; preds = %46
%59 = load %struct.dirent*, %struct.dirent** %13
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = load i8*, i8** %7
%63 = call i32 @strcmp(i8* %61, i8* %62)
%64 = icmp ne i32 %63, 0
br i1 %64, label %88, label %65
65: ; preds = %58
%66 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 0
%67 = call i32 @strlen(i8* %66)
%68 = add nsw i32 %67, 1
%69 = call i8* @calloc(i32 %68, i32 1)
%70 = load i8**, i8*** %9
%71 = load i32, i32* @search.num_found
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8*, i8** %70, i64 %72
store i8* %69, i8** %73
%74 = load i8**, i8*** %9
%75 = load i32, i32* @search.num_found
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8*, i8** %74, i64 %76
%78 = load i8*, i8** %77
%79 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 0
%80 = call i32 @strcpy(i8* %78, i8* %79)
%81 = load i32, i32* @search.num_found
%82 = add nsw i32 %81, 1
store i32 %82, i32* @search.num_found
%83 = load i32, i32* %8
%84 = load i32, i32* @search.num_found
%85 = icmp sle i32 %83, %84
br i1 %85, label %86, label %87
86: ; preds = %65
br label %124
87: ; preds = %65
br label %88
88: ; preds = %87, %58
br label %89
89: ; preds = %88, %46
%90 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%91 = load i32, i32* %90
%92 = call i64 @S_ISDIR(i32 %91)
%93 = icmp ne i64 %92, 0
br i1 %93, label %94, label %112
94: ; preds = %89
%95 = load %struct.dirent*, %struct.dirent** %13
%96 = getelementptr inbounds %struct.dirent, %struct.dirent* %95, i32 0, i32 0
%97 = load i8*, i8** %96
%98 = getelementptr inbounds i8, i8* %97, i64 0
%99 = load i8, i8* %98
%100 = sext i8 %99 to i32
%101 = icmp ne i32 %100, 46
br i1 %101, label %102, label %112
102: ; preds = %94
%103 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 0
%104 = load i8*, i8** %7
%105 = load i32, i32* %8
%106 = load i8**, i8*** %9
%107 = load i32, i32* %8
%108 = load i32, i32* @search.num_found
%109 = icmp sle i32 %107, %108
br i1 %109, label %110, label %111
110: ; preds = %102
br label %124
111: ; preds = %102
br label %112
112: ; preds = %111, %94, %89
%113 = load i32, i32* %12
%114 = add nsw i32 %113, 1
%115 = sext i32 %114 to i64
%116 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 %115
store i8 0, i8* %116
br label %42
117: ; preds = %42
%118 = load i32*, i32** %11
%119 = call i32 @closedir(i32* %118)
br label %124
120: ; preds = %38
%121 = load i8*, i8** %6
%122 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %121)
%123 = call i32 @exit(i32 -1)
unreachable
124: ; preds = %37, %86, %110, %117
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@search.num_found = internal unnamed_addr global i32 0
@.str = [28 x i8] c"Error listing directory %s\0A\00"
define void @search(i8* %0, i8* nocapture %1, i32 %2, i8** nocapture %3, i32 %4) {
%6 = alloca %struct.stat
%7 = alloca [128 x i8]
%8 = bitcast %struct.stat* %6 to i8*
%9 = tail call i32* @opendir(i8* %0)
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%11 = call i32 @strcpy(i8* %10, i8* %0)
%12 = call i32 @strlen(i8* %10)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %13
store i8 47, i8* %14
%15 = add nsw i32 %12, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %16
store i8 0, i8* %17
%18 = icmp eq i32 %4, 0
br i1 %18, label %21, label %19
19: ; preds = %5
%20 = load i32, i32* @search.num_found
br label %22
21: ; preds = %5
store i32 0, i32* @search.num_found
br label %22
22: ; preds = %19, %21
%23 = phi i32 [ %20, %19 ], [ 0, %21 ]
%24 = icmp slt i32 %23, %2
br i1 %24, label %25, label %74
25: ; preds = %22
%26 = icmp eq i32* %9, null
br i1 %26, label %71, label %27
27: ; preds = %25
%28 = call %struct.dirent* @readdir(i32* %9)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %69, label %30
30: ; preds = %27
%31 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
br label %32
32: ; preds = %30, %66
%33 = phi %struct.dirent* [ %28, %30 ], [ %67, %66 ]
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i64 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @strcat(i8* %10, i8* %35)
%37 = call i32 @stat(i8* %10, %struct.stat* %6)
%38 = load i32, i32* %31
%39 = call i64 @S_ISREG(i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %56, label %41
41: ; preds = %32
%42 = load i8*, i8** %34
%43 = call i32 @strcmp(i8* dereferenceable(1) %42, i8* dereferenceable(1) %1)
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %56
45: ; preds = %41
%46 = call i32 @strlen(i8* %10)
%47 = add nsw i32 %46, 1
%48 = call i8* @calloc(i32 %47, i32 1)
%49 = load i32, i32* @search.num_found
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8*, i8** %3, i64 %50
store i8* %48, i8** %51
%52 = call i32 @strcpy(i8* %48, i8* %10)
%53 = load i32, i32* @search.num_found
%54 = add nsw i32 %53, 1
store i32 %54, i32* @search.num_found
%55 = icmp slt i32 %54, %2
br i1 %55, label %56, label %74
56: ; preds = %41, %45, %32
%57 = call i64 @S_ISDIR(i32 %38)
%58 = icmp eq i64 %57, 0
br i1 %58, label %66, label %59
59: ; preds = %56
%60 = load i8*, i8** %34
%61 = load i8, i8* %60
%62 = icmp eq i8 %61, 46
br i1 %62, label %66, label %63
63: ; preds = %59
%64 = load i32, i32* @search.num_found
%65 = icmp slt i32 %64, %2
br i1 %65, label %66, label %74
66: ; preds = %63, %59, %56
store i8 0, i8* %17
%67 = call %struct.dirent* @readdir(i32* %9)
%68 = icmp eq %struct.dirent* %67, null
br i1 %68, label %69, label %32
69: ; preds = %66, %27
%70 = call i32 @closedir(i32* %9)
br label %74
71: ; preds = %25
%72 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
%73 = call i32 @exit(i32 -1)
unreachable
74: ; preds = %63, %45, %22, %69
ret void
}