#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 size_t uint64_t;

/* Variables and functions */
int /*<<< orphan*/ DecompressPage(void *, void *, void *, size_t, void *);
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
void *calloc(size_t, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
scalar_t__ mprotect(void *, size_t, int);
int /*<<< orphan*/ perror(char *);
void DecompressWholeRange(void *cut_start, void *compressed_start,
                          size_t cut_length, size_t page_size) {
  if (mprotect(cut_start, cut_length, PROT_READ | PROT_EXEC | PROT_WRITE)) {
    perror("Failed to enable PROT_WRITE on cut range");
    exit(1);
  }
  void *buffer = calloc(page_size, sizeof(char));
  for (uint64_t offset = 0; offset < cut_length; offset += page_size) {
    DecompressPage(cut_start, compressed_start, cut_start + offset, page_size,
                   buffer);
    memcpy(cut_start + offset, buffer, page_size);
  }
  free(buffer);
  if (mprotect(cut_start, cut_length, PROT_READ | PROT_EXEC)) {
    perror("Failed to disable PROT_WRITE on cut range");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<DecompressWholeRange>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
mov    0x2e40(%rip),%rax
or     (%rax),%edx
mov    0x2e27(%rip),%rax
or     (%rax),%edx
call   1070 <mprotect@plt>
cmp    $0x0,%rax
je     11d0 <DecompressWholeRange+0x60>
lea    0xe3f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    123b <DecompressWholeRange+0xcb>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
add    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1050 <DecompressPage@plt>
mov    -0x8(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11ea <DecompressWholeRange+0x7a>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
mov    0x2d84(%rip),%rax
or     (%rax),%edx
call   1070 <mprotect@plt>
cmp    $0x0,%rax
je     1283 <DecompressWholeRange+0x113>
lea    0xdb5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<DecompressWholeRange>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e76(%rip),%rbp
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
or     0x0(%rbp),%eax
mov    %rcx,%rbx
mov    %rdx,%rbp
mov    %rsi,%r15
mov    0x2e32(%rip),%rcx
or     (%rcx),%eax
mov    %rdi,%r12
mov    %rdx,%rsi
mov    %eax,%edx
call   1070 <mprotect@plt>
test   %rax,%rax
jne    124f <DecompressWholeRange+0xdf>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%r13
test   %rbp,%rbp
mov    %r12,(%rsp)
je     1215 <DecompressWholeRange+0xa5>
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%r14
mov    (%rsp),%rdi
lea    (%rdi,%r12,1),%rbp
mov    %r15,%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
mov    %r13,%r8
call   1050 <DecompressPage@plt>
mov    %rbp,%rdi
mov    %r14,%rbp
mov    %r13,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
add    %rbx,%r12
cmp    %r14,%r12
jb     11e0 <DecompressWholeRange+0x70>
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
mov    0x2dcb(%rip),%rax
or     (%rax),%edx
mov    (%rsp),%rdi
mov    %rbp,%rsi
call   1070 <mprotect@plt>
test   %rax,%rax
jne    1258 <DecompressWholeRange+0xe8>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdaa(%rip),%rdi
jmp    125f <DecompressWholeRange+0xef>
lea    0xdca(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_EXEC = global i32 0
@PROT_WRITE = global i32 0
@.str = [41 x i8] c"Failed to enable PROT_WRITE on cut range\00"
@.str.1 = [42 x i8] c"Failed to disable PROT_WRITE on cut range\00"
define void @DecompressWholeRange(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%11 = load i8*, i8** %5
%12 = load i64, i64* %7
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_EXEC
%15 = or i32 %13, %14
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %15, %16
%18 = call i64 @mprotect(i8* %11, i64 %12, i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %4
%21 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %4
%24 = load i64, i64* %8
%25 = call i8* @calloc(i64 %24, i32 1)
store i8* %25, i8** %9
store i64 0, i64* %10
br label %26
26: ; preds = %45, %23
%27 = load i64, i64* %10
%28 = load i64, i64* %7
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %49
30: ; preds = %26
%31 = load i8*, i8** %5
%32 = load i8*, i8** %6
%33 = load i8*, i8** %5
%34 = load i64, i64* %10
%35 = getelementptr i8, i8* %33, i64 %34
%36 = load i64, i64* %8
%37 = load i8*, i8** %9
%38 = call i32 @DecompressPage(i8* %31, i8* %32, i8* %35, i64 %36, i8* %37)
%39 = load i8*, i8** %5
%40 = load i64, i64* %10
%41 = getelementptr i8, i8* %39, i64 %40
%42 = load i8*, i8** %9
%43 = load i64, i64* %8
%44 = call i32 @memcpy(i8* %41, i8* %42, i64 %43)
br label %45
45: ; preds = %30
%46 = load i64, i64* %8
%47 = load i64, i64* %10
%48 = add i64 %47, %46
store i64 %48, i64* %10
br label %26
49: ; preds = %26
%50 = load i8*, i8** %9
%51 = call i32 @free(i8* %50)
%52 = load i8*, i8** %5
%53 = load i64, i64* %7
%54 = load i32, i32* @PROT_READ
%55 = load i32, i32* @PROT_EXEC
%56 = or i32 %54, %55
%57 = call i64 @mprotect(i8* %52, i64 %53, i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %49
%60 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %49
ret void
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_EXEC = global i32 0
@PROT_WRITE = global i32 0
@.str = [41 x i8] c"Failed to enable PROT_WRITE on cut range\00"
@.str.1 = [42 x i8] c"Failed to disable PROT_WRITE on cut range\00"
define void @DecompressWholeRange(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_EXEC
%7 = or i32 %6, %5
%8 = load i32, i32* @PROT_WRITE
%9 = or i32 %7, %8
%10 = tail call i64 @mprotect(i8* %0, i64 %2, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %4
%13 = tail call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %4
%16 = tail call i8* @calloc(i64 %3, i32 1)
%17 = icmp eq i64 %2, 0
br i1 %17, label %18, label %25
18: ; preds = %25, %15
%19 = tail call i32 @free(i8* %16)
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_EXEC
%22 = or i32 %21, %20
%23 = tail call i64 @mprotect(i8* %0, i64 %2, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %35, label %32
25: ; preds = %15, %25
%26 = phi i64 [ %30, %25 ], [ 0, %15 ]
%27 = getelementptr i8, i8* %0, i64 %26
%28 = tail call i32 @DecompressPage(i8* %0, i8* %1, i8* %27, i64 %3, i8* %16)
%29 = tail call i32 @memcpy(i8* %27, i8* %16, i64 %3)
%30 = add i64 %26, %3
%31 = icmp ult i64 %30, %2
br i1 %31, label %25, label %18
32: ; preds = %18
%33 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %18
ret void
}