#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*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ stat(char *, struct stat *);
scalar_t__ strcmp(char *, char const *);
scalar_t__ strlen(char const *);
char *strrchr(char *, char);
int find_in_path(const char *path, const char *extension,
                 int (*handler)(const char *)) {
  DIR *dir;
  struct dirent *entry;
  struct stat fstat;
  int err = 0;
  dir = opendir(path);
  if (!dir)
    return -1;
  while (!err && (entry = readdir(dir))) {
    char *ext, *fname;
    if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0))
      continue;
    fname = malloc(strlen(path) + strlen(entry->d_name) + 2);
    sprintf(fname, "%s/%s", path, entry->d_name);
    stat(fname, &fstat);
    if (S_ISDIR(fstat.st_mode)) {
      find_in_path(fname, extension, handler);
    }
    ext = strrchr(fname, '.');
    if (ext && strcmp(ext, extension) == 0) {
      err = handler(fname);
    }
    free(fname);
  }
  closedir(dir);
  return err;
}


x86 assembly (clang, -O0):
<find_in_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ff <find_in_path+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    134f <find_in_path+0x18f>
jmp    1204 <find_in_path+0x44>
xor    %eax,%eax
cmpl   $0x0,-0x3c(%rbp)
mov    %al,-0x51(%rbp)
jne    122a <find_in_path+0x6a>
mov    -0x28(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    123a <find_in_path+0x7a>
jmp    1340 <find_in_path+0x180>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xdb9(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     1274 <find_in_path+0xb4>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xd9b(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    1279 <find_in_path+0xb9>
jmp    1204 <find_in_path+0x44>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x60(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    -0x60(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   10d0 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xd47(%rip),%rsi
call   10e0 <sprintf@plt>
mov    -0x50(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10b0 <stat@plt>
mov    -0x38(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12f1 <find_in_path+0x131>
mov    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <find_in_path@plt>
mov    -0x50(%rbp),%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1332 <find_in_path+0x172>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    1332 <find_in_path+0x172>
mov    -0x20(%rbp),%rax
mov    -0x50(%rbp),%rdi
call   *%rax
mov    %eax,-0x3c(%rbp)
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
jmp    1204 <find_in_path+0x44>
mov    -0x28(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_in_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,0x10(%rsp)
mov    %rdi,%r12
call   1040 <opendir@plt>
test   %rax,%rax
je     12eb <find_in_path+0x12b>
mov    %rax,%r13
lea    0xe0e(%rip),%r15
jmp    1210 <find_in_path+0x50>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
jne    12f4 <find_in_path+0x134>
mov    %r13,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12f2 <find_in_path+0x132>
mov    %rax,%rbx
mov    (%rax),%rdi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1208 <find_in_path+0x48>
mov    (%rbx),%rdi
lea    0xdc0(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1208 <find_in_path+0x48>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    (%rbx),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbp,1),%rdi
add    $0x2,%rdi
call   10d0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
lea    0xd89(%rip),%rsi
mov    %r12,%rdx
mov    %rbx,%rcx
call   10e0 <sprintf@plt>
mov    %r14,%rdi
mov    %rsp,%rsi
call   10b0 <stat@plt>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
je     12af <find_in_path+0xef>
mov    %r14,%rdi
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%rdx
call   1080 <find_in_path@plt>
mov    %r14,%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1200 <find_in_path+0x40>
mov    %rax,%rdi
mov    0x10(%rsp),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
jne    1200 <find_in_path+0x40>
mov    %r14,%rdi
call   *0x8(%rsp)
mov    %eax,%ebp
jmp    1200 <find_in_path+0x40>
mov    $0xffffffff,%ebp
jmp    12fc <find_in_path+0x13c>
xor    %ebp,%ebp
mov    %r13,%rdi
call   1090 <closedir@plt>
mov    %ebp,%eax
add    $0x18,%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 }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @find_in_path(i8* %0, i8* %1, i32 (i8*)* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32 (i8*)*
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca %struct.stat
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 (i8*)* %2, i32 (i8*)** %7
store i32 0, i32* %11
%14 = load i8*, i8** %5
%15 = call i32* @opendir(i8* %14)
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %3
store i32 -1, i32* %4
br label %90
19: ; preds = %3
br label %20
20: ; preds = %83, %41, %19
%21 = load i32, i32* %11
%22 = icmp ne i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = load i32*, i32** %8
%25 = call %struct.dirent* @readdir(i32* %24)
store %struct.dirent* %25, %struct.dirent** %9
%26 = icmp ne %struct.dirent* %25, null
br label %27
27: ; preds = %23, %20
%28 = phi i1 [ false, %20 ], [ %26, %23 ]
br i1 %28, label %29, label %86
29: ; preds = %27
%30 = load %struct.dirent*, %struct.dirent** %9
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %41, label %35
35: ; preds = %29
%36 = load %struct.dirent*, %struct.dirent** %9
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i64 @strcmp(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %35, %29
br label %20
42: ; preds = %35
%43 = load i8*, i8** %5
%44 = call i64 @strlen(i8* %43)
%45 = load %struct.dirent*, %struct.dirent** %9
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i64 @strlen(i8* %47)
%49 = add nsw i64 %44, %48
%50 = add nsw i64 %49, 2
%51 = call i8* @malloc(i64 %50)
store i8* %51, i8** %13
%52 = load i8*, i8** %13
%53 = load i8*, i8** %5
%54 = load %struct.dirent*, %struct.dirent** %9
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i32 0, i32 0
%56 = load i8*, i8** %55
%57 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %53, i8* %56)
%58 = load i8*, i8** %13
%59 = call i32 @stat(i8* %58, %struct.stat* %10)
%60 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i64 @S_ISDIR(i32 %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %42
%65 = load i8*, i8** %13
%66 = load i8*, i8** %6
%67 = load i32 (i8*)*, i32 (i8*)** %7
%68 = call i32 @find_in_path(i8* %65, i8* %66, i32 (i8*)* %67)
br label %69
69: ; preds = %64, %42
%70 = load i8*, i8** %13
%71 = call i8* @strrchr(i8* %70, i8 signext 46)
store i8* %71, i8** %12
%72 = load i8*, i8** %12
%73 = icmp ne i8* %72, null
br i1 %73, label %74, label %83
74: ; preds = %69
%75 = load i8*, i8** %12
%76 = load i8*, i8** %6
%77 = call i64 @strcmp(i8* %75, i8* %76)
%78 = icmp eq i64 %77, 0
br i1 %78, label %79, label %83
79: ; preds = %74
%80 = load i32 (i8*)*, i32 (i8*)** %7
%81 = load i8*, i8** %13
%82 = call i32 %80(i8* %81)
store i32 %82, i32* %11
br label %83
83: ; preds = %79, %74, %69
%84 = load i8*, i8** %13
%85 = call i32 @free(i8* %84)
br label %20
86: ; preds = %27
%87 = load i32*, i32** %8
%88 = call i32 @closedir(i32* %87)
%89 = load i32, i32* %11
store i32 %89, i32* %4
br label %90
90: ; preds = %86, %18
%91 = load i32, i32* %4
ret i32 %91
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define i32 @find_in_path(i8* %0, i8* %1, i32 (i8*)* nocapture %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = tail call i32* @opendir(i8* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %53, label %8
8: ; preds = %3
%9 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %10
10: ; preds = %8, %47
%11 = tail call %struct.dirent* @readdir(i32* %6)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %50, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %47, label %18
18: ; preds = %13
%19 = load i8*, i8** %14
%20 = tail call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %47, label %22
22: ; preds = %18
%23 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%24 = load i8*, i8** %14
%25 = tail call i64 @strlen(i8* dereferenceable(1) %24)
%26 = add i64 %23, 2
%27 = add i64 %26, %25
%28 = tail call i8* @malloc(i64 %27)
%29 = tail call i32 @sprintf(i8* dereferenceable(1) %28, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %24)
%30 = call i32 @stat(i8* %28, %struct.stat* %4)
%31 = load i32, i32* %9
%32 = tail call i64 @S_ISDIR(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %22
%35 = tail call i32 @find_in_path(i8* %28, i8* %1, i32 (i8*)* %2)
br label %36
36: ; preds = %34, %22
%37 = tail call i8* @strrchr(i8* dereferenceable(1) %28, i8 signext 46)
%38 = icmp eq i8* %37, null
br i1 %38, label %44, label %39
39: ; preds = %36
%40 = tail call i64 @strcmp(i8* %37, i8* %1)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = tail call i32 %2(i8* %28)
br label %44
44: ; preds = %42, %39, %36
%45 = phi i32 [ %43, %42 ], [ 0, %39 ], [ 0, %36 ]
%46 = tail call i32 @free(i8* %28)
br label %47
47: ; preds = %13, %18, %44
%48 = phi i32 [ %45, %44 ], [ 0, %18 ], [ 0, %13 ]
%49 = icmp eq i32 %48, 0
br i1 %49, label %10, label %50
50: ; preds = %47, %10
%51 = phi i32 [ %48, %47 ], [ 0, %10 ]
%52 = tail call i32 @closedir(i32* %6)
br label %53
53: ; preds = %3, %50
%54 = phi i32 [ %51, %50 ], [ -1, %3 ]
ret i32 %54
}