#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*/ IPC_RMID;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int rand();
int /*<<< orphan*/ sem_ready(int);
int /*<<< orphan*/ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int semget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(char *);
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  int semid;
  int shmid;
  int *mem;
  int i, n;
  int status = 0;
  setbuf(stdout, NULL);
  semid = semget(IPC_PRIVATE, 1, 0644 | IPC_CREAT);
  if (semid == -1) {
    perror("semget()");
    exit(1);
  }
  shmid = shmget(IPC_PRIVATE, sizeof(int), 0644 | IPC_CREAT);
  if (shmid == -1) {
    perror("shmget()");
    exit(1);
  }
  mem = shmat(shmid, 0, 0);
  if (mem == (int *)-1) {
    perror("shmat()");
    exit(1);
  }
  *mem = 0;
  sem_ready(semid);
  switch (fork()) {
  case -1:
    perror("fork()");
    status = 1;
    goto cleanup;
  case 0:
    for (i = 0; i < 5; i++) {
      usleep(rand() % 100000);
      n = *mem;
      printf(" child: *mem = %d\n", *mem);
      usleep(rand() % 100000);
      *mem = n + 1;
    }
    _exit(0);
  default:
    for (i = 0; i < 5; i++) {
      usleep(rand() % 100000);
      n = *mem;
      printf("parent: *mem = %d\n", *mem);
      usleep(rand() % 100000);
      *mem = n + 1;
    }
  }
  wait(NULL);
  printf("\n"
         "  last: *mem = %d\n\n",
         *mem);
cleanup:
  shmdt((char *)mem);
  shmctl(shmid, IPC_RMID, 0);
  semctl(semid, 0, IPC_RMID, 0);
  return status;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <setbuf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
