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

/* Variables and functions */
scalar_t__ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fnmatch(char const *, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int find_file(const char *path, const char *pattern) {
  DIR *dir;
  const struct dirent *de;
  dir = opendir(path);
  if (!dir)
    return 0;
  while ((de = readdir(dir)))
    if (!fnmatch(pattern, de->d_name, 0))
      break;
  if (closedir(dir) < 0) {
    perror("closedir");
    exit(1);
  }
  return !!de;
}


x86 assembly (clang, -O0):
<find_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1194 <find_file+0x34>
movl   $0x0,-0x4(%rbp)
jmp    1211 <find_file+0xb1>
jmp    1199 <find_file+0x39>
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11d4 <find_file+0x74>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fnmatch@plt>
cmp    $0x0,%eax
jne    11cf <find_file+0x6f>
jmp    11d4 <find_file+0x74>
jmp    1199 <find_file+0x39>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
cmp    $0x0,%rax
jge    11fd <find_file+0x9d>
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
setne  %al
xor    $0xff,%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     11b7 <find_file+0x57>
mov    %rax,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
call   1060 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11a0 <find_file+0x40>
mov    (%rbx),%esi
mov    %r14,%rdi
xor    %edx,%edx
call   1040 <fnmatch@plt>
test   %eax,%eax
jne    1180 <find_file+0x20>
mov    %r15,%rdi
call   1050 <closedir@plt>
test   %rax,%rax
js     11bf <find_file+0x5f>
xor    %eax,%eax
test   %rbx,%rbx
setne  %al
jmp    11b9 <find_file+0x59>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"closedir\00"
define i32 @find_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 0, i32* %3
br label %40
13: ; preds = %2
br label %14
14: ; preds = %26, %13
%15 = load i32*, i32** %6
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %7
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %27
18: ; preds = %14
%19 = load i8*, i8** %5
%20 = load %struct.dirent*, %struct.dirent** %7
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @fnmatch(i8* %19, i32 %22, i32 0)
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %18
br label %27
26: ; preds = %18
br label %14
27: ; preds = %25, %14
%28 = load i32*, i32** %6
%29 = call i64 @closedir(i32* %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = load %struct.dirent*, %struct.dirent** %7
%36 = icmp ne %struct.dirent* %35, null
%37 = xor i1 %36, true
%38 = xor i1 %37, true
%39 = zext i1 %38 to i32
store i32 %39, i32* %3
br label %40
40: ; preds = %34, %12
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"closedir\00"
define i32 @find_file(i8* nocapture %0, i8* %1) {
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %22, label %5
5: ; preds = %2, %8
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %13, label %8
8: ; preds = %5
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @fnmatch(i8* %1, i32 %10, i32 0)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %5
13: ; preds = %8, %5
%14 = tail call i64 @closedir(i32* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = xor i1 %7, true
%21 = zext i1 %20 to i32
br label %22
22: ; preds = %2, %19
%23 = phi i32 [ %21, %19 ], [ 0, %2 ]
ret i32 %23
}