#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ ftruncate(int, int);
void *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int shm_open(char *, int, int);
int main(int argc, char *argv[]) {
  int shm_fd = shm_open("/test.shm", O_CREAT | O_RDWR, 0666);
  ftruncate(shm_fd, 1 << 20);
  void *ptr = mmap(0, 1 << 20, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  if (ptr == MAP_FAILED) {
    perror("Map failed");
    return -1;
  }
  char *s = ptr;
  int i;
  for (i = 0; i < 1 << 20; i++) {
    *s++ = 13;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
or     (%rax),%esi
lea    0xe91(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <shm_open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x100000,%esi
call   1040 <ftruncate@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
mov    0x2e27(%rip),%rax
or     (%rax),%edx
mov    0x2e3e(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x100000,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%rax
jne    11e8 <main+0xa8>
lea    0xe33(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <main+0xeb>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x100000,-0x2c(%rbp)
jge    1224 <main+0xe4>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0xd,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11f7 <main+0xb7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe94(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <shm_open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x100000,%esi
call   1040 <ftruncate@plt>
mov    0x2e65(%rip),%rax
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e43(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x100000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
je     11da <main+0x8a>
mov    $0x100000,%edx
mov    %rax,%rdi
mov    $0xd,%esi
call   1050 <memset@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe29(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11d1 <main+0x81>

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

LLVM IR (-O2):
@.str = [10 x i8] c"/test.shm\00"
@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
@MAP_FAILED = global i8* null
@.str.1 = [11 x i8] c"Map failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %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* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5, i32 438)
%7 = tail call i32 @ftruncate(i32 %6, i32 1048576)
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_SHARED
%12 = tail call i8* @mmap(i32 0, i32 1048576, i32 %10, i32 %11, i32 %6, i32 0)
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %16, label %15
15: ; preds = %2
br label %18
16: ; preds = %2
%17 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %15, %16
%19 = phi i32 [ -1, %16 ], [ 0, %15 ]
ret i32 %19
}