or     $0x1a4,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    126d <main+0x6d>
lea    0xda2(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
mov    0x2d4b(%rip),%rax
mov    (%rax),%edx
or     $0x1a4,%edx
mov    $0x4,%esi
call   10d0 <shmget@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12b2 <main+0xb2>
lea    0xd66(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   10c0 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
jne    12e9 <main+0xe9>
lea    0xd38(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
call   1080 <sem_ready@plt>
xor    %eax,%eax
call   1100 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0xffffffff,%eax
je     1325 <main+0x125>
jmp    1315 <main+0x115>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     133d <main+0x13d>
jmp    13b6 <main+0x1b6>
lea    0xcee(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x24(%rbp)
jmp    1448 <main+0x248>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x5,-0x1c(%rbp)
jge    13af <main+0x1af>
mov    $0x0,%al
call   1110 <rand@plt>
mov    $0x186a0,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1120 <usleep@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xca7(%rip),%rdi
call   1070 <printf@plt>
mov    $0x0,%al
call   1110 <rand@plt>
mov    $0x186a0,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1120 <usleep@plt>
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1344 <main+0x144>
xor    %edi,%edi
call   1040 <_exit@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x5,-0x1c(%rbp)
jge    1428 <main+0x228>
mov    $0x0,%al
call   1110 <rand@plt>
mov    $0x186a0,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1120 <usleep@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xc41(%rip),%rdi
call   1070 <printf@plt>
mov    $0x0,%al
call   1110 <rand@plt>
mov    $0x186a0,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1120 <usleep@plt>
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    13bd <main+0x1bd>
jmp    142d <main+0x22d>
xor    %eax,%eax
mov    %eax,%edi
call   10f0 <wait@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xc04(%rip),%rdi
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <shmdt@plt>
mov    -0xc(%rbp),%edi
mov    0x2b7d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <shmctl@plt>
mov    -0x8(%rbp),%edi
mov    0x2b6a(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   10b0 <semctl@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <setbuf@plt>
mov    0x2dc6(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d9c(%rip),%r15
mov    $0x1a4,%ebx
mov    (%r15),%edx
or     %ebx,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     1519 <main+0x319>
mov    %eax,%r14d
mov    0x0(%rbp),%edi
or     (%r15),%ebx
mov    $0x4,%esi
mov    %ebx,%edx
call   10d0 <shmget@plt>
cmp    $0xffffffff,%eax
je     1522 <main+0x322>
mov    %eax,%r12d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     152b <main+0x32b>
mov    %rax,%rbx
movl   $0x0,(%rax)
mov    %r14d,%edi
call   1080 <sem_ready@plt>
xor    %eax,%eax
call   1100 <fork@plt>
cmp    $0xffffffff,%eax
je     1505 <main+0x305>
test   %eax,%eax
je     1541 <main+0x341>
xor    %r15d,%r15d
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
mov    (%rbx),%ebp
lea    0xd55(%rip),%r13
mov    %r13,%rdi
mov    %ebp,%esi
call   1070 <printf@plt>
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
mov    (%rbx),%ebp
mov    %r13,%rdi
mov    %ebp,%esi
call   1070 <printf@plt>
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
mov    (%rbx),%ebp
mov    %r13,%rdi
mov    %ebp,%esi
call   1070 <printf@plt>
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
mov    (%rbx),%ebp
mov    %r13,%rdi
mov    %ebp,%esi
call   1070 <printf@plt>
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
mov    (%rbx),%ebp
mov    %r13,%rdi
mov    %ebp,%esi
call   1070 <printf@plt>
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
xor    %edi,%edi
call   10f0 <wait@plt>
mov    (%rbx),%esi
lea    0xb82(%rip),%rdi
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1050 <shmdt@plt>
mov    0x2aff(%rip),%rbx
mov    (%rbx),%esi
mov    %r12d,%edi
xor    %edx,%edx
call   1090 <shmctl@plt>
mov    (%rbx),%edx
mov    %r14d,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   10b0 <semctl@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb0e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%r15d
jmp    14ca <main+0x2ca>
lea    0xae0(%rip),%rdi
jmp    1532 <main+0x332>
lea    0xae0(%rip),%rdi
jmp    1532 <main+0x332>
lea    0xae0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
mov    (%rbx),%ebp
lea    0xaac(%rip),%r14
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <printf@plt>
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
mov    (%rbx),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <printf@plt>
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
mov    (%rbx),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <printf@plt>
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
mov    (%rbx),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <printf@plt>
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
mov    (%rbx),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <printf@plt>
xor    %eax,%eax
call   1110 <rand@plt>
movslq %eax,%rdi
imul   $0x14f8b589,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%edi
call   1120 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
xor    %edi,%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str = [9 x i8] c"semget()\00"
@.str.1 = [9 x i8] c"shmget()\00"
@.str.2 = [8 x i8] c"shmat()\00"
@.str.3 = [7 x i8] c"fork()\00"
@.str.4 = [19 x i8] c" child: *mem = %d\0A\00"
@.str.5 = [19 x i8] c"parent: *mem = %d\0A\00"
@.str.6 = [21 x i8] c"\0A last: *mem = %d\0A\0A\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %7
%8 = load i32, i32* @stdout
%9 = call i32 @setbuf(i32 %8, i32* null)
%10 = load i32, i32* @IPC_PRIVATE
%11 = load i32, i32* @IPC_CREAT
%12 = or i32 420, %11
%13 = call i32 @semget(i32 %10, i32 1, i32 %12)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %0
%20 = load i32, i32* @IPC_PRIVATE
%21 = load i32, i32* @IPC_CREAT
%22 = or i32 420, %21
%23 = call i32 @shmget(i32 %20, i32 4, i32 %22)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = load i32, i32* %3
%31 = call i32* @shmat(i32 %30, i32 0, i32 0)
store i32* %31, i32** %4
%32 = load i32*, i32** %4
%33 = icmp eq i32* %32, inttoptr (i64 -1 to i32*)
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %29
%38 = load i32*, i32** %4
store i32 0, i32* %38
%39 = load i32, i32* %2
%40 = call i32 @sem_ready(i32 %39)
%41 = call i32 (...) @fork()
switch i32 %41, label %68 [
i32 -1, label %42
i32 0, label %44
]
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %7
br label %96
44: ; preds = %37
store i32 0, i32* %5
br label %45
45: ; preds = %63, %44
%46 = load i32, i32* %5
%47 = icmp slt i32 %46, 5
br i1 %47, label %48, label %66
48: ; preds = %45
%49 = call i32 (...) @rand()
%50 = srem i32 %49, 100000
%51 = call i32 @usleep(i32 %50)
%52 = load i32*, i32** %4
%53 = load i32, i32* %52
store i32 %53, i32* %6
%54 = load i32*, i32** %4
%55 = load i32, i32* %54
%56 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %55)
%57 = call i32 (...) @rand()
%58 = srem i32 %57, 100000
%59 = call i32 @usleep(i32 %58)
%60 = load i32, i32* %6
%61 = add nsw i32 %60, 1
%62 = load i32*, i32** %4
store i32 %61, i32* %62
br label %63
63: ; preds = %48
%64 = load i32, i32* %5
%65 = add nsw i32 %64, 1
store i32 %65, i32* %5
br label %45
66: ; preds = %45
%67 = call i32 @_exit(i32 0)
unreachable
68: ; preds = %37
store i32 0, i32* %5
br label %69
69: ; preds = %87, %68
%70 = load i32, i32* %5
%71 = icmp slt i32 %70, 5
br i1 %71, label %72, label %90
72: ; preds = %69
%73 = call i32 (...) @rand()
%74 = srem i32 %73, 100000
%75 = call i32 @usleep(i32 %74)
%76 = load i32*, i32** %4
%77 = load i32, i32* %76
store i32 %77, i32* %6
%78 = load i32*, i32** %4
%79 = load i32, i32* %78
%80 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %79)
%81 = call i32 (...) @rand()
%82 = srem i32 %81, 100000
%83 = call i32 @usleep(i32 %82)
%84 = load i32, i32* %6
%85 = add nsw i32 %84, 1
%86 = load i32*, i32** %4
store i32 %85, i32* %86
br label %87
87: ; preds = %72
%88 = load i32, i32* %5
%89 = add nsw i32 %88, 1
store i32 %89, i32* %5
br label %69
90: ; preds = %69
br label %91
91: ; preds = %90
%92 = call i32 @wait(i32* null)
%93 = load i32*, i32** %4
%94 = load i32, i32* %93
%95 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %94)
br label %96
96: ; preds = %91, %42
%97 = load i32*, i32** %4
%98 = bitcast i32* %97 to i8*
%99 = call i32 @shmdt(i8* %98)
%100 = load i32, i32* %3
%101 = load i32, i32* @IPC_RMID
%102 = call i32 @shmctl(i32 %100, i32 %101, i32 0)
%103 = load i32, i32* %2
%104 = load i32, i32* @IPC_RMID
%105 = call i32 @semctl(i32 %103, i32 0, i32 %104, i32 0)
%106 = load i32, i32* %7
ret i32 %106
}

LLVM IR (-O2):
@stdout = global i32 0
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str = [9 x i8] c"semget()\00"
@.str.1 = [9 x i8] c"shmget()\00"
@.str.2 = [8 x i8] c"shmat()\00"
@.str.3 = [7 x i8] c"fork()\00"
@.str.4 = [19 x i8] c" child: *mem = %d\0A\00"
@.str.5 = [19 x i8] c"parent: *mem = %d\0A\00"
@.str.6 = [21 x i8] c"\0A last: *mem = %d\0A\0A\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = load i32, i32* @stdout
%2 = tail call i32 @setbuf(i32 %1, i32* null)
%3 = load i32, i32* @IPC_PRIVATE
%4 = load i32, i32* @IPC_CREAT
%5 = or i32 %4, 420
%6 = tail call i32 @semget(i32 %3, i32 1, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* @IPC_PRIVATE
%13 = load i32, i32* @IPC_CREAT
%14 = or i32 %13, 420
%15 = tail call i32 @shmget(i32 %12, i32 4, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = tail call i32* @shmat(i32 %15, i32 0, i32 0)
%22 = icmp eq i32* %21, inttoptr (i64 -1 to i32*)
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %20
store i32 0, i32* %21
%27 = tail call i32 @sem_ready(i32 %6)
%28 = tail call i32 (...) @fork()
switch i32 %28, label %76 [
i32 -1, label %125
i32 0, label %29
]
29: ; preds = %26
%30 = tail call i32 (...) @rand()
%31 = srem i32 %30, 100000
%32 = tail call i32 @usleep(i32 %31)
%33 = load i32, i32* %21
%34 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = tail call i32 (...) @rand()
%36 = srem i32 %35, 100000
%37 = tail call i32 @usleep(i32 %36)
%38 = add nsw i32 %33, 1
store i32 %38, i32* %21
%39 = tail call i32 (...) @rand()
%40 = srem i32 %39, 100000
%41 = tail call i32 @usleep(i32 %40)
%42 = load i32, i32* %21
%43 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %42)
%44 = tail call i32 (...) @rand()
%45 = srem i32 %44, 100000
%46 = tail call i32 @usleep(i32 %45)
%47 = add nsw i32 %42, 1
store i32 %47, i32* %21
%48 = tail call i32 (...) @rand()
%49 = srem i32 %48, 100000
%50 = tail call i32 @usleep(i32 %49)
%51 = load i32, i32* %21
%52 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %51)
%53 = tail call i32 (...) @rand()
%54 = srem i32 %53, 100000
%55 = tail call i32 @usleep(i32 %54)
%56 = add nsw i32 %51, 1
store i32 %56, i32* %21
%57 = tail call i32 (...) @rand()
%58 = srem i32 %57, 100000
%59 = tail call i32 @usleep(i32 %58)
%60 = load i32, i32* %21
%61 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %60)
%62 = tail call i32 (...) @rand()
%63 = srem i32 %62, 100000
%64 = tail call i32 @usleep(i32 %63)
%65 = add nsw i32 %60, 1
store i32 %65, i32* %21
%66 = tail call i32 (...) @rand()
%67 = srem i32 %66, 100000
%68 = tail call i32 @usleep(i32 %67)
%69 = load i32, i32* %21
%70 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %69)
%71 = tail call i32 (...) @rand()
%72 = srem i32 %71, 100000
%73 = tail call i32 @usleep(i32 %72)
%74 = add nsw i32 %69, 1
store i32 %74, i32* %21
%75 = tail call i32 @_exit(i32 0)
unreachable
76: ; preds = %26
%77 = tail call i32 (...) @rand()
%78 = srem i32 %77, 100000
%79 = tail call i32 @usleep(i32 %78)
%80 = load i32, i32* %21
%81 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %80)
%82 = tail call i32 (...) @rand()
%83 = srem i32 %82, 100000
%84 = tail call i32 @usleep(i32 %83)
%85 = add nsw i32 %80, 1
store i32 %85, i32* %21
%86 = tail call i32 (...) @rand()
%87 = srem i32 %86, 100000
%88 = tail call i32 @usleep(i32 %87)
%89 = load i32, i32* %21
%90 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %89)
%91 = tail call i32 (...) @rand()
%92 = srem i32 %91, 100000
%93 = tail call i32 @usleep(i32 %92)
%94 = add nsw i32 %89, 1
store i32 %94, i32* %21
%95 = tail call i32 (...) @rand()
%96 = srem i32 %95, 100000
%97 = tail call i32 @usleep(i32 %96)
%98 = load i32, i32* %21
%99 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %98)
%100 = tail call i32 (...) @rand()
%101 = srem i32 %100, 100000
%102 = tail call i32 @usleep(i32 %101)
%103 = add nsw i32 %98, 1
store i32 %103, i32* %21
%104 = tail call i32 (...) @rand()
%105 = srem i32 %104, 100000
%106 = tail call i32 @usleep(i32 %105)
%107 = load i32, i32* %21
%108 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %107)
%109 = tail call i32 (...) @rand()
%110 = srem i32 %109, 100000
%111 = tail call i32 @usleep(i32 %110)
%112 = add nsw i32 %107, 1
store i32 %112, i32* %21
%113 = tail call i32 (...) @rand()
%114 = srem i32 %113, 100000
%115 = tail call i32 @usleep(i32 %114)
%116 = load i32, i32* %21
%117 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %116)
%118 = tail call i32 (...) @rand()
%119 = srem i32 %118, 100000
%120 = tail call i32 @usleep(i32 %119)
%121 = add nsw i32 %116, 1
store i32 %121, i32* %21
%122 = tail call i32 @wait(i32* null)
%123 = load i32, i32* %21
%124 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %123)
br label %127
125: ; preds = %26
%126 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %127
127: ; preds = %76, %125
%128 = phi i32 [ 0, %76 ], [ 1, %125 ]
%129 = bitcast i32* %21 to i8*
%130 = tail call i32 @shmdt(i8* %129)
%131 = load i32, i32* @IPC_RMID
%132 = tail call i32 @shmctl(i32 %15, i32 %131, i32 0)
%133 = load i32, i32* @IPC_RMID
%134 = tail call i32 @semctl(i32 %6, i32 0, i32 %133, i32 0)
ret i32 %128
}