#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 */
typedef int /*<<< orphan*/ sem_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
scalar_t__ fork();
int /*<<< orphan*/ ftruncate(int, int);
int /*<<< orphan*/ memcpy(void *, int *, int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ reader();
int /*<<< orphan*/ *sem_open(char *, int, int, int);
int /*<<< orphan*/ sem_unlink(char *);
int shm_open(char *, int, int);
int /*<<< orphan*/ shm_unlink(char *);
int wait(int *);
int /*<<< orphan*/ writer();
int main(int argc, char *argv[]) {
  sem_unlink("/read_bin_sem");
  sem_t *read_bin_sem =
      sem_open("/read_bin_sem", O_CREAT | O_EXCL | O_RDWR, 0664, 1);
  if (read_bin_sem == SEM_FAILED) {
    die("sem_open()");
  }
  sem_unlink("/rw_bin_sem");
  sem_t *rw_bin_sem =
      sem_open("/rw_bin_sem", O_CREAT | O_EXCL | O_RDWR, 0664, 1);
  if (rw_bin_sem == SEM_FAILED) {
    die("sem_open()");
  }
  int shm_fd;
  int zero = 0;
  void *addr = NULL;
  shm_unlink("/buffer");
  if ((shm_fd = shm_open("/buffer", O_RDWR | O_CREAT | O_EXCL, 0664)) < 0) {
    die("shm_open()");
  }
  ftruncate(shm_fd, sizeof(int));
  if ((addr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED,
                   shm_fd, 0)) == MAP_FAILED) {
    die("mmap()");
  }
  memcpy(addr, &zero, sizeof(int));
  munmap(addr, sizeof(int));
  close(shm_fd);
  shm_unlink("/reader_counter");
  if ((shm_fd = shm_open("/reader_counter", O_RDWR | O_CREAT | O_EXCL, 0664)) <
      0) {
    die("shm_open()");
  }
  ftruncate(shm_fd, sizeof(int));
  if ((addr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED,
                   shm_fd, 0)) == MAP_FAILED) {
    die("mmap()");
  }
  memcpy(addr, &zero, sizeof(int));
  munmap(addr, sizeof(int));
  if (fork() == 0) {
    writer();
  }
  int i = 0;
  for (i = 0; i < 10; i++) {
    if (fork() == 0) {
      reader();
    }
  }
  for (i = 0; i < 11; i++) {
    int pid = 0;
    int status = 0;
    pid = wait(&status);
    printf("process %d is finished\n", pid);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xdf3(%rip),%rdi
call   1100 <sem_unlink@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2d96(%rip),%rax
or     (%rax),%esi
mov    0x2da5(%rip),%rax
or     (%rax),%esi
lea    0xdcc(%rip),%rdi
mov    $0x1b4,%edx
mov    $0x1,%ecx
call   1080 <sem_open@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d76(%rip),%rcx
cmp    (%rcx),%rax
jne    1267 <main+0x77>
lea    0xdac(%rip),%rdi
call   10c0 <die@plt>
lea    0xdab(%rip),%rdi
call   1100 <sem_unlink@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    0x2d35(%rip),%rax
or     (%rax),%esi
mov    0x2d44(%rip),%rax
or     (%rax),%esi
lea    0xd84(%rip),%rdi
mov    $0x1b4,%edx
mov    $0x1,%ecx
call   1080 <sem_open@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d15(%rip),%rcx
cmp    (%rcx),%rax
jne    12c8 <main+0xd8>
lea    0xd4b(%rip),%rdi
call   10c0 <die@plt>
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xd47(%rip),%rdi
call   10e0 <shm_unlink@plt>
mov    0x2ce6(%rip),%rax
mov    (%rax),%esi
mov    0x2d05(%rip),%rax
or     (%rax),%esi
mov    0x2cbc(%rip),%rax
or     (%rax),%esi
lea    0xd20(%rip),%rdi
mov    $0x1b4,%edx
call   10d0 <shm_open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1327 <main+0x137>
lea    0xd0b(%rip),%rdi
call   10c0 <die@plt>
mov    -0x24(%rbp),%edi
mov    $0x4,%esi
call   1060 <ftruncate@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edx
mov    0x2c6c(%rip),%rax
or     (%rax),%edx
mov    0x2c93(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    0x2c39(%rip),%rcx
cmp    (%rcx),%rax
jne    1384 <main+0x194>
lea    0xcb9(%rip),%rdi
call   10c0 <die@plt>
mov    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    $0x4,%esi
call   10a0 <munmap@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
lea    0xc8c(%rip),%rdi
call   10e0 <shm_unlink@plt>
mov    0x2c11(%rip),%rax
mov    (%rax),%esi
mov    0x2c30(%rip),%rax
or     (%rax),%esi
mov    0x2be7(%rip),%rax
or     (%rax),%esi
lea    0xc65(%rip),%rdi
mov    $0x1b4,%edx
call   10d0 <shm_open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    13fc <main+0x20c>
lea    0xc36(%rip),%rdi
call   10c0 <die@plt>
mov    -0x24(%rbp),%edi
mov    $0x4,%esi
call   1060 <ftruncate@plt>
mov    0x2be0(%rip),%rax
mov    (%rax),%edx
mov    0x2b97(%rip),%rax
or     (%rax),%edx
mov    0x2bbe(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    0x2b64(%rip),%rcx
cmp    (%rcx),%rax
jne    1459 <main+0x269>
lea    0xbe4(%rip),%rdi
call   10c0 <die@plt>
mov    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    $0x4,%esi
call   10a0 <munmap@plt>
mov    $0x0,%al
call   1110 <fork@plt>
cmp    $0x0,%rax
jne    1491 <main+0x2a1>
mov    $0x0,%al
call   10b0 <writer@plt>
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0xa,-0x34(%rbp)
jge    14d4 <main+0x2e4>
mov    $0x0,%al
call   1110 <fork@plt>
cmp    $0x0,%rax
jne    14c1 <main+0x2d1>
mov    $0x0,%al
call   1030 <reader@plt>
jmp    14c6 <main+0x2d6>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    149f <main+0x2af>
movl   $0x0,-0x34(%rbp)
cmpl   $0xb,-0x34(%rbp)
jge    151c <main+0x32c>
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
lea    -0x3c(%rbp),%rdi
call   10f0 <wait@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
lea    0xb46(%rip),%rdi
call   1050 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    14db <main+0x2eb>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xdfe(%rip),%rbx
mov    %rbx,%rdi
call   1100 <sem_unlink@plt>
mov    0x2de7(%rip),%r15
mov    0x2da0(%rip),%r14
mov    (%r14),%esi
or     (%r15),%esi
mov    0x2dab(%rip),%r12
or     (%r12),%esi
mov    %rbx,%rdi
mov    $0x1b4,%edx
mov    $0x1,%ecx
call   1080 <sem_open@plt>
mov    0x2d86(%rip),%rbp
cmp    0x0(%rbp),%rax
jne    1254 <main+0x64>
lea    0xdbf(%rip),%rdi
call   10c0 <die@plt>
lea    0xdbe(%rip),%rbx
mov    %rbx,%rdi
call   1100 <sem_unlink@plt>
mov    (%r14),%esi
or     (%r15),%esi
or     (%r12),%esi
mov    %rbx,%rdi
mov    $0x1b4,%edx
mov    $0x1,%ecx
call   1080 <sem_open@plt>
cmp    0x0(%rbp),%rax
jne    1291 <main+0xa1>
lea    0xd82(%rip),%rdi
call   10c0 <die@plt>
movl   $0x0,0x4(%rsp)
lea    0xd85(%rip),%rbx
mov    %rbx,%rdi
call   10e0 <shm_unlink@plt>
mov    (%r15),%esi
or     (%r12),%esi
or     (%r14),%esi
mov    %rbx,%rdi
mov    $0x1b4,%edx
call   10d0 <shm_open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12d1 <main+0xe1>
lea    0xd61(%rip),%rdi
call   10c0 <die@plt>
mov    %ebp,%edi
mov    $0x4,%esi
call   1060 <ftruncate@plt>
mov    0x2d0c(%rip),%rax
mov    0x2cc5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2cea(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2c94(%rip),%r13
cmp    0x0(%r13),%rax
jne    1326 <main+0x136>
lea    0xd17(%rip),%rdi
call   10c0 <die@plt>
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   10a0 <munmap@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xcec(%rip),%rbx
mov    %rbx,%rdi
call   10e0 <shm_unlink@plt>
mov    (%r15),%esi
or     (%r12),%esi
or     (%r14),%esi
mov    %rbx,%rdi
mov    $0x1b4,%edx
call   10d0 <shm_open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1384 <main+0x194>
lea    0xcae(%rip),%rdi
call   10c0 <die@plt>
mov    %ebx,%edi
mov    $0x4,%esi
call   1060 <ftruncate@plt>
mov    0x2c19(%rip),%rax
mov    (%rax),%edx
mov    0x2c50(%rip),%rax
or     (%rax),%edx
mov    0x2c37(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
cmp    0x0(%r13),%rax
jne    13d2 <main+0x1e2>
lea    0xc6b(%rip),%rdi
call   10c0 <die@plt>
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   10a0 <munmap@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jne    1404 <main+0x214>
xor    %eax,%eax
call   10b0 <writer@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jne    1417 <main+0x227>
xor    %eax,%eax
call   1030 <reader@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jne    142a <main+0x23a>
xor    %eax,%eax
call   1030 <reader@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jne    143d <main+0x24d>
xor    %eax,%eax
call   1030 <reader@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jne    1450 <main+0x260>
xor    %eax,%eax
call   1030 <reader@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jne    1463 <main+0x273>
xor    %eax,%eax
call   1030 <reader@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jne    1476 <main+0x286>
xor    %eax,%eax
call   1030 <reader@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jne    1489 <main+0x299>
xor    %eax,%eax
call   1030 <reader@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jne    149c <main+0x2ac>
xor    %eax,%eax
call   1030 <reader@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jne    14af <main+0x2bf>
xor    %eax,%eax
call   1030 <reader@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jne    14c2 <main+0x2d2>
xor    %eax,%eax
call   1030 <reader@plt>
movl   $0x0,(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
call   10f0 <wait@plt>
lea    0xb74(%rip),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x0,(%rsp)
mov    %r14,%rdi
call   10f0 <wait@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x0,(%rsp)
mov    %r14,%rdi
call   10f0 <wait@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x0,(%rsp)
mov    %r14,%rdi
call   10f0 <wait@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x0,(%rsp)
mov    %r14,%rdi
call   10f0 <wait@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x0,(%rsp)
mov    %r14,%rdi
call   10f0 <wait@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x0,(%rsp)
mov    %r14,%rdi
call   10f0 <wait@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x0,(%rsp)
mov    %r14,%rdi
call   10f0 <wait@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x0,(%rsp)
mov    %r14,%rdi
call   10f0 <wait@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x0,(%rsp)
mov    %r14,%rdi
call   10f0 <wait@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x0,(%rsp)
mov    %r14,%rdi
call   10f0 <wait@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/read_bin_sem\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [11 x i8] c"sem_open()\00"
@.str.2 = [12 x i8] c"/rw_bin_sem\00"
@.str.3 = [8 x i8] c"/buffer\00"
@.str.4 = [11 x i8] c"shm_open()\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [7 x i8] c"mmap()\00"
@.str.6 = [16 x i8] c"/reader_counter\00"
@.str.7 = [24 x i8] c"process %d is finished\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
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = call i32 @sem_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_EXCL
%17 = or i32 %15, %16
%18 = load i32, i32* @O_RDWR
%19 = or i32 %17, %18
%20 = call i32* @sem_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %19, i32 436, i32 1)
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = load i32*, i32** @SEM_FAILED
%23 = icmp eq i32* %21, %22
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %2
%27 = call i32 @sem_unlink(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @O_CREAT
%29 = load i32, i32* @O_EXCL
%30 = or i32 %28, %29
%31 = load i32, i32* @O_RDWR
%32 = or i32 %30, %31
%33 = call i32* @sem_open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 436, i32 1)
store i32* %33, i32** %7
%34 = load i32*, i32** %7
%35 = load i32*, i32** @SEM_FAILED
%36 = icmp eq i32* %34, %35
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %26
store i32 0, i32* %9
store i8* null, i8** %10
%40 = call i32 @shm_unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* @O_RDWR
%42 = load i32, i32* @O_CREAT
%43 = or i32 %41, %42
%44 = load i32, i32* @O_EXCL
%45 = or i32 %43, %44
%46 = call i32 @shm_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %45, i32 436)
store i32 %46, i32* %8
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %39
%49 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48, %39
%51 = load i32, i32* %8
%52 = call i32 @ftruncate(i32 %51, i32 4)
%53 = load i32, i32* @PROT_READ
%54 = load i32, i32* @PROT_WRITE
%55 = or i32 %53, %54
%56 = load i32, i32* @MAP_SHARED
%57 = load i32, i32* %8
%58 = call i8* @mmap(i32* null, i32 4, i32 %55, i32 %56, i32 %57, i32 0)
store i8* %58, i8** %10
%59 = load i8*, i8** @MAP_FAILED
%60 = icmp eq i8* %58, %59
br i1 %60, label %61, label %63
61: ; preds = %50
%62 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %63
63: ; preds = %61, %50
%64 = load i8*, i8** %10
%65 = call i32 @memcpy(i8* %64, i32* %9, i32 4)
%66 = load i8*, i8** %10
%67 = call i32 @munmap(i8* %66, i32 4)
%68 = load i32, i32* %8
%69 = call i32 @close(i32 %68)
%70 = call i32 @shm_unlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%71 = load i32, i32* @O_RDWR
%72 = load i32, i32* @O_CREAT
%73 = or i32 %71, %72
%74 = load i32, i32* @O_EXCL
%75 = or i32 %73, %74
%76 = call i32 @shm_open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %75, i32 436)
store i32 %76, i32* %8
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %63
%79 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %80
80: ; preds = %78, %63
%81 = load i32, i32* %8
%82 = call i32 @ftruncate(i32 %81, i32 4)
%83 = load i32, i32* @PROT_READ
%84 = load i32, i32* @PROT_WRITE
%85 = or i32 %83, %84
%86 = load i32, i32* @MAP_SHARED
%87 = load i32, i32* %8
%88 = call i8* @mmap(i32* null, i32 4, i32 %85, i32 %86, i32 %87, i32 0)
store i8* %88, i8** %10
%89 = load i8*, i8** @MAP_FAILED
%90 = icmp eq i8* %88, %89
br i1 %90, label %91, label %93
91: ; preds = %80
%92 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %93
93: ; preds = %91, %80
%94 = load i8*, i8** %10
%95 = call i32 @memcpy(i8* %94, i32* %9, i32 4)
%96 = load i8*, i8** %10
%97 = call i32 @munmap(i8* %96, i32 4)
%98 = call i64 (...) @fork()
%99 = icmp eq i64 %98, 0
br i1 %99, label %100, label %102
100: ; preds = %93
%101 = call i32 (...) @writer()
br label %102
102: ; preds = %100, %93
store i32 0, i32* %11
store i32 0, i32* %11
br label %103
103: ; preds = %112, %102
%104 = load i32, i32* %11
%105 = icmp slt i32 %104, 10
br i1 %105, label %106, label %115
106: ; preds = %103
%107 = call i64 (...) @fork()
%108 = icmp eq i64 %107, 0
br i1 %108, label %109, label %111
109: ; preds = %106
%110 = call i32 (...) @reader()
br label %111
111: ; preds = %109, %106
br label %112
112: ; preds = %111
%113 = load i32, i32* %11
%114 = add nsw i32 %113, 1
store i32 %114, i32* %11
br label %103
115: ; preds = %103
store i32 0, i32* %11
br label %116
116: ; preds = %123, %115
%117 = load i32, i32* %11
%118 = icmp slt i32 %117, 11
br i1 %118, label %119, label %126
119: ; preds = %116
store i32 0, i32* %12
store i32 0, i32* %13
%120 = call i32 @wait(i32* %13)
store i32 %120, i32* %12
%121 = load i32, i32* %12
%122 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %121)
br label %123
123: ; preds = %119
%124 = load i32, i32* %11
%125 = add nsw i32 %124, 1
store i32 %125, i32* %11
br label %116
126: ; preds = %116
%127 = load i32, i32* %3
ret i32 %127
}

LLVM IR (-O2):
@.str = [14 x i8] c"/read_bin_sem\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [11 x i8] c"sem_open()\00"
@.str.2 = [12 x i8] c"/rw_bin_sem\00"
@.str.3 = [8 x i8] c"/buffer\00"
@.str.4 = [11 x i8] c"shm_open()\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [7 x i8] c"mmap()\00"
@.str.6 = [16 x i8] c"/reader_counter\00"
@.str.7 = [24 x i8] c"process %d is finished\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = tail call i32 @sem_unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_EXCL
%8 = or i32 %7, %6
%9 = load i32, i32* @O_RDWR
%10 = or i32 %8, %9
%11 = tail call i32* @sem_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10, i32 436, i32 1)
%12 = load i32*, i32** @SEM_FAILED
%13 = icmp eq i32* %11, %12
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = tail call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = tail call i32 @sem_unlink(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @O_CREAT
%19 = load i32, i32* @O_EXCL
%20 = or i32 %19, %18
%21 = load i32, i32* @O_RDWR
%22 = or i32 %20, %21
%23 = tail call i32* @sem_open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 436, i32 1)
%24 = load i32*, i32** @SEM_FAILED
%25 = icmp eq i32* %23, %24
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = tail call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %16
%29 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%30 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @O_RDWR
%32 = load i32, i32* @O_CREAT
%33 = or i32 %32, %31
%34 = load i32, i32* @O_EXCL
%35 = or i32 %33, %34
%36 = tail call i32 @shm_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %35, i32 436)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %28
%39 = tail call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %38, %28
%41 = tail call i32 @ftruncate(i32 %36, i32 4)
%42 = load i32, i32* @PROT_READ
%43 = load i32, i32* @PROT_WRITE
%44 = or i32 %43, %42
%45 = load i32, i32* @MAP_SHARED
%46 = tail call i8* @mmap(i32* null, i32 4, i32 %44, i32 %45, i32 %36, i32 0)
%47 = load i8*, i8** @MAP_FAILED
%48 = icmp eq i8* %46, %47
br i1 %48, label %49, label %51
49: ; preds = %40
%50 = tail call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %49, %40
%52 = call i32 @memcpy(i8* %46, i32* %3, i32 4)
%53 = call i32 @munmap(i8* %46, i32 4)
%54 = call i32 @close(i32 %36)
%55 = call i32 @shm_unlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%56 = load i32, i32* @O_RDWR
%57 = load i32, i32* @O_CREAT
%58 = or i32 %57, %56
%59 = load i32, i32* @O_EXCL
%60 = or i32 %58, %59
%61 = call i32 @shm_open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %60, i32 436)
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %51
%64 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %65
65: ; preds = %63, %51
%66 = call i32 @ftruncate(i32 %61, i32 4)
%67 = load i32, i32* @PROT_READ
%68 = load i32, i32* @PROT_WRITE
%69 = or i32 %68, %67
%70 = load i32, i32* @MAP_SHARED
%71 = call i8* @mmap(i32* null, i32 4, i32 %69, i32 %70, i32 %61, i32 0)
%72 = load i8*, i8** @MAP_FAILED
%73 = icmp eq i8* %71, %72
br i1 %73, label %74, label %76
74: ; preds = %65
%75 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %76
76: ; preds = %74, %65
%77 = call i32 @memcpy(i8* %71, i32* %3, i32 4)
%78 = call i32 @munmap(i8* %71, i32 4)
%79 = call i64 (...) @fork()
%80 = icmp eq i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %76
%82 = call i32 (...) @writer()
br label %83
83: ; preds = %81, %76
%84 = call i64 (...) @fork()
%85 = icmp eq i64 %84, 0
br i1 %85, label %86, label %88
86: ; preds = %83
%87 = call i32 (...) @reader()
br label %88
88: ; preds = %83, %86
%89 = call i64 (...) @fork()
%90 = icmp eq i64 %89, 0
br i1 %90, label %91, label %93
91: ; preds = %88
%92 = call i32 (...) @reader()
br label %93
93: ; preds = %91, %88
%94 = call i64 (...) @fork()
%95 = icmp eq i64 %94, 0
br i1 %95, label %96, label %98
96: ; preds = %93
%97 = call i32 (...) @reader()
br label %98
98: ; preds = %96, %93
%99 = call i64 (...) @fork()
%100 = icmp eq i64 %99, 0
br i1 %100, label %101, label %103
101: ; preds = %98
%102 = call i32 (...) @reader()
br label %103
103: ; preds = %101, %98
%104 = call i64 (...) @fork()
%105 = icmp eq i64 %104, 0
br i1 %105, label %106, label %108
106: ; preds = %103
%107 = call i32 (...) @reader()
br label %108
108: ; preds = %106, %103
%109 = call i64 (...) @fork()
%110 = icmp eq i64 %109, 0
br i1 %110, label %111, label %113
111: ; preds = %108
%112 = call i32 (...) @reader()
br label %113
113: ; preds = %111, %108
%114 = call i64 (...) @fork()
%115 = icmp eq i64 %114, 0
br i1 %115, label %116, label %118
116: ; preds = %113
%117 = call i32 (...) @reader()
br label %118
118: ; preds = %116, %113
%119 = call i64 (...) @fork()
%120 = icmp eq i64 %119, 0
br i1 %120, label %121, label %123
121: ; preds = %118
%122 = call i32 (...) @reader()
br label %123
123: ; preds = %121, %118
%124 = call i64 (...) @fork()
%125 = icmp eq i64 %124, 0
br i1 %125, label %126, label %128
126: ; preds = %123
%127 = call i32 (...) @reader()
br label %128
128: ; preds = %126, %123
%129 = call i64 (...) @fork()
%130 = icmp eq i64 %129, 0
br i1 %130, label %131, label %133
131: ; preds = %128
%132 = call i32 (...) @reader()
br label %133
133: ; preds = %131, %128
%134 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%135 = call i32 @wait(i32* %4)
%136 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %135)
store i32 0, i32* %4
%137 = call i32 @wait(i32* %4)
%138 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %137)
store i32 0, i32* %4
%139 = call i32 @wait(i32* %4)
%140 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %139)
store i32 0, i32* %4
%141 = call i32 @wait(i32* %4)
%142 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %141)
store i32 0, i32* %4
%143 = call i32 @wait(i32* %4)
%144 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %143)
store i32 0, i32* %4
%145 = call i32 @wait(i32* %4)
%146 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %145)
store i32 0, i32* %4
%147 = call i32 @wait(i32* %4)
%148 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %147)
store i32 0, i32* %4
%149 = call i32 @wait(i32* %4)
%150 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %149)
store i32 0, i32* %4
%151 = call i32 @wait(i32* %4)
%152 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %151)
store i32 0, i32* %4
%153 = call i32 @wait(i32* %4)
%154 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %153)
store i32 0, i32* %4
%155 = call i32 @wait(i32* %4)
%156 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %155)
ret i32 0
}