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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
FILE *retrieve_file(const char *restrict filename, const char *restrict mode) {
  DIR *d = NULL;
  struct dirent *dir = NULL;
  d = opendir(".");
  if (d) {
    while ((dir = readdir(d)) != NULL) {
      if (strcmp(dir->d_name, filename) == 0) {
        FILE *file = fopen(filename, mode);
        closedir(d);
        return file;
      }
    }
    fprintf(stderr, "Error: no file matching %s exists in current directory.\n",
            filename);
    return NULL;
  }
  fprintf(stderr, "Error: no directory matching . exists.\n");
  return NULL;
}


x86 assembly (clang, -O0):
<retrieve_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe79(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1224 <retrieve_file+0xc4>
jmp    11a0 <retrieve_file+0x40>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11fc <retrieve_file+0x9c>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11f7 <retrieve_file+0x97>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <closedir@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1243 <retrieve_file+0xe3>
jmp    11a0 <retrieve_file+0x40>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1243 <retrieve_file+0xe3>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<retrieve_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe8e(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11b7 <retrieve_file+0x57>
mov    %rax,%rbx
nop
mov    %rbx,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11d0 <retrieve_file+0x70>
mov    (%rax),%edi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1180 <retrieve_file+0x20>
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <closedir@plt>
mov    %r14,%rax
jmp    11ec <retrieve_file+0x8c>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11ea <retrieve_file+0x8a>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@stderr = global i32 0
@.str.1 = [57 x i8] c"Error: no file matching %s exists in current directory.\0A\00"
@.str.2 = [40 x i8] c"Error: no directory matching . exists.\0A\00"
define i32* @retrieve_file(i8* noalias %0, i8* noalias %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* null, i32** %6
store %struct.dirent* null, %struct.dirent** %7
%9 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %36
12: ; preds = %2
br label %13
13: ; preds = %31, %12
%14 = load i32*, i32** %6
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %7
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %32
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %7
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i8*, i8** %4
%22 = call i64 @strcmp(i32 %20, i8* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %17
%25 = load i8*, i8** %4
%26 = load i8*, i8** %5
%27 = call i32* @fopen(i8* %25, i8* %26)
store i32* %27, i32** %8
%28 = load i32*, i32** %6
%29 = call i32 @closedir(i32* %28)
%30 = load i32*, i32** %8
store i32* %30, i32** %3
br label %39
31: ; preds = %17
br label %13
32: ; preds = %13
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %4
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i32* null, i32** %3
br label %39
36: ; preds = %2
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32* null, i32** %3
br label %39
39: ; preds = %36, %32, %24
%40 = load i32*, i32** %3
ret i32* %40
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@stderr = global i32 0
@.str.1 = [57 x i8] c"Error: no file matching %s exists in current directory.\0A\00"
@.str.2 = [40 x i8] c"Error: no directory matching . exists.\0A\00"
define noalias i32* @retrieve_file(i8* noalias %0, i8* noalias nocapture %1) {
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %19, 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 %16, 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 i64 @strcmp(i32 %10, i8* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %5
13: ; preds = %8
%14 = tail call i32* @fopen(i8* %0, i8* %1)
%15 = tail call i32 @closedir(i32* %3)
br label %22
16: ; preds = %5
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %19, %16, %13
%23 = phi i32* [ %14, %13 ], [ null, %16 ], [ null, %19 ]
ret i32* %23
}