#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*/ key_t;

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftok(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ shmdt(char *);
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[], char *env[]) {
  char *array;
  int shmid;
  char pathname[] = "lab07.c";
  key_t key;
  if ((key = ftok(pathname, 0)) < 0) {
    printf("Can\'t generate key\n");
    exit(-1);
  }
  int i = 1000;
  if (argc > 1) {
    if ((shmid = shmget(key, (3 + 10) * sizeof(char),
                        0666 | IPC_CREAT | IPC_EXCL)) < 0) {
      perror("shmget");
      exit(-1);
    }
    if ((array = (char *)shmat(shmid, NULL, 0)) == (char *)(-1)) {
      perror("shmat");
      exit(-1);
    }
    array[0] = 0;
    array[1] = 0;
    array[3] = 0;
    if (shmdt(array) < 0) {
      perror("shmdt");
      exit(-1);
    }
    while (i > 0) {
      sleep(1);
      if ((shmid = shmget(key, (3 + 10) * sizeof(char), 0)) < 0) {
        perror("shmget");
        exit(-1);
      }
      if ((array = (char *)shmat(shmid, NULL, 0)) == (char *)(-1)) {
        perror("shmat");
        exit(-1);
      }
      array[0] = 1;
      array[2] = 1;
      while (array[1] && array[2] == 1)
        ;
      strncpy(array + 3 * sizeof(char), argv[1], 10);
      array[3] = (char)(i % 255);
      printf("w:%s %d %d\n", argv[1], array[3], i);
      array[0] = 0;
      if (shmdt(array) < 0) {
        perror("shmdt");
        exit(-1);
      }
      i--;
    }
  } else {
    while (i > 0) {
      sleep(1);
      if ((shmid = shmget(key, (3 + 10) * sizeof(char), 0)) < 0) {
        perror("shmget");
        exit(-1);
      }
      if ((array = (char *)shmat(shmid, NULL, 0666 | IPC_CREAT)) ==
          (char *)(-1)) {
        perror("shmat");
        exit(-1);
      }
      array[1] = 1;
      array[2] = 0;
      while (array[0] && array[2] == 0)
        ;
      char msg[10];
      strncpy(msg, array + 3 * sizeof(char), 10);
      printf("r:%s %d %d\n", msg, array[3], i);
      array[1] = 0;
      if (shmdt(array) < 0) {
        perror("shmdt");
        exit(-1);
      }
      shmctl(shmid, IPC_RMID, NULL);
      i--;
    }
  }
  return 0;
}


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)
mov    %rdx,-0x18(%rbp)
mov    0xe3f(%rip),%rax
mov    %rax,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
call   10b0 <ftok@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    11f4 <main+0x54>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
movl   $0x3e8,-0x34(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1423 <main+0x283>
mov    -0x30(%rbp),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    0x2dd2(%rip),%rax
or     (%rax),%edx
mov    $0xd,%esi
call   1090 <shmget@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    124c <main+0xac>
lea    0xddf(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1284 <main+0xe4>
lea    0xdae(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x20(%rbp),%rax
movb   $0x0,0x3(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <shmdt@plt>
cmp    $0x0,%rax
jge    12c4 <main+0x124>
lea    0xd74(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    12c9 <main+0x129>
cmpl   $0x0,-0x34(%rbp)
jle    141e <main+0x27e>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    -0x30(%rbp),%edi
mov    $0xd,%esi
xor    %edx,%edx
call   1090 <shmget@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    130e <main+0x16e>
lea    0xd1d(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1346 <main+0x1a6>
lea    0xcec(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x1,0x2(%rax)
mov    -0x20(%rbp),%rax
movsbl 0x1(%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x3f(%rbp)
je     137c <main+0x1dc>
mov    -0x20(%rbp),%rax
movsbl 0x2(%rax),%eax
cmp    $0x1,%eax
sete   %al
mov    %al,-0x3f(%rbp)
mov    -0x3f(%rbp),%al
test   $0x1,%al
jne    138c <main+0x1ec>
jmp    1391 <main+0x1f1>
jmp    1355 <main+0x1b5>
mov    -0x20(%rbp),%rdi
add    $0x3,%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
mov    -0x34(%rbp),%eax
mov    $0xff,%ecx
cltd
idiv   %ecx
mov    %dl,%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x3(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x20(%rbp),%rax
movsbl 0x3(%rax),%edx
mov    -0x34(%rbp),%ecx
lea    0xc56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <shmdt@plt>
cmp    $0x0,%rax
jge    1410 <main+0x270>
lea    0xc28(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    12c9 <main+0x129>
jmp    1589 <main+0x3e9>
jmp    1428 <main+0x288>
cmpl   $0x0,-0x34(%rbp)
jle    1584 <main+0x3e4>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    -0x30(%rbp),%edi
mov    $0xd,%esi
xor    %edx,%edx
call   1090 <shmget@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    146d <main+0x2cd>
lea    0xbbe(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%edi
mov    0x2b59(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    14b2 <main+0x312>
lea    0xb80(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rax
movb   $0x1,0x1(%rax)
mov    -0x20(%rbp),%rax
movb   $0x0,0x2(%rax)
mov    -0x20(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x40(%rbp)
je     14e8 <main+0x348>
mov    -0x20(%rbp),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
sete   %al
mov    %al,-0x40(%rbp)
mov    -0x40(%rbp),%al
test   $0x1,%al
jne    14f8 <main+0x358>
jmp    14fd <main+0x35d>
jmp    14c2 <main+0x322>
lea    -0x3e(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x3,%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
lea    -0x3e(%rbp),%rsi
mov    -0x20(%rbp),%rax
movsbl 0x3(%rax),%edx
mov    -0x34(%rbp),%ecx
lea    0xb12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <shmdt@plt>
cmp    $0x0,%rax
jge    1561 <main+0x3c1>
lea    0xad7(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%edi
mov    0x2a6d(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <shmctl@plt>
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    1428 <main+0x288>
jmp    1589 <main+0x3e9>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,0x8(%rsp)
mov    %edi,%ebx
movabs $0x632e373062616c,%rax
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   10c0 <ftok@plt>
mov    %eax,(%rsp)
test   %eax,%eax
js     145a <main+0x2aa>
cmp    $0x1,%ebx
jle    1352 <main+0x1a2>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    0x2dec(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    (%rsp),%edi
mov    $0xd,%esi
call   10a0 <shmget@plt>
test   %eax,%eax
js     1432 <main+0x282>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     143b <main+0x28b>
movw   $0x0,(%rax)
movb   $0x0,0x3(%rax)
mov    %rax,%rdi
call   1050 <shmdt@plt>
test   %rax,%rax
js     1444 <main+0x294>
mov    $0x3e8,%r15d
mov    $0xe8000000,%r14d
mov    $0x3e9,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    (%rsp),%edi
mov    $0xd,%esi
xor    %edx,%edx
call   10a0 <shmget@plt>
test   %eax,%eax
js     1432 <main+0x282>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     143b <main+0x28b>
mov    %rax,%rbx
movb   $0x1,(%rax)
movb   $0x1,0x2(%rax)
cmpb   $0x0,0x1(%rax)
jne    1350 <main+0x1a0>
mov    %r15d,%ebp
mov    $0x80808081,%eax
imul   %rax,%rbp
shr    $0x27,%rbp
mov    %ebp,%r12d
add    %r15b,%r12b
shl    $0x18,%ebp
add    %r14d,%ebp
add    $0xffffffff,%r13d
mov    %r13d,0x4(%rsp)
mov    %rbx,%rdi
add    $0x3,%rdi
mov    0x8(%rsp),%r13
mov    0x8(%r13),%rsi
mov    $0xa,%edx
call   1030 <strncpy@plt>
mov    %r12b,0x3(%rbx)
mov    0x8(%r13),%rsi
mov    0x4(%rsp),%r13d
sar    $0x18,%ebp
lea    0xd03(%rip),%rdi
mov    %ebp,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
movb   $0x0,(%rbx)
mov    %rbx,%rdi
call   1050 <shmdt@plt>
test   %rax,%rax
js     1444 <main+0x294>
add    $0xffffffff,%r15d
add    $0xff000000,%r14d
cmp    $0x2,%r13d
jae    1270 <main+0xc0>
jmp    1417 <main+0x267>
nopw   0x0(%rax,%rax,1)
jmp    1350 <main+0x1a0>
mov    $0x3e9,%r13d
lea    0x1e(%rsp),%r12
lea    0xcbb(%rip),%r15
mov    0x2c6d(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    (%rsp),%edi
mov    $0xd,%esi
xor    %edx,%edx
call   10a0 <shmget@plt>
test   %eax,%eax
js     1432 <main+0x282>
mov    %eax,%ebp
mov    0x2c36(%rip),%rax
mov    (%rax),%edx
mov    $0x1b6,%eax
or     %eax,%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     143b <main+0x28b>
mov    %rax,%rbx
movw   $0x1,0x1(%rax)
cmpb   $0x0,(%rax)
jne    1430 <main+0x280>
add    $0xffffffff,%r13d
mov    %rbx,%rsi
add    $0x3,%rsi
mov    %r12,%rdi
mov    $0xa,%edx
call   1030 <strncpy@plt>
movsbl 0x3(%rbx),%edx
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13d,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
movb   $0x0,0x1(%rbx)
mov    %rbx,%rdi
call   1050 <shmdt@plt>
test   %rax,%rax
js     1444 <main+0x294>
mov    (%r14),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
cmp    $0x1,%r13d
ja     1370 <main+0x1c0>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
nopl   0x0(%rax,%rax,1)
jmp    1430 <main+0x280>
lea    0xbc7(%rip),%rdi
jmp    144b <main+0x29b>
lea    0xbc5(%rip),%rdi
jmp    144b <main+0x29b>
lea    0xbc2(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xbca(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@__const.main.pathname = [8 x i8] c"lab07.c\00"
@.str = [20 x i8] c"Can't generate key\0A\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [6 x i8] c"shmat\00"
@.str.3 = [6 x i8] c"shmdt\00"
@.str.4 = [12 x i8] c"w:%s %d %d\0A\00"
@.str.5 = [12 x i8] c"r:%s %d %d\0A\00"
@IPC_RMID = global i32 0
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [8 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca [10 x i8]
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%14 = bitcast [8 x i8]* %10 to i8*
%15 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%16 = call i32 @ftok(i8* %15, i32 0)
store i32 %16, i32* %11
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %3
store i32 1000, i32* %12
%22 = load i32, i32* %5
%23 = icmp sgt i32 %22, 1
br i1 %23, label %24, label %129
24: ; preds = %21
%25 = load i32, i32* %11
%26 = load i32, i32* @IPC_CREAT
%27 = or i32 438, %26
%28 = load i32, i32* @IPC_EXCL
%29 = or i32 %27, %28
%30 = call i32 @shmget(i32 %25, i32 13, i32 %29)
store i32 %30, i32* %9
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %24
%36 = load i32, i32* %9
%37 = call i64 @shmat(i32 %36, i32* null, i32 0)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %8
%39 = icmp eq i8* %38, inttoptr (i64 -1 to i8*)
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 -1)
unreachable
43: ; preds = %35
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i64 0
store i8 0, i8* %45
%46 = load i8*, i8** %8
%47 = getelementptr inbounds i8, i8* %46, i64 1
store i8 0, i8* %47
%48 = load i8*, i8** %8
%49 = getelementptr inbounds i8, i8* %48, i64 3
store i8 0, i8* %49
%50 = load i8*, i8** %8
%51 = call i64 @shmdt(i8* %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %43
%54 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 @exit(i32 -1)
unreachable
56: ; preds = %43
br label %57
57: ; preds = %125, %56
%58 = load i32, i32* %12
%59 = icmp sgt i32 %58, 0
br i1 %59, label %60, label %128
60: ; preds = %57
%61 = call i32 @sleep(i32 1)
%62 = load i32, i32* %11
%63 = call i32 @shmget(i32 %62, i32 13, i32 0)
store i32 %63, i32* %9
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %60
%66 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%67 = call i32 @exit(i32 -1)
unreachable
68: ; preds = %60
%69 = load i32, i32* %9
%70 = call i64 @shmat(i32 %69, i32* null, i32 0)
%71 = inttoptr i64 %70 to i8*
store i8* %71, i8** %8
%72 = icmp eq i8* %71, inttoptr (i64 -1 to i8*)
br i1 %72, label %73, label %76
73: ; preds = %68
%74 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%75 = call i32 @exit(i32 -1)
unreachable
76: ; preds = %68
%77 = load i8*, i8** %8
%78 = getelementptr inbounds i8, i8* %77, i64 0
store i8 1, i8* %78
%79 = load i8*, i8** %8
%80 = getelementptr inbounds i8, i8* %79, i64 2
store i8 1, i8* %80
br label %81
81: ; preds = %95, %76
%82 = load i8*, i8** %8
%83 = getelementptr inbounds i8, i8* %82, i64 1
%84 = load i8, i8* %83
%85 = sext i8 %84 to i32
%86 = icmp ne i32 %85, 0
br i1 %86, label %87, label %93
87: ; preds = %81
%88 = load i8*, i8** %8
%89 = getelementptr inbounds i8, i8* %88, i64 2
%90 = load i8, i8* %89
%91 = sext i8 %90 to i32
%92 = icmp eq i32 %91, 1
br label %93
93: ; preds = %87, %81
%94 = phi i1 [ false, %81 ], [ %92, %87 ]
br i1 %94, label %95, label %96
95: ; preds = %93
br label %81
96: ; preds = %93
%97 = load i8*, i8** %8
%98 = getelementptr inbounds i8, i8* %97, i64 3
%99 = load i8**, i8*** %6
%100 = getelementptr inbounds i8*, i8** %99, i64 1
%101 = load i8*, i8** %100
%102 = call i32 @strncpy(i8* %98, i8* %101, i32 10)
%103 = load i32, i32* %12
%104 = srem i32 %103, 255
%105 = trunc i32 %104 to i8
%106 = load i8*, i8** %8
%107 = getelementptr inbounds i8, i8* %106, i64 3
store i8 %105, i8* %107
%108 = load i8**, i8*** %6
%109 = getelementptr inbounds i8*, i8** %108, i64 1
%110 = load i8*, i8** %109
%111 = load i8*, i8** %8
%112 = getelementptr inbounds i8, i8* %111, i64 3
%113 = load i8, i8* %112
%114 = sext i8 %113 to i32
%115 = load i32, i32* %12
%116 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %110, i32 %114, i32 %115)
%117 = load i8*, i8** %8
%118 = getelementptr inbounds i8, i8* %117, i64 0
store i8 0, i8* %118
%119 = load i8*, i8** %8
%120 = call i64 @shmdt(i8* %119)
%121 = icmp slt i64 %120, 0
br i1 %121, label %122, label %125
122: ; preds = %96
%123 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%124 = call i32 @exit(i32 -1)
unreachable
125: ; preds = %96
%126 = load i32, i32* %12
%127 = add nsw i32 %126, -1
store i32 %127, i32* %12
br label %57
128: ; preds = %57
br label %198
129: ; preds = %21
br label %130
130: ; preds = %191, %129
%131 = load i32, i32* %12
%132 = icmp sgt i32 %131, 0
br i1 %132, label %133, label %197
133: ; preds = %130
%134 = call i32 @sleep(i32 1)
%135 = load i32, i32* %11
%136 = call i32 @shmget(i32 %135, i32 13, i32 0)
store i32 %136, i32* %9
%137 = icmp slt i32 %136, 0
br i1 %137, label %138, label %141
138: ; preds = %133
%139 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%140 = call i32 @exit(i32 -1)
unreachable
141: ; preds = %133
%142 = load i32, i32* %9
%143 = load i32, i32* @IPC_CREAT
%144 = or i32 438, %143
%145 = call i64 @shmat(i32 %142, i32* null, i32 %144)
%146 = inttoptr i64 %145 to i8*
store i8* %146, i8** %8
%147 = icmp eq i8* %146, inttoptr (i64 -1 to i8*)
br i1 %147, label %148, label %151
148: ; preds = %141
%149 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%150 = call i32 @exit(i32 -1)
unreachable
151: ; preds = %141
%152 = load i8*, i8** %8
%153 = getelementptr inbounds i8, i8* %152, i64 1
store i8 1, i8* %153
%154 = load i8*, i8** %8
%155 = getelementptr inbounds i8, i8* %154, i64 2
store i8 0, i8* %155
br label %156
156: ; preds = %170, %151
%157 = load i8*, i8** %8
%158 = getelementptr inbounds i8, i8* %157, i64 0
%159 = load i8, i8* %158
%160 = sext i8 %159 to i32
%161 = icmp ne i32 %160, 0
br i1 %161, label %162, label %168
162: ; preds = %156
%163 = load i8*, i8** %8
%164 = getelementptr inbounds i8, i8* %163, i64 2
%165 = load i8, i8* %164
%166 = sext i8 %165 to i32
%167 = icmp eq i32 %166, 0
br label %168
168: ; preds = %162, %156
%169 = phi i1 [ false, %156 ], [ %167, %162 ]
br i1 %169, label %170, label %171
170: ; preds = %168
br label %156
171: ; preds = %168
%172 = getelementptr inbounds [10 x i8], [10 x i8]* %13, i64 0, i64 0
%173 = load i8*, i8** %8
%174 = getelementptr inbounds i8, i8* %173, i64 3
%175 = call i32 @strncpy(i8* %172, i8* %174, i32 10)
%176 = getelementptr inbounds [10 x i8], [10 x i8]* %13, i64 0, i64 0
%177 = load i8*, i8** %8
%178 = getelementptr inbounds i8, i8* %177, i64 3
%179 = load i8, i8* %178
%180 = sext i8 %179 to i32
%181 = load i32, i32* %12
%182 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %176, i32 %180, i32 %181)
%183 = load i8*, i8** %8
%184 = getelementptr inbounds i8, i8* %183, i64 1
store i8 0, i8* %184
%185 = load i8*, i8** %8
%186 = call i64 @shmdt(i8* %185)
%187 = icmp slt i64 %186, 0
br i1 %187, label %188, label %191
188: ; preds = %171
%189 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%190 = call i32 @exit(i32 -1)
unreachable
191: ; preds = %171
%192 = load i32, i32* %9
%193 = load i32, i32* @IPC_RMID
%194 = call i32 @shmctl(i32 %192, i32 %193, i32* null)
%195 = load i32, i32* %12
%196 = add nsw i32 %195, -1
store i32 %196, i32* %12
br label %130
197: ; preds = %130
br label %198
198: ; preds = %197, %128
ret i32 0
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [6 x i8] c"shmat\00"
@.str.3 = [6 x i8] c"shmdt\00"
@.str.4 = [12 x i8] c"w:%s %d %d\0A\00"
@.str.5 = [12 x i8] c"r:%s %d %d\0A\00"
@IPC_RMID = global i32 0
@str = [19 x i8] c"Can't generate key\00"
define i32 @main(i32 %0, i8** nocapture %1, i8** nocapture readnone %2) {
%4 = alloca i64
%5 = alloca [10 x i8]
%6 = bitcast i64* %4 to i8*
store i64 27916837264187756, i64* %4
%7 = call i32 @ftok(i8* %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %3
%13 = icmp sgt i32 %0, 1
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
br label %82
16: ; preds = %12
%17 = load i32, i32* @IPC_CREAT
%18 = load i32, i32* @IPC_EXCL
%19 = or i32 %17, %18
%20 = or i32 %19, 438
%21 = call i32 @shmget(i32 %7, i32 13, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %16
%27 = call i64 @shmat(i32 %21, i32* null, i32 0)
%28 = inttoptr i64 %27 to i8*
%29 = icmp eq i8* %28, inttoptr (i64 -1 to i8*)
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %26
store i8 0, i8* %28
%34 = getelementptr inbounds i8, i8* %28, i64 1
store i8 0, i8* %34
%35 = getelementptr inbounds i8, i8* %28, i64 3
store i8 0, i8* %35
%36 = call i64 @shmdt(i8* %28)
%37 = icmp slt i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %33
%39 = getelementptr inbounds i8*, i8** %1, i64 1
br label %46
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 -1)
unreachable
43: ; preds = %67
%44 = add nsw i32 %47, -1
%45 = icmp ugt i32 %47, 1
br i1 %45, label %46, label %121
46: ; preds = %38, %43
%47 = phi i32 [ 1000, %38 ], [ %44, %43 ]
%48 = call i32 @sleep(i32 1)
%49 = call i32 @shmget(i32 %7, i32 13, i32 0)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%53 = call i32 @exit(i32 -1)
unreachable
54: ; preds = %46
%55 = call i64 @shmat(i32 %49, i32* null, i32 0)
%56 = inttoptr i64 %55 to i8*
%57 = icmp eq i8* %56, inttoptr (i64 -1 to i8*)
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%60 = call i32 @exit(i32 -1)
unreachable
61: ; preds = %54
store i8 1, i8* %56
%62 = getelementptr inbounds i8, i8* %56, i64 2
store i8 1, i8* %62
%63 = getelementptr inbounds i8, i8* %56, i64 1
%64 = load i8, i8* %63
%65 = icmp eq i8 %64, 0
br i1 %65, label %67, label %66
66: ; preds = %61, %66
br label %66
67: ; preds = %61
%68 = getelementptr inbounds i8, i8* %56, i64 3
%69 = load i8*, i8** %39
%70 = call i32 @strncpy(i8* %68, i8* %69, i32 10)
%71 = urem i32 %47, 255
%72 = trunc i32 %71 to i8
store i8 %72, i8* %68
%73 = load i8*, i8** %39
%74 = shl nuw i32 %71, 24
%75 = ashr exact i32 %74, 24
%76 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %73, i32 %75, i32 %47)
store i8 0, i8* %56
%77 = call i64 @shmdt(i8* %56)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %43
79: ; preds = %67
%80 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%81 = call i32 @exit(i32 -1)
unreachable
82: ; preds = %14, %116
%83 = phi i32 [ 1000, %14 ], [ %119, %116 ]
%84 = call i32 @sleep(i32 1)
%85 = call i32 @shmget(i32 %7, i32 13, i32 0)
%86 = icmp slt i32 %85, 0
br i1 %86, label %87, label %90
87: ; preds = %82
%88 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%89 = call i32 @exit(i32 -1)
unreachable
90: ; preds = %82
%91 = load i32, i32* @IPC_CREAT
%92 = or i32 %91, 438
%93 = call i64 @shmat(i32 %85, i32* null, i32 %92)
%94 = inttoptr i64 %93 to i8*
%95 = icmp eq i8* %94, inttoptr (i64 -1 to i8*)
br i1 %95, label %96, label %99
96: ; preds = %90
%97 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%98 = call i32 @exit(i32 -1)
unreachable
99: ; preds = %90
%100 = getelementptr inbounds i8, i8* %94, i64 1
store i8 1, i8* %100
%101 = getelementptr inbounds i8, i8* %94, i64 2
store i8 0, i8* %101
%102 = load i8, i8* %94
%103 = icmp eq i8 %102, 0
br i1 %103, label %105, label %104
104: ; preds = %99, %104
br label %104
105: ; preds = %99
%106 = getelementptr inbounds i8, i8* %94, i64 3
%107 = call i32 @strncpy(i8* %15, i8* %106, i32 10)
%108 = load i8, i8* %106
%109 = sext i8 %108 to i32
%110 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %15, i32 %109, i32 %83)
store i8 0, i8* %100
%111 = call i64 @shmdt(i8* %94)
%112 = icmp slt i64 %111, 0
br i1 %112, label %113, label %116
113: ; preds = %105
%114 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%115 = call i32 @exit(i32 -1)
unreachable
116: ; preds = %105
%117 = load i32, i32* @IPC_RMID
%118 = call i32 @shmctl(i32 %85, i32 %117, i32* null)
%119 = add nsw i32 %83, -1
%120 = icmp ugt i32 %83, 1
br i1 %120, label %82, label %121
121: ; preds = %116, %43
ret i32 0
}