#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 */
int /*<<< orphan*/ free(char *);
int open(char *, int, int);
char *realpath(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, char *, int /*<<< orphan*/);
int cb_open(const char *path, int flags, int mode, int *ret) {
  char *full_path;
  char *private = "/Users/nlogn/Documents/hvexec/ATTRIB";
  char *redirect = "/Users/nlogn/Documents/hvexec/test/plugin.c";
  full_path = realpath(path, NULL);
  if (full_path && strncmp(full_path, private, strlen(private)) == 0) {
    *ret = open(redirect, flags, mode);
    free(full_path);
    return 0;
  } else {
    return 1;
  }
}


x86 assembly (clang, -O0):
<cb_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xead(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <realpath@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f6 <cb_open+0xa6>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    11f6 <cb_open+0xa6>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1070 <open@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fd <cb_open+0xad>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cb_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%r12d
xor    %esi,%esi
call   1060 <realpath@plt>
mov    $0x1,%ebx
test   %rax,%rax
je     11b8 <cb_open+0x68>
mov    %rax,%rbp
lea    0xe81(%rip),%r13
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    11b8 <cb_open+0x68>
lea    0xe85(%rip),%rdi
mov    %r12d,%esi
mov    %r15d,%edx
call   1070 <open@plt>
mov    %eax,(%r14)
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"/Users/nlogn/Documents/hvexec/ATTRIB\00"
@.str.1 = [44 x i8] c"/Users/nlogn/Documents/hvexec/test/plugin.c\00"
define i32 @cb_open(i8* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8** %11
store i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8** %12
%13 = load i8*, i8** %6
%14 = call i8* @realpath(i8* %13, i32* null)
store i8* %14, i8** %10
%15 = load i8*, i8** %10
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %32
17: ; preds = %4
%18 = load i8*, i8** %10
%19 = load i8*, i8** %11
%20 = load i8*, i8** %11
%21 = call i32 @strlen(i8* %20)
%22 = call i64 @strncmp(i8* %18, i8* %19, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %17
%25 = load i8*, i8** %12
%26 = load i32, i32* %7
%27 = load i32, i32* %8
%28 = call i32 @open(i8* %25, i32 %26, i32 %27)
%29 = load i32*, i32** %9
store i32 %28, i32* %29
%30 = load i8*, i8** %10
%31 = call i32 @free(i8* %30)
store i32 0, i32* %5
br label %33
32: ; preds = %17, %4
store i32 1, i32* %5
br label %33
33: ; preds = %32, %24
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
@.str = [37 x i8] c"/Users/nlogn/Documents/hvexec/ATTRIB\00"
@.str.1 = [44 x i8] c"/Users/nlogn/Documents/hvexec/test/plugin.c\00"
define i32 @cb_open(i8* nocapture %0, i32 %1, i32 %2, i32* nocapture writeonly %3) {
%5 = tail call i8* @realpath(i8* %0, i32* null)
%6 = icmp eq i8* %5, null
br i1 %6, label %14, label %7
7: ; preds = %4
%8 = tail call i32 @strlen(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%9 = tail call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = tail call i32 @open(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %2)
store i32 %12, i32* %3
%13 = tail call i32 @free(i8* %5)
br label %14
14: ; preds = %4, %7, %11
%15 = phi i32 [ 0, %11 ], [ 1, %7 ], [ 1, %4 ]
ret i32 %15
}