#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*/ mode_t;

/* Variables and functions */
int EXIT_SUCCESS;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
int ftruncate(int, size_t);
size_t getpagesize();
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int shm_open(char *, int, int);
int main() {
  int opt, fd;
  mode_t perms;
  size_t size;
  void *addr;
  size = getpagesize();
  fd = shm_open("/myshm", O_CREAT | O_RDWR, 666);
  if (fd == -1) {
    perror("shm_open");
    exit(1);
  }
  if (ftruncate(fd, size) == -1) {
    perror("ftruncate");
    exit(1);
  }
  addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (addr == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
lea    0xe6d(%rip),%rdi
mov    $0x29a,%edx
call   1080 <shm_open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c0 <main+0x60>
lea    0xe56(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    11eb <main+0x8b>
lea    0xe34(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rsi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    0x2dc1(%rip),%rax
or     (%rax),%edx
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d87(%rip),%rcx
cmp    (%rcx),%rax
jne    1248 <main+0xe8>
lea    0xde1(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <getpagesize@plt>
mov    %rax,%rbx
mov    0x2e84(%rip),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe7a(%rip),%rdi
mov    $0x29a,%edx
call   1080 <shm_open@plt>
cmp    $0xffffffff,%eax
jne    119e <main+0x3e>
lea    0xe6b(%rip),%rdi
jmp    11f6 <main+0x96>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    11b8 <main+0x58>
lea    0xe5a(%rip),%rdi
jmp    11f6 <main+0x96>
mov    0x2e31(%rip),%rax
mov    0x2dfa(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0f(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2dc6(%rip),%rcx
cmp    (%rcx),%rax
jne    1205 <main+0xa5>
lea    0xe24(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"/myshm\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"shm_open\00"
@.str.2 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
store i32 0, i32* %1
%7 = call i64 (...) @getpagesize()
store i64 %7, i64* %5
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %8, %9
%11 = call i32 @shm_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %10, i32 666)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32, i32* %3
%19 = load i64, i64* %5
%20 = call i32 @ftruncate(i32 %18, i64 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = load i64, i64* %5
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @PROT_WRITE
%29 = or i32 %27, %28
%30 = load i32, i32* @MAP_SHARED
%31 = load i32, i32* %3
%32 = call i8* @mmap(i32* null, i64 %26, i32 %29, i32 %30, i32 %31, i32 0)
store i8* %32, i8** %6
%33 = load i8*, i8** %6
%34 = load i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %39
36: ; preds = %25
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %25
%40 = load i32, i32* @EXIT_SUCCESS
%41 = call i32 @exit(i32 %40)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"/myshm\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"shm_open\00"
@.str.2 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = tail call i64 (...) @getpagesize()
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_RDWR
%4 = or i32 %3, %2
%5 = tail call i32 @shm_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %4, i32 666)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = tail call i32 @ftruncate(i32 %5, i64 %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %18, %17
%20 = load i32, i32* @MAP_SHARED
%21 = tail call i8* @mmap(i32* null, i64 %1, i32 %19, i32 %20, i32 %5, i32 0)
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %16
%28 = load i32, i32* @EXIT_SUCCESS
%29 = tail call i32 @exit(i32 %28)
unreachable
}