#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned int, int);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int getpagesize();
int /*<<< orphan*/ strcat(char *, char *);
void save_page(char *fname, void *ptr) {
  (void)fname;
  (void)ptr;
  int *startAddr = ptr - ((intptr_t)ptr % getpagesize());
  FILE *dest = fopen(fname, "w");
  fwrite(startAddr, 1, getpagesize(), dest);
  fclose(dest);
  char str[80] = "readable.";
  strcat(str, fname);
  FILE *destReadable = fopen(str, "w");
  for (int *i = startAddr; i < startAddr + (getpagesize() / sizeof(int)); i++) {
    fprintf(destReadable, "0x%8.8X\t:\t0x%8.8X\n", (unsigned int)(intptr_t)i,
            *i);
  }
  fclose(destReadable);
  return;
}


x86 assembly (clang, -O0):
<save_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,%ecx
mov    -0x98(%rbp),%rax
movslq %ecx,%rcx
cqto
idiv   %rcx
mov    -0x90(%rbp),%rax
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    -0x88(%rbp),%rdi
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x70(%rbp),%rdi
lea    0xe0b(%rip),%rsi
mov    $0x50,%edx
call   1050 <memcpy@plt>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <strcat@plt>
lea    -0x70(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x78(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    -0xa8(%rbp),%rcx
mov    %eax,%edx
mov    -0xa0(%rbp),%rax
movslq %edx,%rdx
shr    $0x2,%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    12b9 <save_page+0x149>
mov    -0x78(%rbp),%rdi
mov    -0x80(%rbp),%rax
mov    %eax,%edx
mov    -0x80(%rbp),%rax
mov    (%rax),%ecx
lea    0xd5f(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x80(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x80(%rbp)
jmp    1248 <save_page+0xd8>
mov    -0x78(%rbp),%rdi
call   1030 <fclose@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_page>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    %eax,%ecx
movslq %eax,%rsi
mov    %rbx,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     1199 <save_page+0x39>
mov    %rbx,%rax
cqto
idiv   %rsi
mov    %rdx,%r15
jmp    11a2 <save_page+0x42>
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
mov    %edx,%r15d
mov    %rbx,%r14
sub    %r15,%r14
lea    0xe51(%rip),%r13
mov    %r12,%rdi
mov    %r13,%rsi
call   1050 <fopen@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
movaps 0xe7b(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe5f(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe43(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe27(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe0b(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1060 <strcat@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1050 <fopen@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1070 <getpagesize@plt>
cltq
and    $0xfffffffffffffffc,%rax
add    %r14,%rax
cmp    %r14,%rax
jbe    128c <save_page+0x12c>
sub    %r15d,%ebx
lea    0xdae(%rip),%r15
mov    %r14,%rbp
nopw   0x0(%rax,%rax,1)
mov    0x0(%rbp),%ecx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
add    $0x4,%rbp
xor    %eax,%eax
call   1070 <getpagesize@plt>
cltq
and    $0xfffffffffffffffc,%rax
add    %r14,%rax
add    $0x4,%ebx
cmp    %rax,%rbp
jb     1260 <save_page+0x100>
mov    %r12,%rdi
call   1030 <fclose@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@__const.save_page.str = [80 x i8] c"readable.\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str.1 = [19 x i8] c"0x%8.8X\09:\090x%8.8X\0A\00"
define void @save_page(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca [80 x i8]
%8 = alloca i32*
%9 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = load i8*, i8** %4
%12 = load i8*, i8** %4
%13 = load i8*, i8** %4
%14 = ptrtoint i8* %13 to i64
%15 = call i32 (...) @getpagesize()
%16 = sext i32 %15 to i64
%17 = srem i64 %14, %16
%18 = sub i64 0, %17
%19 = getelementptr i8, i8* %12, i64 %18
%20 = bitcast i8* %19 to i32*
store i32* %20, i32** %5
%21 = load i8*, i8** %3
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %5
%24 = call i32 (...) @getpagesize()
%25 = load i32*, i32** %6
%26 = call i32 @fwrite(i32* %23, i32 1, i32 %24, i32* %25)
%27 = load i32*, i32** %6
%28 = call i32 @fclose(i32* %27)
%29 = bitcast [80 x i8]* %7 to i8*
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%31 = load i8*, i8** %3
%32 = call i32 @strcat(i8* %30, i8* %31)
%33 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %34, i32** %8
%35 = load i32*, i32** %5
store i32* %35, i32** %9
br label %36
36: ; preds = %52, %2
%37 = load i32*, i32** %9
%38 = load i32*, i32** %5
%39 = call i32 (...) @getpagesize()
%40 = sext i32 %39 to i64
%41 = udiv i64 %40, 4
%42 = getelementptr inbounds i32, i32* %38, i64 %41
%43 = icmp ult i32* %37, %42
br i1 %43, label %44, label %55
44: ; preds = %36
%45 = load i32*, i32** %8
%46 = load i32*, i32** %9
%47 = ptrtoint i32* %46 to i64
%48 = trunc i64 %47 to i32
%49 = load i32*, i32** %9
%50 = load i32, i32* %49
%51 = call i32 @fprintf(i32* %45, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %48, i32 %50)
br label %52
52: ; preds = %44
%53 = load i32*, i32** %9
%54 = getelementptr inbounds i32, i32* %53, i32 1
store i32* %54, i32** %9
br label %36
55: ; preds = %36
%56 = load i32*, i32** %8
%57 = call i32 @fclose(i32* %56)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@__const.save_page.str = [80 x i8] c"readable.\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str.1 = [19 x i8] c"0x%8.8X\09:\090x%8.8X\0A\00"
define void @save_page(i8* %0, i8* %1) {
%3 = alloca [80 x i8]
%4 = ptrtoint i8* %1 to i64
%5 = tail call i32 (...) @getpagesize()
%6 = sext i32 %5 to i64
%7 = srem i64 %4, %6
%8 = sub nsw i64 0, %7
%9 = getelementptr i8, i8* %1, i64 %8
%10 = bitcast i8* %9 to i32*
%11 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 (...) @getpagesize()
%13 = tail call i32 @fwrite(i32* %10, i32 1, i32 %12, i32* %11)
%14 = tail call i32 @fclose(i32* %11)
%15 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%16 = call i32 @strcat(i8* %15, i8* %0)
%17 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = call i32 (...) @getpagesize()
%19 = sext i32 %18 to i64
%20 = lshr i64 %19, 2
%21 = getelementptr inbounds i32, i32* %10, i64 %20
%22 = icmp ugt i32* %21, %10
br i1 %22, label %25, label %23
23: ; preds = %25, %2
%24 = call i32 @fclose(i32* %17)
ret void
25: ; preds = %2, %25
%26 = phi i32* [ %31, %25 ], [ %10, %2 ]
%27 = ptrtoint i32* %26 to i64
%28 = trunc i64 %27 to i32
%29 = load i32, i32* %26
%30 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29)
%31 = getelementptr inbounds i32, i32* %26, i64 1
%32 = call i32 (...) @getpagesize()
%33 = sext i32 %32 to i64
%34 = lshr i64 %33, 2
%35 = getelementptr inbounds i32, i32* %10, i64 %34
%36 = icmp ult i32* %31, %35
br i1 %36, label %25, label %23
}