#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(int);
int munmap(void *, unsigned int);
int /*<<< orphan*/ printf(char *, int);
void unmapmem(void *addr, unsigned size) {
  const intptr_t offset = (intptr_t)addr % (4 * 1024);
  addr = (char *)addr - offset;
  size = size + offset;
  int s = munmap(addr, size);
  if (s != 0) {
    printf("munmap error %d\n", s);
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<unmapmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x1000,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
xor    %ecx,%ecx
sub    -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    -0x18(%rbp),%rax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <munmap@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     119e <unmapmem+0x6e>
mov    -0x1c(%rbp),%esi
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unmapmem>:
push   %rax
lea    0xfff(%rdi),%rax
test   %rdi,%rdi
cmovns %rdi,%rax
and    $0xfffffffffffff000,%rax
sub    %eax,%edi
add    %edi,%esi
mov    %rax,%rdi
call   1040 <munmap@plt>
test   %eax,%eax
jne    1157 <unmapmem+0x27>
pop    %rax
ret
lea    0xea2(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"munmap error %d\0A\00"
define void @unmapmem(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = ptrtoint i8* %7 to i64
%9 = srem i64 %8, 4096
store i64 %9, i64* %5
%10 = load i8*, i8** %3
%11 = load i64, i64* %5
%12 = sub i64 0, %11
%13 = getelementptr inbounds i8, i8* %10, i64 %12
store i8* %13, i8** %3
%14 = load i32, i32* %4
%15 = zext i32 %14 to i64
%16 = load i64, i64* %5
%17 = add nsw i64 %15, %16
%18 = trunc i64 %17 to i32
store i32 %18, i32* %4
%19 = load i8*, i8** %3
%20 = load i32, i32* %4
%21 = call i32 @munmap(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %2
%25 = load i32, i32* %6
%26 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %2
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"munmap error %d\0A\00"
define void @unmapmem(i8* %0, i32 %1) {
%3 = ptrtoint i8* %0 to i64
%4 = srem i64 %3, 4096
%5 = sub nsw i64 0, %4
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = trunc i64 %4 to i32
%8 = add i32 %7, %1
%9 = tail call i32 @munmap(i8* %6, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %9)
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
ret void
}