#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 */
scalar_t__ ENOENT;
int /*<<< orphan*/ PATH_MAX;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ realpath(char const *, char *);
char *strrchr(char *, char);
char *strstr(char const *, char *);
char *xxstrdup(char const *);
int path_within_dir(const char *path, const char *dir) {
  if (!path)
    return 0;
  char absolute_dir[PATH_MAX + 1];
  if (!realpath(dir, absolute_dir))
    return 0;
  char *p;
  if (path[0] == '/') {
    p = strstr(path, absolute_dir);
    if (p != path) {
      return 0;
    }
  }
  char absolute_path[PATH_MAX + 1];
  char *tmp_path = xxstrdup(path);
  int rv = 1;
  while ((p = strrchr(tmp_path, '/')) != NULL) {
    *p = '\0';
    if (realpath(tmp_path, absolute_path)) {
      p = strstr(absolute_path, absolute_dir);
      if (p != absolute_path) {
        rv = 0;
      }
      break;
    } else {
      if (errno != ENOENT) {
        rv = 0;
        break;
      }
    }
  }
  free(tmp_path);
  return rv;
}


x86 assembly (clang, -O0):
<path_within_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1177 <path_within_dir+0x27>
movl   $0x0,-0x4(%rbp)
jmp    1304 <path_within_dir+0x1b4>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x58(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <realpath@plt>
cmp    $0x0,%rax
jne    11cc <path_within_dir+0x7c>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12fd <path_within_dir+0x1ad>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1213 <path_within_dir+0xc3>
mov    -0x58(%rbp),%rsi
mov    -0x10(%rbp),%rdi
call   1070 <strstr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     120e <path_within_dir+0xbe>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12fd <path_within_dir+0x1ad>
jmp    1213 <path_within_dir+0xc3>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <xxstrdup@plt>
mov    %rax,-0x48(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x48(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     12e7 <path_within_dir+0x197>
mov    -0x60(%rbp),%rsi
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x48(%rbp),%rdi
call   1050 <realpath@plt>
cmp    $0x0,%rax
je     12b7 <path_within_dir+0x167>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
mov    %rcx,-0x38(%rbp)
cmp    %rax,-0x38(%rbp)
je     12b2 <path_within_dir+0x162>
movl   $0x0,-0x4c(%rbp)
jmp    12e7 <path_within_dir+0x197>
mov    0x2d1a(%rip),%rax
mov    (%rax),%rax
mov    0x2d00(%rip),%rcx
cmp    (%rcx),%rax
je     12dd <path_within_dir+0x18d>
movl   $0x0,-0x4c(%rbp)
jmp    12e7 <path_within_dir+0x197>
jmp    12e2 <path_within_dir+0x192>
jmp    124f <path_within_dir+0xff>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_within_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1243 <path_within_dir+0xe3>
mov    %rdi,%r14
mov    %rsp,%rbx
mov    0x2e6c(%rip),%r13
mov    0x0(%r13),%eax
add    $0x1,%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %rsi,%rdi
mov    %r15,%rsi
call   1070 <realpath@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     127b <path_within_dir+0x11b>
cmpb   $0x2f,(%r14)
jne    11d7 <path_within_dir+0x77>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1040 <strncmp@plt>
test   %eax,%eax
jne    127b <path_within_dir+0x11b>
mov    %rbx,-0x30(%rbp)
mov    0x0(%r13),%eax
add    $0x1,%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r14,%rdi
call   1080 <xxstrdup@plt>
mov    %rax,%rbx
mov    0x2dd3(%rip),%r14
mov    0x2dbc(%rip),%r13
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     1248 <path_within_dir+0xe8>
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <realpath@plt>
test   %rax,%rax
jne    1250 <path_within_dir+0xf0>
mov    (%r14),%rax
cmp    0x0(%r13),%rax
je     1210 <path_within_dir+0xb0>
xor    %r12d,%r12d
jmp    126f <path_within_dir+0x10f>
xor    %r12d,%r12d
jmp    127e <path_within_dir+0x11e>
mov    $0x1,%r12d
jmp    126f <path_within_dir+0x10f>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1040 <strncmp@plt>
xor    %r12d,%r12d
test   %eax,%eax
sete   %r12b
mov    %rbx,%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rbx
mov    %rbx,%rsp
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @path_within_dir(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i32 0, i32* %3
br label %75
16: ; preds = %2
%17 = load i32, i32* @PATH_MAX
%18 = add nsw i32 %17, 1
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %6
%21 = alloca i8, i64 %19
store i64 %19, i64* %7
%22 = load i8*, i8** %5
%23 = call i64 @realpath(i8* %22, i8* %21)
%24 = icmp ne i64 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %16
store i32 0, i32* %3
store i32 1, i32* %8
br label %73
26: ; preds = %16
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 47
br i1 %31, label %32, label %40
32: ; preds = %26
%33 = load i8*, i8** %4
%34 = call i8* @strstr(i8* %33, i8* %21)
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = load i8*, i8** %4
%37 = icmp ne i8* %35, %36
br i1 %37, label %38, label %39
38: ; preds = %32
store i32 0, i32* %3
store i32 1, i32* %8
br label %73
39: ; preds = %32
br label %40
40: ; preds = %39, %26
%41 = load i32, i32* @PATH_MAX
%42 = add nsw i32 %41, 1
%43 = zext i32 %42 to i64
%44 = alloca i8, i64 %43
store i64 %43, i64* %10
%45 = load i8*, i8** %4
%46 = call i8* @xxstrdup(i8* %45)
store i8* %46, i8** %11
store i32 1, i32* %12
br label %47
47: ; preds = %68, %40
%48 = load i8*, i8** %11
%49 = call i8* @strrchr(i8* %48, i8 signext 47)
store i8* %49, i8** %9
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %69
51: ; preds = %47
%52 = load i8*, i8** %9
store i8 0, i8* %52
%53 = load i8*, i8** %11
%54 = call i64 @realpath(i8* %53, i8* %44)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %51
%57 = call i8* @strstr(i8* %44, i8* %21)
store i8* %57, i8** %9
%58 = load i8*, i8** %9
%59 = icmp ne i8* %58, %44
br i1 %59, label %60, label %61
60: ; preds = %56
store i32 0, i32* %12
br label %61
61: ; preds = %60, %56
br label %69
62: ; preds = %51
%63 = load i64, i64* @errno
%64 = load i64, i64* @ENOENT
%65 = icmp ne i64 %63, %64
br i1 %65, label %66, label %67
66: ; preds = %62
store i32 0, i32* %12
br label %69
67: ; preds = %62
br label %68
68: ; preds = %67
br label %47
69: ; preds = %66, %61, %47
%70 = load i8*, i8** %11
%71 = call i32 @free(i8* %70)
%72 = load i32, i32* %12
store i32 %72, i32* %3
store i32 1, i32* %8
br label %73
73: ; preds = %69, %38, %25
%74 = load i8*, i8** %6
br label %75
75: ; preds = %73, %15
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @path_within_dir(i8* %0, i8* nocapture %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %45, label %4
4: ; preds = %2
%5 = load i32, i32* @PATH_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i64 @realpath(i8* %1, i8* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %43, label %12
12: ; preds = %4
%13 = load i8, i8* %0
%14 = icmp eq i8 %13, 47
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = call i64 @strlen(i8* %9)
%17 = call i32 @strncmp(i8* %0, i8* %9, i64 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %43
19: ; preds = %15, %12
%20 = load i32, i32* @PATH_MAX
%21 = add nsw i32 %20, 1
%22 = zext i32 %21 to i64
%23 = alloca i8, i64 %22
%24 = call i8* @xxstrdup(i8* %0)
br label %25
25: ; preds = %36, %19
%26 = call i8* @strrchr(i8* dereferenceable(1) %24, i8 signext 47)
%27 = icmp eq i8* %26, null
br i1 %27, label %40, label %28
28: ; preds = %25
store i8 0, i8* %26
%29 = call i64 @realpath(i8* %24, i8* %23)
%30 = icmp eq i64 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %28
%32 = call i64 @strlen(i8* %9)
%33 = call i32 @strncmp(i8* %23, i8* %9, i64 %32)
%34 = icmp eq i32 %33, 0
%35 = zext i1 %34 to i32
br label %40
36: ; preds = %28
%37 = load i64, i64* @errno
%38 = load i64, i64* @ENOENT
%39 = icmp eq i64 %37, %38
br i1 %39, label %25, label %40
40: ; preds = %36, %25, %31
%41 = phi i32 [ %35, %31 ], [ 0, %36 ], [ 1, %25 ]
%42 = call i32 @free(i8* %24)
br label %43
43: ; preds = %40, %15, %4
%44 = phi i32 [ 0, %4 ], [ %41, %40 ], [ 0, %15 ]
br label %45
45: ; preds = %2, %43
%46 = phi i32 [ %44, %43 ], [ 0, %2 ]
ret i32 %46
}