#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;

/* Variables and functions */
int EINVAL;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_FIXED;
int /*<<< orphan*/ 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);
int errno;
int ftruncate(int, long);
int getpid();
void *mmap(void *, size_t, int, int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(int);
long sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  char tmpfname[256];
  long page_size;
  long total_size;
  void *illegal_addr;
  void *pa;
  size_t size;
  int fd, saved_errno;
  page_size = sysconf(_SC_PAGE_SIZE);
  total_size = page_size;
  size = total_size;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_9_1_%d", getpid());
  unlink(tmpfname);
  fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("Error at open(): %s\n", strerror(errno));
    return 2;
  }
  unlink(tmpfname);
  if (ftruncate(fd, total_size) == -1) {
    printf("Error at ftruncate(): %s\n", strerror(errno));
    return 2;
  }
  pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if ((unsigned long)pa % page_size) {
    printf("pa is not multiple of page_size\n");
    illegal_addr = pa;
  } else {
    printf("pa is a multiple of page_size\n");
    illegal_addr = pa + 1;
  }
  munmap(pa, size);
  pa = mmap(illegal_addr, size, PROT_READ | PROT_WRITE, MAP_FIXED, fd, 0);
  saved_errno = errno;
  close(fd);
  munmap(pa, size);
  if (pa == MAP_FAILED && saved_errno == EINVAL) {
    printf("Test PASSED\n");
    return 0;
  }
  printf("Test FAILED, mmap with MAP_FIXED did not get EINVAL"
         " when 'addr' is illegal\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   10c0 <sysconf@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x148(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x148(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xde3(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    0x2d8b(%rip),%rax
or     (%rax),%esi
mov    0x2d6a(%rip),%rax
or     (%rax),%esi
mov    0x2d49(%rip),%rax
mov    (%rax),%edx
mov    0x2d68(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x13c(%rbp)
cmpl   $0xffffffff,-0x13c(%rbp)
jne    12a5 <main+0xf5>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    146a <main+0x2ba>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x13c(%rbp),%edi
mov    -0x120(%rbp),%rsi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12f7 <main+0x147>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    146a <main+0x2ba>
mov    -0x138(%rbp),%rsi
mov    0x2ceb(%rip),%rax
mov    (%rax),%edx
mov    0x2c9a(%rip),%rax
or     (%rax),%edx
mov    0x2cc9(%rip),%rax
mov    (%rax),%ecx
mov    -0x13c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x130(%rbp)
mov    -0x130(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x118(%rbp)
cmp    $0x0,%rdx
je     1370 <main+0x1c0>
lea    0xcee(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x130(%rbp),%rax
mov    %rax,-0x128(%rbp)
jmp    1390 <main+0x1e0>
lea    0xcee(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x130(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
mov    -0x130(%rbp),%rdi
mov    -0x138(%rbp),%rsi
call   10a0 <munmap@plt>
mov    -0x128(%rbp),%rdi
mov    -0x138(%rbp),%rsi
mov    0x2c38(%rip),%rax
mov    (%rax),%edx
mov    0x2be7(%rip),%rax
or     (%rax),%edx
mov    0x2bbe(%rip),%rax
mov    (%rax),%ecx
mov    -0x13c(%rbp),%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x130(%rbp)
mov    0x2bc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x140(%rbp)
mov    -0x13c(%rbp),%edi
call   1090 <close@plt>
mov    -0x130(%rbp),%rdi
mov    -0x138(%rbp),%rsi
call   10a0 <munmap@plt>
mov    -0x130(%rbp),%rax
mov    0x2b63(%rip),%rcx
cmp    (%rcx),%rax
jne    1455 <main+0x2a5>
mov    -0x140(%rbp),%eax
mov    0x2b65(%rip),%rcx
cmp    (%rcx),%eax
jne    1455 <main+0x2a5>
lea    0xc42(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    146a <main+0x2ba>
lea    0xc35(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   10d0 <sysconf@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe10(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2de7(%rip),%rax
mov    0x2db8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d95(%rip),%rax
or     (%rax),%esi
mov    0x2d74(%rip),%rax
mov    0x2d95(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     12b9 <main+0xf9>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1090 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     12d0 <main+0x110>
mov    0x2d8c(%rip),%r12
mov    0x2d3d(%rip),%r13
mov    0x0(%r13),%edx
or     (%r12),%edx
mov    0x2d66(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    %r14,%rax
or     %rbx,%rax
shr    $0x20,%rax
je     12f9 <main+0x139>
mov    %rbx,%rax
xor    %edx,%edx
div    %r14
test   %rdx,%rdx
je     1305 <main+0x145>
lea    0xe0a(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%r15
jmp    1318 <main+0x158>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd47(%rip),%rdi
jmp    12e5 <main+0x125>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd45(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%eax
jmp    139d <main+0x1dd>
mov    %ebx,%eax
xor    %edx,%edx
div    %r14d
test   %rdx,%rdx
jne    12a8 <main+0xe8>
lea    0xd38(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%r15
add    $0x1,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
call   10b0 <munmap@plt>
mov    0x0(%r13),%edx
or     (%r12),%edx
mov    0x2c56(%rip),%rax
mov    (%rax),%ecx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    0x2c61(%rip),%rax
mov    (%rax),%r15d
mov    %ebp,%edi
call   10a0 <close@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10b0 <munmap@plt>
mov    0x2c15(%rip),%rax
cmp    (%rax),%rbx
jne    138c <main+0x1cc>
mov    0x2c21(%rip),%rax
cmp    (%rax),%r15d
jne    138c <main+0x1cc>
lea    0xd2a(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    139d <main+0x1dd>
lea    0xccf(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@_SC_PAGE_SIZE = global i32 0
@.str = [21 x i8] c"/tmp/pts_mmap_9_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 = [21 x i8] c"Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Error at ftruncate(): %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FIXED = global i32 0
@MAP_FAILED = global i8* null
@EINVAL = global i32 0
@str = [30 x i8] c"pa is a multiple of page_size\00"
@str.7 = [75 x i8] c"Test FAILED, mmap with MAP_FIXED did not get EINVAL when 'addr' is illegal\00"
@str.8 = [12 x i8] c"Test PASSED\00"
@str.9 = [32 x i8] c"pa is not multiple of page_size\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 i64 @sysconf(i32 %3)
%5 = tail call i32 (...) @getpid()
%6 = call i32 @snprintf(i8* %2, i32 256, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i32 @unlink(i8* %2)
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %9, %8
%11 = load i32, i32* @O_EXCL
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %14, %13
%16 = call i32 @open(i8* %2, i32 %12, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %0
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %64
22: ; preds = %0
%23 = call i32 @unlink(i8* %2)
%24 = call i32 @ftruncate(i32 %16, i64 %4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %64
30: ; preds = %22
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @PROT_WRITE
%33 = or i32 %32, %31
%34 = load i32, i32* @MAP_SHARED
%35 = call i8* @mmap(i8* null, i64 %4, i32 %33, i32 %34, i32 %16, i32 0)
%36 = ptrtoint i8* %35 to i64
%37 = urem i64 %36, %4
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %30
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.9, i64 0, i64 0))
br label %44
41: ; preds = %30
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%43 = getelementptr i8, i8* %35, i64 1
br label %44
44: ; preds = %41, %39
%45 = phi i8* [ %35, %39 ], [ %43, %41 ]
%46 = call i32 @munmap(i8* %35, i64 %4)
%47 = load i32, i32* @PROT_READ
%48 = load i32, i32* @PROT_WRITE
%49 = or i32 %48, %47
%50 = load i32, i32* @MAP_FIXED
%51 = call i8* @mmap(i8* %45, i64 %4, i32 %49, i32 %50, i32 %16, i32 0)
%52 = load i32, i32* @errno
%53 = call i32 @close(i32 %16)
%54 = call i32 @munmap(i8* %51, i64 %4)
%55 = load i8*, i8** @MAP_FAILED
%56 = icmp eq i8* %51, %55
%57 = load i32, i32* @EINVAL
%58 = icmp eq i32 %52, %57
%59 = select i1 %56, i1 %58, i1 false
br i1 %59, label %60, label %62
60: ; preds = %44
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.8, i64 0, i64 0))
br label %64
62: ; preds = %44
%63 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @str.7, i64 0, i64 0))
br label %64
64: ; preds = %62, %60, %26, %18
%65 = phi i32 [ 2, %18 ], [ 2, %26 ], [ 0, %60 ], [ 1, %62 ]
ret i32 %65
}