#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_ANONYMOUS;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ mmap(int /*<<< orphan*/, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *, int *);
int /*<<< orphan*/ usleep(int);
scalar_t__ wait(int /*<<< orphan*/ *);
int main() {
  int size = 128;
  char *map = (char *)mmap(0, size, PROT_READ | PROT_WRITE,
                           MAP_ANONYMOUS | MAP_SHARED, -1, 0);
  if (map <= 0) {
    perror("mmap");
    return 1;
  }
  map[0] = '-';
  if (fork() == 0) {
    while (map[0] != 'Q') {
      printf("%c\n", map[0]);
      if (map[0] == 'C') {
        int a, b;
        sscanf(map + 1, "%d %d", &a, &b);
        sprintf(map + 1, "%d", a + b);
        map[0] = 'D';
        printf("%d + %d = %d\n", a, b, a + b);
      }
      usleep(1000);
    }
    exit(0);
  }
  for (int i = 0; i < 10; i++) {
    snprintf(map + 1, size, "%d %d", i, i + 1);
    map[0] = 'C';
    while (map[0] != 'D') {
      usleep(1000);
    }
    printf("got result\n");
  }
  map[0] = 'Q';
  while (wait(NULL) > 0)
    ;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x80,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    0x2dff(%rip),%rax
or     (%rax),%edx
mov    0x2e06(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0d(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
ja     1215 <main+0x75>
lea    0xdfc(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1368 <main+0x1c8>
mov    -0x10(%rbp),%rax
movb   $0x2d,(%rax)
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    12d4 <main+0x134>
jmp    1232 <main+0x92>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x51,%eax
je     12cd <main+0x12d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%esi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x43,%eax
jne    12be <main+0x11e>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
lea    0xd93(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1060 <sscanf@plt>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    -0x14(%rbp),%edx
add    -0x18(%rbp),%edx
lea    0xd74(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x10(%rbp),%rax
movb   $0x44,(%rax)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
add    -0x18(%rbp),%ecx
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3e8,%edi
call   10c0 <usleep@plt>
jmp    1232 <main+0x92>
xor    %edi,%edi
call   1090 <exit@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0xa,-0x1c(%rbp)
jge    1349 <main+0x1a9>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    -0x8(%rbp),%esi
mov    -0x1c(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
add    $0x1,%r8d
lea    0xd07(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rax
movb   $0x43,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x44,%eax
je     132d <main+0x18d>
mov    $0x3e8,%edi
call   10c0 <usleep@plt>
jmp    130e <main+0x16e>
lea    0xce9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12db <main+0x13b>
mov    -0x10(%rbp),%rax
movb   $0x51,(%rax)
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
cmp    $0x0,%rax
jle    1368 <main+0x1c8>
jmp    1350 <main+0x1b0>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e36(%rip),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e04(%rip),%rax
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x80,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
test   %rax,%rax
je     128f <main+0xdf>
mov    %rax,%rbx
movb   $0x2d,(%rax)
xor    %ebp,%ebp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     12a2 <main+0xf2>
mov    %rbx,%r14
add    $0x1,%r14
lea    0xde6(%rip),%r15
lea    0xdf3(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %ebp,%ecx
lea    0x1(%rcx),%ebp
mov    %r14,%rdi
mov    $0x80,%esi
mov    %r15,%rdx
mov    %ebp,%r8d
call   1060 <snprintf@plt>
movb   $0x43,(%rbx)
nopl   0x0(%rax,%rax,1)
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
cmpb   $0x44,(%rbx)
jne    1250 <main+0xa0>
mov    %r12,%rdi
call   1030 <puts@plt>
cmp    $0xa,%ebp
jne    1230 <main+0x80>
movb   $0x51,(%rbx)
nop
xor    %ebx,%ebx
xor    %edi,%edi
call   10b0 <wait@plt>
test   %rax,%rax
jg     1270 <main+0xc0>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    127e <main+0xce>
mov    (%rbx),%al
cmp    $0x51,%al
jne    12af <main+0xff>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    %rbx,%r14
add    $0x1,%r14
lea    0xd48(%rip),%r15
mov    %rsp,%rbp
lea    0xd45(%rip),%r12
lea    0xd41(%rip),%r13
jmp    12e1 <main+0x131>
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
movzbl (%rbx),%eax
cmp    $0x51,%al
je     12a8 <main+0xf8>
movsbl %al,%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmpb   $0x43,(%rbx)
jne    12d0 <main+0x120>
mov    %r14,%rdi
lea    0xd0c(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbp,%rcx
call   1070 <sscanf@plt>
mov    (%rsp),%edx
add    0x4(%rsp),%edx
mov    %r14,%rdi
mov    %r12,%rsi
call   1090 <sprintf@plt>
movb   $0x44,(%rbx)
mov    0x4(%rsp),%esi
mov    (%rsp),%edx
lea    (%rdx,%rsi,1),%ecx
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12d0 <main+0x120>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_SHARED = global i32 0
@.str = [5 x i8] c"mmap\00"
@.str.1 = [4 x i8] c"%c\0A\00"
@.str.2 = [6 x i8] c"%d %d\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [14 x i8] c"%d + %d = %d\0A\00"
@.str.5 = [12 x i8] c"got result\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 128, i32* %2
%7 = load i32, i32* %2
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %8, %9
%11 = load i32, i32* @MAP_ANONYMOUS
%12 = load i32, i32* @MAP_SHARED
%13 = or i32 %11, %12
%14 = call i64 @mmap(i32 0, i32 %7, i32 %10, i32 %13, i32 -1, i32 0)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %3
%16 = load i8*, i8** %3
%17 = icmp ule i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %99
20: ; preds = %0
%21 = load i8*, i8** %3
%22 = getelementptr inbounds i8, i8* %21, i64 0
store i8 45, i8* %22
%23 = call i64 (...) @fork()
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %65
25: ; preds = %20
br label %26
26: ; preds = %61, %25
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 81
br i1 %31, label %32, label %63
32: ; preds = %26
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = load i8*, i8** %3
%39 = getelementptr inbounds i8, i8* %38, i64 0
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 67
br i1 %42, label %43, label %61
43: ; preds = %32
%44 = load i8*, i8** %3
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = call i32 @sscanf(i8* %45, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %4, i32* %5)
%47 = load i8*, i8** %3
%48 = getelementptr inbounds i8, i8* %47, i64 1
%49 = load i32, i32* %4
%50 = load i32, i32* %5
%51 = add nsw i32 %49, %50
%52 = call i32 @sprintf(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %51)
%53 = load i8*, i8** %3
%54 = getelementptr inbounds i8, i8* %53, i64 0
store i8 68, i8* %54
%55 = load i32, i32* %4
%56 = load i32, i32* %5
%57 = load i32, i32* %4
%58 = load i32, i32* %5
%59 = add nsw i32 %57, %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %55, i32 %56, i32 %59)
br label %61
61: ; preds = %43, %32
%62 = call i32 @usleep(i32 1000)
br label %26
63: ; preds = %26
%64 = call i32 @exit(i32 0)
unreachable
65: ; preds = %20
store i32 0, i32* %6
br label %66
66: ; preds = %89, %65
%67 = load i32, i32* %6
%68 = icmp slt i32 %67, 10
br i1 %68, label %69, label %92
69: ; preds = %66
%70 = load i8*, i8** %3
%71 = getelementptr inbounds i8, i8* %70, i64 1
%72 = load i32, i32* %2
%73 = load i32, i32* %6
%74 = load i32, i32* %6
%75 = add nsw i32 %74, 1
%76 = call i32 @snprintf(i8* %71, i32 %72, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %73, i32 %75)
%77 = load i8*, i8** %3
%78 = getelementptr inbounds i8, i8* %77, i64 0
store i8 67, i8* %78
br label %79
79: ; preds = %85, %69
%80 = load i8*, i8** %3
%81 = getelementptr inbounds i8, i8* %80, i64 0
%82 = load i8, i8* %81
%83 = sext i8 %82 to i32
%84 = icmp ne i32 %83, 68
br i1 %84, label %85, label %87
85: ; preds = %79
%86 = call i32 @usleep(i32 1000)
br label %79
87: ; preds = %79
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %89
89: ; preds = %87
%90 = load i32, i32* %6
%91 = add nsw i32 %90, 1
store i32 %91, i32* %6
br label %66
92: ; preds = %66
%93 = load i8*, i8** %3
%94 = getelementptr inbounds i8, i8* %93, i64 0
store i8 81, i8* %94
br label %95
95: ; preds = %98, %92
%96 = call i64 @wait(i32* null)
%97 = icmp sgt i64 %96, 0
br i1 %97, label %98, label %99
98: ; preds = %95
br label %95
99: ; preds = %18, %95
%100 = load i32, i32* %1
ret i32 %100
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_SHARED = global i32 0
@.str = [5 x i8] c"mmap\00"
@.str.1 = [4 x i8] c"%c\0A\00"
@.str.2 = [6 x i8] c"%d %d\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [14 x i8] c"%d + %d = %d\0A\00"
@str = [11 x i8] c"got result\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %4, %3
%6 = load i32, i32* @MAP_ANONYMOUS
%7 = load i32, i32* @MAP_SHARED
%8 = or i32 %7, %6
%9 = tail call i64 @mmap(i32 0, i32 128, i32 %5, i32 %8, i32 -1, i32 0)
%10 = inttoptr i64 %9 to i8*
%11 = icmp eq i64 %9, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %63
14: ; preds = %0
store i8 45, i8* %10
%15 = tail call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = getelementptr inbounds i8, i8* %10, i64 1
br label %49
19: ; preds = %14
%20 = load i8, i8* %10
%21 = icmp eq i8 %20, 81
br i1 %21, label %46, label %22
22: ; preds = %19
%23 = bitcast i32* %1 to i8*
%24 = bitcast i32* %2 to i8*
%25 = getelementptr inbounds i8, i8* %10, i64 1
br label %26
26: ; preds = %22, %42
%27 = phi i8 [ %20, %22 ], [ %44, %42 ]
%28 = sext i8 %27 to i32
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = load i8, i8* %10
%31 = icmp eq i8 %30, 67
br i1 %31, label %32, label %42
32: ; preds = %26
%33 = call i32 @sscanf(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %1, i32* %2)
%34 = load i32, i32* %1
%35 = load i32, i32* %2
%36 = add nsw i32 %35, %34
%37 = call i32 @sprintf(i8* %25, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %36)
store i8 68, i8* %10
%38 = load i32, i32* %1
%39 = load i32, i32* %2
%40 = add nsw i32 %39, %38
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %38, i32 %39, i32 %40)
br label %42
42: ; preds = %32, %26
%43 = call i32 @usleep(i32 1000)
%44 = load i8, i8* %10
%45 = icmp eq i8 %44, 81
br i1 %45, label %46, label %26
46: ; preds = %42, %19
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %57
store i8 81, i8* %10
br label %60
49: ; preds = %17, %57
%50 = phi i32 [ 0, %17 ], [ %51, %57 ]
%51 = add nuw nsw i32 %50, 1
%52 = tail call i32 @snprintf(i8* %18, i32 128, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %50, i32 %51)
store i8 67, i8* %10
br label %53
53: ; preds = %49, %53
%54 = tail call i32 @usleep(i32 1000)
%55 = load i8, i8* %10
%56 = icmp eq i8 %55, 68
br i1 %56, label %57, label %53
57: ; preds = %53
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%59 = icmp eq i32 %51, 10
br i1 %59, label %48, label %49
60: ; preds = %60, %48
%61 = tail call i64 @wait(i32* null)
%62 = icmp sgt i64 %61, 0
br i1 %62, label %60, label %63
63: ; preds = %60, %12
%64 = phi i32 [ 1, %12 ], [ 0, %60 ]
ret i32 %64
}