#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 /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int fstat(int, struct stat *);
int get_dir_fd(char const *);
int open(char *, int /*<<< orphan*/);
int openat(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncmp(char *, char *, int);
scalar_t__ strstr(char *, char *);
int check_and_open(char *url, const char *document_root) {
  int dir_fd;
  int fd;
  struct stat file_stat;
  if (!strncmp(url, "..", 2) || strstr(url, "/..") || url[0] == '/' ||
      strstr(url, "//"))
    return -2;
  if (document_root != NULL) {
    if ((dir_fd = get_dir_fd(document_root)) == -1)
      return -1;
    if ((fd = openat(dir_fd, url, 0)) == -1)
      return -1;
  } else {
    if ((fd = open(url, O_RDONLY)) == -1)
      return -1;
  }
  if (fstat(fd, &file_stat) == -1)
    return -1;
  if (!S_ISREG(file_stat.st_mode))
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<check_and_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe76(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
je     11e2 <check_and_open+0x72>
mov    -0x10(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
jne    11e2 <check_and_open+0x72>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11e2 <check_and_open+0x72>
mov    -0x10(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     11ee <check_and_open+0x7e>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12b8 <check_and_open+0x148>
cmpq   $0x0,-0x18(%rbp)
je     1245 <check_and_open+0xd5>
mov    -0x18(%rbp),%rdi
call   1040 <get_dir_fd@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    121a <check_and_open+0xaa>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <check_and_open+0x148>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1050 <openat@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1240 <check_and_open+0xd0>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <check_and_open+0x148>
jmp    1274 <check_and_open+0x104>
mov    -0x10(%rbp),%rdi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    126f <check_and_open+0xff>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <check_and_open+0x148>
jmp    1274 <check_and_open+0x104>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1295 <check_and_open+0x125>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <check_and_open+0x148>
mov    -0x28(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    12b2 <check_and_open+0x142>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b8 <check_and_open+0x148>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_and_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe7c(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
mov    $0xfffffffe,%ebp
test   %eax,%eax
je     11c5 <check_and_open+0x55>
lea    0xe61(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strstr@plt>
test   %rax,%rax
jne    11c5 <check_and_open+0x55>
cmpb   $0x2f,(%rbx)
je     11c5 <check_and_open+0x55>
lea    0xe4c(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strstr@plt>
test   %rax,%rax
je     11d0 <check_and_open+0x60>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
test   %r14,%r14
je     11fc <check_and_open+0x8c>
mov    %r14,%rdi
call   1040 <get_dir_fd@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11c5 <check_and_open+0x55>
mov    %eax,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1050 <openat@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1214 <check_and_open+0xa4>
jmp    11c5 <check_and_open+0x55>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1237 <check_and_open+0xc7>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
je     1237 <check_and_open+0xc7>
mov    0x8(%rsp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x1,%eax
sbb    %ebp,%ebp
or     %ebx,%ebp
jmp    11c5 <check_and_open+0x55>
mov    $0xffffffff,%ebp
jmp    11c5 <check_and_open+0x55>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"..\00"
@.str.1 = [4 x i8] c"/..\00"
@.str.2 = [3 x i8] c"//\00"
@O_RDONLY = global i32 0
define i32 @check_and_open(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @strncmp(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i64 @strstr(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp ne i64 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 47
br i1 %21, label %26, label %22
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = call i64 @strstr(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22, %16, %12, %2
store i32 -2, i32* %3
br label %62
27: ; preds = %22
%28 = load i8*, i8** %5
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %42
30: ; preds = %27
%31 = load i8*, i8** %5
%32 = call i32 @get_dir_fd(i8* %31)
store i32 %32, i32* %6
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 -1, i32* %3
br label %62
35: ; preds = %30
%36 = load i32, i32* %6
%37 = load i8*, i8** %4
%38 = call i32 @openat(i32 %36, i8* %37, i32 0)
store i32 %38, i32* %7
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %35
store i32 -1, i32* %3
br label %62
41: ; preds = %35
br label %49
42: ; preds = %27
%43 = load i8*, i8** %4
%44 = load i32, i32* @O_RDONLY
%45 = call i32 @open(i8* %43, i32 %44)
store i32 %45, i32* %7
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %42
store i32 -1, i32* %3
br label %62
48: ; preds = %42
br label %49
49: ; preds = %48, %41
%50 = load i32, i32* %7
%51 = call i32 @fstat(i32 %50, %struct.stat* %8)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %49
store i32 -1, i32* %3
br label %62
54: ; preds = %49
%55 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @S_ISREG(i32 %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %60, label %59
59: ; preds = %54
store i32 -1, i32* %3
br label %62
60: ; preds = %54
%61 = load i32, i32* %7
store i32 %61, i32* %3
br label %62
62: ; preds = %60, %59, %53, %47, %40, %34, %26
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"..\00"
@.str.1 = [4 x i8] c"/..\00"
@.str.2 = [3 x i8] c"//\00"
@O_RDONLY = global i32 0
define i32 @check_and_open(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%6 = icmp eq i32 %5, 0
br i1 %6, label %38, label %7
7: ; preds = %2
%8 = tail call i64 @strstr(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %38
10: ; preds = %7
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 47
br i1 %12, label %38, label %13
13: ; preds = %10
%14 = tail call i64 @strstr(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %38
16: ; preds = %13
%17 = icmp eq i8* %1, null
br i1 %17, label %24, label %18
18: ; preds = %16
%19 = tail call i32 @get_dir_fd(i8* %1)
%20 = icmp eq i32 %19, -1
br i1 %20, label %38, label %21
21: ; preds = %18
%22 = tail call i32 @openat(i32 %19, i8* %0, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %38, label %28
24: ; preds = %16
%25 = load i32, i32* @O_RDONLY
%26 = tail call i32 @open(i8* %0, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %38, label %28
28: ; preds = %24, %21
%29 = phi i32 [ %22, %21 ], [ %26, %24 ]
%30 = call i32 @fstat(i32 %29, %struct.stat* %3)
%31 = icmp eq i32 %30, -1
br i1 %31, label %38, label %32
32: ; preds = %28
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%34 = load i32, i32* %33
%35 = tail call i32 @S_ISREG(i32 %34)
%36 = icmp eq i32 %35, 0
%37 = select i1 %36, i32 -1, i32 %29
br label %38
38: ; preds = %32, %28, %24, %21, %18, %2, %7, %10, %13
%39 = phi i32 [ -2, %13 ], [ -2, %10 ], [ -2, %7 ], [ -2, %2 ], [ -1, %18 ], [ -1, %21 ], [ -1, %24 ], [ -1, %28 ], [ %37, %32 ]
ret i32 %39
}