#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 *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
void reader(int fd) {
  int *sharedMemory;
  int i;
  sharedMemory = mmap(0, sizeof(int), PROT_READ, MAP_SHARED, fd, 0);
  if (sharedMemory == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  for (i = -1;;) {
    if (*sharedMemory > i) {
      i = *sharedMemory;
      printf("%d ", i);
      fflush(stdout);
    }
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<reader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%edx
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x4,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <reader+0x5f>
lea    0xe50(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
jle    11fb <reader+0x9b>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11c6 <reader+0x66>

x86 assembly (clang, -O2):
<reader>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r8d
mov    0x2e86(%rip),%rax
mov    (%rax),%edx
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r12
mov    0x2e32(%rip),%rax
cmp    (%rax),%r12
je     11d8 <reader+0x78>
mov    $0xffffffff,%ebx
lea    0xe5e(%rip),%r14
mov    0x2e22(%rip),%r15
jmp    11ba <reader+0x5a>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    (%r12),%ebp
cmp    %ebx,%ebp
jle    11b0 <reader+0x50>
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    (%r15),%edi
call   1050 <fflush@plt>
mov    %ebp,%ebx
jmp    11b0 <reader+0x50>
lea    0xe21(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [4 x i8] c"%d \00"
@stdout = global i32 0
define void @reader(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @MAP_SHARED
%7 = load i32, i32* %2
%8 = call i32* @mmap(i32 0, i32 4, i32 %5, i32 %6, i32 %7, i32 0)
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = load i32*, i32** @MAP_FAILED
%11 = icmp eq i32* %9, %10
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
store i32 -1, i32* %4
br label %16
16: ; preds = %28, %15
%17 = load i32*, i32** %3
%18 = load i32, i32* %17
%19 = load i32, i32* %4
%20 = icmp sgt i32 %18, %19
br i1 %20, label %21, label %28
21: ; preds = %16
%22 = load i32*, i32** %3
%23 = load i32, i32* %22
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = load i32, i32* @stdout
%27 = call i32 @fflush(i32 %26)
br label %28
28: ; preds = %21, %16
%29 = call i32 @sleep(i32 1)
br label %16
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [4 x i8] c"%d \00"
@stdout = global i32 0
define void @reader(i32 %0) {
%2 = load i32, i32* @PROT_READ
%3 = load i32, i32* @MAP_SHARED
%4 = tail call i32* @mmap(i32 0, i32 4, i32 %2, i32 %3, i32 %0, i32 0)
%5 = load i32*, i32** @MAP_FAILED
%6 = icmp eq i32* %4, %5
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1, %18
%11 = phi i32 [ %19, %18 ], [ -1, %1 ]
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, %11
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%16 = load i32, i32* @stdout
%17 = tail call i32 @fflush(i32 %16)
br label %18
18: ; preds = %14, %10
%19 = phi i32 [ %12, %14 ], [ %11, %10 ]
%20 = tail call i32 @sleep(i32 1)
br label %10
}