#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 */
typedef struct shared_page TYPE_1__;

/* Type definitions */
typedef scalar_t__ pid_t;
struct shared_page {
  char *parentbuf;
  char *childbuf;
};

/* Variables and functions */
int MAP_ANON;
TYPE_1__ *MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ arc4random_buf(char *, int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ memcmp(char *, char *, int);
TYPE_1__ *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  struct shared_page {
    char parentbuf[8];
    char childbuf[8];
  } * page;
  pid_t pid;
  char c;
  printf("1..1\n");
  page = mmap(NULL, sizeof(struct shared_page), PROT_READ | PROT_WRITE,
              MAP_ANON | MAP_SHARED, -1, 0);
  if (page == MAP_FAILED) {
    printf("fail 1 - mmap\n");
    exit(1);
  }
  arc4random_buf(&c, 1);
  pid = fork();
  if (pid < 0) {
    printf("fail 1 - fork\n");
    exit(1);
  }
  if (pid == 0) {
    arc4random_buf(page->childbuf, 8);
    exit(0);
  } else {
    int status;
    arc4random_buf(page->parentbuf, 8);
    wait(&status);
  }
  if (memcmp(page->parentbuf, page->childbuf, 8) == 0) {
    printf("fail 1 - sequences are the same\n");
    exit(1);
  }
  printf("ok 1 - sequences are different\n");
  exit(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)
lea    0xe73(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
mov    0x2e2e(%rip),%rax
or     (%rax),%edx
mov    0x2e1d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e34(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x10,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2de4(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jne    11fe <main+0x8e>
lea    0xe17(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x21(%rbp),%rdi
mov    $0x1,%esi
call   1030 <arc4random_buf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1238 <main+0xc8>
lea    0xdec(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    125c <main+0xec>
mov    -0x18(%rbp),%rdi
add    $0x8,%rdi
mov    $0x8,%esi
call   1030 <arc4random_buf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   1030 <arc4random_buf@plt>
lea    -0x28(%rbp),%rdi
call   1080 <wait@plt>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x8,%rsi
mov    $0x8,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
jne    12a9 <main+0x139>
lea    0xd8a(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xd95(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe84(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e70(%rip),%rax
mov    0x2e41(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2e(%rip),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x10,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2dfc(%rip),%rax
cmp    %rbx,(%rax)
jne    11d2 <main+0x62>
lea    0xe82(%rip),%rdi
jmp    1247 <main+0xd7>
lea    0xb(%rsp),%rdi
mov    $0x1,%esi
call   1040 <arc4random_buf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jns    11f6 <main+0x86>
lea    0xe50(%rip),%rdi
jmp    1247 <main+0xd7>
jne    1210 <main+0xa0>
add    $0x8,%rbx
mov    %rbx,%rdi
mov    $0x8,%esi
call   1040 <arc4random_buf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1040 <arc4random_buf@plt>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
mov    %rbx,%rsi
add    $0x8,%rsi
mov    %rbx,%rdi
mov    $0x8,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
jne    1256 <main+0xe6>
lea    0xddd(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xda8(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.shared_page.0 = type { i8*, i8* }
%struct.shared_page = type { [8 x i8], [8 x i8] }
@.str = [6 x i8] c"1..1\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global %struct.shared_page.0* null
@.str.1 = [15 x i8] c"fail 1 - mmap\0A\00"
@.str.2 = [15 x i8] c"fail 1 - fork\0A\00"
@.str.3 = [33 x i8] c"fail 1 - sequences are the same\0A\00"
@.str.4 = [32 x i8] c"ok 1 - sequences are different\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.shared_page*
%7 = alloca i64
%8 = alloca i8
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %11, %12
%14 = load i32, i32* @MAP_ANON
%15 = load i32, i32* @MAP_SHARED
%16 = or i32 %14, %15
%17 = call %struct.shared_page.0* @mmap(i32* null, i32 16, i32 %13, i32 %16, i32 -1, i32 0)
%18 = bitcast %struct.shared_page.0* %17 to %struct.shared_page*
store %struct.shared_page* %18, %struct.shared_page** %6
%19 = load %struct.shared_page*, %struct.shared_page** %6
%20 = load %struct.shared_page.0*, %struct.shared_page.0** @MAP_FAILED
%21 = bitcast %struct.shared_page.0* %20 to %struct.shared_page*
%22 = icmp eq %struct.shared_page* %19, %21
br i1 %22, label %23, label %26
23: ; preds = %2
%24 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = call i32 @arc4random_buf(i8* %8, i32 1)
%28 = call i64 (...) @fork()
store i64 %28, i64* %7
%29 = load i64, i64* %7
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = load i64, i64* %7
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = load %struct.shared_page*, %struct.shared_page** %6
%39 = getelementptr inbounds %struct.shared_page, %struct.shared_page* %38, i32 0, i32 1
%40 = getelementptr inbounds [8 x i8], [8 x i8]* %39, i64 0, i64 0
%41 = call i32 @arc4random_buf(i8* %40, i32 8)
%42 = call i32 @exit(i32 0)
unreachable
43: ; preds = %34
%44 = load %struct.shared_page*, %struct.shared_page** %6
%45 = getelementptr inbounds %struct.shared_page, %struct.shared_page* %44, i32 0, i32 0
%46 = getelementptr inbounds [8 x i8], [8 x i8]* %45, i64 0, i64 0
%47 = call i32 @arc4random_buf(i8* %46, i32 8)
%48 = call i32 @wait(i32* %9)
br label %49
49: ; preds = %43
%50 = load %struct.shared_page*, %struct.shared_page** %6
%51 = getelementptr inbounds %struct.shared_page, %struct.shared_page* %50, i32 0, i32 0
%52 = getelementptr inbounds [8 x i8], [8 x i8]* %51, i64 0, i64 0
%53 = load %struct.shared_page*, %struct.shared_page** %6
%54 = getelementptr inbounds %struct.shared_page, %struct.shared_page* %53, i32 0, i32 1
%55 = getelementptr inbounds [8 x i8], [8 x i8]* %54, i64 0, i64 0
%56 = call i64 @memcmp(i8* %52, i8* %55, i32 8)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %49
%59 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %49
%62 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%63 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.shared_page.0 = type { i8*, i8* }
%struct.shared_page = type { [8 x i8], [8 x i8] }
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global %struct.shared_page.0* null
@str = [5 x i8] c"1..1\00"
@str.5 = [31 x i8] c"ok 1 - sequences are different\00"
@str.6 = [32 x i8] c"fail 1 - sequences are the same\00"
@str.7 = [14 x i8] c"fail 1 - fork\00"
@str.8 = [14 x i8] c"fail 1 - mmap\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = alloca i32
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @PROT_WRITE
%8 = or i32 %7, %6
%9 = load i32, i32* @MAP_ANON
%10 = load i32, i32* @MAP_SHARED
%11 = or i32 %10, %9
%12 = tail call %struct.shared_page.0* @mmap(i32* null, i32 16, i32 %8, i32 %11, i32 -1, i32 0)
%13 = bitcast %struct.shared_page.0* %12 to %struct.shared_page*
%14 = load %struct.shared_page*, %struct.shared_page** bitcast (%struct.shared_page.0** @MAP_FAILED to %struct.shared_page**)
%15 = icmp eq %struct.shared_page* %14, %13
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i32 @arc4random_buf(i8* %3, i32 1)
%21 = call i64 (...) @fork()
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = icmp eq i64 %21, 0
br i1 %27, label %28, label %33
28: ; preds = %26
%29 = getelementptr inbounds %struct.shared_page.0, %struct.shared_page.0* %12, i64 0, i32 1
%30 = bitcast i8** %29 to i8*
%31 = call i32 @arc4random_buf(i8* %30, i32 8)
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %26
%34 = bitcast i32* %4 to i8*
%35 = bitcast %struct.shared_page.0* %12 to i8*
%36 = call i32 @arc4random_buf(i8* %35, i32 8)
%37 = call i32 @wait(i32* %4)
%38 = getelementptr inbounds %struct.shared_page.0, %struct.shared_page.0* %12, i64 0, i32 1
%39 = bitcast i8** %38 to i8*
%40 = call i64 @memcmp(i8* %35, i8* %39, i32 8)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %33
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.6, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %33
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.5, i64 0, i64 0))
%47 = call i32 @exit(i32 0)
unreachable
}