#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ ftruncate(int, int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(int *, int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int shm_open(char *, int, int);
int /*<<< orphan*/ shm_unlink(char *);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
char *strerror(int);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(void) {
  int fd;
  int i;
  int *ptr;
  pid_t pid;
  srand(time(NULL));
  fd = shm_open("/pipeautique1", O_CREAT | O_RDWR, 0644);
  printf("shm_open returned %d (%d: %s)\n", fd, errno, strerror(errno));
  ftruncate(fd, 100);
  ptr = (int *)mmap(NULL, 100, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  printf("mmap returned %p (%d: %s)\n", ptr, errno, strerror(errno));
  pid = fork();
  if (pid > 0) {
    for (i = 0; i < 100; i++) {
      ptr[i] = rand() % 100;
      printf("%d ", ptr[i]);
    }
    printf("\n");
  } else {
    for (i = 0; i < 100; i++) {
      printf("%d ", ptr[i]);
    }
    printf("\n");
  }
  munmap(ptr, 100);
  if (pid > 0) {
    shm_unlink("/pipeautique1");
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
lea    0xe08(%rip),%rdi
mov    $0x1a4,%edx
call   10a0 <shm_open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x64,%esi
call   1050 <ftruncate@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
mov    0x2d5f(%rip),%rax
or     (%rax),%edx
mov    0x2d7e(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x64,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    %rax,%rcx
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    1332 <main+0x172>
movl   $0x0,-0xc(%rbp)
cmpl   $0x64,-0xc(%rbp)
jge    131f <main+0x15f>
mov    $0x0,%al
call   10e0 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12d4 <main+0x114>
lea    0xd05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1378 <main+0x1b8>
movl   $0x0,-0xc(%rbp)
cmpl   $0x64,-0xc(%rbp)
jge    136a <main+0x1aa>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xcf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1339 <main+0x179>
lea    0xcba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x64,%esi
call   1080 <munmap@plt>
cmpq   $0x0,-0x20(%rbp)
jle    139d <main+0x1dd>
lea    0xc68(%rip),%rdi
call   10c0 <shm_unlink@plt>
mov    0x2c0c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    0x2e0c(%rip),%rax
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe02(%rip),%rdi
mov    $0x1a4,%edx
call   10b0 <shm_open@plt>
mov    %eax,%ebx
mov    0x2daf(%rip),%r14
mov    (%r14),%ebp
mov    %ebp,%edi
call   10c0 <strerror@plt>
lea    0xdec(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    $0x64,%esi
call   1060 <ftruncate@plt>
mov    0x2dad(%rip),%rax
mov    0x2d6e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d8b(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x64,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    (%r14),%ebp
mov    %ebp,%edi
call   10c0 <strerror@plt>
lea    0xdb0(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %rax,%r14
lea    0xdab(%rip),%r15
xor    %ebp,%ebp
test   %rax,%rax
jle    12f0 <main+0x120>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   10f0 <rand@plt>
movslq %eax,%rsi
imul   $0x51eb851f,%rsi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x25,%rax
add    %ecx,%eax
imul   $0x64,%eax,%eax
sub    %eax,%esi
mov    %esi,(%rbx,%rbp,4)
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
cmp    $0x64,%rbp
jne    12b0 <main+0xe0>
jmp    1307 <main+0x137>
nopl   0x0(%rax)
mov    (%rbx,%rbp,4),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
cmp    $0x64,%rbp
jne    12f0 <main+0x120>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
mov    $0x64,%esi
call   1090 <munmap@plt>
test   %r14,%r14
jle    132f <main+0x15f>
lea    0xcd6(%rip),%rdi
call   10d0 <shm_unlink@plt>
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/pipeautique1\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [31 x i8] c"shm_open returned %d (%d: %s)\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [27 x i8] c"mmap returned %p (%d: %s)\0A\00"
@.str.3 = [4 x i8] c"%d \00"
@.str.4 = [2 x i8] c"\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
store i32 0, i32* %1
%6 = call i32 @time(i32* null)
%7 = call i32 @srand(i32 %6)
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %8, %9
%11 = call i32 @shm_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10, i32 420)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = load i32, i32* @errno
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %13, i8* %15)
%17 = load i32, i32* %2
%18 = call i32 @ftruncate(i32 %17, i32 100)
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %19, %20
%22 = load i32, i32* @MAP_SHARED
%23 = load i32, i32* %2
%24 = call i64 @mmap(i32* null, i32 100, i32 %21, i32 %22, i32 %23, i32 0)
%25 = inttoptr i64 %24 to i32*
store i32* %25, i32** %4
%26 = load i32*, i32** %4
%27 = load i32, i32* @errno
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32* %26, i32 %27, i8* %29)
%31 = call i64 (...) @fork()
store i64 %31, i64* %5
%32 = load i64, i64* %5
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %56
34: ; preds = %0
store i32 0, i32* %3
br label %35
35: ; preds = %51, %34
%36 = load i32, i32* %3
%37 = icmp slt i32 %36, 100
br i1 %37, label %38, label %54
38: ; preds = %35
%39 = call i32 (...) @rand()
%40 = srem i32 %39, 100
%41 = load i32*, i32** %4
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
store i32 %40, i32* %44
%45 = load i32*, i32** %4
%46 = load i32, i32* %3
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = load i32, i32* %48
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %38
%52 = load i32, i32* %3
%53 = add nsw i32 %52, 1
store i32 %53, i32* %3
br label %35
54: ; preds = %35
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %72
56: ; preds = %0
store i32 0, i32* %3
br label %57
57: ; preds = %67, %56
%58 = load i32, i32* %3
%59 = icmp slt i32 %58, 100
br i1 %59, label %60, label %70
60: ; preds = %57
%61 = load i32*, i32** %4
%62 = load i32, i32* %3
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i32, i32* %61, i64 %63
%65 = load i32, i32* %64
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %60
%68 = load i32, i32* %3
%69 = add nsw i32 %68, 1
store i32 %69, i32* %3
br label %57
70: ; preds = %57
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %72
72: ; preds = %70, %54
%73 = load i32*, i32** %4
%74 = call i32 @munmap(i32* %73, i32 100)
%75 = load i64, i64* %5
%76 = icmp sgt i64 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %72
%78 = call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %79
79: ; preds = %77, %72
%80 = load i32, i32* @EXIT_SUCCESS
%81 = call i32 @exit(i32 %80)
unreachable
}

LLVM IR (-O2):
@.str = [14 x i8] c"/pipeautique1\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [31 x i8] c"shm_open returned %d (%d: %s)\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [27 x i8] c"mmap returned %p (%d: %s)\0A\00"
@.str.3 = [4 x i8] c"%d \00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = tail call i32 @time(i32* null)
%2 = tail call i32 @srand(i32 %1)
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = tail call i32 @shm_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5, i32 420)
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %7, i8* %8)
%10 = tail call i32 @ftruncate(i32 %6, i32 100)
%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 i64 @mmap(i32* null, i32 100, i32 %13, i32 %14, i32 %6, i32 0)
%16 = inttoptr i64 %15 to i32*
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32* %16, i32 %17, i8* %18)
%20 = tail call i64 (...) @fork()
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %0, %22
%23 = phi i64 [ %28, %22 ], [ 0, %0 ]
%24 = tail call i32 (...) @rand()
%25 = srem i32 %24, 100
%26 = getelementptr inbounds i32, i32* %16, i64 %23
store i32 %25, i32* %26
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%28 = add nuw nsw i64 %23, 1
%29 = icmp eq i64 %28, 100
br i1 %29, label %37, label %22
30: ; preds = %0, %30
%31 = phi i64 [ %35, %30 ], [ 0, %0 ]
%32 = getelementptr inbounds i32, i32* %16, i64 %31
%33 = load i32, i32* %32
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = add nuw nsw i64 %31, 1
%36 = icmp eq i64 %35, 100
br i1 %36, label %37, label %30
37: ; preds = %30, %22
%38 = tail call i32 @putchar(i32 10)
%39 = tail call i32 @munmap(i32* %16, i32 100)
br i1 %21, label %40, label %42
40: ; preds = %37
%41 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = load i32, i32* @EXIT_SUCCESS
%44 = tail call i32 @exit(i32 %43)
unreachable
}