#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 dummy;
};

/* Variables and functions */
char *getenv(char *);
scalar_t__ match(char *, char *);
char *strstr(char *, char *);
int check_filename(char *filename) {
  static int inited = 0;
  static char *cgi_pattern;
  int fnl;
  char *cp;
  char *dirname;
  char *authname;
  struct stat sb;
  int r;
  if (!inited) {
    cgi_pattern = getenv("CGI_PATTERN");
    inited = 1;
  }
  if (strstr(filename, "../") != (char *)0)
    return 0;
  if (cgi_pattern != (char *)0 && match(cgi_pattern, filename))
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<check_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
cmpl   $0x0,0x2efd(%rip)
jne    1166 <check_filename+0x36>
lea    0xeb0(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2eec(%rip)
movl   $0x1,0x2eda(%rip)
mov    -0x10(%rbp),%rdi
lea    0xe9b(%rip),%rsi
call   1050 <strstr@plt>
cmp    $0x0,%rax
je     118c <check_filename+0x5c>
movl   $0x0,-0x4(%rbp)
jmp    11c7 <check_filename+0x97>
cmpq   $0x0,0x2eb4(%rip)
je     11c0 <check_filename+0x90>
mov    0x2ea7(%rip),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <match@plt>
cmp    $0x0,%rax
je     11c0 <check_filename+0x90>
movl   $0x0,-0x4(%rbp)
jmp    11c7 <check_filename+0x97>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_filename>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
cmpb   $0x0,0x2f03(%rip)
jne    1159 <check_filename+0x29>
lea    0xeba(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2ef6(%rip)
movb   $0x1,0x2ee7(%rip)
lea    0xeac(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strstr@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    118d <check_filename+0x5d>
mov    0x2ed2(%rip),%rdi
test   %rdi,%rdi
je     1188 <check_filename+0x58>
mov    %rbx,%rsi
call   1040 <match@plt>
test   %rax,%rax
jne    118d <check_filename+0x5d>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@check_filename.inited = internal global i32 0
@check_filename.cgi_pattern = internal global i8* null
@.str = [12 x i8] c"CGI_PATTERN\00"
@.str.1 = [4 x i8] c"../\00"
define i32 @check_filename(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.stat
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i32, i32* @check_filename.inited
%11 = icmp ne i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %1
%13 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** @check_filename.cgi_pattern
store i32 1, i32* @check_filename.inited
br label %14
14: ; preds = %12, %1
%15 = load i8*, i8** %3
%16 = call i8* @strstr(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 0, i32* %2
br label %29
19: ; preds = %14
%20 = load i8*, i8** @check_filename.cgi_pattern
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i8*, i8** @check_filename.cgi_pattern
%24 = load i8*, i8** %3
%25 = call i64 @match(i8* %23, i8* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 0, i32* %2
br label %29
28: ; preds = %22, %19
store i32 1, i32* %2
br label %29
29: ; preds = %28, %27, %18
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@check_filename.inited = internal unnamed_addr global i1 false
@check_filename.cgi_pattern = internal unnamed_addr global i8* null
@.str = [12 x i8] c"CGI_PATTERN\00"
@.str.1 = [4 x i8] c"../\00"
define i32 @check_filename(i8* %0) {
%2 = load i1, i1* @check_filename.inited
br i1 %2, label %5, label %3
3: ; preds = %1
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** @check_filename.cgi_pattern
store i1 true, i1* @check_filename.inited
br label %5
5: ; preds = %3, %1
%6 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %15
8: ; preds = %5
%9 = load i8*, i8** @check_filename.cgi_pattern
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i64 @match(i8* %9, i8* %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11, %8
br label %15
15: ; preds = %11, %5, %14
%16 = phi i32 [ 1, %14 ], [ 0, %5 ], [ 0, %11 ]
ret i32 %16
}