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

/* Variables and functions */
int S_IFDIR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ globcmp(int /*<<< orphan*/, char const *, int);
char *kl_sprintf(char *, char *, int /*<<< orphan*/);
scalar_t__ kl_streq(int /*<<< orphan*/, char *);
char *kl_strerror(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *vfs_opendir(char *);
int vfs_stat(char *, struct stat *);
void find_files(char *path, char const *name) {
  DIR *dir = vfs_opendir(path);
  if (!dir) {
    printf("Cannot open %s: %s\n", path, kl_strerror(errno));
    return;
  }
  struct dirent *node;
  struct stat stbuf;
  char *npath = NULL;
  while ((node = readdir(dir))) {
    if (kl_streq(node->d_name, ".") || kl_streq(node->d_name, "..")) {
      continue;
    }
    free(npath);
    npath = kl_sprintf("%s/%s", path, node->d_name);
    if (vfs_stat(npath, &stbuf) == -1) {
      printf("Cannot stat %s: %s\n", npath, kl_strerror(errno));
      continue;
    }
    if (globcmp(node->d_name, name,
                (int)(1 << 0) | (int)(1 << 1) | (int)(1 << 2))) {
      if (stbuf.st_mode & S_IFDIR) {
        printf("%s/\n", npath);
      } else {
        printf("%s\n", npath);
      }
    }
    if (stbuf.st_mode & S_IFDIR) {
      find_files(npath, name);
    }
  }
  closedir(dir);
  free(npath);
}


x86 assembly (clang, -O0):
<find_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <vfs_opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1208 <find_files+0x58>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   10c0 <kl_strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1367 <find_files+0x1b7>
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1355 <find_files+0x1a5>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
call   1050 <kl_streq@plt>
cmp    $0x0,%rax
jne    125f <find_files+0xaf>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
call   1050 <kl_streq@plt>
cmp    $0x0,%rax
je     1264 <find_files+0xb4>
jmp    1210 <find_files+0x60>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xd99(%rip),%rdi
call   1040 <kl_sprintf@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10b0 <vfs_stat@plt>
cmp    $0xffffffff,%eax
jne    12cd <find_files+0x11d>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   10c0 <kl_strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xd5c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1210 <find_files+0x60>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    $0x7,%edx
call   10d0 <globcmp@plt>
cmp    $0x0,%rax
je     132e <find_files+0x17e>
mov    -0x28(%rbp),%eax
mov    0x2cdb(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1317 <find_files+0x167>
mov    -0x30(%rbp),%rsi
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1329 <find_files+0x179>
mov    -0x30(%rbp),%rsi
lea    0xcee(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    132e <find_files+0x17e>
mov    -0x28(%rbp),%eax
mov    0x2c98(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1350 <find_files+0x1a0>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <find_files@plt>
jmp    1210 <find_files+0x60>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    %rdi,%r14
call   1090 <vfs_opendir@plt>
test   %rax,%rax
je     11fb <find_files+0x4b>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     122b <find_files+0x7b>
mov    %rax,%r15
mov    %rbp,0x10(%rsp)
lea    0xe28(%rip),%r12
lea    0xe20(%rip),%r13
xor    %ebp,%ebp
jmp    1280 <find_files+0xd0>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   10c0 <kl_strerror@plt>
lea    0xdf0(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printf@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   10c0 <kl_strerror@plt>
lea    0xdbc(%rip),%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
xchg   %ax,%ax
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     122d <find_files+0x7d>
mov    (%r15),%edi
mov    %r12,%rsi
call   1050 <kl_streq@plt>
test   %rax,%rax
jne    1270 <find_files+0xc0>
mov    (%r15),%edi
mov    %r13,%rsi
call   1050 <kl_streq@plt>
test   %rax,%rax
jne    1270 <find_files+0xc0>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    (%r15),%edx
lea    0xd65(%rip),%rdi
mov    %r14,%rsi
call   1040 <kl_sprintf@plt>
mov    %rax,%rbp
mov    %rax,%rdi
lea    0x8(%rsp),%rsi
call   10b0 <vfs_stat@plt>
cmp    $0xffffffff,%eax
je     124c <find_files+0x9c>
mov    (%r15),%edi
mov    0x10(%rsp),%rsi
mov    $0x7,%edx
call   10d0 <globcmp@plt>
test   %rax,%rax
mov    0x2ce1(%rip),%r15
je     1314 <find_files+0x164>
mov    (%r15),%eax
and    0x8(%rsp),%eax
lea    0xd32(%rip),%rdi
lea    0xd0a(%rip),%rax
cmove  %rax,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r15),%eax
test   %eax,0x8(%rsp)
je     1270 <find_files+0xc0>
mov    %rbp,%rdi
mov    0x10(%rsp),%rsi
call   1080 <find_files@plt>
jmp    1270 <find_files+0xc0>

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i32 }
@.str = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [20 x i8] c"Cannot stat %s: %s\0A\00"
@S_IFDIR = global i32 0
@.str.5 = [5 x i8] c"%s/\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define void @find_files(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca %struct.stat
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i32* @vfs_opendir(i8* %9)
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = load i32, i32* @errno
%16 = call i8* @kl_strerror(i32 %15)
%17 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %16)
br label %87
18: ; preds = %2
store i8* null, i8** %8
br label %19
19: ; preds = %81, %47, %35, %18
%20 = load i32*, i32** %5
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %6
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %82
23: ; preds = %19
%24 = load %struct.dirent*, %struct.dirent** %6
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i64 @kl_streq(i32 %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp ne i64 %27, 0
br i1 %28, label %35, label %29
29: ; preds = %23
%30 = load %struct.dirent*, %struct.dirent** %6
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i64 @kl_streq(i32 %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29, %23
br label %19
36: ; preds = %29
%37 = load i8*, i8** %8
%38 = call i32 @free(i8* %37)
%39 = load i8*, i8** %3
%40 = load %struct.dirent*, %struct.dirent** %6
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i8* @kl_sprintf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %39, i32 %42)
store i8* %43, i8** %8
%44 = load i8*, i8** %8
%45 = call i32 @vfs_stat(i8* %44, %struct.stat* %7)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %36
%48 = load i8*, i8** %8
%49 = load i32, i32* @errno
%50 = call i8* @kl_strerror(i32 %49)
%51 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %48, i8* %50)
br label %19
52: ; preds = %36
%53 = load %struct.dirent*, %struct.dirent** %6
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = load i8*, i8** %4
%57 = call i64 @globcmp(i32 %55, i8* %56, i32 7)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %72
59: ; preds = %52
%60 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%61 = load i32, i32* %60
%62 = load i32, i32* @S_IFDIR
%63 = and i32 %61, %62
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %59
%66 = load i8*, i8** %8
%67 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %66)
br label %71
68: ; preds = %59
%69 = load i8*, i8** %8
%70 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %69)
br label %71
71: ; preds = %68, %65
br label %72
72: ; preds = %71, %52
%73 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%74 = load i32, i32* %73
%75 = load i32, i32* @S_IFDIR
%76 = and i32 %74, %75
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %72
%79 = load i8*, i8** %8
%80 = load i8*, i8** %4
br label %81
81: ; preds = %78, %72
br label %19
82: ; preds = %19
%83 = load i32*, i32** %5
%84 = call i32 @closedir(i32* %83)
%85 = load i8*, i8** %8
%86 = call i32 @free(i8* %85)
br label %87
87: ; preds = %82, %13
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [20 x i8] c"Cannot stat %s: %s\0A\00"
@S_IFDIR = global i32 0
@.str.5 = [5 x i8] c"%s/\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define void @find_files(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = tail call i32* @vfs_opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = tail call i8* @kl_strerror(i32 %7)
%9 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %8)
br label %66
10: ; preds = %2
%11 = bitcast %struct.stat* %3 to i8*
%12 = call %struct.dirent* @readdir(i32* %4)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %62, label %14
14: ; preds = %10
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %16
16: ; preds = %14, %42
%17 = phi %struct.dirent* [ %12, %14 ], [ %43, %42 ]
%18 = phi i8* [ null, %14 ], [ %35, %42 ]
br label %19
19: ; preds = %16, %29
%20 = phi %struct.dirent* [ %17, %16 ], [ %30, %29 ]
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i64 @kl_streq(i32 %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* %21
%27 = call i64 @kl_streq(i32 %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25, %19
%30 = call %struct.dirent* @readdir(i32* %4)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %62, label %19
32: ; preds = %25
%33 = call i32 @free(i8* %18)
%34 = load i32, i32* %21
%35 = call i8* @kl_sprintf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %34)
%36 = call i32 @vfs_stat(i8* %35, %struct.stat* %3)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %45
38: ; preds = %32
%39 = load i32, i32* @errno
%40 = call i8* @kl_strerror(i32 %39)
%41 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %35, i8* %40)
br label %42
42: ; preds = %56, %61, %38
%43 = call %struct.dirent* @readdir(i32* %4)
%44 = icmp eq %struct.dirent* %43, null
br i1 %44, label %62, label %16
45: ; preds = %32
%46 = load i32, i32* %21
%47 = call i64 @globcmp(i32 %46, i8* %1, i32 7)
%48 = icmp eq i64 %47, 0
br i1 %48, label %56, label %49
49: ; preds = %45
%50 = load i32, i32* %15
%51 = load i32, i32* @S_IFDIR
%52 = and i32 %51, %50
%53 = icmp eq i32 %52, 0
%54 = select i1 %53, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0)
%55 = call i32 (i8*, i8*, ...) @printf(i8* %54, i8* %35)
br label %56
56: ; preds = %49, %45
%57 = load i32, i32* %15
%58 = load i32, i32* @S_IFDIR
%59 = and i32 %58, %57
%60 = icmp eq i32 %59, 0
br i1 %60, label %42, label %61
61: ; preds = %56
br label %42
62: ; preds = %42, %29, %10
%63 = phi i8* [ null, %10 ], [ %18, %29 ], [ %35, %42 ]
%64 = call i32 @closedir(i32* %4)
%65 = call i32 @free(i8* %63)
br label %66
66: ; preds = %62, %6
ret void
}