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

/* Variables and functions */
int EINVAL;
void *MAP_FAILED;
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(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int, long);
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 *pa;
  size_t size;
  int fd, saved_errno;
  off_t off;
  page_size = sysconf(_SC_PAGE_SIZE);
  total_size = 3 * page_size;
  size = page_size;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_11_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;
  }
  off = page_size + 1;
  errno = 0;
  pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, off);
  saved_errno = errno;
  close(fd);
  munmap(pa, size);
  if (pa == MAP_FAILED && saved_errno == EINVAL) {
    printf("Got EINVAL when 'off' is not multiple of page size\n");
    printf("Test PASSED\n");
    return 0;
  }
  printf("Test FAILED: Did not get EINVAL"
         " when 'off' is not a multiple of page size, get: %s\n",
         strerror(saved_errno));
  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)
imul   $0x3,-0x118(%rbp),%rax
03
mov    %rax,-0x120(%rbp)
mov    -0x118(%rbp),%rax
mov    %rax,-0x130(%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    0xde2(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    0x2d8a(%rip),%rax
or     (%rax),%esi
mov    0x2d69(%rip),%rax
or     (%rax),%esi
mov    0x2d48(%rip),%rax
mov    (%rax),%edx
mov    0x2d67(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0xffffffff,-0x134(%rbp)
jne    12a6 <main+0xf6>
mov    0x2d2e(%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    13fb <main+0x24b>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x134(%rbp),%edi
mov    -0x120(%rbp),%rsi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12f8 <main+0x148>
mov    0x2cdc(%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    13fb <main+0x24b>
mov    -0x118(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x140(%rbp)
mov    0x2c9f(%rip),%rax
movl   $0x0,(%rax)
mov    -0x130(%rbp),%rsi
mov    0x2ccb(%rip),%rax
mov    (%rax),%edx
mov    0x2c7a(%rip),%rax
or     (%rax),%edx
mov    0x2ca9(%rip),%rax
mov    (%rax),%ecx
mov    -0x134(%rbp),%r8d
mov    -0x140(%rbp),%r9
xor    %eax,%eax
mov    %eax,%edi
call   1050 <mmap@plt>
mov    %rax,-0x128(%rbp)
mov    0x2c52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x138(%rbp)
mov    -0x134(%rbp),%edi
call   1090 <close@plt>
mov    -0x128(%rbp),%rdi
mov    -0x130(%rbp),%rsi
call   10a0 <munmap@plt>
mov    -0x128(%rbp),%rax
mov    0x2bf6(%rip),%rcx
cmp    (%rcx),%rax
jne    13d8 <main+0x228>
mov    -0x138(%rbp),%eax
mov    0x2bf0(%rip),%rcx
cmp    (%rcx),%eax
jne    13d8 <main+0x228>
lea    0xc8e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xcb4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13fb <main+0x24b>
mov    -0x138(%rbp),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xc99(%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   %rbx
sub    $0x108,%rsp
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10d0 <sysconf@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe14(%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    0x2deb(%rip),%rax
mov    0x2dbc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d99(%rip),%rax
or     (%rax),%esi
mov    0x2d78(%rip),%rax
mov    0x2d99(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbp,%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     12ee <main+0x12e>
mov    %eax,%ebp
lea    (%r15,%r15,2),%rbx
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     1305 <main+0x145>
mov    %r15,%r9
add    $0x1,%r9
mov    0x2d3d(%rip),%r14
movl   $0x0,(%r14)
mov    0x2d6f(%rip),%rax
mov    0x2d20(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d4d(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%r8d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    (%r14),%r14d
mov    %ebp,%edi
call   10a0 <close@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10b0 <munmap@plt>
mov    0x2cc7(%rip),%rax
cmp    (%rax),%rbx
jne    132b <main+0x16b>
mov    0x2ccb(%rip),%rax
cmp    (%rax),%r14d
jne    132b <main+0x16b>
lea    0xdc0(%rip),%rdi
call   1040 <puts@plt>
lea    0xde7(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    1349 <main+0x189>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd13(%rip),%rdi
jmp    131a <main+0x15a>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd11(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%eax
jmp    1349 <main+0x189>
mov    %r14d,%edi
call   10e0 <strerror@plt>
lea    0xd0b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_SC_PAGE_SIZE = global i32 0
@.str = [22 x i8] c"/tmp/pts_mmap_11_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_FAILED = global i8* null
@EINVAL = global i32 0
@.str.3 = [52 x i8] c"Got EINVAL when 'off' is not multiple of page size\0A\00"
@.str.4 = [13 x i8] c"Test PASSED\0A\00"
@.str.5 = [84 x i8] c"Test FAILED: Did not get EINVAL when 'off' is not a multiple of page size, get: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
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
%13 = mul nsw i64 3, %12
store i64 %13, i64* %4
%14 = load i64, i64* %3
store i64 %14, i64* %6
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%16 = call i32 (...) @getpid()
%17 = call i32 @snprintf(i8* %15, i32 256, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%19 = call i32 @unlink(i8* %18)
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_RDWR
%23 = or i32 %21, %22
%24 = load i32, i32* @O_EXCL
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IRUSR
%27 = load i32, i32* @S_IWUSR
%28 = or i32 %26, %27
%29 = call i32 @open(i8* %20, i32 %25, i32 %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %0
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i32 2, i32* %1
br label %78
36: ; preds = %0
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%38 = call i32 @unlink(i8* %37)
%39 = load i32, i32* %7
%40 = load i64, i64* %4
%41 = call i32 @ftruncate(i32 %39, i64 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %36
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %45)
store i32 2, i32* %1
br label %78
47: ; preds = %36
%48 = load i64, i64* %3
%49 = add nsw i64 %48, 1
store i64 %49, i64* %9
store i32 0, i32* @errno
%50 = load i64, i64* %6
%51 = load i32, i32* @PROT_READ
%52 = load i32, i32* @PROT_WRITE
%53 = or i32 %51, %52
%54 = load i32, i32* @MAP_SHARED
%55 = load i32, i32* %7
%56 = load i64, i64* %9
%57 = call i8* @mmap(i32* null, i64 %50, i32 %53, i32 %54, i32 %55, i64 %56)
store i8* %57, i8** %5
%58 = load i32, i32* @errno
store i32 %58, i32* %8
%59 = load i32, i32* %7
%60 = call i32 @close(i32 %59)
%61 = load i8*, i8** %5
%62 = load i64, i64* %6
%63 = call i32 @munmap(i8* %61, i64 %62)
%64 = load i8*, i8** %5
%65 = load i8*, i8** @MAP_FAILED
%66 = icmp eq i8* %64, %65
br i1 %66, label %67, label %74
67: ; preds = %47
%68 = load i32, i32* %8
%69 = load i32, i32* @EINVAL
%70 = icmp eq i32 %68, %69
br i1 %70, label %71, label %74
71: ; preds = %67
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0))
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %78
74: ; preds = %67, %47
%75 = load i32, i32* %8
%76 = call i8* @strerror(i32 %75)
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.5, i64 0, i64 0), i8* %76)
store i32 1, i32* %1
br label %78
78: ; preds = %74, %71, %43, %32
%79 = load i32, i32* %1
ret i32 %79
}

LLVM IR (-O2):
@_SC_PAGE_SIZE = global i32 0
@.str = [22 x i8] c"/tmp/pts_mmap_11_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_FAILED = global i8* null
@EINVAL = global i32 0
@.str.5 = [84 x i8] c"Test FAILED: Did not get EINVAL when 'off' is not a multiple of page size, get: %s\0A\00"
@str = [51 x i8] c"Got EINVAL when 'off' is not multiple of page size\00"
@str.6 = [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 i64 @sysconf(i32 %3)
%5 = tail call i32 (...) @getpid()
%6 = call i32 @snprintf(i8* %2, i32 256, i8* getelementptr inbounds ([22 x i8], [22 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 %52
22: ; preds = %0
%23 = mul nsw i64 %4, 3
%24 = call i32 @unlink(i8* %2)
%25 = call i32 @ftruncate(i32 %16, i64 %23)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %52
31: ; preds = %22
%32 = add nsw i64 %4, 1
store i32 0, i32* @errno
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %34, %33
%36 = load i32, i32* @MAP_SHARED
%37 = call i8* @mmap(i32* null, i64 %4, i32 %35, i32 %36, i32 %16, i64 %32)
%38 = load i32, i32* @errno
%39 = call i32 @close(i32 %16)
%40 = call i32 @munmap(i8* %37, i64 %4)
%41 = load i8*, i8** @MAP_FAILED
%42 = icmp eq i8* %37, %41
%43 = load i32, i32* @EINVAL
%44 = icmp eq i32 %38, %43
%45 = select i1 %42, i1 %44, i1 false
br i1 %45, label %46, label %49
46: ; preds = %31
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
br label %52
49: ; preds = %31
%50 = call i8* @strerror(i32 %38)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([84 x i8], [84 x i8]* @.str.5, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %49, %46, %27, %18
%53 = phi i32 [ 2, %18 ], [ 2, %27 ], [ 0, %46 ], [ 1, %49 ]
ret i32 %53
}