#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_lock(int);
int /*<<< orphan*/ sem_ready(int);
int /*<<< orphan*/ sem_unlock(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);
      sem_lock(semid);
      n = *mem;
      printf(" child: *mem = %d\n", *mem);
      usleep(rand() % 100000);
      *mem = n + 1;
      sem_unlock(semid);
    }
    _exit(0);
  default:
    for (i = 0; i < 5; i++) {
      usleep(rand() % 100000);
      sem_lock(semid);
      n = *mem;
      printf("parent: *mem = %d\n", *mem);
      usleep(rand() % 100000);
      *mem = n + 1;
      sem_unlock(semid);
    }
  }
  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    0x2d93(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <setbuf@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
or     $0x1a4,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    128d <main+0x6d>
lea    0xd82(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
mov    0x2d2b(%rip),%rax
mov    (%rax),%edx
or     $0x1a4,%edx
mov    $0x4,%esi
call   10f0 <shmget@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12d2 <main+0xb2>
lea    0xd46(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   10e0 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
jne    1309 <main+0xe9>
lea    0xd18(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
call   10a0 <sem_ready@plt>
xor    %eax,%eax
call   1120 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0xffffffff,%eax
je     1345 <main+0x125>
jmp    1335 <main+0x115>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     135d <main+0x13d>
jmp    13e6 <main+0x1c6>
lea    0xcce(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x24(%rbp)
jmp    1488 <main+0x268>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x5,-0x1c(%rbp)
jge    13df <main+0x1bf>
mov    $0x0,%al
call   1130 <rand@plt>
mov    $0x186a0,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1140 <usleep@plt>
mov    -0x8(%rbp),%edi
call   1040 <sem_lock@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xc7f(%rip),%rdi
call   1080 <printf@plt>
mov    $0x0,%al
call   1130 <rand@plt>
mov    $0x186a0,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1140 <usleep@plt>
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
call   1090 <sem_unlock@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1364 <main+0x144>
xor    %edi,%edi
call   1050 <_exit@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x5,-0x1c(%rbp)
jge    1468 <main+0x248>
mov    $0x0,%al
call   1130 <rand@plt>
mov    $0x186a0,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1140 <usleep@plt>
mov    -0x8(%rbp),%edi
call   1040 <sem_lock@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xc09(%rip),%rdi
call   1080 <printf@plt>
mov    $0x0,%al
call   1130 <rand@plt>
mov    $0x186a0,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1140 <usleep@plt>
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
call   1090 <sem_unlock@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    13ed <main+0x1cd>
jmp    146d <main+0x24d>
xor    %eax,%eax
mov    %eax,%edi
call   1110 <wait@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xbc4(%rip),%rdi
call   1080 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <shmdt@plt>
mov    -0xc(%rbp),%edi
mov    0x2b3d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <shmctl@plt>
mov    -0x8(%rbp),%edi
mov    0x2b2a(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   10d0 <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    0x2d9e(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1070 <setbuf@plt>
mov    0x2da6(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d7c(%rip),%r14
mov    $0x1a4,%ebx
mov    (%r14),%edx
or     %ebx,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     1589 <main+0x369>
mov    %eax,%r13d
mov    0x0(%rbp),%edi
or     (%r14),%ebx
mov    $0x4,%esi
mov    %ebx,%edx
call   10f0 <shmget@plt>
cmp    $0xffffffff,%eax
je     1592 <main+0x372>
mov    %eax,%r14d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     159b <main+0x37b>
mov    %rax,%rbx
movl   $0x0,(%rax)
mov    %r13d,%edi
call   10a0 <sem_ready@plt>
xor    %eax,%eax
call   1120 <fork@plt>
cmp    $0xffffffff,%eax
je     1575 <main+0x355>
test   %eax,%eax
je     15b1 <main+0x391>
xor    %r15d,%r15d
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
mov    %r13d,%edi
call   1040 <sem_lock@plt>
mov    (%rbx),%ebp
lea    0xd2d(%rip),%r12
mov    %r12,%rdi
mov    %ebp,%esi
call   1080 <printf@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
mov    %r13d,%edi
call   1090 <sem_unlock@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
mov    %r13d,%edi
call   1040 <sem_lock@plt>
mov    (%rbx),%ebp
mov    %r12,%rdi
mov    %ebp,%esi
call   1080 <printf@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
mov    %r13d,%edi
call   1090 <sem_unlock@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
mov    %r13d,%edi
call   1040 <sem_lock@plt>
mov    (%rbx),%ebp
mov    %r12,%rdi
mov    %ebp,%esi
call   1080 <printf@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
mov    %r13d,%edi
call   1090 <sem_unlock@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
mov    %r13d,%edi
call   1040 <sem_lock@plt>
mov    (%rbx),%ebp
mov    %r12,%rdi
mov    %ebp,%esi
call   1080 <printf@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
mov    %r13d,%edi
call   1090 <sem_unlock@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
mov    %r13d,%edi
call   1040 <sem_lock@plt>
mov    (%rbx),%ebp
mov    %r12,%rdi
mov    %ebp,%esi
call   1080 <printf@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
mov    %r13d,%edi
call   1090 <sem_unlock@plt>
xor    %edi,%edi
call   1110 <wait@plt>
mov    (%rbx),%esi
lea    0xb12(%rip),%rdi
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1060 <shmdt@plt>
mov    0x2a8f(%rip),%rbx
mov    (%rbx),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   10b0 <shmctl@plt>
mov    (%rbx),%edx
mov    %r13d,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   10d0 <semctl@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xa9e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%r15d
jmp    153a <main+0x31a>
lea    0xa70(%rip),%rdi
jmp    15a2 <main+0x382>
lea    0xa70(%rip),%rdi
jmp    15a2 <main+0x382>
lea    0xa70(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
mov    %r13d,%edi
call   1040 <sem_lock@plt>
mov    (%rbx),%r15d
lea    0xa33(%rip),%r14
mov    %r14,%rdi
mov    %r15d,%esi
call   1080 <printf@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
add    $0x1,%r15d
mov    %r15d,(%rbx)
mov    %r13d,%edi
call   1090 <sem_unlock@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
mov    %r13d,%edi
call   1040 <sem_lock@plt>
mov    (%rbx),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1080 <printf@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
mov    %r13d,%edi
call   1090 <sem_unlock@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
mov    %r13d,%edi
call   1040 <sem_lock@plt>
mov    (%rbx),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1080 <printf@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
mov    %r13d,%edi
call   1090 <sem_unlock@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
mov    %r13d,%edi
call   1040 <sem_lock@plt>
mov    (%rbx),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1080 <printf@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
mov    %r13d,%edi
call   1090 <sem_unlock@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
mov    %r13d,%edi
call   1040 <sem_lock@plt>
mov    (%rbx),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1080 <printf@plt>
xor    %eax,%eax
call   1130 <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   1140 <usleep@plt>
add    $0x1,%ebp
mov    %ebp,(%rbx)
mov    %r13d,%edi
call   1090 <sem_unlock@plt>
xor    %edi,%edi
call   1050 <_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 %72 [
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 %104
44: ; preds = %37
store i32 0, i32* %5
br label %45
45: ; preds = %67, %44
%46 = load i32, i32* %5
%47 = icmp slt i32 %46, 5
br i1 %47, label %48, label %70
48: ; preds = %45
%49 = call i32 (...) @rand()
%50 = srem i32 %49, 100000
%51 = call i32 @usleep(i32 %50)
%52 = load i32, i32* %2
%53 = call i32 @sem_lock(i32 %52)
%54 = load i32*, i32** %4
%55 = load i32, i32* %54
store i32 %55, i32* %6
%56 = load i32*, i32** %4
%57 = load i32, i32* %56
%58 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %57)
%59 = call i32 (...) @rand()
%60 = srem i32 %59, 100000
%61 = call i32 @usleep(i32 %60)
%62 = load i32, i32* %6
%63 = add nsw i32 %62, 1
%64 = load i32*, i32** %4
store i32 %63, i32* %64
%65 = load i32, i32* %2
%66 = call i32 @sem_unlock(i32 %65)
br label %67
67: ; preds = %48
%68 = load i32, i32* %5
%69 = add nsw i32 %68, 1
store i32 %69, i32* %5
br label %45
70: ; preds = %45
%71 = call i32 @_exit(i32 0)
unreachable
72: ; preds = %37
store i32 0, i32* %5
br label %73
73: ; preds = %95, %72
%74 = load i32, i32* %5
%75 = icmp slt i32 %74, 5
br i1 %75, label %76, label %98
76: ; preds = %73
%77 = call i32 (...) @rand()
%78 = srem i32 %77, 100000
%79 = call i32 @usleep(i32 %78)
%80 = load i32, i32* %2
%81 = call i32 @sem_lock(i32 %80)
%82 = load i32*, i32** %4
%83 = load i32, i32* %82
store i32 %83, i32* %6
%84 = load i32*, i32** %4
%85 = load i32, i32* %84
%86 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %85)
%87 = call i32 (...) @rand()
%88 = srem i32 %87, 100000
%89 = call i32 @usleep(i32 %88)
%90 = load i32, i32* %6
%91 = add nsw i32 %90, 1
%92 = load i32*, i32** %4
store i32 %91, i32* %92
%93 = load i32, i32* %2
%94 = call i32 @sem_unlock(i32 %93)
br label %95
95: ; preds = %76
%96 = load i32, i32* %5
%97 = add nsw i32 %96, 1
store i32 %97, i32* %5
br label %73
98: ; preds = %73
br label %99
99: ; preds = %98
%100 = call i32 @wait(i32* null)
%101 = load i32*, i32** %4
%102 = load i32, i32* %101
%103 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %102)
br label %104
104: ; preds = %99, %42
%105 = load i32*, i32** %4
%106 = bitcast i32* %105 to i8*
%107 = call i32 @shmdt(i8* %106)
%108 = load i32, i32* %3
%109 = load i32, i32* @IPC_RMID
%110 = call i32 @shmctl(i32 %108, i32 %109, i32 0)
%111 = load i32, i32* %2
%112 = load i32, i32* @IPC_RMID
%113 = call i32 @semctl(i32 %111, i32 0, i32 %112, i32 0)
%114 = load i32, i32* %7
ret i32 %114
}

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 %86 [
i32 -1, label %145
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 = tail call i32 @sem_lock(i32 %6)
%34 = load i32, i32* %21
%35 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = tail call i32 (...) @rand()
%37 = srem i32 %36, 100000
%38 = tail call i32 @usleep(i32 %37)
%39 = add nsw i32 %34, 1
store i32 %39, i32* %21
%40 = tail call i32 @sem_unlock(i32 %6)
%41 = tail call i32 (...) @rand()
%42 = srem i32 %41, 100000
%43 = tail call i32 @usleep(i32 %42)
%44 = tail call i32 @sem_lock(i32 %6)
%45 = load i32, i32* %21
%46 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = tail call i32 (...) @rand()
%48 = srem i32 %47, 100000
%49 = tail call i32 @usleep(i32 %48)
%50 = add nsw i32 %45, 1
store i32 %50, i32* %21
%51 = tail call i32 @sem_unlock(i32 %6)
%52 = tail call i32 (...) @rand()
%53 = srem i32 %52, 100000
%54 = tail call i32 @usleep(i32 %53)
%55 = tail call i32 @sem_lock(i32 %6)
%56 = load i32, i32* %21
%57 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %56)
%58 = tail call i32 (...) @rand()
%59 = srem i32 %58, 100000
%60 = tail call i32 @usleep(i32 %59)
%61 = add nsw i32 %56, 1
store i32 %61, i32* %21
%62 = tail call i32 @sem_unlock(i32 %6)
%63 = tail call i32 (...) @rand()
%64 = srem i32 %63, 100000
%65 = tail call i32 @usleep(i32 %64)
%66 = tail call i32 @sem_lock(i32 %6)
%67 = load i32, i32* %21
%68 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %67)
%69 = tail call i32 (...) @rand()
%70 = srem i32 %69, 100000
%71 = tail call i32 @usleep(i32 %70)
%72 = add nsw i32 %67, 1
store i32 %72, i32* %21
%73 = tail call i32 @sem_unlock(i32 %6)
%74 = tail call i32 (...) @rand()
%75 = srem i32 %74, 100000
%76 = tail call i32 @usleep(i32 %75)
%77 = tail call i32 @sem_lock(i32 %6)
%78 = load i32, i32* %21
%79 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %78)
%80 = tail call i32 (...) @rand()
%81 = srem i32 %80, 100000
%82 = tail call i32 @usleep(i32 %81)
%83 = add nsw i32 %78, 1
store i32 %83, i32* %21
%84 = tail call i32 @sem_unlock(i32 %6)
%85 = tail call i32 @_exit(i32 0)
unreachable
86: ; preds = %26
%87 = tail call i32 (...) @rand()
%88 = srem i32 %87, 100000
%89 = tail call i32 @usleep(i32 %88)
%90 = tail call i32 @sem_lock(i32 %6)
%91 = load i32, i32* %21
%92 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %91)
%93 = tail call i32 (...) @rand()
%94 = srem i32 %93, 100000
%95 = tail call i32 @usleep(i32 %94)
%96 = add nsw i32 %91, 1
store i32 %96, i32* %21
%97 = tail call i32 @sem_unlock(i32 %6)
%98 = tail call i32 (...) @rand()
%99 = srem i32 %98, 100000
%100 = tail call i32 @usleep(i32 %99)
%101 = tail call i32 @sem_lock(i32 %6)
%102 = load i32, i32* %21
%103 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %102)
%104 = tail call i32 (...) @rand()
%105 = srem i32 %104, 100000
%106 = tail call i32 @usleep(i32 %105)
%107 = add nsw i32 %102, 1
store i32 %107, i32* %21
%108 = tail call i32 @sem_unlock(i32 %6)
%109 = tail call i32 (...) @rand()
%110 = srem i32 %109, 100000
%111 = tail call i32 @usleep(i32 %110)
%112 = tail call i32 @sem_lock(i32 %6)
%113 = load i32, i32* %21
%114 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %113)
%115 = tail call i32 (...) @rand()
%116 = srem i32 %115, 100000
%117 = tail call i32 @usleep(i32 %116)
%118 = add nsw i32 %113, 1
store i32 %118, i32* %21
%119 = tail call i32 @sem_unlock(i32 %6)
%120 = tail call i32 (...) @rand()
%121 = srem i32 %120, 100000
%122 = tail call i32 @usleep(i32 %121)
%123 = tail call i32 @sem_lock(i32 %6)
%124 = load i32, i32* %21
%125 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %124)
%126 = tail call i32 (...) @rand()
%127 = srem i32 %126, 100000
%128 = tail call i32 @usleep(i32 %127)
%129 = add nsw i32 %124, 1
store i32 %129, i32* %21
%130 = tail call i32 @sem_unlock(i32 %6)
%131 = tail call i32 (...) @rand()
%132 = srem i32 %131, 100000
%133 = tail call i32 @usleep(i32 %132)
%134 = tail call i32 @sem_lock(i32 %6)
%135 = load i32, i32* %21
%136 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %135)
%137 = tail call i32 (...) @rand()
%138 = srem i32 %137, 100000
%139 = tail call i32 @usleep(i32 %138)
%140 = add nsw i32 %135, 1
store i32 %140, i32* %21
%141 = tail call i32 @sem_unlock(i32 %6)
%142 = tail call i32 @wait(i32* null)
%143 = load i32, i32* %21
%144 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %143)
br label %147
145: ; preds = %26
%146 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %147
147: ; preds = %86, %145
%148 = phi i32 [ 0, %86 ], [ 1, %145 ]
%149 = bitcast i32* %21 to i8*
%150 = tail call i32 @shmdt(i8* %149)
%151 = load i32, i32* @IPC_RMID
%152 = tail call i32 @shmctl(i32 %15, i32 %151, i32 0)
%153 = load i32, i32* @IPC_RMID
%154 = tail call i32 @semctl(i32 %6, i32 0, i32 %153, i32 0)
ret i32 %148
}