#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  scalar_t__ mode;
};
struct shmid_ds {
  TYPE_1__ shm_perm;
  scalar_t__ shm_nattch;
};
typedef scalar_t__ pid_t;
typedef int key_t;

/* Variables and functions */
scalar_t__ EIDRM;
scalar_t__ ENOSPC;
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ IPC_STAT;
void *atoi(char *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int random();
int /*<<< orphan*/ sched_yield();
unsigned char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
scalar_t__ shmdt(unsigned char *);
int shmget(int, int, int);
int /*<<< orphan*/ srandom(int /*<<< orphan*/);
int main(int ac, char **av) {
  int segs, size, proc, rmpr;
  unsigned long long iter;
  struct shmid_ds buf;
  pid_t pid;
  if (ac < 6) {
    printf("usage: icp001 segs size proc iter rm%%\n");
    exit(1);
  }
  segs = atoi(av[1]);
  size = atoi(av[2]);
  proc = atoi(av[3]);
  iter = atoi(av[4]);
  rmpr = atoi(av[5]);
  iter = 1 << iter;
  printf("using %d segs of size %d (%llu iterations)\n", segs, size, iter);
  while (--proc) {
    if ((pid = fork()) > 0) {
      printf("started process %d\n", (int)pid);
    } else {
      break;
    }
  }
  srandom(getpid());
  while (iter--) {
    key_t key;
    int seg, i, rm;
    unsigned char c, *ptr;
    volatile unsigned char *p;
    key = random() % segs + 1;
    if ((seg = shmget(key, size, IPC_CREAT | 0600)) == -1) {
      perror("shmget");
      if (errno != EIDRM && errno != ENOSPC)
        exit(1);
      continue;
    }
    if (1)
      sched_yield();
    if ((ptr = shmat(seg, 0, 0)) == (unsigned char *)-1) {
      perror("shmat");
      continue;
    }
    if (random() % 100 < rmpr) {
      if (random() % 1)
        rm = 1;
      else
        rm = -1;
    } else {
      rm = 0;
    }
    if (rm < 0 && shmctl(seg, IPC_RMID, NULL) == -1)
      perror("pre: shmctl IPC_RMID");
    for (p = ptr; p < ptr + size; p += 4097)
      *p = (unsigned char)(p - ptr);
    for (p = ptr; p < ptr + size; p += 4097) {
      c = *p;
      if (c == (unsigned char)(p - ptr))
        continue;
      shmctl(seg, IPC_STAT, &buf);
      printf("n=%i, m = %i: %i != %i", (int)buf.shm_nattch,
             (int)buf.shm_perm.mode, (int)(unsigned char)(p - ptr), (int)c);
      for (i = 0; i < 5; i++) {
        printf(", %i", (int)*p);
        sched_yield();
      }
      printf("\n");
    }
    if (shmdt(ptr) != 0) {
      perror("shmdt");
      exit(1);
    }
    if (rm > 0 && shmctl(seg, IPC_RMID, NULL) == -1)
      perror("post shmctl IPC_RMID");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x6,-0x8(%rbp)
jge    1208 <main+0x38>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   10d0 <atoi@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x28(%rbp),%rcx
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     12ce <main+0xfe>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jle    12c4 <main+0xf4>
mov    -0x40(%rbp),%rax
mov    %eax,%esi
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12c9 <main+0xf9>
jmp    12ce <main+0xfe>
jmp    1284 <main+0xb4>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%edi
call   1030 <srandom@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x28(%rbp)
cmp    $0x0,%rax
je     15c6 <main+0x3f6>
mov    $0x0,%al
call   1080 <random@plt>
cltd
idivl  -0x14(%rbp)
add    $0x1,%edx
mov    %edx,-0x44(%rbp)
mov    -0x44(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    0x2ca5(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
call   10c0 <shmget@plt>
mov    %eax,-0x48(%rbp)
cmp    $0xffffffff,%eax
jne    137b <main+0x1ab>
lea    0xd35(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c91(%rip),%rax
mov    (%rax),%rax
mov    0x2c8f(%rip),%rcx
cmp    (%rcx),%rax
je     1376 <main+0x1a6>
mov    0x2c77(%rip),%rax
mov    (%rax),%rax
mov    0x2c5d(%rip),%rcx
cmp    (%rcx),%rax
je     1376 <main+0x1a6>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    12dc <main+0x10c>
mov    $0x0,%al
call   1070 <sched_yield@plt>
mov    -0x48(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   10b0 <shmat@plt>
mov    %rax,-0x60(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    13b3 <main+0x1e3>
lea    0xcc6(%rip),%rdi
call   10a0 <perror@plt>
jmp    12dc <main+0x10c>
mov    $0x0,%al
call   1080 <random@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
cmp    -0x20(%rbp),%edx
jge    13fb <main+0x22b>
mov    $0x0,%al
call   1080 <random@plt>
mov    $0x1,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     13ef <main+0x21f>
movl   $0x1,-0x50(%rbp)
jmp    13f6 <main+0x226>
movl   $0xffffffff,-0x50(%rbp)
jmp    1402 <main+0x232>
movl   $0x0,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jge    1436 <main+0x266>
mov    -0x48(%rbp),%edi
mov    0x2bb2(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    1436 <main+0x266>
lea    0xc44(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
mov    -0x60(%rbp),%rcx
movslq -0x18(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    147c <main+0x2ac>
mov    -0x68(%rbp),%rax
mov    -0x60(%rbp),%rcx
sub    %rcx,%rax
mov    %al,%cl
mov    -0x68(%rbp),%rax
mov    %cl,(%rax)
mov    -0x68(%rbp),%rax
add    $0x1001,%rax
mov    %rax,-0x68(%rbp)
jmp    143e <main+0x26e>
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
mov    -0x60(%rbp),%rcx
movslq -0x18(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    1564 <main+0x394>
mov    -0x68(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x51(%rbp)
movzbl -0x51(%rbp),%eax
mov    -0x68(%rbp),%rcx
mov    -0x60(%rbp),%rdx
sub    %rdx,%rcx
movzbl %cl,%ecx
cmp    %ecx,%eax
jne    14c4 <main+0x2f4>
jmp    1551 <main+0x381>
mov    -0x48(%rbp),%edi
mov    0x2b12(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1090 <shmctl@plt>
mov    -0x30(%rbp),%rax
mov    %eax,%esi
mov    -0x38(%rbp),%rax
mov    %eax,%edx
mov    -0x68(%rbp),%rax
mov    -0x60(%rbp),%rcx
sub    %rcx,%rax
movzbl %al,%ecx
movzbl -0x51(%rbp),%r8d
lea    0xb8b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4c(%rbp)
cmpl   $0x5,-0x4c(%rbp)
jge    1543 <main+0x373>
mov    -0x68(%rbp),%rax
mov    (%rax),%al
movzbl %al,%esi
lea    0xb7a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <sched_yield@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    150d <main+0x33d>
lea    0xadc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x68(%rbp),%rax
add    $0x1001,%rax
mov    %rax,-0x68(%rbp)
jmp    1484 <main+0x2b4>
mov    -0x60(%rbp),%rdi
call   1040 <shmdt@plt>
cmp    $0x0,%rax
je     158d <main+0x3bd>
lea    0xb28(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
cmpl   $0x0,-0x50(%rbp)
jle    15c1 <main+0x3f1>
mov    -0x48(%rbp),%edi
mov    0x2a27(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    15c1 <main+0x3f1>
lea    0xaf0(%rip),%rdi
call   10a0 <perror@plt>
jmp    12dc <main+0x10c>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
cmp    $0x5,%edi
jle    1549 <main+0x369>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   10e0 <atoi@plt>
mov    %rax,%r12
mov    0x10(%rbp),%rdi
call   10e0 <atoi@plt>
mov    %rax,%r15
mov    %rax,0x8(%rsp)
mov    %eax,%r13d
mov    0x18(%rbp),%rdi
call   10e0 <atoi@plt>
mov    %rax,%rbx
mov    0x20(%rbp),%rdi
call   10e0 <atoi@plt>
mov    %rax,%r14
mov    0x28(%rbp),%rdi
call   10e0 <atoi@plt>
mov    %rax,0x20(%rsp)
mov    $0x1,%eax
mov    %r14d,%ecx
shl    %cl,%eax
movslq %eax,%rbp
lea    0xdd4(%rip),%rdi
mov    %r12,0x28(%rsp)
mov    %r12d,%esi
mov    %r13d,0x10(%rsp)
mov    %r13d,%edx
mov    %rbp,%rcx
xor    %eax,%eax
call   1070 <printf@plt>
add    $0xffffffff,%ebx
je     129d <main+0xbd>
lea    0xdda(%rip),%r14
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
jle    129d <main+0xbd>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0xffffffff,%ebx
jne    1280 <main+0xa0>
xor    %eax,%eax
call   1060 <getpid@plt>
mov    %eax,%edi
call   1040 <srandom@plt>
shlq   $0x20,0x8(%rsp)
movslq %r15d,%rax
mov    %rax,0x18(%rsp)
lea    0xde1(%rip),%r14
jmp    12f0 <main+0x110>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <shmdt@plt>
test   %rax,%rax
mov    0x30(%rsp),%rbp
jne    1533 <main+0x353>
add    $0xffffffffffffffff,%rbp
je     1522 <main+0x342>
xor    %eax,%eax
call   1090 <random@plt>
cltd
idivl  0x28(%rsp)
lea    0x1(%rdx),%edi
mov    0x2cb2(%rip),%rax
mov    (%rax),%edx
mov    $0x180,%eax
or     %eax,%edx
mov    0x10(%rsp),%esi
call   10d0 <shmget@plt>
cmp    $0xffffffff,%eax
je     14c8 <main+0x2e8>
mov    %eax,%r15d
xor    %eax,%eax
call   1080 <sched_yield@plt>
mov    %r15d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1500 <main+0x320>
mov    %rax,%rbx
mov    %rbp,0x30(%rsp)
xor    %eax,%eax
call   1090 <random@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x64,%ecx,%ecx
sub    %ecx,%eax
cmp    0x20(%rsp),%eax
jge    1394 <main+0x1b4>
xor    %eax,%eax
call   1090 <random@plt>
mov    0x2c49(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %edx,%edx
call   10a0 <shmctl@plt>
cmp    $0xffffffff,%eax
je     1511 <main+0x331>
mov    %r15d,0x14(%rsp)
cmpq   $0x0,0x8(%rsp)
jle    12d0 <main+0xf0>
mov    0x18(%rsp),%rax
lea    (%rbx,%rax,1),%rbp
xor    %eax,%eax
mov    %rbx,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %al,(%rcx)
add    $0x1001,%rcx
add    $0x1,%al
cmp    %rbp,%rcx
jb     13c0 <main+0x1e0>
cmpq   $0x0,0x8(%rsp)
jle    12d0 <main+0xf0>
xor    %r12d,%r12d
jmp    140a <main+0x22a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%rbx,%r12,1),%rax
add    $0x1001,%rax
add    $0x1001,%r12
cmp    %rbp,%rax
jae    12d0 <main+0xf0>
movzbl (%rbx,%r12,1),%r13d
movzbl %r12b,%r15d
cmp    %r13d,%r15d
je     13f0 <main+0x210>
mov    0x2bc1(%rip),%rax
mov    (%rax),%esi
mov    0x14(%rsp),%edi
lea    0x38(%rsp),%rdx
call   10a0 <shmctl@plt>
mov    0x38(%rsp),%edx
mov    0x40(%rsp),%esi
lea    0xc4c(%rip),%rdi
mov    %r15d,%ecx
mov    %r13d,%r8d
xor    %eax,%eax
call   1070 <printf@plt>
movzbl (%rbx,%r12,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1080 <sched_yield@plt>
movzbl (%rbx,%r12,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1080 <sched_yield@plt>
movzbl (%rbx,%r12,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1080 <sched_yield@plt>
movzbl (%rbx,%r12,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1080 <sched_yield@plt>
movzbl (%rbx,%r12,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1080 <sched_yield@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    13f0 <main+0x210>
lea    0xb99(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2af5(%rip),%rax
mov    (%rax),%rax
mov    0x2af3(%rip),%rcx
cmp    (%rcx),%rax
je     12e6 <main+0x106>
mov    0x2acb(%rip),%rcx
cmp    (%rcx),%rax
je     12e6 <main+0x106>
jmp    153f <main+0x35f>
lea    0xb68(%rip),%rdi
call   10b0 <perror@plt>
jmp    12e6 <main+0x106>
lea    0xb5d(%rip),%rdi
call   10b0 <perror@plt>
jmp    1394 <main+0x1b4>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb6c(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xab0(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.shmid_ds = type { %struct.TYPE_2__, i64 }
%struct.TYPE_2__ = type { i64 }
@.str = [40 x i8] c"usage: icp001 segs size proc iter rm%%\0A\00"
@.str.1 = [44 x i8] c"using %d segs of size %d (%llu iterations)\0A\00"
@.str.2 = [20 x i8] c"started process %d\0A\00"
@IPC_CREAT = global i32 0
@.str.3 = [7 x i8] c"shmget\00"
@errno = global i64 0
@EIDRM = global i64 0
@ENOSPC = global i64 0
@.str.4 = [6 x i8] c"shmat\00"
@IPC_RMID = global i32 0
@.str.5 = [21 x i8] c"pre: shmctl IPC_RMID\00"
@IPC_STAT = global i32 0
@.str.6 = [23 x i8] c"n=%i, m = %i: %i != %i\00"
@.str.7 = [5 x i8] c", %i\00"
@.str.8 = [2 x i8] c"\0A\00"
@.str.9 = [6 x i8] c"shmdt\00"
@.str.10 = [21 x i8] c"post shmctl IPC_RMID\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 i64
%11 = alloca %struct.shmid_ds
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i8
%18 = alloca i8*
%19 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 6
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i8* @atoi(i8* %28)
%30 = ptrtoint i8* %29 to i32
store i32 %30, i32* %6
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i8* @atoi(i8* %33)
%35 = ptrtoint i8* %34 to i32
store i32 %35, i32* %7
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 3
%38 = load i8*, i8** %37
%39 = call i8* @atoi(i8* %38)
%40 = ptrtoint i8* %39 to i32
store i32 %40, i32* %8
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 4
%43 = load i8*, i8** %42
%44 = call i8* @atoi(i8* %43)
%45 = ptrtoint i8* %44 to i64
store i64 %45, i64* %10
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 5
%48 = load i8*, i8** %47
%49 = call i8* @atoi(i8* %48)
%50 = ptrtoint i8* %49 to i32
store i32 %50, i32* %9
%51 = load i64, i64* %10
%52 = trunc i64 %51 to i32
%53 = shl i32 1, %52
%54 = sext i32 %53 to i64
store i64 %54, i64* %10
%55 = load i32, i32* %6
%56 = load i32, i32* %7
%57 = load i64, i64* %10
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %55, i32 %56, i64 %57)
br label %59
59: ; preds = %71, %25
%60 = load i32, i32* %8
%61 = add nsw i32 %60, -1
store i32 %61, i32* %8
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %72
63: ; preds = %59
%64 = call i64 (...) @fork()
store i64 %64, i64* %12
%65 = icmp sgt i64 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %63
%67 = load i64, i64* %12
%68 = trunc i64 %67 to i32
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %68)
br label %71
70: ; preds = %63
br label %72
71: ; preds = %66
br label %59
72: ; preds = %70, %59
%73 = call i32 (...) @getpid()
%74 = call i32 @srandom(i32 %73)
br label %75
75: ; preds = %230, %107, %101, %72
%76 = load i64, i64* %10
%77 = add i64 %76, -1
store i64 %77, i64* %10
%78 = icmp ne i64 %76, 0
br i1 %78, label %79, label %231
79: ; preds = %75
%80 = call i32 (...) @random()
%81 = load i32, i32* %6
%82 = srem i32 %80, %81
%83 = add nsw i32 %82, 1
store i32 %83, i32* %13
%84 = load i32, i32* %13
%85 = load i32, i32* %7
%86 = load i32, i32* @IPC_CREAT
%87 = or i32 %86, 384
%88 = call i32 @shmget(i32 %84, i32 %85, i32 %87)
store i32 %88, i32* %14
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %102
90: ; preds = %79
%91 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%92 = load i64, i64* @errno
%93 = load i64, i64* @EIDRM
%94 = icmp ne i64 %92, %93
br i1 %94, label %95, label %101
95: ; preds = %90
%96 = load i64, i64* @errno
%97 = load i64, i64* @ENOSPC
%98 = icmp ne i64 %96, %97
br i1 %98, label %99, label %101
99: ; preds = %95
%100 = call i32 @exit(i32 1)
unreachable
101: ; preds = %95, %90
br label %75
102: ; preds = %79
%103 = call i32 (...) @sched_yield()
%104 = load i32, i32* %14
%105 = call i8* @shmat(i32 %104, i32 0, i32 0)
store i8* %105, i8** %18
%106 = icmp eq i8* %105, inttoptr (i64 -1 to i8*)
br i1 %106, label %107, label %109
107: ; preds = %102
%108 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %75
109: ; preds = %102
%110 = call i32 (...) @random()
%111 = srem i32 %110, 100
%112 = load i32, i32* %9
%113 = icmp slt i32 %111, %112
br i1 %113, label %114, label %121
114: ; preds = %109
%115 = call i32 (...) @random()
%116 = srem i32 %115, 1
%117 = icmp ne i32 %116, 0
br i1 %117, label %118, label %119
118: ; preds = %114
store i32 1, i32* %16
br label %120
119: ; preds = %114
store i32 -1, i32* %16
br label %120
120: ; preds = %119, %118
br label %122
121: ; preds = %109
store i32 0, i32* %16
br label %122
122: ; preds = %121, %120
%123 = load i32, i32* %16
%124 = icmp slt i32 %123, 0
br i1 %124, label %125, label %132
125: ; preds = %122
%126 = load i32, i32* %14
%127 = load i32, i32* @IPC_RMID
%128 = call i32 @shmctl(i32 %126, i32 %127, %struct.shmid_ds* null)
%129 = icmp eq i32 %128, -1
br i1 %129, label %130, label %132
130: ; preds = %125
%131 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %132
132: ; preds = %130, %125, %122
%133 = load i8*, i8** %18
store i8* %133, i8** %19
br label %134
134: ; preds = %149, %132
%135 = load i8*, i8** %19
%136 = load i8*, i8** %18
%137 = load i32, i32* %7
%138 = sext i32 %137 to i64
%139 = getelementptr inbounds i8, i8* %136, i64 %138
%140 = icmp ult i8* %135, %139
br i1 %140, label %141, label %152
141: ; preds = %134
%142 = load i8*, i8** %19
%143 = load i8*, i8** %18
%144 = ptrtoint i8* %142 to i64
%145 = ptrtoint i8* %143 to i64
%146 = sub i64 %144, %145
%147 = trunc i64 %146 to i8
%148 = load i8*, i8** %19
store volatile i8 %147, i8* %148
br label %149
149: ; preds = %141
%150 = load i8*, i8** %19
%151 = getelementptr inbounds i8, i8* %150, i64 4097
store i8* %151, i8** %19
br label %134
152: ; preds = %134
%153 = load i8*, i8** %18
store i8* %153, i8** %19
br label %154
154: ; preds = %210, %152
%155 = load i8*, i8** %19
%156 = load i8*, i8** %18
%157 = load i32, i32* %7
%158 = sext i32 %157 to i64
%159 = getelementptr inbounds i8, i8* %156, i64 %158
%160 = icmp ult i8* %155, %159
br i1 %160, label %161, label %213
161: ; preds = %154
%162 = load i8*, i8** %19
%163 = load volatile i8, i8* %162
store i8 %163, i8* %17
%164 = load i8, i8* %17
%165 = zext i8 %164 to i32
%166 = load i8*, i8** %19
%167 = load i8*, i8** %18
%168 = ptrtoint i8* %166 to i64
%169 = ptrtoint i8* %167 to i64
%170 = sub i64 %168, %169
%171 = trunc i64 %170 to i8
%172 = zext i8 %171 to i32
%173 = icmp eq i32 %165, %172
br i1 %173, label %174, label %175
174: ; preds = %161
br label %210
175: ; preds = %161
%176 = load i32, i32* %14
%177 = load i32, i32* @IPC_STAT
%178 = call i32 @shmctl(i32 %176, i32 %177, %struct.shmid_ds* %11)
%179 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %11, i32 0, i32 1
%180 = load i64, i64* %179
%181 = trunc i64 %180 to i32
%182 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %11, i32 0, i32 0
%183 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %182, i32 0, i32 0
%184 = load i64, i64* %183
%185 = trunc i64 %184 to i32
%186 = load i8*, i8** %19
%187 = load i8*, i8** %18
%188 = ptrtoint i8* %186 to i64
%189 = ptrtoint i8* %187 to i64
%190 = sub i64 %188, %189
%191 = trunc i64 %190 to i8
%192 = zext i8 %191 to i32
%193 = load i8, i8* %17
%194 = zext i8 %193 to i32
%195 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %181, i32 %185, i32 %192, i32 %194)
store i32 0, i32* %15
br label %196
196: ; preds = %205, %175
%197 = load i32, i32* %15
%198 = icmp slt i32 %197, 5
br i1 %198, label %199, label %208
199: ; preds = %196
%200 = load i8*, i8** %19
%201 = load volatile i8, i8* %200
%202 = zext i8 %201 to i32
%203 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %202)
%204 = call i32 (...) @sched_yield()
br label %205
205: ; preds = %199
%206 = load i32, i32* %15
%207 = add nsw i32 %206, 1
store i32 %207, i32* %15
br label %196
208: ; preds = %196
%209 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
br label %210
210: ; preds = %208, %174
%211 = load i8*, i8** %19
%212 = getelementptr inbounds i8, i8* %211, i64 4097
store i8* %212, i8** %19
br label %154
213: ; preds = %154
%214 = load i8*, i8** %18
%215 = call i64 @shmdt(i8* %214)
%216 = icmp ne i64 %215, 0
br i1 %216, label %217, label %220
217: ; preds = %213
%218 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%219 = call i32 @exit(i32 1)
unreachable
220: ; preds = %213
%221 = load i32, i32* %16
%222 = icmp sgt i32 %221, 0
br i1 %222, label %223, label %230
223: ; preds = %220
%224 = load i32, i32* %14
%225 = load i32, i32* @IPC_RMID
%226 = call i32 @shmctl(i32 %224, i32 %225, %struct.shmid_ds* null)
%227 = icmp eq i32 %226, -1
br i1 %227, label %228, label %230
228: ; preds = %223
%229 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0))
br label %230
230: ; preds = %228, %223, %220
br label %75
231: ; preds = %75
ret i32 0
}

LLVM IR (-O2):
%struct.shmid_ds = type { %struct.TYPE_2__, i64 }
%struct.TYPE_2__ = type { i64 }
@.str = [40 x i8] c"usage: icp001 segs size proc iter rm%%\0A\00"
@.str.1 = [44 x i8] c"using %d segs of size %d (%llu iterations)\0A\00"
@.str.2 = [20 x i8] c"started process %d\0A\00"
@IPC_CREAT = global i32 0
@.str.3 = [7 x i8] c"shmget\00"
@errno = global i64 0
@EIDRM = global i64 0
@ENOSPC = global i64 0
@.str.4 = [6 x i8] c"shmat\00"
@IPC_RMID = global i32 0
@.str.5 = [21 x i8] c"pre: shmctl IPC_RMID\00"
@IPC_STAT = global i32 0
@.str.6 = [23 x i8] c"n=%i, m = %i: %i != %i\00"
@.str.7 = [5 x i8] c", %i\00"
@.str.9 = [6 x i8] c"shmdt\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.shmid_ds
%4 = bitcast %struct.shmid_ds* %3 to i8*
%5 = icmp slt i32 %0, 6
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i8* @atoi(i8* %11)
%13 = ptrtoint i8* %12 to i64
%14 = trunc i64 %13 to i32
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i8* @atoi(i8* %16)
%18 = ptrtoint i8* %17 to i64
%19 = trunc i64 %18 to i32
%20 = getelementptr inbounds i8*, i8** %1, i64 3
%21 = load i8*, i8** %20
%22 = tail call i8* @atoi(i8* %21)
%23 = ptrtoint i8* %22 to i64
%24 = trunc i64 %23 to i32
%25 = getelementptr inbounds i8*, i8** %1, i64 4
%26 = load i8*, i8** %25
%27 = tail call i8* @atoi(i8* %26)
%28 = ptrtoint i8* %27 to i64
%29 = getelementptr inbounds i8*, i8** %1, i64 5
%30 = load i8*, i8** %29
%31 = tail call i8* @atoi(i8* %30)
%32 = ptrtoint i8* %31 to i64
%33 = trunc i64 %32 to i32
%34 = trunc i64 %28 to i32
%35 = shl nuw i32 1, %34
%36 = sext i32 %35 to i64
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %19, i64 %36)
%38 = add nsw i32 %24, -1
%39 = icmp eq i32 %38, 0
br i1 %39, label %49, label %40
40: ; preds = %9, %44
%41 = phi i32 [ %47, %44 ], [ %38, %9 ]
%42 = tail call i64 (...) @fork()
%43 = icmp sgt i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = trunc i64 %42 to i32
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = add nsw i32 %41, -1
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %40
49: ; preds = %44, %40, %9
%50 = tail call i32 (...) @getpid()
%51 = tail call i32 @srandom(i32 %50)
%52 = shl i64 %18, 32
%53 = ashr exact i64 %52, 32
%54 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 1
%55 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 0, i32 0
%56 = icmp sgt i64 %52, 0
br label %57
57: ; preds = %49, %155
%58 = phi i64 [ %36, %49 ], [ %59, %155 ]
%59 = add i64 %58, -1
%60 = call i32 (...) @random()
%61 = srem i32 %60, %14
%62 = add nsw i32 %61, 1
%63 = load i32, i32* @IPC_CREAT
%64 = or i32 %63, 384
%65 = call i32 @shmget(i32 %62, i32 %19, i32 %64)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %77
67: ; preds = %57
%68 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%69 = load i64, i64* @errno
%70 = load i64, i64* @EIDRM
%71 = icmp eq i64 %69, %70
%72 = load i64, i64* @ENOSPC
%73 = icmp eq i64 %69, %72
%74 = select i1 %71, i1 true, i1 %73
br i1 %74, label %155, label %75
75: ; preds = %67
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %57
%78 = call i32 (...) @sched_yield()
%79 = call i8* @shmat(i32 %65, i32 0, i32 0)
%80 = icmp eq i8* %79, inttoptr (i64 -1 to i8*)
br i1 %80, label %81, label %83
81: ; preds = %77
%82 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %155
83: ; preds = %77
%84 = call i32 (...) @random()
%85 = srem i32 %84, 100
%86 = icmp slt i32 %85, %33
br i1 %86, label %87, label %94
87: ; preds = %83
%88 = call i32 (...) @random()
%89 = load i32, i32* @IPC_RMID
%90 = call i32 @shmctl(i32 %65, i32 %89, %struct.shmid_ds* null)
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %94
92: ; preds = %87
%93 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %94
94: ; preds = %83, %92, %87
%95 = getelementptr inbounds i8, i8* %79, i64 %53
br i1 %56, label %96, label %149
96: ; preds = %94
%97 = ptrtoint i8* %79 to i64
br label %101
98: ; preds = %101
br i1 %56, label %99, label %149
99: ; preds = %98
%100 = ptrtoint i8* %79 to i64
br label %108
101: ; preds = %96, %101
%102 = phi i8* [ %79, %96 ], [ %106, %101 ]
%103 = ptrtoint i8* %102 to i64
%104 = sub i64 %103, %97
%105 = trunc i64 %104 to i8
store volatile i8 %105, i8* %102
%106 = getelementptr inbounds i8, i8* %102, i64 4097
%107 = icmp ult i8* %106, %95
br i1 %107, label %101, label %98
108: ; preds = %99, %146
%109 = phi i8* [ %79, %99 ], [ %147, %146 ]
%110 = load volatile i8, i8* %109
%111 = zext i8 %110 to i32
%112 = ptrtoint i8* %109 to i64
%113 = sub i64 %112, %100
%114 = trunc i64 %113 to i32
%115 = and i32 %114, 255
%116 = icmp eq i32 %115, %111
br i1 %116, label %146, label %117
117: ; preds = %108
%118 = load i32, i32* @IPC_STAT
%119 = call i32 @shmctl(i32 %65, i32 %118, %struct.shmid_ds* %3)
%120 = load i64, i64* %54
%121 = trunc i64 %120 to i32
%122 = load i64, i64* %55
%123 = trunc i64 %122 to i32
%124 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %121, i32 %123, i32 %115, i32 %111)
%125 = load volatile i8, i8* %109
%126 = zext i8 %125 to i32
%127 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %126)
%128 = call i32 (...) @sched_yield()
%129 = load volatile i8, i8* %109
%130 = zext i8 %129 to i32
%131 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %130)
%132 = call i32 (...) @sched_yield()
%133 = load volatile i8, i8* %109
%134 = zext i8 %133 to i32
%135 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %134)
%136 = call i32 (...) @sched_yield()
%137 = load volatile i8, i8* %109
%138 = zext i8 %137 to i32
%139 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %138)
%140 = call i32 (...) @sched_yield()
%141 = load volatile i8, i8* %109
%142 = zext i8 %141 to i32
%143 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %142)
%144 = call i32 (...) @sched_yield()
%145 = call i32 @putchar(i32 10)
br label %146
146: ; preds = %108, %117
%147 = getelementptr inbounds i8, i8* %109, i64 4097
%148 = icmp ult i8* %147, %95
br i1 %148, label %108, label %149
149: ; preds = %146, %94, %98
%150 = call i64 @shmdt(i8* %79)
%151 = icmp eq i64 %150, 0
br i1 %151, label %155, label %152
152: ; preds = %149
%153 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%154 = call i32 @exit(i32 1)
unreachable
155: ; preds = %149, %67, %81
%156 = icmp eq i64 %59, 0
br i1 %156, label %157, label %57
157: ; preds = %155
ret i32 0
}