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


x86 assembly (clang, -O0):
<check_and_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x18(%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    0xe3e(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    1203 <check_and_open+0xa3>
jmp    11e2 <check_and_open+0x82>
mov    -0x40(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1249 <check_and_open+0xe9>
mov    -0x30(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     1236 <check_and_open+0xd6>
mov    -0x40(%rbp),%rdi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1249 <check_and_open+0xe9>
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
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
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %rbx,%rax
add    $0x10,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %r15,%rcx
call   1080 <sprintf@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     11da <check_and_open+0x7a>
mov    -0x20(%rbp),%edi
call   1050 <S_ISREG@plt>
test   %rax,%rax
je     11e2 <check_and_open+0x82>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
jmp    11eb <check_and_open+0x8b>
mov    %rbx,%rdi
call   1070 <perror@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%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"
@EXIT_FAILURE = 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 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** %4
%11 = call i64 @strlen(i8* %10)
%12 = load i8*, i8** %5
%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 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %2
br label %24
24: ; preds = %23
%25 = call i32 @perror(i8* %17)
%26 = load i32, i32* @EXIT_FAILURE
store i32 %26, i32* %3
store i32 1, i32* %9
br label %39
27: ; No predecessors!
br label %28
28: ; preds = %27
br label %29
29: ; preds = %28, %2
%30 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i64 @S_ISREG(i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* %17, i32 %35)
store i32 %36, i32* %3
store i32 1, i32* %9
br label %39
37: ; preds = %29
%38 = load i32, i32* @EXIT_FAILURE
store i32 %38, i32* %3
store i32 1, i32* %9
br label %39
39: ; preds = %37, %34, %24
%40 = load i8*, i8** %6
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"%s%s\00"
@EXIT_FAILURE = global i32 0
@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) %0)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%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 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* %8)
%15 = load i32, i32* @EXIT_FAILURE
br label %26
16: ; preds = %2
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i64 @S_ISREG(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %8, i32 %22)
br label %26
24: ; preds = %16
%25 = load i32, i32* @EXIT_FAILURE
br label %26
26: ; preds = %24, %21, %13
%27 = phi i32 [ %15, %13 ], [ %23, %21 ], [ %25, %24 ]
ret i32 %27
}