#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 O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ ftruncate(int, int);
int *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int shm_open(char *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int fd;
  int *compteur;
  if (argc != 2) {
    fprintf(stderr, "usage: %s mem\n", argv[0]);
    exit(1);
  }
  fd = shm_open(argv[1], O_RDWR | O_CREAT, 0666);
  if (fd == -1) {
    perror(argv[1]);
    exit(2);
  }
  ftruncate(fd, sizeof(int));
  compteur = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (compteur == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  while (1) {
    fprintf(stderr, "%d\n", *compteur);
    sleep(1);
    (*compteur)++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b8 <main+0x48>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1080 <shm_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1200 <main+0x90>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
call   1040 <ftruncate@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
mov    0x2d9b(%rip),%rax
or     (%rax),%edx
mov    0x2dba(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d64(%rip),%rcx
cmp    (%rcx),%rax
jne    126b <main+0xfb>
lea    0xdb3(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1270 <main+0x100>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,-0x24(%rbp)
mov    %ecx,(%rax)
jmp    1270 <main+0x100>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1230 <main+0xc0>
mov    0x8(%rbx),%rdi
mov    0x2e45(%rip),%rax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
call   1080 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1254 <main+0xe4>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x4,%esi
call   1040 <ftruncate@plt>
mov    0x2e32(%rip),%rax
mov    0x2df3(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e10(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2dc2(%rip),%rax
cmp    (%rax),%rbx
je     1267 <main+0xf7>
mov    (%rbx),%edx
mov    0x2dc4(%rip),%r14
lea    0xe11(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r14),%edi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    (%rbx),%edx
add    $0x1,%edx
mov    %edx,(%rbx)
jmp    1210 <main+0xa0>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdbd(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x8(%rbx),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
lea    0xda1(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"usage: %s mem\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = call i32 @shm_open(i8* %20, i32 %23, i32 438)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %17
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @perror(i8* %30)
%32 = call i32 @exit(i32 2)
unreachable
33: ; preds = %17
%34 = load i32, i32* %6
%35 = call i32 @ftruncate(i32 %34, i32 4)
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @PROT_WRITE
%38 = or i32 %36, %37
%39 = load i32, i32* @MAP_SHARED
%40 = load i32, i32* %6
%41 = call i32* @mmap(i32* null, i32 4, i32 %38, i32 %39, i32 %40, i32 0)
store i32* %41, i32** %7
%42 = load i32*, i32** %7
%43 = load i32*, i32** @MAP_FAILED
%44 = icmp eq i32* %42, %43
br i1 %44, label %45, label %48
45: ; preds = %33
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %33
br label %49
49: ; preds = %48, %49
%50 = load i32, i32* @stderr
%51 = load i32*, i32** %7
%52 = load i32, i32* %51
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %52)
%54 = call i32 @sleep(i32 1)
%55 = load i32*, i32** %7
%56 = load i32, i32* %55
%57 = add nsw i32 %56, 1
store i32 %57, i32* %55
br label %49
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"usage: %s mem\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = tail call i32 @shm_open(i8* %11, i32 %14, i32 438)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %9
%18 = load i8*, i8** %10
%19 = tail call i32 @perror(i8* %18)
%20 = tail call i32 @exit(i32 2)
unreachable
21: ; preds = %9
%22 = tail call i32 @ftruncate(i32 %15, i32 4)
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %24, %23
%26 = load i32, i32* @MAP_SHARED
%27 = tail call i32* @mmap(i32* null, i32 4, i32 %25, i32 %26, i32 %15, i32 0)
%28 = load i32*, i32** @MAP_FAILED
%29 = icmp eq i32* %27, %28
br i1 %29, label %32, label %30
30: ; preds = %21
%31 = load i32, i32* %27
br label %35
32: ; preds = %21
%33 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %30, %35
%36 = phi i32 [ %31, %30 ], [ %41, %35 ]
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%39 = tail call i32 @sleep(i32 1)
%40 = load i32, i32* %27
%41 = add nsw i32 %40, 1
store i32 %41, i32* %27
br label %35
}