#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);
scalar_t__ fork();
int getpid();
int isprimer(int);
int *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int /*<<< orphan*/,
          int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int *p = mmap(0, 4, PROT_READ | PROT_WRITE, MAP_SHARED, 0, 0);
  int id = 1;
  int a, b;
  while (1) {
    if (id == 1) {
      a = 1;
      b = 5000;
    }
    if (id == 2) {
      a = 5000;
      b = 10000;
    }
    if (fork()) {
      if (++id > 2) {
        int fd = open("primer.dat", O_RDWR | O_CREAT, 0666);
        if (fd == -1) {
          printf("open error:%m\n");
          exit(-1);
        }
        int *q = p;
        while (q != NULL) {
          printf("%d\n", *q++);
        }
        close(fd);
      }
    } else {
      int i;
      int r;
      int *q = p;
      for (i = a; i < b; i++) {
        r = isprimer(i);
        if (r == 1) {
          *q++ = i;
        }
      }
      printf("pid:%d task complete!\n", getpid());
      exit(0);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
mov    0x2e21(%rip),%rax
or     (%rax),%edx
mov    0x2e38(%rip),%rax
mov    (%rax),%ecx
xor    %r9d,%r9d
mov    $0x4,%esi
mov    %r9d,%edi
mov    %r9d,%r8d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    11e0 <main+0x60>
movl   $0x1,-0x18(%rbp)
movl   $0x1388,-0x1c(%rbp)
cmpl   $0x2,-0x14(%rbp)
jne    11f8 <main+0x78>
movl   $0x1388,-0x18(%rbp)
movl   $0x2710,-0x1c(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
je     12a7 <main+0x127>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x2,%eax
jle    12a2 <main+0x122>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
or     (%rax),%esi
lea    0xdcc(%rip),%rdi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1263 <main+0xe3>
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     129a <main+0x11a>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%esi
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    126b <main+0xeb>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
jmp    131b <main+0x19b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12fd <main+0x17d>
mov    -0x2c(%rbp),%edi
call   1070 <isprimer@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x1,-0x30(%rbp)
jne    12ea <main+0x16a>
mov    -0x2c(%rbp),%ecx
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x38(%rbp)
mov    %ecx,(%rax)
jmp    12ef <main+0x16f>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12b5 <main+0x135>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xd11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    11c8 <main+0x48>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e5e(%rip),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e3c(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     129d <main+0x11d>
mov    $0x1388,%r12d
mov    $0x1,%ebp
mov    $0x2710,%r13d
mov    $0x1388,%r15d
mov    $0x1,%r14d
jmp    1213 <main+0x93>
nopw   0x0(%rax,%rax,1)
mov    %eax,%edi
call   1060 <close@plt>
cmp    $0x1,%r14d
cmove  %r13d,%r15d
cmove  %r12d,%ebp
xor    %eax,%eax
call   10a0 <fork@plt>
add    $0x1,%r14d
test   %rax,%rax
je     1262 <main+0xe2>
cmp    $0x2,%r14d
jb     11f7 <main+0x77>
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    0x2da7(%rip),%rax
or     (%rax),%esi
lea    0xdce(%rip),%rdi
mov    $0x1b6,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1285 <main+0x105>
test   %rbx,%rbx
je     11f0 <main+0x70>
lea    0xdcd(%rip),%rbp
nopl   (%rax)
mov    (%rbx),%esi
add    $0x4,%rbx
mov    %rbp,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1250 <main+0xd0>
cmp    %r15d,%ebp
jl     12b8 <main+0x138>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xda9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xd7f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    $0x1388,%r15d
mov    $0x1,%ebp
jmp    12b8 <main+0x138>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    %ebp,%r15d
je     1267 <main+0xe7>
mov    %ebp,%edi
call   1070 <isprimer@plt>
cmp    $0x1,%eax
jne    12b0 <main+0x130>
mov    %ebp,(%rbx)
add    $0x4,%rbx
jmp    12b0 <main+0x130>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str = [11 x i8] c"primer.dat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [15 x i8] c"open error:%m\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [23 x i8] c"pid:%d task complete!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i32 0, i32* %1
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %11, %12
%14 = load i32, i32* @MAP_SHARED
%15 = call i32* @mmap(i32 0, i32 4, i32 %13, i32 %14, i32 0, i32 0)
store i32* %15, i32** %2
store i32 1, i32* %3
br label %16
16: ; preds = %0, %79
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 1, i32* %4
store i32 5000, i32* %5
br label %20
20: ; preds = %19, %16
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, 2
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 5000, i32* %4
store i32 10000, i32* %5
br label %24
24: ; preds = %23, %20
%25 = call i64 (...) @fork()
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %55
27: ; preds = %24
%28 = load i32, i32* %3
%29 = add nsw i32 %28, 1
store i32 %29, i32* %3
%30 = icmp sgt i32 %29, 2
br i1 %30, label %31, label %54
31: ; preds = %27
%32 = load i32, i32* @O_RDWR
%33 = load i32, i32* @O_CREAT
%34 = or i32 %32, %33
%35 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %34, i32 438)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %31
%42 = load i32*, i32** %2
store i32* %42, i32** %7
br label %43
43: ; preds = %46, %41
%44 = load i32*, i32** %7
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %51
46: ; preds = %43
%47 = load i32*, i32** %7
%48 = getelementptr inbounds i32, i32* %47, i32 1
store i32* %48, i32** %7
%49 = load i32, i32* %47
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %49)
br label %43
51: ; preds = %43
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %51, %27
br label %79
55: ; preds = %24
%56 = load i32*, i32** %2
store i32* %56, i32** %10
%57 = load i32, i32* %4
store i32 %57, i32* %8
br label %58
58: ; preds = %72, %55
%59 = load i32, i32* %8
%60 = load i32, i32* %5
%61 = icmp slt i32 %59, %60
br i1 %61, label %62, label %75
62: ; preds = %58
%63 = load i32, i32* %8
%64 = call i32 @isprimer(i32 %63)
store i32 %64, i32* %9
%65 = load i32, i32* %9
%66 = icmp eq i32 %65, 1
br i1 %66, label %67, label %71
67: ; preds = %62
%68 = load i32, i32* %8
%69 = load i32*, i32** %10
%70 = getelementptr inbounds i32, i32* %69, i32 1
store i32* %70, i32** %10
store i32 %68, i32* %69
br label %71
71: ; preds = %67, %62
br label %72
72: ; preds = %71
%73 = load i32, i32* %8
%74 = add nsw i32 %73, 1
store i32 %74, i32* %8
br label %58
75: ; preds = %58
%76 = call i32 (...) @getpid()
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %76)
%78 = call i32 @exit(i32 0)
unreachable
79: ; preds = %54
br label %16
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str = [11 x i8] c"primer.dat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [15 x i8] c"open error:%m\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [23 x i8] c"pid:%d task complete!\0A\00"
define i32 @main() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @PROT_WRITE
%3 = or i32 %2, %1
%4 = load i32, i32* @MAP_SHARED
%5 = tail call i32* @mmap(i32 0, i32 4, i32 %3, i32 %4, i32 0, i32 0)
%6 = tail call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = icmp eq i32* %5, null
br label %15
10: ; preds = %53
%11 = icmp slt i32 %56, %55
br i1 %11, label %12, label %49
12: ; preds = %0, %10
%13 = phi i32 [ %56, %10 ], [ 1, %0 ]
%14 = phi i32 [ %55, %10 ], [ 5000, %0 ]
br label %38
15: ; preds = %8, %53
%16 = phi i32 [ 1, %8 ], [ %56, %53 ]
%17 = phi i32 [ 5000, %8 ], [ %55, %53 ]
%18 = phi i32 [ 1, %8 ], [ %19, %53 ]
%19 = add nuw nsw i32 %18, 1
%20 = icmp ugt i32 %18, 1
br i1 %20, label %21, label %53
21: ; preds = %15
%22 = load i32, i32* @O_RDWR
%23 = load i32, i32* @O_CREAT
%24 = or i32 %23, %22
%25 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %24, i32 438)
%26 = icmp eq i32 %25, -1
br i1 %26, label %28, label %27
27: ; preds = %21
br i1 %9, label %36, label %31
28: ; preds = %21
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%30 = tail call i32 @exit(i32 -1)
unreachable
31: ; preds = %27, %31
%32 = phi i32* [ %33, %31 ], [ %5, %27 ]
%33 = getelementptr inbounds i32, i32* %32, i64 1
%34 = load i32, i32* %32
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %34)
br label %31
36: ; preds = %27
%37 = tail call i32 @close(i32 %25)
br label %53
38: ; preds = %12, %45
%39 = phi i32* [ %46, %45 ], [ %5, %12 ]
%40 = phi i32 [ %47, %45 ], [ %13, %12 ]
%41 = tail call i32 @isprimer(i32 %40)
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = getelementptr inbounds i32, i32* %39, i64 1
store i32 %40, i32* %39
br label %45
45: ; preds = %38, %43
%46 = phi i32* [ %44, %43 ], [ %39, %38 ]
%47 = add i32 %40, 1
%48 = icmp eq i32 %47, %14
br i1 %48, label %49, label %38
49: ; preds = %45, %10
%50 = tail call i32 (...) @getpid()
%51 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %50)
%52 = tail call i32 @exit(i32 0)
unreachable
53: ; preds = %15, %36
%54 = icmp eq i32 %19, 2
%55 = select i1 %54, i32 10000, i32 %17
%56 = select i1 %54, i32 5000, i32 %16
%57 = tail call i64 (...) @fork()
%58 = icmp eq i64 %57, 0
br i1 %58, label %10, label %15
}