#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 */
scalar_t__ EINVAL;
int MAP_ANON;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
void *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int munmap(void *, int);
void munmap_good(void) {
  void *addr;
  int result;
  addr = mmap((NULL), (0x1000), PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
              -1, 0);
  result = munmap(addr, 0x1000);
  assert(result == 0);
  munmap(addr, 0x1000);
  assert(errno == EINVAL);
  addr = mmap((NULL), (0x5000), PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
              -1, 0);
  result = munmap(addr, 0x2000);
  assert(result == 0);
  result = munmap(addr + 0x2000, 0x3000);
  assert(result == 0);
}


x86 assembly (clang, -O0):
<munmap_good>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2eb9(%rip),%rax
mov    (%rax),%edx
mov    0x2e88(%rip),%rax
or     (%rax),%edx
mov    0x2e6f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5e(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%rax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
mov    0x2df8(%rip),%rax
or     (%rax),%edx
mov    0x2ddf(%rip),%rax
mov    (%rax),%ecx
mov    0x2dce(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x5000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2000,%esi
call   1050 <munmap@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
add    $0x2000,%rdi
mov    $0x3000,%esi
call   1050 <munmap@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<munmap_good>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    0x2eb8(%rip),%r14
mov    0x2e89(%rip),%r12
mov    (%r12),%edx
or     (%r14),%edx
mov    0x2e6b(%rip),%r15
mov    0x2e5c(%rip),%r13
mov    0x0(%r13),%ecx
or     (%r15),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    0x2e15(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%rax
sete   %dil
call   1040 <assert@plt>
mov    (%r12),%edx
or     (%r14),%edx
mov    0x0(%r13),%ecx
or     (%r15),%ecx
xor    %edi,%edi
mov    $0x5000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2000,%esi
call   1050 <munmap@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
add    $0x2000,%rbx
mov    %rbx,%rdi
mov    $0x3000,%esi
call   1050 <munmap@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <assert@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
define void @munmap_good() {
%1 = alloca i8*
%2 = alloca i32
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %3, %4
%6 = load i32, i32* @MAP_ANON
%7 = load i32, i32* @MAP_PRIVATE
%8 = or i32 %6, %7
%9 = call i8* @mmap(i32* null, i32 4096, i32 %5, i32 %8, i32 -1, i32 0)
store i8* %9, i8** %1
%10 = load i8*, i8** %1
%11 = call i32 @munmap(i8* %10, i32 4096)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i8*, i8** %1
%17 = call i32 @munmap(i8* %16, i32 4096)
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINVAL
%20 = icmp eq i64 %18, %19
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %23, %24
%26 = load i32, i32* @MAP_ANON
%27 = load i32, i32* @MAP_PRIVATE
%28 = or i32 %26, %27
%29 = call i8* @mmap(i32* null, i32 20480, i32 %25, i32 %28, i32 -1, i32 0)
store i8* %29, i8** %1
%30 = load i8*, i8** %1
%31 = call i32 @munmap(i8* %30, i32 8192)
store i32 %31, i32* %2
%32 = load i32, i32* %2
%33 = icmp eq i32 %32, 0
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load i8*, i8** %1
%37 = getelementptr i8, i8* %36, i64 8192
%38 = call i32 @munmap(i8* %37, i32 12288)
store i32 %38, i32* %2
%39 = load i32, i32* %2
%40 = icmp eq i32 %39, 0
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
ret void
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
define void @munmap_good() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @PROT_WRITE
%3 = or i32 %2, %1
%4 = load i32, i32* @MAP_ANON
%5 = load i32, i32* @MAP_PRIVATE
%6 = or i32 %5, %4
%7 = tail call i8* @mmap(i32* null, i32 4096, i32 %3, i32 %6, i32 -1, i32 0)
%8 = tail call i32 @munmap(i8* %7, i32 4096)
%9 = icmp eq i32 %8, 0
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = tail call i32 @munmap(i8* %7, i32 4096)
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINVAL
%15 = icmp eq i64 %13, %14
%16 = zext i1 %15 to i32
%17 = tail call i32 @assert(i32 %16)
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %19, %18
%21 = load i32, i32* @MAP_ANON
%22 = load i32, i32* @MAP_PRIVATE
%23 = or i32 %22, %21
%24 = tail call i8* @mmap(i32* null, i32 20480, i32 %20, i32 %23, i32 -1, i32 0)
%25 = tail call i32 @munmap(i8* %24, i32 8192)
%26 = icmp eq i32 %25, 0
%27 = zext i1 %26 to i32
%28 = tail call i32 @assert(i32 %27)
%29 = getelementptr i8, i8* %24, i64 8192
%30 = tail call i32 @munmap(i8* %29, i32 12288)
%31 = icmp eq i32 %30, 0
%32 = zext i1 %31 to i32
%33 = tail call i32 @assert(i32 %32)
ret void
}