#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 EXIT_FAILURE;
int /*<<< orphan*/ getpagesize();
int munmap(void *, size_t);
int /*<<< orphan*/ vsd_get_size(size_t *);
int vsd_munmap(void *addr, size_t offset) {
  size_t sz = -1;
  size_t page_sz = (size_t)getpagesize();
  if (offset % page_sz != 0) {
    return EXIT_FAILURE;
  }
  vsd_get_size(&sz);
  return munmap(addr, sz - offset);
}


x86 assembly (clang, -O0):
<vsd_munmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
mov    $0x0,%al
call   1050 <getpagesize@plt>
cltq
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x28(%rbp)
cmp    $0x0,%rdx
je     117c <vsd_munmap+0x4c>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1199 <vsd_munmap+0x69>
lea    -0x20(%rbp),%rdi
call   1030 <vsd_get_size@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
sub    -0x18(%rbp),%rsi
call   1040 <munmap@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vsd_munmap>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
movq   $0xffffffffffffffff,(%rsp)
ff
xor    %eax,%eax
call   1050 <getpagesize@plt>
mov    %eax,%ecx
movslq %eax,%rsi
mov    %rbx,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     1172 <vsd_munmap+0x42>
mov    %rbx,%rax
xor    %edx,%edx
div    %rsi
test   %rdx,%rdx
je     117d <vsd_munmap+0x4d>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
jmp    1194 <vsd_munmap+0x64>
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
test   %rdx,%rdx
jne    1167 <vsd_munmap+0x37>
mov    %rsp,%rdi
call   1030 <vsd_get_size@plt>
mov    (%rsp),%rsi
sub    %rbx,%rsi
mov    %r14,%rdi
call   1040 <munmap@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
define i32 @vsd_munmap(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 -1, i64* %6
%8 = call i32 (...) @getpagesize()
%9 = sext i32 %8 to i64
store i64 %9, i64* %7
%10 = load i64, i64* %5
%11 = load i64, i64* %7
%12 = urem i64 %10, %11
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* @EXIT_FAILURE
store i32 %15, i32* %3
br label %23
16: ; preds = %2
%17 = call i32 @vsd_get_size(i64* %6)
%18 = load i8*, i8** %4
%19 = load i64, i64* %6
%20 = load i64, i64* %5
%21 = sub i64 %19, %20
%22 = call i32 @munmap(i8* %18, i64 %21)
store i32 %22, i32* %3
br label %23
23: ; preds = %16, %14
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
define i32 @vsd_munmap(i8* %0, i64 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
store i64 -1, i64* %3
%5 = tail call i32 (...) @getpagesize()
%6 = sext i32 %5 to i64
%7 = urem i64 %1, %6
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = load i32, i32* @EXIT_FAILURE
br label %16
11: ; preds = %2
%12 = call i32 @vsd_get_size(i64* %3)
%13 = load i64, i64* %3
%14 = sub i64 %13, %1
%15 = call i32 @munmap(i8* %0, i64 %14)
br label %16
16: ; preds = %11, %9
%17 = phi i32 [ %10, %9 ], [ %15, %11 ]
ret i32 %17
}