#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 *, unsigned 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("read: shared=%d\n", (unsigned)shared);
  printf("read: value=%d\n", *(unsigned *)shared);
  while (!*(unsigned *)shared)
    ;
  printf("read: new value=%d\n", *(unsigned *)shared);
  fclose(map_file);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe93(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    0x2e43(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
mov    $0x4,%esi
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xe3c(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xe3b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1201 <main+0xb1>
jmp    1206 <main+0xb6>
jmp    11e8 <main+0x98>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xe1b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xea5(%rip),%rdi
lea    0xea8(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
mov    0x2e87(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x2e65(%rip),%rax
mov    (%rax),%ebx
mov    %r14,%rdi
call   1060 <fileno@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
lea    0xe60(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0xe61(%rip),%rdi
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0xe63(%rip),%rdi
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <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 = [17 x i8] c"read: shared=%d\0A\00"
@.str.3 = [16 x i8] c"read: value=%d\0A\00"
@.str.4 = [20 x i8] c"read: new value=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
%5 = 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* %5, i32** %2
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @PROT_WRITE
%8 = or i32 %6, %7
%9 = load i32, i32* @MAP_SHARED
%10 = load i32*, i32** %2
%11 = call i32 @fileno(i32* %10)
%12 = call i8* @mmap(i32 0, i32 4, i32 %8, i32 %9, i32 %11, i32 0)
store i8* %12, i8** %3
%13 = load i8*, i8** %3
%14 = ptrtoint i8* %13 to i32
%15 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = load i8*, i8** %3
%17 = bitcast i8* %16 to i32*
%18 = load i32, i32* %17
%19 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %26, %0
%21 = load i8*, i8** %3
%22 = bitcast i8* %21 to i32*
%23 = load i32, i32* %22
%24 = icmp ne i32 %23, 0
%25 = xor i1 %24, true
br i1 %25, label %26, label %27
26: ; preds = %20
br label %20
27: ; preds = %20
%28 = load i8*, i8** %3
%29 = bitcast i8* %28 to i32*
%30 = load i32, i32* %29
%31 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = load i32*, i32** %2
%33 = call i32 @fclose(i32* %32)
%34 = load i32, i32* %1
ret i32 %34
}

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 = [17 x i8] c"read: shared=%d\0A\00"
@.str.3 = [16 x i8] c"read: value=%d\0A\00"
@.str.4 = [20 x i8] c"read: new value=%d\0A\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 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = bitcast i8* %7 to i32*
%12 = load i32, i32* %11
%13 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %11
%15 = icmp ne i32 %14, 0
%16 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %14)
%17 = tail call i32 @fclose(i32* %1)
ret i32 0
}