#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
void *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main() {
  FILE *map_file;
  void *shared;
  int i = 0;
  map_file = fopen("mmap.file", "r+");
  shared = mmap(0, 4, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(map_file), 0);
  printf("write: shared=%d\n", (unsigned)shared);
  *(unsigned *)shared = 0;
  printf("write: sleeping... (shared value=0)\n");
  sleep(10);
  *(unsigned *)shared = 1;
  fclose(map_file);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x14(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    0x2e3a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fileno@plt>
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
mov    $0x4,%esi
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xe85(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
mov    0x2e67(%rip),%rax
mov    0x2e38(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x2e45(%rip),%rax
mov    (%rax),%ebx
mov    %r14,%rdi
call   1070 <fileno@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
lea    0xe40(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
movl   $0x0,(%rbx)
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
movl   $0x1,(%rbx)
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"mmap.file\00"
@.str.1 = [3 x i8] c"r+\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [18 x i8] c"write: shared=%d\0A\00"
@.str.3 = [37 x i8] c"write: sleeping... (shared value=0)\0A\00"
define i32 @main() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %3
%4 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %1
%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** %1
%10 = call i32 @fileno(i32* %9)
%11 = call i8* @mmap(i32 0, i32 4, i32 %7, i32 %8, i32 %10, i32 0)
store i8* %11, i8** %2
%12 = load i8*, i8** %2
%13 = ptrtoint i8* %12 to i32
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = load i8*, i8** %2
%16 = bitcast i8* %15 to i32*
store i32 0, i32* %16
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 @sleep(i32 10)
%19 = load i8*, i8** %2
%20 = bitcast i8* %19 to i32*
store i32 1, i32* %20
%21 = load i32*, i32** %1
%22 = call i32 @fclose(i32* %21)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"mmap.file\00"
@.str.1 = [3 x i8] c"r+\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [18 x i8] c"write: shared=%d\0A\00"
@str = [36 x i8] c"write: sleeping... (shared value=0)\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 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 @fileno(i32* %1)
%7 = tail call i8* @mmap(i32 0, i32 4, i32 %4, i32 %5, i32 %6, i32 0)
%8 = ptrtoint i8* %7 to i64
%9 = trunc i64 %8 to i32
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = bitcast i8* %7 to i32*
store i32 0, i32* %11
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @sleep(i32 10)
store i32 1, i32* %11
%14 = tail call i32 @fclose(i32* %1)
ret i32 0
}