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

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
int ftruncate(int, long);
scalar_t__ mmap(int /*<<< orphan*/, long, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int shm_open(char const *, int, int);
char *map_memory(const char *designator, long size) {
  int md;
  md = shm_open(designator, O_CREAT | O_RDWR, 0600);
  if (md == -1) {
    printf("Got an error opening shm\n");
    exit(1);
  }
  int ferror = ftruncate(md, size);
  if (ferror == -1) {
    printf("Error in ftruncate\n");
  }
  char *ret = (char *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, md, 0);
  return ret;
}


x86 assembly (clang, -O0):
<map_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
mov    0x2e5c(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1070 <shm_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a3 <map_memory+0x53>
lea    0xe6c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <ftruncate@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11c8 <map_memory+0x78>
lea    0xe57(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    0x2de4(%rip),%rax
or     (%rax),%edx
mov    0x2dfb(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_memory>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e9b(%rip),%rax
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x180,%edx
call   1070 <shm_open@plt>
cmp    $0xffffffff,%eax
je     11c5 <map_memory+0x75>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    1194 <map_memory+0x44>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e55(%rip),%rax
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e33(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <mmap@plt>
lea    0xe47(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [26 x i8] c"Got an error opening shm\0A\00"
@.str.1 = [20 x i8] c"Error in ftruncate\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i8* @map_memory(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %9, %10
%12 = call i32 @shm_open(i8* %8, i32 %11, i32 384)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %5
%20 = load i64, i64* %4
%21 = call i32 @ftruncate(i32 %19, i64 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = load i64, i64* %4
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @PROT_WRITE
%30 = or i32 %28, %29
%31 = load i32, i32* @MAP_SHARED
%32 = load i32, i32* %5
%33 = call i64 @mmap(i32 0, i64 %27, i32 %30, i32 %31, i32 %32, i32 0)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %7
%35 = load i8*, i8** %7
ret i8* %35
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@str = [19 x i8] c"Error in ftruncate\00"
@str.2 = [25 x i8] c"Got an error opening shm\00"
define i8* @map_memory(i8* %0, i64 %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = tail call i32 @shm_open(i8* %0, i32 %5, i32 384)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.2, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = tail call i32 @ftruncate(i32 %6, i64 %1)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%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 i64 @mmap(i32 0, i64 %1, i32 %19, i32 %20, i32 %6, i32 0)
%22 = inttoptr i64 %21 to i8*
ret i8* %22
}