#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char **);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, char *, size_t);
int main() {
  char *buffer;
  int ret = 0, fd;
  FILE *fp;
  size_t buf_size = 16;
  fp = fopen("/tmp/mem_addr", "r");
  if (!fp) {
    fprintf(stderr, "error opening file: %s\n", strerror(errno));
    exit(1);
  }
  fscanf(fp, "%d\n", &buffer);
  printf("buffer address is 0x%08x(%d)\n", (int)buffer, (int)buffer);
  fd = open("/tmp/data", O_RDWR);
  if (fd == -1) {
    fprintf(stderr, "error opening file: %s\n", strerror(errno));
    exit(1);
  }
  ret = read(fd, buffer, buf_size);
  if (ret == -1) {
    printf("PASS: read file content to memory in another process, failed as "
           "expected\n");
  } else {
    printf("FAIL: read file content to memory in another process, return value "
           "%d\n",
           ret);
  }
  ret = write(fd, buffer, buf_size);
  if (ret == -1) {
    printf("PASS: write file from memory in another process, failed as "
           "expected\n");
  } else {
    printf("FAIL: write file from memory in another process, return value %d\n",
           ret);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x10,-0x28(%rbp)
lea    0xe4b(%rip),%rdi
lea    0xe50(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1206 <main+0x76>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe17(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xf51(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    %eax,%edx
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
lea    0xe00(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    128c <main+0xfc>
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xd91(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12bc <main+0x12c>
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12cd <main+0x13d>
mov    -0x14(%rbp),%esi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12fd <main+0x16d>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    130e <main+0x17e>
mov    -0x14(%rbp),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe54(%rip),%rdi
lea    0xe59(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1275 <main+0xd5>
lea    0xf0b(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    0x8(%rsp),%edx
lea    0xe46(%rip),%rdi
mov    %edx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1275 <main+0xd5>
mov    %eax,%ebx
mov    0x8(%rsp),%rsi
mov    $0x10,%edx
mov    %eax,%edi
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     122d <main+0x8d>
lea    0xe2c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1239 <main+0x99>
lea    0xee7(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%rsi
mov    $0x10,%edx
mov    %ebx,%edi
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1261 <main+0xc1>
lea    0xe3f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    126d <main+0xcd>
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2d5c(%rip),%rax
mov    (%rax),%ebx
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd7b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/tmp/mem_addr\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"error opening file: %s\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [30 x i8] c"buffer address is 0x%08x(%d)\0A\00"
@.str.5 = [10 x i8] c"/tmp/data\00"
@O_RDWR = global i32 0
@.str.6 = [74 x i8] c"PASS: read file content to memory in another process, failed as expected\0A\00"
@.str.7 = [71 x i8] c"FAIL: read file content to memory in another process, return value %d\0A\00"
@.str.8 = [69 x i8] c"PASS: write file from memory in another process, failed as expected\0A\00"
@.str.9 = [66 x i8] c"FAIL: write file from memory in another process, return value %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %3
store i64 16, i64* %6
%7 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %16, label %10
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32*, i32** %5
%18 = call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %2)
%19 = load i8*, i8** %2
%20 = ptrtoint i8* %19 to i32
%21 = load i8*, i8** %2
%22 = ptrtoint i8* %21 to i32
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %20, i32 %22)
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %24)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %16
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %16
%35 = load i32, i32* %4
%36 = load i8*, i8** %2
%37 = load i64, i64* %6
%38 = call i32 @read(i32 %35, i8* %36, i64 %37)
store i32 %38, i32* %3
%39 = load i32, i32* %3
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.6, i64 0, i64 0))
br label %46
43: ; preds = %34
%44 = load i32, i32* %3
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.7, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %43, %41
%47 = load i32, i32* %4
%48 = load i8*, i8** %2
%49 = load i64, i64* %6
%50 = call i32 @write(i32 %47, i8* %48, i64 %49)
store i32 %50, i32* %3
%51 = load i32, i32* %3
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %46
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.8, i64 0, i64 0))
br label %58
55: ; preds = %46
%56 = load i32, i32* %3
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.9, i64 0, i64 0), i32 %56)
br label %58
58: ; preds = %55, %53
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"/tmp/mem_addr\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"error opening file: %s\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [30 x i8] c"buffer address is 0x%08x(%d)\0A\00"
@.str.5 = [10 x i8] c"/tmp/data\00"
@O_RDWR = global i32 0
@.str.7 = [71 x i8] c"FAIL: read file content to memory in another process, return value %d\0A\00"
@.str.9 = [66 x i8] c"FAIL: write file from memory in another process, return value %d\0A\00"
@str = [68 x i8] c"PASS: write file from memory in another process, failed as expected\00"
@str.10 = [73 x i8] c"PASS: read file content to memory in another process, failed as expected\00"
define i32 @main() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %11
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %1)
%13 = load i8*, i8** %1
%14 = ptrtoint i8* %13 to i64
%15 = trunc i64 %14 to i32
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %15, i32 %15)
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %11
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %11
%27 = load i8*, i8** %1
%28 = call i32 @read(i32 %18, i8* %27, i64 16)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([73 x i8], [73 x i8]* @str.10, i64 0, i64 0))
br label %34
32: ; preds = %26
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([71 x i8], [71 x i8]* @.str.7, i64 0, i64 0), i32 %28)
br label %34
34: ; preds = %32, %30
%35 = load i8*, i8** %1
%36 = call i32 @write(i32 %18, i8* %35, i64 16)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @str, i64 0, i64 0))
br label %42
40: ; preds = %34
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.9, i64 0, i64 0), i32 %36)
br label %42
42: ; preds = %40, %38
ret i32 0
}