#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *realpath(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
scalar_t__ strncmp(char const *, char *, int);
int check_and_open(const char *url, const char *document_root) {
  char url_cp[strlen(url)];
  int len = strlen(document_root);
  char doc_cp[len];
  strcpy(url_cp, url);
  strcpy(doc_cp, document_root);
  char *real_path;
  if ((real_path = realpath(strcat(doc_cp, url_cp), NULL)) == NULL)
    return -404;
  printf("%s\n", real_path);
  if (strncmp(document_root, real_path, len) != 0)
    return -403;
  int fd;
  if ((fd = open(real_path, O_RDONLY)) == -1) {
    perror("open");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<check_and_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x58(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   10a0 <strcat@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <realpath@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    123c <check_and_open+0xbc>
movl   $0xfffffe6c,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12c3 <check_and_open+0x143>
mov    -0x40(%rbp),%rsi
lea    0xdb9(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1279 <check_and_open+0xf9>
movl   $0xfffffe6d,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12c3 <check_and_open+0x143>
mov    -0x40(%rbp),%rdi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x48(%rbp)
cmp    $0xffffffff,%eax
jne    12b6 <check_and_open+0x136>
lea    0xd66(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12c3 <check_and_open+0x143>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_and_open>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r12
call   1050 <strlen@plt>
mov    %eax,%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %eax,%r14d
mov    %eax,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r13,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10a0 <strcat@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1070 <realpath@plt>
test   %rax,%rax
je     124f <check_and_open+0xcf>
mov    %rax,%rbx
lea    0xdfd(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
mov    $0xfffffe6d,%eax
test   %rcx,%rcx
jne    1254 <check_and_open+0xd4>
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    1254 <check_and_open+0xd4>
lea    0xdc1(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    1254 <check_and_open+0xd4>
mov    $0xfffffe6c,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
define i32 @check_and_open(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* %13)
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
%18 = load i8*, i8** %5
%19 = call i32 @strlen(i8* %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = zext i32 %20 to i64
%22 = alloca i8, i64 %21
store i64 %21, i64* %9
%23 = load i8*, i8** %4
%24 = call i32 @strcpy(i8* %17, i8* %23)
%25 = load i8*, i8** %5
%26 = call i32 @strcpy(i8* %22, i8* %25)
%27 = call i32 @strcat(i8* %22, i8* %17)
%28 = call i8* @realpath(i32 %27, i32* null)
store i8* %28, i8** %10
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %2
store i32 -404, i32* %3
store i32 1, i32* %11
br label %49
31: ; preds = %2
%32 = load i8*, i8** %10
%33 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = load i8*, i8** %5
%35 = load i8*, i8** %10
%36 = load i32, i32* %8
%37 = call i64 @strncmp(i8* %34, i8* %35, i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %31
store i32 -403, i32* %3
store i32 1, i32* %11
br label %49
40: ; preds = %31
%41 = load i8*, i8** %10
%42 = load i32, i32* @O_RDONLY
%43 = call i32 @open(i8* %41, i32 %42)
store i32 %43, i32* %12
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %11
br label %49
47: ; preds = %40
%48 = load i32, i32* %12
store i32 %48, i32* %3
store i32 1, i32* %11
br label %49
49: ; preds = %47, %45, %39, %30
%50 = load i8*, i8** %6
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
define i32 @check_and_open(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i32 @strlen(i8* %1)
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = call i32 @strcpy(i8* %5, i8* %0)
%10 = call i32 @strcpy(i8* %8, i8* %1)
%11 = call i32 @strcat(i8* %8, i8* %5)
%12 = call i8* @realpath(i32 %11, i32* null)
%13 = icmp eq i8* %12, null
br i1 %13, label %24, label %14
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %12)
%16 = call i64 @strncmp(i8* %1, i8* %12, i32 %6)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %12, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %18, %14, %2
%25 = phi i32 [ -404, %2 ], [ -403, %14 ], [ -1, %22 ], [ %20, %18 ]
ret i32 %25
}