#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;
struct rlimit {
  size_t rlim_cur;
};

/* Variables and functions */
scalar_t__ ENOMEM;
void *MAP_FAILED;
int MAP_FIXED;
int MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ RLIMIT_AS;
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, size_t);
int getpid();
int getrlimit(int /*<<< orphan*/, struct rlimit *);
void *mmap(void *, size_t, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int shm_open(char *, int, int);
int /*<<< orphan*/ shm_unlink(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(scalar_t__);
unsigned long sysconf(int /*<<< orphan*/);
int main(void) {
  char tmpfname[256];
  void *pa;
  void *addr;
  size_t len;
  int fd;
  size_t shm_size;
  struct rlimit rlim;
  unsigned long page_size = sysconf(_SC_PAGE_SIZE);
  shm_size = 2 * page_size;
  snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_24_2_%d", getpid());
  shm_unlink(tmpfname);
  fd = shm_open(tmpfname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("Error at shm_open(): %s\n", strerror(errno));
    return 2;
  }
  shm_unlink(tmpfname);
  if (ftruncate(fd, shm_size) == -1) {
    printf("Error at ftruncate(): %s\n", strerror(errno));
    return 2;
  }
  if (getrlimit(RLIMIT_AS, &rlim) == -1) {
    printf("Error at getrlimit(): %s\n", strerror(errno));
    return 2;
  }
  printf("available memory: %lu\n", rlim.rlim_cur);
  len = shm_size;
  pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (pa == MAP_FAILED) {
    printf("Error at first mmap(): %s\n", strerror(errno));
    exit(1);
  }
  len = rlim.rlim_cur;
  addr = pa;
  printf("addr: %lx, len: %lx\n", (unsigned long)addr, (unsigned long)len);
  if ((unsigned long)addr % page_size) {
    addr += page_size;
    addr = (void *)((unsigned long)addr & ~(page_size - 1));
  }
  if (len % page_size) {
    len &= ~(page_size - 1);
  }
  printf("addr: %lx, len: %lx\n", (unsigned long)addr, (unsigned long)len);
  pa = mmap(addr, len, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, fd, 0);
  if (pa == MAP_FAILED && errno == ENOMEM) {
    printf("Got ENOMEM: %s\nTest PASSED\n", strerror(errno));
    exit(0);
  }
  if (pa == MAP_FAILED)
    perror("Error at mmap()");
  else
    munmap(pa, len);
  close(fd);
  printf("Test Fail: Did not get ENOMEM as expected\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %rax,-0x148(%rbp)
mov    -0x148(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x138(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x150(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x150(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xdbd(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1100 <shm_unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
mov    0x2d8d(%rip),%rax
or     (%rax),%esi
mov    0x2d3c(%rip),%rax
or     (%rax),%esi
mov    0x2d13(%rip),%rax
mov    (%rax),%edx
mov    0x2d42(%rip),%rax
or     (%rax),%edx
call   10d0 <shm_open@plt>
mov    %eax,-0x12c(%rbp)
cmpl   $0xffffffff,-0x12c(%rbp)
jne    12cc <main+0xec>
mov    0x2d01(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    15e2 <main+0x402>
lea    -0x110(%rbp),%rdi
call   1100 <shm_unlink@plt>
mov    -0x12c(%rbp),%edi
mov    -0x138(%rbp),%rsi
call   1070 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    131f <main+0x13f>
mov    0x2cae(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    15e2 <main+0x402>
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
lea    -0x140(%rbp),%rsi
call   10e0 <getrlimit@plt>
cmp    $0xffffffff,%eax
jne    1369 <main+0x189>
mov    0x2c64(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xcee(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    15e2 <main+0x402>
mov    -0x140(%rbp),%rsi
lea    0xce7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x138(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rsi
mov    0x2c56(%rip),%rax
mov    (%rax),%edx
mov    0x2bf5(%rip),%rax
or     (%rax),%edx
mov    0x2c34(%rip),%rax
mov    (%rax),%ecx
mov    -0x12c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
mov    0x2ba2(%rip),%rcx
cmp    (%rcx),%rax
jne    1409 <main+0x229>
mov    0x2bc2(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xc7d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x140(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x118(%rbp),%rax
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rsi
mov    -0x128(%rbp),%rdx
lea    0xc56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x120(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x148(%rbp)
cmp    $0x0,%rdx
je     1492 <main+0x2b2>
mov    -0x148(%rbp),%rax
add    -0x120(%rbp),%rax
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rax
mov    -0x148(%rbp),%rcx
sub    $0x1,%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x120(%rbp)
mov    -0x128(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x148(%rbp)
cmp    $0x0,%rdx
je     14cb <main+0x2eb>
mov    -0x148(%rbp),%rax
sub    $0x1,%rax
xor    $0xffffffffffffffff,%rax
and    -0x128(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x120(%rbp),%rsi
mov    -0x128(%rbp),%rdx
lea    0xbb0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x120(%rbp),%rdi
mov    -0x128(%rbp),%rsi
mov    0x2af4(%rip),%rax
mov    (%rax),%edx
mov    0x2a93(%rip),%rax
or     (%rax),%edx
mov    0x2a72(%rip),%rax
mov    (%rax),%ecx
mov    0x2ac9(%rip),%rax
or     (%rax),%ecx
mov    -0x12c(%rbp),%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
mov    0x2a3b(%rip),%rcx
cmp    (%rcx),%rax
jne    1587 <main+0x3a7>
mov    0x2a5b(%rip),%rax
mov    (%rax),%rax
mov    0x2a61(%rip),%rcx
cmp    (%rcx),%rax
jne    1587 <main+0x3a7>
mov    0x2a41(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xb2c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x118(%rbp),%rax
mov    0x29e3(%rip),%rcx
cmp    (%rcx),%rax
jne    15af <main+0x3cf>
lea    0xb1c(%rip),%rdi
call   10a0 <perror@plt>
jmp    15c2 <main+0x3e2>
mov    -0x118(%rbp),%rdi
mov    -0x128(%rbp),%rsi
call   1090 <munmap@plt>
mov    -0x12c(%rbp),%edi
call   1080 <close@plt>
lea    0xafd(%rip),%rdi
mov    $0x0,%al
call   1050 <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    $0x118,%rsp
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   10c0 <sysconf@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xde0(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1110 <shm_unlink@plt>
mov    0x2d8d(%rip),%rax
mov    0x2dae(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d5b(%rip),%rax
or     (%rax),%esi
mov    0x2d32(%rip),%rax
mov    0x2d63(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10e0 <shm_open@plt>
cmp    $0xffffffff,%eax
je     133e <main+0x14e>
mov    %eax,%r15d
lea    (%r12,%r12,1),%rbx
lea    0x10(%rsp),%rdi
call   1110 <shm_unlink@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     1356 <main+0x166>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   10f0 <getrlimit@plt>
cmp    $0xffffffff,%eax
je     136e <main+0x17e>
mov    0x8(%rsp),%rsi
lea    0xd97(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d1b(%rip),%rax
mov    0x2cbc(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2cf9(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2c75(%rip),%rax
cmp    (%rax),%rbx
je     148a <main+0x29a>
mov    %r12,%r13
mov    0x8(%rsp),%r14
lea    0xd75(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rax
or     %r12,%rax
shr    $0x20,%rax
je     1398 <main+0x1a8>
mov    %rbx,%rax
xor    %edx,%edx
div    %r13
jmp    139f <main+0x1af>
mov    0x2c63(%rip),%rax
mov    (%rax),%rdi
call   1100 <strerror@plt>
lea    0xcbd(%rip),%rdi
jmp    1384 <main+0x194>
mov    0x2c4b(%rip),%rax
mov    (%rax),%rdi
call   1100 <strerror@plt>
lea    0xcbe(%rip),%rdi
jmp    1384 <main+0x194>
mov    0x2c33(%rip),%rax
mov    (%rax),%rdi
call   1100 <strerror@plt>
lea    0xcc0(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x2,%eax
jmp    1454 <main+0x264>
mov    %ebx,%eax
xor    %edx,%edx
div    %r13d
lea    (%rbx,%r12,1),%rbp
mov    %r14,%rax
or     %r12,%rax
neg    %r12
and    %r12,%rbp
test   %rdx,%rdx
cmove  %rbx,%rbp
shr    $0x20,%rax
je     13c6 <main+0x1d6>
mov    %r14,%rax
xor    %edx,%edx
div    %r13
jmp    13ce <main+0x1de>
mov    %r14d,%eax
xor    %edx,%edx
div    %r13d
cmp    $0x1,%rdx
sbb    %rbx,%rbx
or     %r12,%rbx
and    %r14,%rbx
lea    0xcae(%rip),%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2ba2(%rip),%rax
mov    (%rax),%edx
mov    0x2bf1(%rip),%rax
or     (%rax),%edx
mov    0x2b78(%rip),%rax
mov    0x2bd1(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    0x2b4d(%rip),%rcx
cmp    (%rcx),%rax
je     1466 <main+0x276>
mov    %rax,%rdi
mov    %rbx,%rsi
call   10a0 <munmap@plt>
mov    %r15d,%edi
call   1090 <close@plt>
lea    0xc87(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2b3b(%rip),%rax
mov    (%rax),%rdi
mov    0x2b41(%rip),%rax
cmp    (%rax),%rdi
je     14b4 <main+0x2c4>
lea    0xc3e(%rip),%rdi
call   10b0 <perror@plt>
jmp    143b <main+0x24b>
mov    0x2b17(%rip),%rax
mov    (%rax),%rdi
call   1100 <strerror@plt>
lea    0xbd5(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
call   1100 <strerror@plt>
lea    0xbe5(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i64 }
@_SC_PAGE_SIZE = global i32 0
@.str = [17 x i8] c"pts_mmap_24_2_%d\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [25 x i8] c"Error at shm_open(): %s\0A\00"
@errno = global i64 0
@.str.2 = [26 x i8] c"Error at ftruncate(): %s\0A\00"
@RLIMIT_AS = global i32 0
@.str.3 = [26 x i8] c"Error at getrlimit(): %s\0A\00"
@.str.4 = [23 x i8] c"available memory: %lu\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [27 x i8] c"Error at first mmap(): %s\0A\00"
@.str.6 = [21 x i8] c"addr: %lx, len: %lx\0A\00"
@MAP_FIXED = global i32 0
@ENOMEM = global i64 0
@.str.7 = [28 x i8] c"Got ENOMEM: %s\0ATest PASSED\0A\00"
@.str.8 = [16 x i8] c"Error at mmap()\00"
@.str.9 = [43 x i8] c"Test Fail: Did not get ENOMEM as expected\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.rlimit
%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* %9
%12 = load i64, i64* %9
%13 = mul i64 2, %12
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 ([17 x i8], [17 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 @shm_unlink(i8* %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_CREAT
%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 @shm_open(i8* %19, i32 %24, i32 %27)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %0
%32 = load i64, i64* @errno
%33 = call i8* @strerror(i64 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %33)
store i32 2, i32* %1
br label %150
35: ; preds = %0
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%37 = call i32 @shm_unlink(i8* %36)
%38 = load i32, i32* %6
%39 = load i64, i64* %7
%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 i64, i64* @errno
%44 = call i8* @strerror(i64 %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 %150
46: ; preds = %35
%47 = load i32, i32* @RLIMIT_AS
%48 = call i32 @getrlimit(i32 %47, %struct.rlimit* %8)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i64, i64* @errno
%52 = call i8* @strerror(i64 %51)
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %52)
store i32 2, i32* %1
br label %150
54: ; preds = %46
%55 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
%56 = load i64, i64* %55
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i64 %56)
%58 = load i64, i64* %7
store i64 %58, i64* %5
%59 = load i64, i64* %5
%60 = load i32, i32* @PROT_READ
%61 = load i32, i32* @PROT_WRITE
%62 = or i32 %60, %61
%63 = load i32, i32* @MAP_SHARED
%64 = load i32, i32* %6
%65 = call i8* @mmap(i8* null, i64 %59, i32 %62, i32 %63, i32 %64, i32 0)
store i8* %65, i8** %3
%66 = load i8*, i8** %3
%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 ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %71)
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %54
%75 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
%76 = load i64, i64* %75
store i64 %76, i64* %5
%77 = load i8*, i8** %3
store i8* %77, i8** %4
%78 = load i8*, i8** %4
%79 = ptrtoint i8* %78 to i64
%80 = load i64, i64* %5
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %79, i64 %80)
%82 = load i8*, i8** %4
%83 = ptrtoint i8* %82 to i64
%84 = load i64, i64* %9
%85 = urem i64 %83, %84
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %98
87: ; preds = %74
%88 = load i64, i64* %9
%89 = load i8*, i8** %4
%90 = getelementptr i8, i8* %89, i64 %88
store i8* %90, i8** %4
%91 = load i8*, i8** %4
%92 = ptrtoint i8* %91 to i64
%93 = load i64, i64* %9
%94 = sub i64 %93, 1
%95 = xor i64 %94, -1
%96 = and i64 %92, %95
%97 = inttoptr i64 %96 to i8*
store i8* %97, i8** %4
br label %98
98: ; preds = %87, %74
%99 = load i64, i64* %5
%100 = load i64, i64* %9
%101 = urem i64 %99, %100
%102 = icmp ne i64 %101, 0
br i1 %102, label %103, label %109
103: ; preds = %98
%104 = load i64, i64* %9
%105 = sub i64 %104, 1
%106 = xor i64 %105, -1
%107 = load i64, i64* %5
%108 = and i64 %107, %106
store i64 %108, i64* %5
br label %109
109: ; preds = %103, %98
%110 = load i8*, i8** %4
%111 = ptrtoint i8* %110 to i64
%112 = load i64, i64* %5
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %111, i64 %112)
%114 = load i8*, i8** %4
%115 = load i64, i64* %5
%116 = load i32, i32* @PROT_READ
%117 = load i32, i32* @PROT_WRITE
%118 = or i32 %116, %117
%119 = load i32, i32* @MAP_FIXED
%120 = load i32, i32* @MAP_SHARED
%121 = or i32 %119, %120
%122 = load i32, i32* %6
%123 = call i8* @mmap(i8* %114, i64 %115, i32 %118, i32 %121, i32 %122, i32 0)
store i8* %123, i8** %3
%124 = load i8*, i8** %3
%125 = load i8*, i8** @MAP_FAILED
%126 = icmp eq i8* %124, %125
br i1 %126, label %127, label %136
127: ; preds = %109
%128 = load i64, i64* @errno
%129 = load i64, i64* @ENOMEM
%130 = icmp eq i64 %128, %129
br i1 %130, label %131, label %136
131: ; preds = %127
%132 = load i64, i64* @errno
%133 = call i8* @strerror(i64 %132)
%134 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i8* %133)
%135 = call i32 @exit(i32 0)
unreachable
136: ; preds = %127, %109
%137 = load i8*, i8** %3
%138 = load i8*, i8** @MAP_FAILED
%139 = icmp eq i8* %137, %138
br i1 %139, label %140, label %142
140: ; preds = %136
%141 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
br label %146
142: ; preds = %136
%143 = load i8*, i8** %3
%144 = load i64, i64* %5
%145 = call i32 @munmap(i8* %143, i64 %144)
br label %146
146: ; preds = %142, %140
%147 = load i32, i32* %6
%148 = call i32 @close(i32 %147)
%149 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %1
br label %150
150: ; preds = %146, %50, %42, %31
%151 = load i32, i32* %1
ret i32 %151
}

LLVM IR (-O2):
%struct.rlimit = type { i64 }
@_SC_PAGE_SIZE = global i32 0
@.str = [17 x i8] c"pts_mmap_24_2_%d\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [25 x i8] c"Error at shm_open(): %s\0A\00"
@errno = global i64 0
@.str.2 = [26 x i8] c"Error at ftruncate(): %s\0A\00"
@RLIMIT_AS = global i32 0
@.str.3 = [26 x i8] c"Error at getrlimit(): %s\0A\00"
@.str.4 = [23 x i8] c"available memory: %lu\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [27 x i8] c"Error at first mmap(): %s\0A\00"
@.str.6 = [21 x i8] c"addr: %lx, len: %lx\0A\00"
@MAP_FIXED = global i32 0
@ENOMEM = global i64 0
@.str.7 = [28 x i8] c"Got ENOMEM: %s\0ATest PASSED\0A\00"
@.str.8 = [16 x i8] c"Error at mmap()\00"
@str = [42 x i8] c"Test Fail: Did not get ENOMEM as expected\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca %struct.rlimit
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.rlimit* %2 to i8*
%5 = load i32, i32* @_SC_PAGE_SIZE
%6 = tail call i64 @sysconf(i32 %5)
%7 = shl i64 %6, 1
%8 = tail call i32 (...) @getpid()
%9 = call i32 @snprintf(i8* %3, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 @shm_unlink(i8* %3)
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = load i32, i32* @O_EXCL
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %17, %16
%19 = call i32 @shm_open(i8* %3, i32 %15, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %0
%22 = load i64, i64* @errno
%23 = call i8* @strerror(i64 %22)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %99
25: ; preds = %0
%26 = call i32 @shm_unlink(i8* %3)
%27 = call i32 @ftruncate(i32 %19, i64 %7)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = call i8* @strerror(i64 %30)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %99
33: ; preds = %25
%34 = load i32, i32* @RLIMIT_AS
%35 = call i32 @getrlimit(i32 %34, %struct.rlimit* %2)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = load i64, i64* @errno
%39 = call i8* @strerror(i64 %38)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %39)
br label %99
41: ; preds = %33
%42 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
%43 = load i64, i64* %42
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i64 %43)
%45 = load i32, i32* @PROT_READ
%46 = load i32, i32* @PROT_WRITE
%47 = or i32 %46, %45
%48 = load i32, i32* @MAP_SHARED
%49 = call i8* @mmap(i8* null, i64 %7, i32 %47, i32 %48, i32 %19, i32 0)
%50 = load i8*, i8** @MAP_FAILED
%51 = icmp eq i8* %49, %50
br i1 %51, label %52, label %57
52: ; preds = %41
%53 = load i64, i64* @errno
%54 = call i8* @strerror(i64 %53)
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %41
%58 = load i64, i64* %42
%59 = ptrtoint i8* %49 to i64
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %59, i64 %58)
%61 = urem i64 %59, %6
%62 = icmp eq i64 %61, 0
%63 = getelementptr i8, i8* %49, i64 %6
%64 = ptrtoint i8* %63 to i64
%65 = sub i64 0, %6
%66 = and i64 %64, %65
%67 = inttoptr i64 %66 to i8*
%68 = select i1 %62, i8* %49, i8* %67
%69 = urem i64 %58, %6
%70 = icmp eq i64 %69, 0
%71 = select i1 %70, i64 -1, i64 %65
%72 = and i64 %71, %58
%73 = ptrtoint i8* %68 to i64
%74 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %73, i64 %72)
%75 = load i32, i32* @PROT_READ
%76 = load i32, i32* @PROT_WRITE
%77 = or i32 %76, %75
%78 = load i32, i32* @MAP_FIXED
%79 = load i32, i32* @MAP_SHARED
%80 = or i32 %79, %78
%81 = call i8* @mmap(i8* %68, i64 %72, i32 %77, i32 %80, i32 %19, i32 0)
%82 = load i8*, i8** @MAP_FAILED
%83 = icmp eq i8* %81, %82
br i1 %83, label %84, label %94
84: ; preds = %57
%85 = load i64, i64* @errno
%86 = load i64, i64* @ENOMEM
%87 = icmp eq i64 %85, %86
br i1 %87, label %88, label %92
88: ; preds = %84
%89 = call i8* @strerror(i64 %85)
%90 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i8* %89)
%91 = call i32 @exit(i32 0)
unreachable
92: ; preds = %84
%93 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
br label %96
94: ; preds = %57
%95 = call i32 @munmap(i8* %81, i64 %72)
br label %96
96: ; preds = %94, %92
%97 = call i32 @close(i32 %19)
%98 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
br label %99
99: ; preds = %96, %37, %29, %21
%100 = phi i32 [ 2, %21 ], [ 2, %29 ], [ 2, %37 ], [ 1, %96 ]
ret i32 %100
}