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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int S_IFMT;
int S_IFREG;
int open(char *, int /*<<< orphan*/);
int stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int check_and_open(const char *url, const char *document_root) {
  int o = -1;
  char filePath[50];
  char docRoot[100];
  strcpy(filePath, url);
  strcpy(docRoot, document_root);
  strcat(docRoot, filePath);
  struct stat statStruct;
  if (stat(docRoot, &statStruct) == -1)
    return -1;
  if ((statStruct.st_mode & S_IFMT) == S_IFREG) {
    o = open(docRoot, O_RDONLY);
    if (o == -1)
      return -1;
  }
  return o;
}


x86 assembly (clang, -O0):
<check_and_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0xc0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0xc0(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1060 <strcat@plt>
lea    -0xc0(%rbp),%rdi
lea    -0xc8(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    11af <check_and_open+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <check_and_open+0xc6>
mov    -0xc8(%rbp),%eax
mov    0x2e14(%rip),%rcx
and    (%rcx),%eax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%eax
jne    1200 <check_and_open+0xc0>
lea    -0xc0(%rbp),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11fb <check_and_open+0xbb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <check_and_open+0xc6>
jmp    1200 <check_and_open+0xc0>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_and_open>:
push   %r15
push   %r14
push   %rbx
sub    $0xc0,%rsp
mov    %rsi,%rbx
mov    %rdi,%rsi
lea    0x80(%rsp),%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     11c4 <check_and_open+0x84>
mov    0x2e3a(%rip),%rax
mov    (%rax),%ecx
and    0x8(%rsp),%ecx
mov    0x2e3d(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%ecx
jne    11c9 <check_and_open+0x89>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
jne    11c9 <check_and_open+0x89>
mov    $0xffffffff,%eax
add    $0xc0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
@S_IFREG = global i32 0
@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 [50 x i8]
%8 = alloca [100 x i8]
%9 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 -1, i32* %6
%10 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = call i32 @strcpy(i8* %10, i8* %11)
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%14 = load i8*, i8** %5
%15 = call i32 @strcpy(i8* %13, i8* %14)
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%17 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%18 = call i32 @strcat(i8* %16, i8* %17)
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%20 = call i32 @stat(i8* %19, %struct.stat* %9)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %2
store i32 -1, i32* %3
br label %40
23: ; preds = %2
%24 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @S_IFMT
%27 = and i32 %25, %26
%28 = load i32, i32* @S_IFREG
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %38
30: ; preds = %23
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* %31, i32 %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %30
store i32 -1, i32* %3
br label %40
37: ; preds = %30
br label %38
38: ; preds = %37, %23
%39 = load i32, i32* %6
store i32 %39, i32* %3
br label %40
40: ; preds = %38, %36, %22
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
@S_IFREG = global i32 0
@O_RDONLY = global i32 0
define i32 @check_and_open(i8* %0, i8* %1) {
%3 = alloca [50 x i8]
%4 = alloca [100 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%8 = call i32 @strcpy(i8* %6, i8* %0)
%9 = call i32 @strcpy(i8* %7, i8* %1)
%10 = call i32 @strcat(i8* %7, i8* %6)
%11 = bitcast %struct.stat* %5 to i8*
%12 = call i32 @stat(i8* %7, %struct.stat* %5)
%13 = icmp eq i32 %12, -1
br i1 %13, label %27, label %14
14: ; preds = %2
%15 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @S_IFMT
%18 = and i32 %17, %16
%19 = load i32, i32* @S_IFREG
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %7, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %27, label %25
25: ; preds = %21, %14
%26 = phi i32 [ %23, %21 ], [ -1, %14 ]
br label %27
27: ; preds = %21, %2, %25
%28 = phi i32 [ %26, %25 ], [ -1, %2 ], [ -1, %21 ]
ret i32 %28
}