#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 getpagesize();
unsigned char *malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, unsigned char *, int);
int /*<<< orphan*/ test_contents(unsigned char *, int, int, int, int);
int /*<<< orphan*/ write(int, unsigned char *, int);
int test_read_write(int fd, unsigned char *mmap_addr) {
  unsigned char *local_addr;
  int len = 16 * getpagesize(), i;
  printf("\nWrite test ...\n");
  local_addr = malloc(len);
  if (!local_addr)
    return -1;
  memset(local_addr, 0, len);
  for (i = 0; i < 16 * getpagesize(); i += getpagesize()) {
    local_addr[i] = 0xa0;
    local_addr[i + 1] = 0xb0;
    local_addr[i + 2] = 0xc0;
    local_addr[i + 3] = 0xd0;
  }
  write(fd, local_addr, len);
  test_contents(mmap_addr, 0xa0, 0xb0, 0xc0, 0xd0);
  printf("\nRead test ...\n");
  memset(local_addr, 0, len);
  read(fd, local_addr, len);
  test_contents(local_addr, 0xa0, 0xb0, 0xc0, 0xd0);
  return 0;
}


x86 assembly (clang, -O0):
<test_read_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <getpagesize@plt>
shl    $0x4,%eax
mov    %eax,-0x1c(%rbp)
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bb <test_read_write+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <test_read_write+0x149>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
shl    $0x4,%ecx
cmp    %ecx,%eax
jge    123e <test_read_write+0xce>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0xa0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0xb0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movb   $0xc0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
movb   $0xd0,(%rax,%rcx,1)
mov    $0x0,%al
call   1090 <getpagesize@plt>
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11d0 <test_read_write+0x60>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%rdi
mov    $0xa0,%esi
mov    $0xb0,%edx
mov    $0xc0,%ecx
mov    $0xd0,%r8d
call   1070 <test_contents@plt>
lea    0xd9f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <read@plt>
mov    -0x18(%rbp),%rdi
mov    $0xa0,%esi
mov    $0xb0,%edx
mov    $0xc0,%ecx
mov    $0xd0,%r8d
call   1070 <test_contents@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_read_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%r15d
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    %eax,%r13d
shl    $0x4,%r13d
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
mov    %r13d,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1262 <test_read_write+0xf2>
mov    %rax,%rbx
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
mov    %r13d,%edx
call   1050 <memset@plt>
xor    %eax,%eax
call   1090 <getpagesize@plt>
test   %eax,%eax
jle    11f1 <test_read_write+0x81>
xor    %ebp,%ebp
nopl   0x0(%rax)
movslq %ebp,%rbp
movl   $0xd0c0b0a0,(%rbx,%rbp,1)
xor    %eax,%eax
call   1090 <getpagesize@plt>
add    %eax,%ebp
xor    %eax,%eax
call   1090 <getpagesize@plt>
shl    $0x4,%eax
cmp    %eax,%ebp
jl     11d0 <test_read_write+0x60>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1040 <write@plt>
mov    %r12,%rdi
mov    $0xa0,%esi
mov    $0xb0,%edx
mov    $0xc0,%ecx
mov    $0xd0,%r8d
call   1070 <test_contents@plt>
lea    0xded(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r13d,%edx
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1060 <read@plt>
mov    %rbx,%rdi
mov    $0xa0,%esi
mov    $0xb0,%edx
mov    $0xc0,%ecx
mov    $0xd0,%r8d
call   1070 <test_contents@plt>
jmp    1268 <test_read_write+0xf8>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"\0AWrite test ...\0A\00"
@.str.1 = [16 x i8] c"\0ARead test ...\0A\00"
define i32 @test_read_write(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = call i32 (...) @getpagesize()
%10 = mul nsw i32 16, %9
store i32 %10, i32* %7
%11 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* %7
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %2
store i32 -1, i32* %3
br label %67
17: ; preds = %2
%18 = load i8*, i8** %6
%19 = load i32, i32* %7
%20 = call i32 @memset(i8* %18, i32 0, i32 %19)
store i32 0, i32* %8
br label %21
21: ; preds = %46, %17
%22 = load i32, i32* %8
%23 = call i32 (...) @getpagesize()
%24 = mul nsw i32 16, %23
%25 = icmp slt i32 %22, %24
br i1 %25, label %26, label %50
26: ; preds = %21
%27 = load i8*, i8** %6
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 -96, i8* %30
%31 = load i8*, i8** %6
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 -80, i8* %35
%36 = load i8*, i8** %6
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 -64, i8* %40
%41 = load i8*, i8** %6
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 3
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
store i8 -48, i8* %45
br label %46
46: ; preds = %26
%47 = call i32 (...) @getpagesize()
%48 = load i32, i32* %8
%49 = add nsw i32 %48, %47
store i32 %49, i32* %8
br label %21
50: ; preds = %21
%51 = load i32, i32* %4
%52 = load i8*, i8** %6
%53 = load i32, i32* %7
%54 = call i32 @write(i32 %51, i8* %52, i32 %53)
%55 = load i8*, i8** %5
%56 = call i32 @test_contents(i8* %55, i32 160, i32 176, i32 192, i32 208)
%57 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%58 = load i8*, i8** %6
%59 = load i32, i32* %7
%60 = call i32 @memset(i8* %58, i32 0, i32 %59)
%61 = load i32, i32* %4
%62 = load i8*, i8** %6
%63 = load i32, i32* %7
%64 = call i32 @read(i32 %61, i8* %62, i32 %63)
%65 = load i8*, i8** %6
%66 = call i32 @test_contents(i8* %65, i32 160, i32 176, i32 192, i32 208)
store i32 0, i32* %3
br label %67
67: ; preds = %50, %16
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@str = [16 x i8] c"\0AWrite test ...\00"
@str.2 = [15 x i8] c"\0ARead test ...\00"
define i32 @test_read_write(i32 %0, i8* %1) {
%3 = tail call i32 (...) @getpagesize()
%4 = shl nsw i32 %3, 4
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%6 = tail call i8* @malloc(i32 %4)
%7 = icmp eq i8* %6, null
br i1 %7, label %37, label %8
8: ; preds = %2
%9 = tail call i32 @memset(i8* %6, i32 0, i32 %4)
%10 = tail call i32 (...) @getpagesize()
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %30
12: ; preds = %8, %12
%13 = phi i32 [ %26, %12 ], [ 0, %8 ]
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %6, i64 %14
store i8 -96, i8* %15
%16 = add nsw i32 %13, 1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %6, i64 %17
store i8 -80, i8* %18
%19 = add nsw i32 %13, 2
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %6, i64 %20
store i8 -64, i8* %21
%22 = add nsw i32 %13, 3
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %6, i64 %23
store i8 -48, i8* %24
%25 = tail call i32 (...) @getpagesize()
%26 = add nsw i32 %25, %13
%27 = tail call i32 (...) @getpagesize()
%28 = shl nsw i32 %27, 4
%29 = icmp slt i32 %26, %28
br i1 %29, label %12, label %30
30: ; preds = %12, %8
%31 = tail call i32 @write(i32 %0, i8* %6, i32 %4)
%32 = tail call i32 @test_contents(i8* %1, i32 160, i32 176, i32 192, i32 208)
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.2, i64 0, i64 0))
%34 = tail call i32 @memset(i8* %6, i32 0, i32 %4)
%35 = tail call i32 @read(i32 %0, i8* %6, i32 %4)
%36 = tail call i32 @test_contents(i8* %6, i32 160, i32 176, i32 192, i32 208)
br label %37
37: ; preds = %2, %30
%38 = phi i32 [ 0, %30 ], [ -1, %2 ]
ret i32 %38
}