#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;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int munmap(void *, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(scalar_t__);
int main(void) {
  int rc;
  void *pa;
  pa = (void *)-1;
  rc = munmap(pa, 1);
  if (rc == -1 && errno == EINVAL) {
    printf("Got EINVAL\n");
    printf("Test PASSED\n");
    exit(0);
  } else {
    printf("Test FAILED: Expect EINVAL but get: %s\n", strerror(errno));
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1040 <munmap@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b2 <main+0x72>
mov    0x2e64(%rip),%rax
mov    (%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
jne    11b2 <main+0x72>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    $0xffffffffffffffff,%rdi
mov    $0x1,%esi
call   1050 <munmap@plt>
mov    0x2e77(%rip),%rcx
mov    (%rcx),%rdi
cmp    $0xffffffff,%eax
jne    117d <main+0x2d>
mov    0x2e60(%rip),%rax
cmp    (%rax),%rdi
je     119a <main+0x4a>
call   1070 <strerror@plt>
lea    0xe77(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
pop    %rcx
ret
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINVAL = global i64 0
@.str = [12 x i8] c"Got EINVAL\0A\00"
@.str.1 = [13 x i8] c"Test PASSED\0A\00"
@.str.2 = [40 x i8] c"Test FAILED: Expect EINVAL but get: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
store i8* inttoptr (i64 -1 to i8*), i8** %3
%4 = load i8*, i8** %3
%5 = call i32 @munmap(i8* %4, i32 1)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %16
8: ; preds = %0
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINVAL
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %8, %0
%17 = load i64, i64* @errno
%18 = call i8* @strerror(i64 %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %18)
ret i32 1
}

LLVM IR (-O2):
@errno = global i64 0
@EINVAL = global i64 0
@.str.2 = [40 x i8] c"Test FAILED: Expect EINVAL but get: %s\0A\00"
@str = [11 x i8] c"Got EINVAL\00"
@str.3 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = tail call i32 @munmap(i8* inttoptr (i64 -1 to i8*), i32 1)
%2 = icmp eq i32 %1, -1
%3 = load i64, i64* @errno
%4 = load i64, i64* @EINVAL
%5 = icmp eq i64 %3, %4
%6 = select i1 %2, i1 %5, i1 false
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.3, i64 0, i64 0))
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %0
%12 = tail call i8* @strerror(i64 %3)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %12)
ret i32 1
}