#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 */
scalar_t__ ENOMEM;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
size_t SIZE_MAX;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int ftruncate(int, size_t);
int getpid();
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
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__);
int main(void) {
  char tmpfname[256];
  void *pa;
  size_t len;
  int fd;
  size_t shm_size = 1024;
  size_t mapped_size = 0;
  snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_25_1_%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;
  }
  len = shm_size;
  mapped_size = 0;
  while (mapped_size < SIZE_MAX) {
    pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (pa == MAP_FAILED && errno == ENOMEM) {
      printf("Total mapped size is %lu bytes\n", (unsigned long)mapped_size);
      printf("Test PASSED\n");
      return 0;
    }
    mapped_size += shm_size;
    if (pa == MAP_FAILED)
      perror("Error at mmap()");
  }
  close(fd);
  printf("Test FAILED: Did not get ENOMEM as expected\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x400,-0x130(%rbp)
movq   $0x0,-0x138(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x140(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xe0e(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   10c0 <shm_unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    0x2dde(%rip),%rax
or     (%rax),%esi
mov    0x2d8d(%rip),%rax
or     (%rax),%esi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
mov    0x2d93(%rip),%rax
or     (%rax),%edx
call   10a0 <shm_open@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    127b <main+0xdb>
mov    0x2d52(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13f7 <main+0x257>
lea    -0x110(%rbp),%rdi
call   10c0 <shm_unlink@plt>
mov    -0x124(%rbp),%edi
mov    -0x130(%rbp),%rsi
call   1070 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12ce <main+0x12e>
mov    0x2cff(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13f7 <main+0x257>
mov    -0x130(%rbp),%rax
mov    %rax,-0x120(%rbp)
movq   $0x0,-0x138(%rbp)
mov    -0x138(%rbp),%rax
mov    0x2c8b(%rip),%rcx
cmp    (%rcx),%rax
jae    13d7 <main+0x237>
mov    -0x120(%rbp),%rsi
mov    0x2ce4(%rip),%rax
mov    (%rax),%edx
mov    0x2c8b(%rip),%rax
or     (%rax),%edx
mov    0x2cc2(%rip),%rax
mov    (%rax),%ecx
mov    -0x124(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
mov    0x2c48(%rip),%rcx
cmp    (%rcx),%rax
jne    139a <main+0x1fa>
mov    0x2c50(%rip),%rax
mov    (%rax),%rax
mov    0x2c56(%rip),%rcx
cmp    (%rcx),%rax
jne    139a <main+0x1fa>
mov    -0x138(%rbp),%rsi
lea    0xccb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xcdd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13f7 <main+0x257>
mov    -0x130(%rbp),%rax
add    -0x138(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    -0x118(%rbp),%rax
mov    0x2bd3(%rip),%rcx
cmp    (%rcx),%rax
jne    13d2 <main+0x232>
lea    0xca4(%rip),%rdi
call   1090 <perror@plt>
jmp    12e7 <main+0x147>
mov    -0x124(%rbp),%edi
call   1080 <close@plt>
lea    0xc98(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe31(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   10d0 <shm_unlink@plt>
mov    0x2de0(%rip),%rax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dae(%rip),%rax
or     (%rax),%esi
mov    0x2d8d(%rip),%rax
mov    0x2db6(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10b0 <shm_open@plt>
cmp    $0xffffffff,%eax
je     12eb <main+0x13b>
mov    %eax,%ebp
mov    %rsp,%rdi
call   10d0 <shm_unlink@plt>
mov    $0x400,%esi
mov    %ebp,%edi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     1303 <main+0x153>
mov    0x2d33(%rip),%r15
cmpq   $0x0,(%r15)
je     132a <main+0x17a>
xor    %ebx,%ebx
mov    0x2d40(%rip),%r13
mov    0x2d79(%rip),%r14
mov    0x2d22(%rip),%r12
jmp    128c <main+0xdc>
lea    0xded(%rip),%rdi
call   10a0 <perror@plt>
add    $0x400,%rbx
cmp    (%r15),%rbx
jae    132a <main+0x17a>
mov    0x0(%r13),%edx
mov    0x2d59(%rip),%rax
or     (%rax),%edx
mov    (%r14),%ecx
mov    $0x400,%esi
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    (%r12),%rax
jne    127c <main+0xcc>
mov    0x2ced(%rip),%rax
mov    (%rax),%rax
mov    0x2cf3(%rip),%rcx
cmp    (%rcx),%rax
jne    1270 <main+0xc0>
lea    0xd73(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdbc(%rip),%rdi
call   1030 <puts@plt>
jmp    1342 <main+0x192>
mov    0x2cb6(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xd10(%rip),%rdi
jmp    1319 <main+0x169>
mov    0x2c9e(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xd11(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x2,%ebp
jmp    1342 <main+0x192>
mov    %ebp,%edi
call   1090 <close@plt>
lea    0xd3c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"pts_mmap_25_1_%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"
@SIZE_MAX = global i64 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@ENOMEM = global i64 0
@.str.3 = [32 x i8] c"Total mapped size is %lu bytes\0A\00"
@.str.4 = [13 x i8] c"Test PASSED\0A\00"
@.str.5 = [16 x i8] c"Error at mmap()\00"
@.str.6 = [45 x i8] c"Test FAILED: Did not get ENOMEM as expected\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i32 0, i32* %1
store i64 1024, i64* %6
store i64 0, i64* %7
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%9 = call i32 (...) @getpid()
%10 = call i32 @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%12 = call i32 @shm_unlink(i8* %11)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @O_EXCL
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %19, %20
%22 = call i32 @shm_open(i8* %13, i32 %18, i32 %21)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %0
%26 = load i64, i64* @errno
%27 = call i8* @strerror(i64 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 2, i32* %1
br label %79
29: ; preds = %0
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%31 = call i32 @shm_unlink(i8* %30)
%32 = load i32, i32* %5
%33 = load i64, i64* %6
%34 = call i32 @ftruncate(i32 %32, i64 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i64, i64* @errno
%38 = call i8* @strerror(i64 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %38)
store i32 2, i32* %1
br label %79
40: ; preds = %29
%41 = load i64, i64* %6
store i64 %41, i64* %4
store i64 0, i64* %7
br label %42
42: ; preds = %74, %40
%43 = load i64, i64* %7
%44 = load i64, i64* @SIZE_MAX
%45 = icmp ult i64 %43, %44
br i1 %45, label %46, label %75
46: ; preds = %42
%47 = load i64, i64* %4
%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* %5
%53 = call i8* @mmap(i32* null, i64 %47, i32 %50, i32 %51, i32 %52, i32 0)
store i8* %53, i8** %3
%54 = load i8*, i8** %3
%55 = load i8*, i8** @MAP_FAILED
%56 = icmp eq i8* %54, %55
br i1 %56, label %57, label %65
57: ; preds = %46
%58 = load i64, i64* @errno
%59 = load i64, i64* @ENOMEM
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %65
61: ; preds = %57
%62 = load i64, i64* %7
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %62)
%64 = 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 %79
65: ; preds = %57, %46
%66 = load i64, i64* %6
%67 = load i64, i64* %7
%68 = add i64 %67, %66
store i64 %68, i64* %7
%69 = load i8*, i8** %3
%70 = load i8*, i8** @MAP_FAILED
%71 = icmp eq i8* %69, %70
br i1 %71, label %72, label %74
72: ; preds = %65
%73 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %74
74: ; preds = %72, %65
br label %42
75: ; preds = %42
%76 = load i32, i32* %5
%77 = call i32 @close(i32 %76)
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %79
79: ; preds = %75, %61, %36, %25
%80 = load i32, i32* %1
ret i32 %80
}

LLVM IR (-O2):
@.str = [17 x i8] c"pts_mmap_25_1_%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"
@SIZE_MAX = global i64 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@ENOMEM = global i64 0
@.str.3 = [32 x i8] c"Total mapped size is %lu bytes\0A\00"
@.str.5 = [16 x i8] c"Error at mmap()\00"
@str = [44 x i8] c"Test FAILED: Did not get ENOMEM as expected\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 = tail call i32 (...) @getpid()
%4 = call i32 @snprintf(i8* %2, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 @shm_unlink(i8* %2)
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = call i32 @shm_open(i8* %2, i32 %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %0
%17 = load i64, i64* @errno
%18 = call i8* @strerror(i64 %17)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %56
20: ; preds = %0
%21 = call i32 @shm_unlink(i8* %2)
%22 = call i32 @ftruncate(i32 %14, i64 1024)
%23 = icmp eq i32 %22, -1
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = load i64, i64* @SIZE_MAX
%26 = icmp eq i64 %25, 0
br i1 %26, label %53, label %31
27: ; preds = %20
%28 = load i64, i64* @errno
%29 = call i8* @strerror(i64 %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 %56
31: ; preds = %24, %49
%32 = phi i64 [ %50, %49 ], [ 0, %24 ]
%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 1024, i32 %35, i32 %36, i32 %14, i32 0)
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %49
40: ; preds = %31
%41 = load i64, i64* @errno
%42 = load i64, i64* @ENOMEM
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %32)
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %56
47: ; preds = %40
%48 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %31, %47
%50 = add i64 %32, 1024
%51 = load i64, i64* @SIZE_MAX
%52 = icmp ult i64 %50, %51
br i1 %52, label %31, label %53
53: ; preds = %49, %24
%54 = call i32 @close(i32 %14)
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
br label %56
56: ; preds = %53, %44, %27, %16
%57 = phi i32 [ 2, %16 ], [ 2, %27 ], [ 0, %44 ], [ 1, %53 ]
ret i32 %57
}