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

/* Variables and functions */
int /*<<< orphan*/ copy_str(char *, int, char const *);
int path_starts_with(char *, char *);
char *realpath(char const *, char *);
int /*<<< orphan*/ remove_last_path_component(char *);
int is_in_subtree(const char path[], const char root[]) {
  char path_copy[4096];
  char path_real[4096];
  char root_real[4096];
  copy_str(path_copy, sizeof(path_copy), path);
  remove_last_path_component(path_copy);
  if (realpath(path_copy, path_real) != path_real) {
    return 0;
  }
  if (realpath(root, root_real) != root_real) {
    return 0;
  }
  return path_starts_with(path_real, root_real);
}


x86 assembly (clang, -O0):
<is_in_subtree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3020,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1050 <copy_str@plt>
lea    -0x1020(%rbp),%rdi
call   1030 <remove_last_path_component@plt>
lea    -0x1020(%rbp),%rdi
lea    -0x2020(%rbp),%rsi
call   1040 <realpath@plt>
lea    -0x2020(%rbp),%rcx
cmp    %rcx,%rax
je     11a3 <is_in_subtree+0x63>
movl   $0x0,-0x4(%rbp)
jmp    11e5 <is_in_subtree+0xa5>
mov    -0x18(%rbp),%rdi
lea    -0x3020(%rbp),%rsi
call   1040 <realpath@plt>
lea    -0x3020(%rbp),%rcx
cmp    %rcx,%rax
je     11cf <is_in_subtree+0x8f>
movl   $0x0,-0x4(%rbp)
jmp    11e5 <is_in_subtree+0xa5>
lea    -0x2020(%rbp),%rdi
lea    -0x3020(%rbp),%rsi
call   1060 <path_starts_with@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x3020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_in_subtree>:
push   %rbp
push   %r14
push   %rbx
sub    $0x3000,%rsp
mov    %rsi,%r14
mov    %rdi,%rdx
lea    0x2000(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x1000,%esi
call   1050 <copy_str@plt>
mov    %rbp,%rdi
call   1030 <remove_last_path_component@plt>
lea    0x1000(%rsp),%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <realpath@plt>
xor    %ebp,%ebp
cmp    %rbx,%rax
jne    11ad <is_in_subtree+0x6d>
mov    %rsp,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <realpath@plt>
cmp    %rbx,%rax
jne    11ad <is_in_subtree+0x6d>
lea    0x1000(%rsp),%rdi
mov    %rsp,%rsi
call   1060 <path_starts_with@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x3000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @is_in_subtree(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [4096 x i8]
%7 = alloca [4096 x i8]
%8 = alloca [4096 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%10 = load i8*, i8** %4
%11 = call i32 @copy_str(i8* %9, i32 4096, i8* %10)
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%13 = call i32 @remove_last_path_component(i8* %12)
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%16 = call i8* @realpath(i8* %14, i8* %15)
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%18 = icmp ne i8* %16, %17
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 0, i32* %3
br label %31
20: ; preds = %2
%21 = load i8*, i8** %5
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%23 = call i8* @realpath(i8* %21, i8* %22)
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%25 = icmp ne i8* %23, %24
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 0, i32* %3
br label %31
27: ; preds = %20
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%30 = call i32 @path_starts_with(i8* %28, i8* %29)
store i32 %30, i32* %3
br label %31
31: ; preds = %27, %26, %19
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
define i32 @is_in_subtree(i8* %0, i8* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca [4096 x i8]
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%9 = call i32 @copy_str(i8* %6, i32 4096, i8* %0)
%10 = call i32 @remove_last_path_component(i8* %6)
%11 = call i8* @realpath(i8* %6, i8* %7)
%12 = icmp eq i8* %11, %7
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = call i8* @realpath(i8* %1, i8* %8)
%15 = icmp eq i8* %14, %8
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @path_starts_with(i8* %7, i8* %8)
br label %18
18: ; preds = %13, %2, %16
%19 = phi i32 [ %17, %16 ], [ 0, %2 ], [ 0, %13 ]
ret i32 %19
}