#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 */
typedef int /*<<< orphan*/ tmpfname;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
scalar_t__ EINVAL;
void *MAP_FAILED;
int MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ _SC_PAGE_SIZE;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int ftruncate(int, long);
int getpid();
void *mmap(void *, size_t, int, int, int, int /*<<< orphan*/);
int munmap(void *, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(scalar_t__);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  char tmpfname[256];
  long file_size;
  void *pa = NULL;
  void *addr = NULL;
  size_t len;
  int flag;
  int fd;
  off_t off = 0;
  int prot;
  int page_size;
  page_size = sysconf(_SC_PAGE_SIZE);
  file_size = 2 * page_size;
  len = page_size + 1;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_munmap_1_1_%d", getpid());
  unlink(tmpfname);
  fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("munmap/9-1.c"
           " Error at open(): %s\n",
           strerror(errno));
    exit(2);
  }
  unlink(tmpfname);
  if (ftruncate(fd, file_size) == -1) {
    printf("Error at ftruncate: %s\n", strerror(errno));
    exit(2);
  }
  flag = MAP_SHARED;
  prot = PROT_READ | PROT_WRITE;
  pa = mmap(addr, len, prot, flag, fd, off);
  if (pa == MAP_FAILED) {
    printf("Test Unresolved: "
           "munmap/9-1.c"
           " Error at mmap: %s\n",
           strerror(errno));
    exit(2);
  }
  close(fd);
  if (munmap(pa, 0) == -1 && errno == EINVAL) {
    printf("Get EINVAL when len=0\n");
    printf("Test PASSED\n");
    exit(0);
  } else {
    printf("Test Fail: Expect EINVAL while get %s\n", strerror(errno));
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x120(%rbp)
movq   $0x0,-0x128(%rbp)
movl   $0x0,-0x13c(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   10c0 <sysconf@plt>
mov    %eax,-0x144(%rbp)
mov    -0x144(%rbp),%eax
shl    $0x1,%eax
cltq
mov    %rax,-0x118(%rbp)
mov    -0x144(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x130(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x150(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x150(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xdac(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
mov    0x2d54(%rip),%rax
or     (%rax),%esi
mov    0x2d33(%rip),%rax
or     (%rax),%esi
mov    0x2d12(%rip),%rax
mov    (%rax),%edx
mov    0x2d31(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x138(%rbp)
cmpl   $0xffffffff,-0x138(%rbp)
jne    12db <main+0x11b>
mov    0x2cf8(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x138(%rbp),%edi
mov    -0x118(%rbp),%rsi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    132c <main+0x16c>
mov    0x2ca7(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
mov    0x2cad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x134(%rbp)
mov    0x2cae(%rip),%rax
mov    (%rax),%eax
mov    0x2c5d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x140(%rbp)
mov    -0x128(%rbp),%rdi
mov    -0x130(%rbp),%rsi
mov    -0x140(%rbp),%edx
mov    -0x134(%rbp),%ecx
mov    -0x138(%rbp),%r8d
mov    -0x13c(%rbp),%r9d
call   1050 <mmap@plt>
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rax
mov    0x2bf3(%rip),%rcx
cmp    (%rcx),%rax
jne    13c8 <main+0x208>
mov    0x2c0b(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xc9a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
mov    -0x138(%rbp),%edi
call   1090 <close@plt>
mov    -0x120(%rbp),%rdi
xor    %esi,%esi
call   10a0 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1427 <main+0x267>
mov    0x2bbf(%rip),%rax
mov    (%rax),%rax
mov    0x2b9d(%rip),%rcx
cmp    (%rcx),%rax
jne    1427 <main+0x267>
lea    0xc77(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xc80(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    0x2b82(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xc66(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   10d0 <sysconf@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe06(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    0x2ddd(%rip),%rax
mov    0x2dae(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d8b(%rip),%rax
or     (%rax),%esi
mov    0x2d6a(%rip),%rax
mov    0x2d8b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbp,%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     130f <main+0x13f>
mov    %eax,%ebp
lea    (%r14,%r14,1),%eax
movslq %eax,%rbx
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     1327 <main+0x157>
add    $0x1,%r14d
movslq %r14d,%rsi
mov    0x2d5c(%rip),%rax
mov    (%rax),%ecx
mov    0x2d63(%rip),%rax
mov    0x2d14(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    0x2cd9(%rip),%rax
cmp    (%rax),%rbx
je     133f <main+0x16f>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <munmap@plt>
mov    0x2ce0(%rip),%rcx
mov    (%rcx),%rdi
cmp    $0xffffffff,%eax
jne    12e8 <main+0x118>
mov    0x2cb9(%rip),%rax
cmp    (%rax),%rdi
je     1369 <main+0x199>
call   10f0 <strerror@plt>
lea    0xd8e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2c9a(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xcf2(%rip),%rdi
jmp    1355 <main+0x185>
mov    0x2c82(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xcfc(%rip),%rdi
jmp    1355 <main+0x185>
mov    0x2c6a(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xcfc(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
lea    0xd39(%rip),%rdi
call   1040 <puts@plt>
lea    0xd43(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@_SC_PAGE_SIZE = global i32 0
@.str = [23 x i8] c"/tmp/pts_munmap_1_1_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [34 x i8] c"munmap/9-1.c Error at open(): %s\0A\00"
@errno = global i64 0
@.str.2 = [24 x i8] c"Error at ftruncate: %s\0A\00"
@MAP_SHARED = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [49 x i8] c"Test Unresolved: munmap/9-1.c Error at mmap: %s\0A\00"
@EINVAL = global i64 0
@.str.4 = [23 x i8] c"Get EINVAL when len=0\0A\00"
@.str.5 = [13 x i8] c"Test PASSED\0A\00"
@.str.6 = [39 x i8] c"Test Fail: Expect EINVAL while get %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %1
store i8* null, i8** %4
store i8* null, i8** %5
store i32 0, i32* %9
%12 = load i32, i32* @_SC_PAGE_SIZE
%13 = call i32 @sysconf(i32 %12)
store i32 %13, i32* %11
%14 = load i32, i32* %11
%15 = mul nsw i32 2, %14
%16 = sext i32 %15 to i64
store i64 %16, i64* %3
%17 = load i32, i32* %11
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
store i64 %19, i64* %6
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%21 = call i32 (...) @getpid()
%22 = call i32 @snprintf(i8* %20, i32 256, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%24 = call i32 @unlink(i8* %23)
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%26 = load i32, i32* @O_CREAT
%27 = load i32, i32* @O_RDWR
%28 = or i32 %26, %27
%29 = load i32, i32* @O_EXCL
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IRUSR
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %31, %32
%34 = call i32 @open(i8* %25, i32 %30, i32 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %0
%38 = load i64, i64* @errno
%39 = call i8* @strerror(i64 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = call i32 @exit(i32 2)
unreachable
42: ; preds = %0
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%44 = call i32 @unlink(i8* %43)
%45 = load i32, i32* %8
%46 = load i64, i64* %3
%47 = call i32 @ftruncate(i32 %45, i64 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %54
49: ; preds = %42
%50 = load i64, i64* @errno
%51 = call i8* @strerror(i64 %50)
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %51)
%53 = call i32 @exit(i32 2)
unreachable
54: ; preds = %42
%55 = load i32, i32* @MAP_SHARED
store i32 %55, i32* %7
%56 = load i32, i32* @PROT_READ
%57 = load i32, i32* @PROT_WRITE
%58 = or i32 %56, %57
store i32 %58, i32* %10
%59 = load i8*, i8** %5
%60 = load i64, i64* %6
%61 = load i32, i32* %10
%62 = load i32, i32* %7
%63 = load i32, i32* %8
%64 = load i32, i32* %9
%65 = call i8* @mmap(i8* %59, i64 %60, i32 %61, i32 %62, i32 %63, i32 %64)
store i8* %65, i8** %4
%66 = load i8*, i8** %4
%67 = load i8*, i8** @MAP_FAILED
%68 = icmp eq i8* %66, %67
br i1 %68, label %69, label %74
69: ; preds = %54
%70 = load i64, i64* @errno
%71 = call i8* @strerror(i64 %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %71)
%73 = call i32 @exit(i32 2)
unreachable
74: ; preds = %54
%75 = load i32, i32* %8
%76 = call i32 @close(i32 %75)
%77 = load i8*, i8** %4
%78 = call i32 @munmap(i8* %77, i32 0)
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %88
80: ; preds = %74
%81 = load i64, i64* @errno
%82 = load i64, i64* @EINVAL
%83 = icmp eq i64 %81, %82
br i1 %83, label %84, label %88
84: ; preds = %80
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%87 = call i32 @exit(i32 0)
unreachable
88: ; preds = %80, %74
%89 = load i64, i64* @errno
%90 = call i8* @strerror(i64 %89)
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i8* %90)
ret i32 1
}

LLVM IR (-O2):
@_SC_PAGE_SIZE = global i32 0
@.str = [23 x i8] c"/tmp/pts_munmap_1_1_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [34 x i8] c"munmap/9-1.c Error at open(): %s\0A\00"
@errno = global i64 0
@.str.2 = [24 x i8] c"Error at ftruncate: %s\0A\00"
@MAP_SHARED = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [49 x i8] c"Test Unresolved: munmap/9-1.c Error at mmap: %s\0A\00"
@EINVAL = global i64 0
@.str.6 = [39 x i8] c"Test Fail: Expect EINVAL while get %s\0A\00"
@str = [22 x i8] c"Get EINVAL when len=0\00"
@str.7 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @_SC_PAGE_SIZE
%4 = tail call i32 @sysconf(i32 %3)
%5 = add nsw i32 %4, 1
%6 = sext i32 %5 to i64
%7 = tail call i32 (...) @getpid()
%8 = call i32 @snprintf(i8* %2, i32 256, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 @unlink(i8* %2)
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_RDWR
%12 = or i32 %11, %10
%13 = load i32, i32* @O_EXCL
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %16, %15
%18 = call i32 @open(i8* %2, i32 %14, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %0
%21 = load i64, i64* @errno
%22 = call i8* @strerror(i64 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 2)
unreachable
25: ; preds = %0
%26 = shl nsw i32 %4, 1
%27 = sext i32 %26 to i64
%28 = call i32 @unlink(i8* %2)
%29 = call i32 @ftruncate(i32 %18, i64 %27)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %36
31: ; preds = %25
%32 = load i64, i64* @errno
%33 = call i8* @strerror(i64 %32)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = call i32 @exit(i32 2)
unreachable
36: ; preds = %25
%37 = load i32, i32* @MAP_SHARED
%38 = load i32, i32* @PROT_READ
%39 = load i32, i32* @PROT_WRITE
%40 = or i32 %39, %38
%41 = call i8* @mmap(i8* null, i64 %6, i32 %40, i32 %37, i32 %18, i32 0)
%42 = load i8*, i8** @MAP_FAILED
%43 = icmp eq i8* %41, %42
br i1 %43, label %44, label %49
44: ; preds = %36
%45 = load i64, i64* @errno
%46 = call i8* @strerror(i64 %45)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = call i32 @exit(i32 2)
unreachable
49: ; preds = %36
%50 = call i32 @close(i32 %18)
%51 = call i32 @munmap(i8* %41, i32 0)
%52 = icmp eq i32 %51, -1
%53 = load i64, i64* @errno
%54 = load i64, i64* @EINVAL
%55 = icmp eq i64 %53, %54
%56 = select i1 %52, i1 %55, i1 false
br i1 %56, label %57, label %61
57: ; preds = %49
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
%60 = call i32 @exit(i32 0)
unreachable
61: ; preds = %49
%62 = call i8* @strerror(i64 %53)
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i8* %62)
ret i32 1
}