#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;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ die(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcpy(char *, char const *, int);
scalar_t__ stat(char const *, struct stat *);
char *strchr(char const *, char);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void verify_document_root(const char *root) {
  const char *p, *path;
  char buf[PATH_MAX];
  struct stat st;
  path = root;
  if ((p = strchr(root, ',')) != NULL && (size_t)(p - root) < sizeof(buf)) {
    memcpy(buf, root, p - root);
    buf[p - root] = '\0';
    path = buf;
  }
  if (stat(path, &st) != 0 || !S_ISDIR(st.st_mode)) {
    die("Invalid root directory: [%s]: %s", root, strerror(errno));
  }
}


x86 assembly (clang, -O0):
<verify_document_root>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2c,%esi
call   1030 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1209 <verify_document_root+0xa9>
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rdx
sub    %rdx,%rax
cmp    %rcx,%rax
jae    1209 <verify_document_root+0xa9>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    1231 <verify_document_root+0xd1>
mov    -0x30(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1259 <verify_document_root+0xf9>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    0xdac(%rip),%rdi
call   1070 <die@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<verify_document_root>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e77(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%r15
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    $0x2c,%esi
call   1030 <strchr@plt>
mov    %r14,%rdi
test   %rax,%rax
je     11c3 <verify_document_root+0x63>
mov    %rax,%rbx
sub    %r14,%rbx
mov    %r14,%rdi
cmp    %r12,%rbx
jae    11c3 <verify_document_root+0x63>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     11f2 <verify_document_root+0x92>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe1a(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <die@plt>
jmp    11fe <verify_document_root+0x9e>
mov    -0x28(%rbp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
je     11d1 <verify_document_root+0x71>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [33 x i8] c"Invalid root directory: [%s]: %s\00"
@errno = global i32 0
define void @verify_document_root(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.stat
store i8* %0, i8** %2
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %5
%11 = alloca i8, i64 %9
store i64 %9, i64* %6
%12 = load i8*, i8** %2
store i8* %12, i8** %4
%13 = load i8*, i8** %2
%14 = call i8* @strchr(i8* %13, i8 signext 44)
store i8* %14, i8** %3
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %38
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = load i8*, i8** %2
%19 = ptrtoint i8* %17 to i64
%20 = ptrtoint i8* %18 to i64
%21 = sub i64 %19, %20
%22 = icmp ult i64 %21, %9
br i1 %22, label %23, label %38
23: ; preds = %16
%24 = load i8*, i8** %2
%25 = load i8*, i8** %3
%26 = load i8*, i8** %2
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
%30 = trunc i64 %29 to i32
%31 = call i32 @memcpy(i8* %11, i8* %24, i32 %30)
%32 = load i8*, i8** %3
%33 = load i8*, i8** %2
%34 = ptrtoint i8* %32 to i64
%35 = ptrtoint i8* %33 to i64
%36 = sub i64 %34, %35
%37 = getelementptr inbounds i8, i8* %11, i64 %36
store i8 0, i8* %37
store i8* %11, i8** %4
br label %38
38: ; preds = %23, %16, %1
%39 = load i8*, i8** %4
%40 = call i64 @stat(i8* %39, %struct.stat* %7)
%41 = icmp ne i64 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %38
%43 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @S_ISDIR(i32 %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %52, label %47
47: ; preds = %42, %38
%48 = load i8*, i8** %2
%49 = load i32, i32* @errno
%50 = call i32 @strerror(i32 %49)
%51 = call i32 @die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %48, i32 %50)
br label %52
52: ; preds = %47, %42
%53 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [33 x i8] c"Invalid root directory: [%s]: %s\00"
@errno = global i32 0
define void @verify_document_root(i8* %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = bitcast %struct.stat* %2 to i8*
%7 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 44)
%8 = icmp eq i8* %7, null
br i1 %8, label %18, label %9
9: ; preds = %1
%10 = ptrtoint i8* %7 to i64
%11 = ptrtoint i8* %0 to i64
%12 = sub i64 %10, %11
%13 = icmp ult i64 %12, %4
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = trunc i64 %12 to i32
%16 = call i32 @memcpy(i8* %5, i8* %0, i32 %15)
%17 = getelementptr inbounds i8, i8* %5, i64 %12
store i8 0, i8* %17
br label %18
18: ; preds = %14, %9, %1
%19 = phi i8* [ %5, %14 ], [ %0, %9 ], [ %0, %1 ]
%20 = call i64 @stat(i8* %19, %struct.stat* %2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @S_ISDIR(i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %22, %18
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 @die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %29)
br label %31
31: ; preds = %27, %22
ret void
}