#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;
  volatile int val;
  int i;
  sharedMemory = mmap(0, sizeof(int), PROT_READ, MAP_SHARED, fd, 0);
  if (sharedMemory == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  for (i = -1;;) {
    val = *sharedMemory;
    if (val > 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,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    1201 <reader+0xa1>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dd6(%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   %rbx
push   %rax
mov    %edi,%r8d
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
mov    0x2e6e(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e33(%rip),%rax
cmp    (%rax),%rbx
je     11de <reader+0x7e>
mov    $0xffffffff,%ebp
lea    0xe5f(%rip),%r14
mov    0x2e23(%rip),%r15
jmp    11ba <reader+0x5a>
nop
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    0x4(%rsp),%eax
cmp    %ebp,%eax
jle    11b0 <reader+0x50>
mov    (%rbx),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    (%r15),%edi
call   1050 <fflush@plt>
jmp    11b0 <reader+0x50>
lea    0xe1b(%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
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @MAP_SHARED
%8 = load i32, i32* %2
%9 = call i32* @mmap(i32 0, i32 4, i32 %6, i32 %7, i32 %8, i32 0)
store i32* %9, i32** %3
%10 = load i32*, i32** %3
%11 = load i32*, i32** @MAP_FAILED
%12 = icmp eq i32* %10, %11
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
store i32 -1, i32* %5
br label %17
17: ; preds = %30, %16
%18 = load i32*, i32** %3
%19 = load i32, i32* %18
store volatile i32 %19, i32* %4
%20 = load volatile i32, i32* %4
%21 = load i32, i32* %5
%22 = icmp sgt i32 %20, %21
br i1 %22, label %23, label %30
23: ; preds = %17
%24 = load i32*, i32** %3
%25 = load i32, i32* %24
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32, i32* @stdout
%29 = call i32 @fflush(i32 %28)
br label %30
30: ; preds = %23, %17
%31 = call i32 @sleep(i32 1)
br label %17
}

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 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @MAP_SHARED
%6 = tail call i32* @mmap(i32 0, i32 4, i32 %4, i32 %5, i32 %0, i32 0)
%7 = load i32*, i32** @MAP_FAILED
%8 = icmp eq i32* %6, %7
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1, %22
%13 = phi i32 [ %23, %22 ], [ -1, %1 ]
%14 = load i32, i32* %6
store volatile i32 %14, i32* %2
%15 = load volatile i32, i32* %2
%16 = icmp sgt i32 %15, %13
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i32, i32* %6
%19 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @stdout
%21 = tail call i32 @fflush(i32 %20)
br label %22
22: ; preds = %17, %12
%23 = phi i32 [ %18, %17 ], [ %13, %12 ]
%24 = tail call i32 @sleep(i32 1)
br label %12
}