#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 PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
int *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
void writer(int fd) {
  int *sharedMemory;
  int i;
  sharedMemory =
      mmap(0, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (sharedMemory == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  for (i = 0;; i++) {
    *sharedMemory = i;
    sleep(2);
  }
}


x86 assembly (clang, -O0):
<writer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%edx
mov    0x2e75(%rip),%rax
or     (%rax),%edx
mov    0x2e84(%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    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
jne    11a8 <writer+0x68>
lea    0xe67(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    $0x2,%edi
call   1060 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11af <writer+0x6f>

x86 assembly (clang, -O2):
<writer>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%r8d
mov    0x2eab(%rip),%rax
mov    0x2e7c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e89(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e4c(%rip),%rax
cmp    (%rax),%rbx
je     11a1 <writer+0x61>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,(%rbx)
mov    $0x2,%edi
call   1060 <sleep@plt>
add    $0x1,%ebp
jmp    1190 <writer+0x50>
lea    0xe58(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str = [5 x i8] c"mmap\00"
define void @writer(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* @PROT_WRITE
%7 = or i32 %5, %6
%8 = load i32, i32* @MAP_SHARED
%9 = load i32, i32* %2
%10 = call i32* @mmap(i32 0, i32 4, i32 %7, i32 %8, i32 %9, i32 0)
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = load i32*, i32** @MAP_FAILED
%13 = icmp eq i32* %11, %12
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
store i32 0, i32* %4
br label %18
18: ; preds = %22, %17
%19 = load i32, i32* %4
%20 = load i32*, i32** %3
store i32 %19, i32* %20
%21 = call i32 @sleep(i32 2)
br label %22
22: ; preds = %18
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %18
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str = [5 x i8] c"mmap\00"
define void @writer(i32 %0) {
%2 = load i32, i32* @PROT_READ
%3 = load i32, i32* @PROT_WRITE
%4 = or i32 %3, %2
%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, %12
%13 = phi i32 [ %15, %12 ], [ 0, %1 ]
store i32 %13, i32* %6
%14 = tail call i32 @sleep(i32 2)
%15 = add nuw nsw i32 %13, 1
br label %12
}