#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 O_CREAT;
int 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, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int main() {
  int *p;
  int fd;
  int i;
  fd = open("tmp", O_RDWR | O_CREAT, 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);
  i = 0;
  while (1) {
    sleep(1);
    *p = i++;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
lea    0xe78(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b5 <main+0x55>
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
call   1050 <ftruncate@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    0x2dee(%rip),%rax
or     (%rax),%edx
mov    0x2e05(%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)
movl   $0x0,-0x18(%rbp)
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x18(%rbp),%ecx
mov    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    11fc <main+0x9c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e66(%rip),%rax
mov    0x2e87(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe84(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e1 <main+0x81>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x4,%esi
call   1050 <ftruncate@plt>
mov    0x2e50(%rip),%rax
mov    0x2e19(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2e(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebp,(%rbx)
add    $0x1,%ebp
jmp    11d0 <main+0x70>
lea    0xe1c(%rip),%rdi
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
@O_CREAT = 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
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %7, i32 438)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %3
%16 = call i32 @ftruncate(i32 %15, i32 4)
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %17, %18
%20 = load i32, i32* @MAP_SHARED
%21 = load i32, i32* %3
%22 = call i32* @mmap(i32 0, i32 4, i32 %19, i32 %20, i32 %21, i32 0)
store i32* %22, i32** %2
store i32 0, i32* %4
br label %23
23: ; preds = %14, %23
%24 = call i32 @sleep(i32 1)
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
%27 = load i32*, i32** %2
store i32 %25, i32* %27
br label %23
}

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