#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 */
char *MAP_FAILED;
int MAP_FILE;
int MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
char *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putchar(char);
int main() {
  int shmid;
  char *shm, *s;
  if ((shmid = open("/tmp/mymemory", O_RDWR)) < 0) {
    perror("shm_open");
    exit(1);
  }
  if ((shm = mmap(NULL, 27, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
                  shmid, 0)) == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  for (s = shm; *s != '\0'; s++) {
    putchar(*s);
  }
  putchar('\n');
  *shm = '*';
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1196 <main+0x46>
lea    0xe87(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
mov    0x2e1a(%rip),%rax
or     (%rax),%edx
mov    0x2e21(%rip),%rax
mov    (%rax),%ecx
mov    0x2e30(%rip),%rax
or     (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1b,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    11f9 <main+0xa9>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     122e <main+0xde>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <putchar@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1201 <main+0xb1>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x10(%rbp),%rax
movb   $0x2a,(%rax)
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
jns    1176 <main+0x26>
lea    0xe9a(%rip),%rdi
jmp    11c2 <main+0x72>
mov    0x2e7b(%rip),%rcx
mov    0x2e3c(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e41(%rip),%rsi
mov    0x2e52(%rip),%rcx
mov    (%rcx),%ecx
or     (%rsi),%ecx
xor    %edi,%edi
mov    $0x1b,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
mov    0x2e02(%rip),%rax
cmp    (%rax),%r14
jne    11d1 <main+0x81>
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    (%r14),%al
test   %al,%al
je     11f3 <main+0xa3>
mov    %r14,%rbx
add    $0x1,%rbx
nop
movsbl %al,%edi
call   1030 <putchar@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11e0 <main+0x90>
mov    $0xa,%edi
call   1030 <putchar@plt>
movb   $0x2a,(%r14)
xor    %edi,%edi
call   1070 <exit@plt>

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

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