#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_PRIVATE;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(int *);
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ wait_on_futex_value(int *, int);
int /*<<< orphan*/ wake_futex_blocking(int *);
int main(int argc, char **argv) {
  int shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
  if (shm_id < 0) {
    perror("shmget");
    exit(1);
  }
  int *shared_data = shmat(shm_id, NULL, 0);
  *shared_data = 0;
  int forkstatus = fork();
  if (forkstatus < 0) {
    perror("fork");
    exit(1);
  }
  if (forkstatus == 0) {
    printf("child waiting for A\n");
    wait_on_futex_value(shared_data, 0xA);
    printf("child writing B\n");
    *shared_data = 0xB;
    wake_futex_blocking(shared_data);
  } else {
    printf("parent writing A\n");
    *shared_data = 0xA;
    wake_futex_blocking(shared_data);
    printf("parent waiting for B\n");
    wait_on_futex_value(shared_data, 0xB);
    wait(NULL);
    shmdt(shared_data);
  }
  return 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)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x1000,%esi
call   1070 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fb <main+0x5b>
lea    0xe14(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1241 <main+0xa1>
lea    0xdd5(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jne    1289 <main+0xe9>
lea    0xdba(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1090 <wait_on_futex_value@plt>
lea    0xdb5(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movl   $0xb,(%rax)
mov    -0x20(%rbp),%rdi
call   10a0 <wake_futex_blocking@plt>
jmp    12d4 <main+0x134>
lea    0xda2(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movl   $0xa,(%rax)
mov    -0x20(%rbp),%rdi
call   10a0 <wake_futex_blocking@plt>
lea    0xd95(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0xb,%esi
call   1090 <wait_on_futex_value@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
mov    -0x20(%rbp),%rdi
call   1030 <shmdt@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    0x2e27(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x1000,%esi
call   1070 <shmget@plt>
test   %eax,%eax
js     126a <main+0xca>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,%rbx
movl   $0x0,(%rax)
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     1273 <main+0xd3>
je     1233 <main+0x93>
lea    0xe16(%rip),%rdi
call   1030 <puts@plt>
movl   $0xa,(%rbx)
mov    %rbx,%rdi
call   10a0 <wake_futex_blocking@plt>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0xb,%esi
call   1090 <wait_on_futex_value@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
jmp    1266 <main+0xc6>
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1090 <wait_on_futex_value@plt>
lea    0xdf3(%rip),%rdi
call   1030 <puts@plt>
movl   $0xb,(%rbx)
mov    %rbx,%rdi
call   10a0 <wake_futex_blocking@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xd8f(%rip),%rdi
jmp    127a <main+0xda>
lea    0xd8d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [21 x i8] c"child waiting for A\0A\00"
@.str.3 = [17 x i8] c"child writing B\0A\00"
@.str.4 = [18 x i8] c"parent writing A\0A\00"
@.str.5 = [22 x i8] c"parent waiting for B\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @IPC_PRIVATE
%10 = load i32, i32* @IPC_CREAT
%11 = or i32 %10, 438
%12 = call i32 @shmget(i32 %9, i32 4096, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %6
%20 = call i32* @shmat(i32 %19, i32* null, i32 0)
store i32* %20, i32** %7
%21 = load i32*, i32** %7
store i32 0, i32* %21
%22 = call i32 (...) @fork()
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %18
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %39
31: ; preds = %28
%32 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32*, i32** %7
%34 = call i32 @wait_on_futex_value(i32* %33, i32 10)
%35 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32*, i32** %7
store i32 11, i32* %36
%37 = load i32*, i32** %7
%38 = call i32 @wake_futex_blocking(i32* %37)
br label %50
39: ; preds = %28
%40 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%41 = load i32*, i32** %7
store i32 10, i32* %41
%42 = load i32*, i32** %7
%43 = call i32 @wake_futex_blocking(i32* %42)
%44 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%45 = load i32*, i32** %7
%46 = call i32 @wait_on_futex_value(i32* %45, i32 11)
%47 = call i32 @wait(i32* null)
%48 = load i32*, i32** %7
%49 = call i32 @shmdt(i32* %48)
br label %50
50: ; preds = %39, %31
ret i32 0
}

LLVM IR (-O2):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [5 x i8] c"fork\00"
@str = [17 x i8] c"parent writing A\00"
@str.6 = [21 x i8] c"parent waiting for B\00"
@str.7 = [20 x i8] c"child waiting for A\00"
@str.8 = [16 x i8] c"child writing B\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @IPC_PRIVATE
%4 = load i32, i32* @IPC_CREAT
%5 = or i32 %4, 438
%6 = tail call i32 @shmget(i32 %3, i32 4096, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = tail call i32* @shmat(i32 %6, i32* null, i32 0)
store i32 0, i32* %12
%13 = tail call i32 (...) @fork()
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = icmp eq i32 %13, 0
br i1 %19, label %20, label %25
20: ; preds = %18
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
%22 = tail call i32 @wait_on_futex_value(i32* %12, i32 10)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
store i32 11, i32* %12
%24 = tail call i32 @wake_futex_blocking(i32* %12)
br label %32
25: ; preds = %18
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
store i32 10, i32* %12
%27 = tail call i32 @wake_futex_blocking(i32* %12)
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.6, i64 0, i64 0))
%29 = tail call i32 @wait_on_futex_value(i32* %12, i32 11)
%30 = tail call i32 @wait(i32* null)
%31 = tail call i32 @shmdt(i32* %12)
br label %32
32: ; preds = %25, %20
ret i32 0
}