#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpagesize();
int munmap(char *, int);
int /*<<< orphan*/ printf(char *);
void deallocate_protected_space(char *p, int size) {
  int page = getpagesize();
  int status;
  int aligned_size = ((size + page - 1) / page) * page;
  status = munmap(p - page, aligned_size + 2 * page);
  if (status != 0) {
    printf("Deallocation failed in deallocating protected space\n");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<deallocate_protected_space>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1050 <getpagesize@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
sub    $0x1,%eax
cltd
idivl  -0x10(%rbp)
imul   -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x10(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
add    %eax,%esi
call   1040 <munmap@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11b4 <deallocate_protected_space+0x74>
lea    0xe5f(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<deallocate_protected_space>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <getpagesize@plt>
mov    %eax,%ecx
lea    (%r14,%rcx,1),%esi
add    $0xffffffff,%esi
mov    %esi,%eax
cltd
idiv   %ecx
sub    %edx,%esi
movslq %ecx,%rax
sub    %rax,%rbx
lea    (%rsi,%rcx,2),%esi
mov    %rbx,%rdi
call   1040 <munmap@plt>
test   %eax,%eax
jne    117e <deallocate_protected_space+0x3e>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [53 x i8] c"Deallocation failed in deallocating protected space\0A\00"
@EXIT_FAILURE = global i32 0
define void @deallocate_protected_space(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = call i32 (...) @getpagesize()
store i32 %8, i32* %5
%9 = load i32, i32* %4
%10 = load i32, i32* %5
%11 = add nsw i32 %9, %10
%12 = sub nsw i32 %11, 1
%13 = load i32, i32* %5
%14 = sdiv i32 %12, %13
%15 = load i32, i32* %5
%16 = mul nsw i32 %14, %15
store i32 %16, i32* %7
%17 = load i8*, i8** %3
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = sub i64 0, %19
%21 = getelementptr inbounds i8, i8* %17, i64 %20
%22 = load i32, i32* %7
%23 = load i32, i32* %5
%24 = mul nsw i32 2, %23
%25 = add nsw i32 %22, %24
%26 = call i32 @munmap(i8* %21, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %2
%30 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %2
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@str = [52 x i8] c"Deallocation failed in deallocating protected space\00"
define void @deallocate_protected_space(i8* %0, i32 %1) {
%3 = tail call i32 (...) @getpagesize()
%4 = add i32 %1, -1
%5 = add i32 %4, %3
%6 = srem i32 %5, %3
%7 = sub i32 %5, %6
%8 = sext i32 %3 to i64
%9 = sub nsw i64 0, %8
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = shl nsw i32 %3, 1
%12 = add nsw i32 %7, %11
%13 = tail call i32 @munmap(i8* %10, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
ret void
}