#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 const);
void *mmap(int /*<<< orphan*/, int const, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int shm_open(char const *, int, int);
int /*<<< orphan*/ sprintf(void *, char *, char const *);
int /*<<< orphan*/ strlen(char const *);
int main() {
  const int SIZE = 4096;
  const char *name = "OS";
  const char *message0 = "Studying ";
  const char *message1 = "Operating Systems ";
  const char *message2 = "Is Fun!";
  int shm_fd;
  void *ptr;
  shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
  ftruncate(shm_fd, SIZE);
  ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  if (ptr == MAP_FAILED) {
    printf("Map failed\n");
    return -1;
  }
  sprintf(ptr, "%s", message0);
  ptr += strlen(message0);
  sprintf(ptr, "%s", message1);
  ptr += strlen(message1);
  sprintf(ptr, "%s\n", message2);
  ptr += strlen(message2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1000,-0x8(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe7b(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe82(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1080 <shm_open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    $0x1000,%esi
call   1060 <ftruncate@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    0x2dde(%rip),%rax
or     (%rax),%edx
mov    0x2df5(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x1000,%esi
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2da8(%rip),%rcx
cmp    (%rcx),%rax
jne    1231 <main+0xd1>
lea    0xe08(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <main+0x15f>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdf4(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xdc7(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xd9d(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e8d(%rip),%rax
mov    0x2e5e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe83(%rip),%rdi
mov    $0x1b6,%edx
call   1080 <shm_open@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1000,%esi
call   1060 <ftruncate@plt>
mov    0x2e54(%rip),%rax
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e32(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2de9(%rip),%rax
cmp    (%rax),%rbx
je     1240 <main+0xe0>
movabs $0x676e697964757453,%rax
69 6e 67
mov    %rax,(%rbx)
movw   $0x20,0x8(%rbx)
lea    0xe15(%rip),%rdi
call   1040 <strlen@plt>
cltq
mov    %rbx,%rbp
add    %rax,%rbp
movups 0xe0b(%rip),%xmm0
movups %xmm0,(%rbx,%rax,1)
movl   $0x20736d,0xf(%rbx,%rax,1)
lea    0xdf8(%rip),%rdi
call   1040 <strlen@plt>
movslq %eax,%rdi
add    %rbp,%rdi
lea    0xe01(%rip),%rsi
lea    0xdf2(%rip),%rbx
mov    %rbx,%rdx
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
jmp    1252 <main+0xf2>
lea    0xde5(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"OS\00"
@.str.1 = [10 x i8] c"Studying \00"
@.str.2 = [19 x i8] c"Operating Systems \00"
@.str.3 = [8 x i8] c"Is Fun!\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.4 = [12 x i8] c"Map failed\0A\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %1
store i32 4096, i32* %2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %3
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %4
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8** %5
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %6
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_RDWR
%12 = or i32 %10, %11
%13 = call i32 @shm_open(i8* %9, i32 %12, i32 438)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = call i32 @ftruncate(i32 %14, i32 4096)
%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* %7
%21 = call i8* @mmap(i32 0, i32 4096, i32 %18, i32 %19, i32 %20, i32 0)
store i8* %21, i8** %8
%22 = load i8*, i8** %8
%23 = load i8*, i8** @MAP_FAILED
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %27
25: ; preds = %0
%26 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %52
27: ; preds = %0
%28 = load i8*, i8** %8
%29 = load i8*, i8** %4
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %4
%32 = call i32 @strlen(i8* %31)
%33 = load i8*, i8** %8
%34 = sext i32 %32 to i64
%35 = getelementptr i8, i8* %33, i64 %34
store i8* %35, i8** %8
%36 = load i8*, i8** %8
%37 = load i8*, i8** %5
%38 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %37)
%39 = load i8*, i8** %5
%40 = call i32 @strlen(i8* %39)
%41 = load i8*, i8** %8
%42 = sext i32 %40 to i64
%43 = getelementptr i8, i8* %41, i64 %42
store i8* %43, i8** %8
%44 = load i8*, i8** %8
%45 = load i8*, i8** %6
%46 = call i32 @sprintf(i8* %44, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %45)
%47 = load i8*, i8** %6
%48 = call i32 @strlen(i8* %47)
%49 = load i8*, i8** %8
%50 = sext i32 %48 to i64
%51 = getelementptr i8, i8* %49, i64 %50
store i8* %51, i8** %8
store i32 0, i32* %1
br label %52
52: ; preds = %27, %25
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
@.str = [3 x i8] c"OS\00"
@.str.1 = [10 x i8] c"Studying \00"
@.str.2 = [19 x i8] c"Operating Systems \00"
@.str.3 = [8 x i8] c"Is Fun!\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.6 = [4 x i8] c"%s\0A\00"
@str = [11 x i8] c"Map failed\00"
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_RDWR
%3 = or i32 %2, %1
%4 = tail call i32 @shm_open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438)
%5 = tail call i32 @ftruncate(i32 %4, i32 4096)
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @PROT_WRITE
%8 = or i32 %7, %6
%9 = load i32, i32* @MAP_SHARED
%10 = tail call i8* @mmap(i32 0, i32 4096, i32 %8, i32 %9, i32 %4, i32 0)
%11 = load i8*, i8** @MAP_FAILED
%12 = icmp eq i8* %10, %11
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %24
15: ; preds = %0
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(10) %10, i8* align 1 dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64 10, i1 false)
%16 = tail call i32 @strlen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%17 = sext i32 %16 to i64
%18 = getelementptr i8, i8* %10, i64 %17
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(19) %18, i8* align 1 dereferenceable(19) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i64 19, i1 false)
%19 = tail call i32 @strlen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%20 = sext i32 %19 to i64
%21 = getelementptr i8, i8* %18, i64 %20
%22 = tail call i32 @sprintf(i8* dereferenceable(1) %21, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %15, %13
%25 = phi i32 [ -1, %13 ], [ 0, %15 ]
ret i32 %25
}