#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*/ 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 *counter;
  if (argc != 2) {
    fprintf(stderr, "usage: %s shm_name\n", argv[0]);
    exit(1);
  }
  if ((fd = shm_open(argv[1], O_CREAT | O_RDWR, 0666)) < 0) {
    perror(argv[1]);
    exit(1);
  }
  ftruncate(fd, sizeof(int));
  counter = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  for (;;) {
    fprintf(stderr, "counter = %d\n", *counter);
    sleep(1);
    (*counter)++;
  }
  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    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e00(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1080 <shm_open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ff <main+0x8f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
call   1040 <ftruncate@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
mov    0x2d9c(%rip),%rax
or     (%rax),%edx
mov    0x2dbb(%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    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xdbe(%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    1240 <main+0xd0>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1220 <main+0xb0>
mov    0x8(%rbx),%rdi
mov    0x2e6d(%rip),%rax
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
call   1080 <shm_open@plt>
test   %eax,%eax
js     1244 <main+0xd4>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x4,%esi
call   1040 <ftruncate@plt>
mov    0x2e33(%rip),%rax
mov    0x2df4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e11(%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    (%rax),%edx
mov    0x2dd1(%rip),%r14
lea    0xe1e(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
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    1200 <main+0x90>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdcd(%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    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"usage: %s shm_name\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [14 x i8] c"counter = %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 ([20 x i8], [20 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_CREAT
%22 = load i32, i32* @O_RDWR
%23 = or i32 %21, %22
%24 = call i32 @shm_open(i8* %20, i32 %23, i32 438)
store i32 %24, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %17
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @perror(i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %17
%33 = load i32, i32* %6
%34 = call i32 @ftruncate(i32 %33, i32 4)
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @PROT_WRITE
%37 = or i32 %35, %36
%38 = load i32, i32* @MAP_SHARED
%39 = load i32, i32* %6
%40 = call i32* @mmap(i32* null, i32 4, i32 %37, i32 %38, i32 %39, i32 0)
store i32* %40, i32** %7
br label %41
41: ; preds = %41, %32
%42 = load i32, i32* @stderr
%43 = load i32*, i32** %7
%44 = load i32, i32* %43
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = call i32 @sleep(i32 1)
%47 = load i32*, i32** %7
%48 = load i32, i32* %47
%49 = add nsw i32 %48, 1
store i32 %49, i32* %47
br label %41
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"usage: %s shm_name\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [14 x i8] c"counter = %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 ([20 x i8], [20 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_CREAT
%13 = load i32, i32* @O_RDWR
%14 = or i32 %13, %12
%15 = tail call i32 @shm_open(i8* %11, i32 %14, i32 438)
%16 = icmp slt i32 %15, 0
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 1)
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* %27
br label %29
29: ; preds = %29, %21
%30 = phi i32 [ %35, %29 ], [ %28, %21 ]
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%33 = tail call i32 @sleep(i32 1)
%34 = load i32, i32* %27
%35 = add nsw i32 %34, 1
store i32 %35, i32* %27
br label %29
}