#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 open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ stat(char *, struct stat *);
scalar_t__ strlen(char const *);
int check_and_open(const char *url, const char *document_root) {
  char tmp[strlen(document_root) + strlen(url) + 1];
  sprintf(tmp, "%s%s", document_root, url);
  struct stat fstat;
  stat(tmp, &fstat);
  if (!S_ISREG(fstat.st_mode))
    return -1;
  return open(tmp, O_RDONLY);
}


x86 assembly (clang, -O0):
<check_and_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rcx
mov    %rax,%rdx
lea    0x1(%rcx,%rdx,1),%rax
mov    %rsp,%rsi
mov    %rsi,-0x20(%rbp)
lea    0x10(%rcx,%rdx,1),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe4e(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
mov    -0x30(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11e8 <check_and_open+0x98>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1204 <check_and_open+0xb4>
mov    -0x40(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%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   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
add    %rbx,%rax
add    $0x10,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe72(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %r15,%rcx
call   1070 <sprintf@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
mov    -0x20(%rbp),%edi
call   1050 <S_ISREG@plt>
test   %eax,%eax
je     11c7 <check_and_open+0x77>
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
jmp    11cc <check_and_open+0x7c>
mov    $0xffffffff,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"%s%s\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 i8*
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i64 @strlen(i8* %10)
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
%14 = add nsw i64 %11, %13
%15 = add nsw i64 %14, 1
%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 = load i8*, i8** %4
%20 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %19)
%21 = call i32 @stat(i8* %17, %struct.stat* %8)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @S_ISREG(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %9
br label %30
27: ; preds = %2
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %17, i32 %28)
store i32 %29, i32* %3
store i32 1, i32* %9
br label %30
30: ; preds = %27, %26
%31 = load i8*, i8** %6
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"%s%s\00"
@O_RDONLY = global i32 0
define i32 @check_and_open(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add i64 %4, 1
%7 = add i64 %6, %5
%8 = alloca i8, i64 %7
%9 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %0)
%10 = bitcast %struct.stat* %3 to i8*
%11 = call i32 @stat(i8* %8, %struct.stat* %3)
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i32 @S_ISREG(i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %8, i32 %17)
br label %19
19: ; preds = %2, %16
%20 = phi i32 [ %18, %16 ], [ -1, %2 ]
ret i32 %20
}