#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 IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ shmdt(char *);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strncmp(char *, char *, int);
int main() {
  if (fork() == 0) {
    int id = shmget(0x8888, 1000, 0600 | IPC_CREAT);
    if (id == -1) {
      perror("shmget 0x8888");
      return 1;
    }
    char *p = shmat(id, NULL, 0);
    while (1) {
      if (*p != (char)0xf4)
        sleep(1);
      else {
        printf("get data: %s\n", p + 1);
        if (!strncmp(p + 1, "quit", 4))
          break;
        *p = 0x4f;
      }
    }
    *p = 0x4f;
    shmdt(p);
  } else {
    int id = shmget(0x8888, 1000, 0600 | IPC_CREAT);
    char *p = shmat(id, NULL, 0);
    *p = 0x4f;
    for (;;) {
      if (*p == 0x4f) {
        printf("input:");
        scanf(" %[^\n]", p + 1);
        *p = 0xf4;
        if (!strncmp(p + 1, "quit", 4))
          break;
      } else
        sleep(1);
    }
    while (*p != 0x4f)
      sleep(1);
    shmdt(p);
    shmctl(id, IPC_RMID, NULL);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%rax
jne    1292 <main+0xf2>
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0x8888,%edi
mov    $0x3e8,%esi
call   10a0 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1203 <main+0x63>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1382 <main+0x1e2>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xfffffff4,%eax
je     1234 <main+0x94>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1278 <main+0xd8>
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
lea    0xdc3(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1271 <main+0xd1>
jmp    127d <main+0xdd>
mov    -0x10(%rbp),%rax
movb   $0x4f,(%rax)
jmp    1215 <main+0x75>
mov    -0x10(%rbp),%rax
movb   $0x4f,(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <shmdt@plt>
jmp    1382 <main+0x1e2>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0x8888,%edi
mov    $0x3e8,%esi
call   10a0 <shmget@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x4f,(%rax)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x4f,%eax
jne    1331 <main+0x191>
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
lea    0xd2f(%rip),%rdi
call   1060 <scanf@plt>
mov    -0x20(%rbp),%rax
movb   $0xf4,(%rax)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
lea    0xd08(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    132c <main+0x18c>
jmp    1340 <main+0x1a0>
jmp    133b <main+0x19b>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    12cc <main+0x12c>
jmp    1345 <main+0x1a5>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x4f,%eax
je     1364 <main+0x1c4>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1345 <main+0x1a5>
mov    -0x20(%rbp),%rdi
call   1040 <shmdt@plt>
mov    -0x14(%rbp),%edi
mov    0x2c69(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <shmctl@plt>
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
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %rax,%rbx
mov    0x2e1c(%rip),%rax
mov    $0x180,%edx
or     (%rax),%edx
mov    $0x8888,%edi
mov    $0x3e8,%esi
call   10a0 <shmget@plt>
mov    %eax,%r14d
test   %rbx,%rbx
je     127e <main+0xde>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,%rbx
movb   $0x4f,(%rax)
mov    %rax,%rbp
add    $0x1,%rbp
mov    $0x4f,%al
lea    0xe21(%rip),%r15
lea    0xe21(%rip),%r12
lea    0xe0e(%rip),%r13
jmp    121d <main+0x7d>
mov    $0x1,%edi
call   10b0 <sleep@plt>
movzbl (%rbx),%eax
cmp    $0x4f,%al
jne    1210 <main+0x70>
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1060 <scanf@plt>
movb   $0xf4,(%rbx)
mov    %rbp,%rdi
mov    %r13,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    121a <main+0x7a>
jmp    125a <main+0xba>
nop
mov    $0x1,%edi
call   10b0 <sleep@plt>
cmpb   $0x4f,(%rbx)
jne    1250 <main+0xb0>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
mov    %r14d,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
jmp    12ed <main+0x14d>
cmp    $0xffffffff,%r14d
je     12fe <main+0x15e>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,%rbx
mov    %rax,%rbp
add    $0x1,%rbp
lea    0xd6d(%rip),%r14
lea    0xd74(%rip),%r15
jmp    12ba <main+0x11a>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10b0 <sleep@plt>
cmpb   $0xf4,(%rbx)
jne    12b0 <main+0x110>
mov    %r14,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
movb   $0x4f,(%rbx)
test   %eax,%eax
jne    12ba <main+0x11a>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcfb(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    12ed <main+0x14d>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [14 x i8] c"shmget 0x8888\00"
@.str.1 = [14 x i8] c"get data: %s\0A\00"
@.str.2 = [5 x i8] c"quit\00"
@.str.3 = [7 x i8] c"input:\00"
@.str.4 = [7 x i8] c" %[^\0A]\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i32 0, i32* %1
%6 = call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %42
8: ; preds = %0
%9 = load i32, i32* @IPC_CREAT
%10 = or i32 384, %9
%11 = call i32 @shmget(i32 34952, i32 1000, i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %83
16: ; preds = %8
%17 = load i32, i32* %2
%18 = call i8* @shmat(i32 %17, i32* null, i32 0)
store i8* %18, i8** %3
br label %19
19: ; preds = %16, %37
%20 = load i8*, i8** %3
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, -12
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @sleep(i32 1)
br label %37
26: ; preds = %19
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = call i32 @strncmp(i8* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%33 = icmp ne i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %26
br label %38
35: ; preds = %26
%36 = load i8*, i8** %3
store i8 79, i8* %36
br label %37
37: ; preds = %35, %24
br label %19
38: ; preds = %34
%39 = load i8*, i8** %3
store i8 79, i8* %39
%40 = load i8*, i8** %3
%41 = call i32 @shmdt(i8* %40)
br label %83
42: ; preds = %0
%43 = load i32, i32* @IPC_CREAT
%44 = or i32 384, %43
%45 = call i32 @shmget(i32 34952, i32 1000, i32 %44)
store i32 %45, i32* %4
%46 = load i32, i32* %4
%47 = call i8* @shmat(i32 %46, i32* null, i32 0)
store i8* %47, i8** %5
%48 = load i8*, i8** %5
store i8 79, i8* %48
br label %49
49: ; preds = %68, %42
%50 = load i8*, i8** %5
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 79
br i1 %53, label %54, label %66
54: ; preds = %49
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%56 = load i8*, i8** %5
%57 = getelementptr inbounds i8, i8* %56, i64 1
%58 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %57)
%59 = load i8*, i8** %5
store i8 -12, i8* %59
%60 = load i8*, i8** %5
%61 = getelementptr inbounds i8, i8* %60, i64 1
%62 = call i32 @strncmp(i8* %61, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%63 = icmp ne i32 %62, 0
br i1 %63, label %65, label %64
64: ; preds = %54
br label %69
65: ; preds = %54
br label %68
66: ; preds = %49
%67 = call i32 @sleep(i32 1)
br label %68
68: ; preds = %66, %65
br label %49
69: ; preds = %64
br label %70
70: ; preds = %75, %69
%71 = load i8*, i8** %5
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = icmp ne i32 %73, 79
br i1 %74, label %75, label %77
75: ; preds = %70
%76 = call i32 @sleep(i32 1)
br label %70
77: ; preds = %70
%78 = load i8*, i8** %5
%79 = call i32 @shmdt(i8* %78)
%80 = load i32, i32* %4
%81 = load i32, i32* @IPC_RMID
%82 = call i32 @shmctl(i32 %80, i32 %81, i32* null)
br label %83
83: ; preds = %14, %77, %38
%84 = load i32, i32* %1
ret i32 %84
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [14 x i8] c"shmget 0x8888\00"
@.str.1 = [14 x i8] c"get data: %s\0A\00"
@.str.2 = [5 x i8] c"quit\00"
@.str.3 = [7 x i8] c"input:\00"
@.str.4 = [7 x i8] c" %[^\0A]\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
%3 = load i32, i32* @IPC_CREAT
%4 = or i32 %3, 384
%5 = tail call i32 @shmget(i32 34952, i32 1000, i32 %4)
br i1 %2, label %6, label %25
6: ; preds = %0
%7 = icmp eq i32 %5, -1
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %51
10: ; preds = %6
%11 = tail call i8* @shmat(i32 %5, i32* null, i32 0)
%12 = getelementptr inbounds i8, i8* %11, i64 1
br label %13
13: ; preds = %22, %10
%14 = load i8, i8* %11
%15 = icmp eq i8 %14, -12
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @sleep(i32 1)
br label %22
18: ; preds = %13
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%20 = tail call i32 @strncmp(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%21 = icmp eq i32 %20, 0
store i8 79, i8* %11
br i1 %21, label %23, label %22
22: ; preds = %18, %16
br label %13
23: ; preds = %18
%24 = tail call i32 @shmdt(i8* %11)
br label %51
25: ; preds = %0
%26 = tail call i8* @shmat(i32 %5, i32* null, i32 0)
store i8 79, i8* %26
%27 = getelementptr inbounds i8, i8* %26, i64 1
br label %28
28: ; preds = %41, %25
%29 = phi i8 [ %42, %41 ], [ 79, %25 ]
%30 = icmp eq i8 %29, 79
br i1 %30, label %31, label %39
31: ; preds = %28
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%33 = tail call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %27)
store i8 -12, i8* %26
%34 = tail call i32 @strncmp(i8* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %31
%37 = load i8, i8* %26
%38 = icmp eq i8 %37, 79
br i1 %38, label %47, label %43
39: ; preds = %28
%40 = tail call i32 @sleep(i32 1)
br label %41
41: ; preds = %31, %39
%42 = load i8, i8* %26
br label %28
43: ; preds = %36, %43
%44 = tail call i32 @sleep(i32 1)
%45 = load i8, i8* %26
%46 = icmp eq i8 %45, 79
br i1 %46, label %47, label %43
47: ; preds = %43, %36
%48 = tail call i32 @shmdt(i8* %26)
%49 = load i32, i32* @IPC_RMID
%50 = tail call i32 @shmctl(i32 %5, i32 %49, i32* null)
br label %51
51: ; preds = %8, %23, %47
%52 = phi i32 [ 0, %47 ], [ 1, %8 ], [ 0, %23 ]
ret i32 %52
}