#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*/ _SC_PAGE_SIZE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(void *);
void *malloc(int);
int munmap(void *, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
int main(void) {
  int rc;
  int page_size;
  void *buffer = NULL, *new_addr = NULL;
  page_size = sysconf(_SC_PAGE_SIZE);
  buffer = malloc(page_size * 2);
  if (buffer == NULL) {
    printf("Error at malloc\n");
    exit(2);
  }
  new_addr = buffer + (page_size - (unsigned long)buffer % page_size);
  rc = munmap(new_addr, page_size);
  if (rc == -1) {
    printf("Test FAILED "
           "munmap/2-1.c"
           " Error at munmap(): %s\n",
           strerror(errno));
    free(buffer);
    exit(1);
  }
  free(buffer);
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
shl    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d2 <main+0x62>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movslq -0xc(%rbp),%rcx
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rsi
xor    %edx,%edx
div    %rsi
mov    -0x28(%rbp),%rax
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <munmap@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1244 <main+0xd4>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%r14d
lea    (%r14,%r14,1),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11f7 <main+0x77>
mov    %rax,%rbx
movslq %r14d,%rcx
or     %rcx,%rax
shr    $0x20,%rax
je     11bc <main+0x3c>
mov    %rbx,%rax
xor    %edx,%edx
div    %rcx
jmp    11c3 <main+0x43>
mov    %ebx,%eax
xor    %edx,%edx
div    %r14d
sub    %rdx,%rcx
mov    %rbx,%rdi
add    %rcx,%rdi
mov    %r14d,%esi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
je     120d <main+0x8d>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe48(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe3e(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdde(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@_SC_PAGE_SIZE = global i32 0
@.str = [17 x i8] c"Error at malloc\0A\00"
@.str.1 = [48 x i8] c"Test FAILED munmap/2-1.c Error at munmap(): %s\0A\00"
@errno = global i32 0
@.str.2 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
store i8* null, i8** %4
store i8* null, i8** %5
%6 = load i32, i32* @_SC_PAGE_SIZE
%7 = call i32 @sysconf(i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = mul nsw i32 %8, 2
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 2)
unreachable
16: ; preds = %0
%17 = load i8*, i8** %4
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = load i8*, i8** %4
%21 = ptrtoint i8* %20 to i64
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = urem i64 %21, %23
%25 = sub i64 %19, %24
%26 = getelementptr i8, i8* %17, i64 %25
store i8* %26, i8** %5
%27 = load i8*, i8** %5
%28 = load i32, i32* %3
%29 = call i32 @munmap(i8* %27, i32 %28)
store i32 %29, i32* %2
%30 = load i32, i32* %2
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %39
32: ; preds = %16
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = load i8*, i8** %4
%37 = call i32 @free(i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %16
%40 = load i8*, i8** %4
%41 = call i32 @free(i8* %40)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@_SC_PAGE_SIZE = global i32 0
@.str.1 = [48 x i8] c"Test FAILED munmap/2-1.c Error at munmap(): %s\0A\00"
@errno = global i32 0
@str = [12 x i8] c"Test PASSED\00"
@str.3 = [16 x i8] c"Error at malloc\00"
define i32 @main() {
%1 = load i32, i32* @_SC_PAGE_SIZE
%2 = tail call i32 @sysconf(i32 %1)
%3 = shl nsw i32 %2, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.3, i64 0, i64 0))
%8 = tail call i32 @exit(i32 2)
unreachable
9: ; preds = %0
%10 = sext i32 %2 to i64
%11 = ptrtoint i8* %4 to i64
%12 = urem i64 %11, %10
%13 = sub i64 %10, %12
%14 = getelementptr i8, i8* %4, i64 %13
%15 = tail call i32 @munmap(i8* %14, i32 %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %9
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = tail call i32 @free(i8* %4)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %9
%24 = tail call i32 @free(i8* %4)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
ret i32 0
}