#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 /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftruncate(int, int);
int *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
          int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main() {
  int *p;
  int fd;
  fd = open("tmp", O_RDWR, 0666);
  if (fd == -1) {
    printf("error : %m\n");
    exit(-1);
  }
  ftruncate(fd, 4);
  p = mmap(0, 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  while (1) {
    sleep(1);
    printf("%d ", *p);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ae <main+0x4e>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
call   1050 <ftruncate@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
mov    0x2dfd(%rip),%rax
or     (%rax),%edx
mov    0x2e14(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x4,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ee <main+0x8e>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e8 <main+0x88>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x4,%esi
call   1050 <ftruncate@plt>
mov    0x2e60(%rip),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e3e(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
lea    0xe48(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d0 <main+0x70>
lea    0xe15(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"tmp\00"
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"error : %m\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [4 x i8] c"%d \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %4, i32 438)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = load i32, i32* %3
%13 = call i32 @ftruncate(i32 %12, i32 4)
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_SHARED
%18 = load i32, i32* %3
%19 = call i32* @mmap(i32 0, i32 4, i32 %16, i32 %17, i32 %18, i32 0)
store i32* %19, i32** %2
br label %20
20: ; preds = %11, %20
%21 = call i32 @sleep(i32 1)
%22 = load i32*, i32** %2
%23 = load i32, i32* %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %20
}

LLVM IR (-O2):
@.str = [4 x i8] c"tmp\00"
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"error : %m\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [4 x i8] c"%d \00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1, i32 438)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %0
%8 = tail call i32 @ftruncate(i32 %2, i32 4)
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_SHARED
%13 = tail call i32* @mmap(i32 0, i32 4, i32 %11, i32 %12, i32 %2, i32 0)
br label %14
14: ; preds = %14, %7
%15 = tail call i32 @sleep(i32 1)
%16 = load i32, i32* %13
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %16)
br label %14
}