#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ exit(int);
void *mmap(int /*<<< orphan*/, int const, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int shm_open(char const *, int /*<<< orphan*/, int);
int shm_unlink(char const *);
int main() {
  const int SIZE = 4096;
  const char *Obj = "Shm";
  int shm_fd;
  void *ptr;
  shm_fd = shm_open(Obj, O_RDONLY, 0666);
  if (shm_fd == -1) {
    printf("Shared memory failed\n");
    exit(-1);
  }
  ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
  if (ptr == MAP_FAILED) {
    printf("Map failed\n");
    exit(-1);
  }
  printf("Consumer: The output sequence is: %s", (char *)ptr);
  if (shm_unlink(Obj) == -1) {
    printf("Error removing %s\n", Obj);
    exit(-1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1000,-0x8(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    $0x1b6,%edx
call   1060 <shm_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ad <main+0x5d>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
mov    0x2e2b(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x1000,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
jne    1203 <main+0xb3>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <shm_unlink@plt>
cmp    $0xffffffff,%eax
jne    1243 <main+0xf3>
mov    -0x10(%rbp),%rsi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <shm_open@plt>
cmp    $0xffffffff,%eax
je     11d6 <main+0x76>
mov    0x2e71(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e58(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
je     11df <main+0x7f>
lea    0xe4d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe38(%rip),%rdi
call   1080 <shm_unlink@plt>
cmp    $0xffffffff,%eax
je     11f5 <main+0x95>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe6a(%rip),%rdi
jmp    11e6 <main+0x86>
lea    0xe56(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
lea    0xe2d(%rip),%rdi
lea    0xdfd(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"Shm\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Shared memory failed\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [12 x i8] c"Map failed\0A\00"
@.str.3 = [37 x i8] c"Consumer: The output sequence is: %s\00"
@.str.4 = [19 x i8] c"Error removing %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i32 0, i32* %1
store i32 4096, i32* %2
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @shm_open(i8* %6, i32 %7, i32 438)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %0
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @MAP_SHARED
%17 = load i32, i32* %4
%18 = call i8* @mmap(i32 0, i32 4096, i32 %15, i32 %16, i32 %17, i32 0)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %14
%26 = load i8*, i8** %5
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = load i8*, i8** %3
%29 = call i32 @shm_unlink(i8* %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = load i8*, i8** %3
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %25
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"Shm\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [37 x i8] c"Consumer: The output sequence is: %s\00"
@.str.4 = [19 x i8] c"Error removing %s\0A\00"
@str = [11 x i8] c"Map failed\00"
@str.5 = [21 x i8] c"Shared memory failed\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @shm_open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1, i32 438)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.5, 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* @MAP_SHARED
%10 = tail call i8* @mmap(i32 0, i32 4096, i32 %8, i32 %9, i32 %2, i32 0)
%11 = load i8*, i8** @MAP_FAILED
%12 = icmp eq i8* %10, %11
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %7
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %10)
%18 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %16
ret i32 0
}