#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 PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ exit(int);
scalar_t__ mprotect(void *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
size_t sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ xor_block(unsigned char *, int);
void uncrypt() {
  int p = *((int *)(((unsigned char *)0x400000) + 0x09));
  int len = *((short *)(((unsigned char *)0x400000) + 0xd));
  unsigned char *ptr = ((unsigned char *)0x400000) + p;
  unsigned char *ptr1 = ((unsigned char *)0x400000) + p + len;
  size_t pagesize = sysconf(_SC_PAGESIZE);
  uintptr_t pagestart = (uintptr_t)ptr & -pagesize;
  int psize = (ptr1 - (unsigned char *)pagestart);
  if (mprotect((void *)pagestart, psize, PROT_READ | PROT_WRITE | PROT_EXEC) <
      0) {
    perror("mprotect:");
    exit(1);
  };
  xor_block(((unsigned char *)0x400000) + p, len);
  if (mprotect((void *)pagestart, psize, PROT_READ | PROT_EXEC) < 0) {
    perror("mprotect:");
    exit(1);
  };
  printf("+ Ready to run!\n");
}


x86 assembly (clang, -O0):
<uncrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x400000,%eax
mov    0x9(%rax),%eax
mov    %eax,-0x4(%rbp)
mov    $0x400000,%eax
movswl 0xd(%rax),%eax
mov    %eax,-0x8(%rbp)
movslq -0x4(%rbp),%rcx
mov    $0x400000,%eax
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x4(%rbp),%rcx
mov    $0x400000,%eax
add    %rcx,%rax
movslq -0x8(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
sub    -0x20(%rbp),%rcx
and    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    0x2dda(%rip),%rax
or     (%rax),%edx
mov    0x2de1(%rip),%rax
or     (%rax),%edx
call   1050 <mprotect@plt>
cmp    $0x0,%rax
jge    121e <uncrypt+0xbe>
lea    0xdf1(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movslq -0x4(%rbp),%rax
mov    $0x400000,%edi
add    %rax,%rdi
mov    -0x8(%rbp),%esi
call   1040 <xor_block@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
mov    0x2d8f(%rip),%rax
or     (%rax),%edx
call   1050 <mprotect@plt>
cmp    $0x0,%rax
jge    1270 <uncrypt+0x110>
lea    0xd9f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xd93(%rip),%rdi
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uncrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movslq 0x400009,%rbx
movswl 0x40000d,%r14d
add    $0x400000,%rbx
lea    (%rbx,%r14,1),%r15d
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,%rbp
neg    %rbp
and    %rbx,%rbp
mov    0x2e53(%rip),%r12
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
or     (%r12),%edx
sub    %ebp,%r15d
mov    0x2e1c(%rip),%r13
or     0x0(%r13),%edx
mov    %rbp,%rdi
mov    %r15d,%esi
call   1050 <mprotect@plt>
test   %rax,%rax
js     120d <uncrypt+0xad>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <xor_block@plt>
mov    0x0(%r13),%edx
or     (%r12),%edx
mov    %rbp,%rdi
mov    %r15d,%esi
call   1050 <mprotect@plt>
test   %rax,%rax
js     120d <uncrypt+0xad>
lea    0xe10(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>
lea    0xdec(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@.str = [10 x i8] c"mprotect:\00"
@.str.1 = [17 x i8] c"+ Ready to run!\0A\00"
define void @uncrypt() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = load i32, i32* bitcast (i8* getelementptr inbounds (i8, i8* inttoptr (i64 4194304 to i8*), i64 9) to i32*)
store i32 %8, i32* %1
%9 = load i16, i16* bitcast (i8* getelementptr inbounds (i8, i8* inttoptr (i64 4194304 to i8*), i64 13) to i16*)
%10 = sext i16 %9 to i32
store i32 %10, i32* %2
%11 = load i32, i32* %1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* inttoptr (i64 4194304 to i8*), i64 %12
store i8* %13, i8** %3
%14 = load i32, i32* %1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* inttoptr (i64 4194304 to i8*), i64 %15
%17 = load i32, i32* %2
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
store i8* %19, i8** %4
%20 = load i32, i32* @_SC_PAGESIZE
%21 = call i64 @sysconf(i32 %20)
store i64 %21, i64* %5
%22 = load i8*, i8** %3
%23 = ptrtoint i8* %22 to i64
%24 = load i64, i64* %5
%25 = sub i64 0, %24
%26 = and i64 %23, %25
store i64 %26, i64* %6
%27 = load i8*, i8** %4
%28 = load i64, i64* %6
%29 = inttoptr i64 %28 to i8*
%30 = ptrtoint i8* %27 to i64
%31 = ptrtoint i8* %29 to i64
%32 = sub i64 %30, %31
%33 = trunc i64 %32 to i32
store i32 %33, i32* %7
%34 = load i64, i64* %6
%35 = inttoptr i64 %34 to i8*
%36 = load i32, i32* %7
%37 = load i32, i32* @PROT_READ
%38 = load i32, i32* @PROT_WRITE
%39 = or i32 %37, %38
%40 = load i32, i32* @PROT_EXEC
%41 = or i32 %39, %40
%42 = call i64 @mprotect(i8* %35, i32 %36, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %0
%45 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %0
%48 = load i32, i32* %1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* inttoptr (i64 4194304 to i8*), i64 %49
%51 = load i32, i32* %2
%52 = call i32 @xor_block(i8* %50, i32 %51)
%53 = load i64, i64* %6
%54 = inttoptr i64 %53 to i8*
%55 = load i32, i32* %7
%56 = load i32, i32* @PROT_READ
%57 = load i32, i32* @PROT_EXEC
%58 = or i32 %56, %57
%59 = call i64 @mprotect(i8* %54, i32 %55, i32 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %47
%62 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %47
%65 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@.str = [10 x i8] c"mprotect:\00"
@str = [16 x i8] c"+ Ready to run!\00"
define void @uncrypt() {
%1 = load i32, i32* inttoptr (i64 4194313 to i32*)
%2 = load i16, i16* inttoptr (i64 4194317 to i16*)
%3 = sext i32 %1 to i64
%4 = getelementptr inbounds i8, i8* inttoptr (i64 4194304 to i8*), i64 %3
%5 = sext i16 %2 to i64
%6 = getelementptr inbounds i8, i8* %4, i64 %5
%7 = load i32, i32* @_SC_PAGESIZE
%8 = tail call i64 @sysconf(i32 %7)
%9 = ptrtoint i8* %4 to i64
%10 = sub i64 0, %8
%11 = and i64 %9, %10
%12 = inttoptr i64 %11 to i8*
%13 = ptrtoint i8* %6 to i64
%14 = sub i64 %13, %11
%15 = trunc i64 %14 to i32
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %17, %16
%19 = load i32, i32* @PROT_EXEC
%20 = or i32 %18, %19
%21 = tail call i64 @mprotect(i8* %12, i32 %15, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %0
%24 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %0
%27 = sext i16 %2 to i32
%28 = tail call i32 @xor_block(i8* %4, i32 %27)
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_EXEC
%31 = or i32 %30, %29
%32 = tail call i64 @mprotect(i8* %12, i32 %15, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %26
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
ret void
}