#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 */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, int /*<<< orphan*/, int /*<<< orphan*/);
bool find_file(char *dir_name, char *file_name) {
  bool found = false;
  DIR *dir = opendir(dir_name);
  if (dir == NULL) {
    perror("opendir failed");
    return found;
  }
  struct dirent *dent = NULL;
  while (1) {
    errno = 0;
    dent = readdir(dir);
    if (dent == NULL) {
      if (errno == 0)
        break;
      perror("readdir failed");
      goto out;
    }
    if (strncmp(file_name, dent->d_name, strlen(file_name)) == 0) {
      found = true;
      break;
    }
  }
out:
  closedir(dir);
  return found;
}


x86 assembly (clang, -O0):
<find_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a6 <find_file+0x46>
lea    0xe6a(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1252 <find_file+0xf2>
movq   $0x0,-0x30(%rbp)
mov    0x2e2b(%rip),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11fb <find_file+0x9b>
mov    0x2e05(%rip),%rax
cmpq   $0x0,(%rax)
jne    11ea <find_file+0x8a>
jmp    123e <find_file+0xde>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
jmp    1243 <find_file+0xe3>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1239 <find_file+0xd9>
movl   $0x1,-0x1c(%rbp)
jmp    123e <find_file+0xde>
jmp    11ae <find_file+0x4e>
jmp    1243 <find_file+0xe3>
mov    -0x28(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
call   1040 <opendir@plt>
test   %rax,%rax
je     11d3 <find_file+0x73>
mov    %rax,%r14
mov    0x2e62(%rip),%r15
xchg   %ax,%ax
movq   $0x0,(%r15)
mov    %r14,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11b6 <find_file+0x56>
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1180 <find_file+0x20>
mov    $0x1,%ebx
jmp    11be <find_file+0x5e>
xor    %ebx,%ebx
cmpq   $0x0,(%r15)
jne    11e3 <find_file+0x83>
mov    %r14,%rdi
call   1060 <closedir@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe26(%rip),%rdi
call   1080 <perror@plt>
xor    %ebx,%ebx
jmp    11c6 <find_file+0x66>
lea    0xe25(%rip),%rdi
call   1080 <perror@plt>
jmp    11be <find_file+0x5e>

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [15 x i8] c"opendir failed\00"
@errno = global i64 0
@.str.1 = [15 x i8] c"readdir failed\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 %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %24
7: ; preds = %2, %15
store i64 0, i64* @errno
%8 = tail call %struct.dirent* @readdir(i32* %3)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %21
15: ; preds = %7
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @strlen(i8* %1)
%19 = tail call i64 @strncmp(i8* %1, i32 %17, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %7
21: ; preds = %15, %10, %13
%22 = phi i32 [ 0, %10 ], [ 0, %13 ], [ 1, %15 ]
%23 = tail call i32 @closedir(i32* %3)
br label %24
24: ; preds = %21, %5
%25 = phi i32 [ 0, %5 ], [ %22, %21 ]
ret i32 %25
}