#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 scalar_t__ uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fread(unsigned char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseeko(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(unsigned char **, int, int, int /*<<< orphan*/ *);
void dump_memory_region(FILE *pMemFile, uint64_t start_address, uint64_t length,
                        FILE *out_file) {
  uint64_t address;
  int pageLength = 4096;
  fseeko(pMemFile, start_address, SEEK_SET);
  unsigned char page[pageLength];
  for (address = start_address; address < start_address + length;
       address += pageLength) {
    fread(&page, 1, pageLength, pMemFile);
    fwrite(&page, 1, pageLength, out_file);
  }
}


x86 assembly (clang, -O0):
<dump_memory_region>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x1000,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e82(%rip),%rax
mov    (%rax),%edx
call   1040 <fseeko@plt>
mov    -0x2c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    -0x18(%rbp),%rcx
cmp    %rcx,%rax
jge    11e3 <dump_memory_region+0xb3>
mov    -0x48(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x48(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fwrite@plt>
movslq -0x2c(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1193 <dump_memory_region+0x63>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_memory_region>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
mov    0x2e8d(%rip),%rax
mov    (%rax),%edx
call   1040 <fseeko@plt>
test   %r15,%r15
jle    11a6 <dump_memory_region+0x76>
add    %rbx,%r15
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r14,%rcx
call   1050 <fwrite@plt>
add    $0x1000,%rbx
cmp    %r15,%rbx
jl     1170 <dump_memory_region+0x40>
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define void @dump_memory_region(i32* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
store i32* %0, i32** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
store i32 4096, i32* %10
%13 = load i32*, i32** %5
%14 = load i64, i64* %6
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @fseeko(i32* %13, i64 %14, i32 %15)
%17 = load i32, i32* %10
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %11
%20 = alloca i8, i64 %18
store i64 %18, i64* %12
%21 = load i64, i64* %6
store i64 %21, i64* %9
br label %22
22: ; preds = %37, %4
%23 = load i64, i64* %9
%24 = load i64, i64* %6
%25 = load i64, i64* %7
%26 = add nsw i64 %24, %25
%27 = icmp slt i64 %23, %26
br i1 %27, label %28, label %42
28: ; preds = %22
%29 = bitcast i8* %20 to i8**
%30 = load i32, i32* %10
%31 = load i32*, i32** %5
%32 = call i32 @fread(i8** %29, i32 1, i32 %30, i32* %31)
%33 = bitcast i8* %20 to i8**
%34 = load i32, i32* %10
%35 = load i32*, i32** %8
%36 = call i32 @fwrite(i8** %33, i32 1, i32 %34, i32* %35)
br label %37
37: ; preds = %28
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = load i64, i64* %9
%41 = add nsw i64 %40, %39
store i64 %41, i64* %9
br label %22
42: ; preds = %22
%43 = load i8*, i8** %11
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define void @dump_memory_region(i32* nocapture %0, i64 %1, i64 %2, i32* nocapture %3) {
%5 = load i32, i32* @SEEK_SET
%6 = tail call i32 @fseeko(i32* %0, i64 %1, i32 %5)
%7 = alloca [512 x i8*]
%8 = getelementptr inbounds [512 x i8*], [512 x i8*]* %7, i64 0, i64 0
%9 = add nsw i64 %2, %1
%10 = icmp sgt i64 %2, 0
br i1 %10, label %11, label %17
11: ; preds = %4, %11
%12 = phi i64 [ %15, %11 ], [ %1, %4 ]
%13 = call i32 @fread(i8** %8, i32 1, i32 4096, i32* %0)
%14 = call i32 @fwrite(i8** %8, i32 1, i32 4096, i32* %3)
%15 = add nsw i64 %12, 4096
%16 = icmp slt i64 %15, %9
br i1 %16, label %11, label %17
17: ; preds = %11, %4
ret void
}