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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char const *, char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *, char const *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strclone(char const *);
char *strlower(int /*<<< orphan*/);
char *strupper(int /*<<< orphan*/);
FILE *fopen_anycase(const char *fn, const char *mode, const char *routine) {
  FILE *fp, *lp, *up;
  char *lower, *upper;
  fp = fopen(fn, mode);
  if (fp != NULL)
    return fp;
  lower = strlower(strclone(fn));
  lp = fopen(lower, mode);
  free(lower);
  if (lp != NULL)
    return lp;
  upper = strupper(strclone(fn));
  up = fopen(upper, mode);
  free(upper);
  if (up != NULL)
    return up;
  {
    fprintf(stderr, "%s: cannot open(mode \"%s\") file %s\n",
            routine ? routine : "fopen_anycase", mode, fn);
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<fopen_anycase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11ad <fopen_anycase+0x3d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1292 <fopen_anycase+0x122>
mov    -0x10(%rbp),%rdi
call   1080 <strclone@plt>
mov    %eax,%edi
call   1090 <strlower@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x30(%rbp)
je     11f3 <fopen_anycase+0x83>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1292 <fopen_anycase+0x122>
mov    -0x10(%rbp),%rdi
call   1080 <strclone@plt>
mov    %eax,%edi
call   1050 <strupper@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x38(%rbp)
je     1239 <fopen_anycase+0xc9>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1292 <fopen_anycase+0x122>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     125d <fopen_anycase+0xed>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    126d <fopen_anycase+0xfd>
lea    0xdc0(%rip),%rax
mov    %rax,-0x58(%rbp)
jmp    126d <fopen_anycase+0xfd>
mov    -0x4c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xd7d(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen_anycase>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e9 <fopen_anycase+0x79>
mov    %r12,%rdi
call   1080 <strclone@plt>
mov    %eax,%edi
call   1090 <strlower@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1030 <free@plt>
test   %rbx,%rbx
jne    11e9 <fopen_anycase+0x79>
mov    %r12,%rdi
call   1080 <strclone@plt>
mov    %eax,%edi
call   1050 <strupper@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     11f6 <fopen_anycase+0x86>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
test   %r15,%r15
lea    0xe1b(%rip),%rdx
cmovne %r15,%rdx
lea    0xdec(%rip),%rsi
mov    %r14,%rcx
mov    %r12,%r8
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"%s: cannot open(mode \22%s\22) file %s\0A\00"
@.str.1 = [14 x i8] c"fopen_anycase\00"
define i32* @fopen_anycase(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i8*, i8** %5
%14 = load i8*, i8** %6
%15 = call i32* @fopen(i8* %13, i8* %14)
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i32*, i32** %8
store i32* %19, i32** %4
br label %59
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = call i32 @strclone(i8* %21)
%23 = call i8* @strlower(i32 %22)
store i8* %23, i8** %11
%24 = load i8*, i8** %11
%25 = load i8*, i8** %6
%26 = call i32* @fopen(i8* %24, i8* %25)
store i32* %26, i32** %9
%27 = load i8*, i8** %11
%28 = call i32 @free(i8* %27)
%29 = load i32*, i32** %9
%30 = icmp ne i32* %29, null
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = load i32*, i32** %9
store i32* %32, i32** %4
br label %59
33: ; preds = %20
%34 = load i8*, i8** %5
%35 = call i32 @strclone(i8* %34)
%36 = call i8* @strupper(i32 %35)
store i8* %36, i8** %12
%37 = load i8*, i8** %12
%38 = load i8*, i8** %6
%39 = call i32* @fopen(i8* %37, i8* %38)
store i32* %39, i32** %10
%40 = load i8*, i8** %12
%41 = call i32 @free(i8* %40)
%42 = load i32*, i32** %10
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %46
44: ; preds = %33
%45 = load i32*, i32** %10
store i32* %45, i32** %4
br label %59
46: ; preds = %33
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %7
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = load i8*, i8** %7
br label %53
52: ; preds = %46
br label %53
53: ; preds = %52, %50
%54 = phi i8* [ %51, %50 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %52 ]
%55 = load i8*, i8** %6
%56 = load i8*, i8** %5
%57 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %54, i8* %55, i8* %56)
%58 = call i32 @exit(i32 -1)
unreachable
59: ; preds = %44, %31, %18
%60 = load i32*, i32** %4
ret i32* %60
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"%s: cannot open(mode \22%s\22) file %s\0A\00"
@.str.1 = [14 x i8] c"fopen_anycase\00"
define noalias i32* @fopen_anycase(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32* @fopen(i8* %0, i8* %1)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %24
6: ; preds = %3
%7 = tail call i32 @strclone(i8* %0)
%8 = tail call i8* @strlower(i32 %7)
%9 = tail call i32* @fopen(i8* %8, i8* %1)
%10 = tail call i32 @free(i8* %8)
%11 = icmp eq i32* %9, null
br i1 %11, label %12, label %24
12: ; preds = %6
%13 = tail call i32 @strclone(i8* %0)
%14 = tail call i8* @strupper(i32 %13)
%15 = tail call i32* @fopen(i8* %14, i8* %1)
%16 = tail call i32 @free(i8* %14)
%17 = icmp eq i32* %15, null
br i1 %17, label %18, label %24
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = icmp eq i8* %2, null
%21 = select i1 %20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %2
%22 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %1, i8* %0)
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %12, %6, %3
%25 = phi i32* [ %4, %3 ], [ %9, %6 ], [ %15, %12 ]
ret i32* %25
}