#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 */
scalar_t__ EEXIST;
int IPC_CREAT;
int IPC_EXCL;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftok(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ shmdt(int *);
int shmget(int /*<<< orphan*/, int, int);
int main() {
  int *array;
  int shmid;
  int new1 = 1;
  char pathname[] = "temp7";
  key_t key;
  if ((key = ftok(pathname, 0)) < 0) {
    printf("Can\'t generate key\n");
    exit(-1);
  }
  if ((shmid = shmget(key, 6 * sizeof(int), 0666 | IPC_CREAT | IPC_EXCL)) < 0) {
    if (errno != EEXIST) {
      printf("Can\'t create shared memory\n");
      exit(-1);
    } else {
      if ((shmid = shmget(key, 3 * sizeof(int), 0)) < 0) {
        printf("Can\'t find shared memory\n");
        exit(-1);
      }
      new1 = 0;
    }
  }
  if ((array = (int *)shmat(shmid, NULL, 0)) == (int *)(-1)) {
    printf("Can't attach shared memory\n");
    exit(-1);
  }
  if (new1) {
    array[3] = 0;
    array[4] = 1;
    array[5] = 1;
  } else {
    array[1] = 1;
    array[2] = 0;
    while (array[0] && array[2] == 0)
      ;
    array[4] += 1;
    for (int i = 0; i < 1000000000; i++)
      ;
    array[5] += 1;
    array[1] = 0;
  }
  printf(
      "Program 1 was spawn %d times, program 2 - %d times, total - %d times\n",
      array[3], array[4], array[5]);
  if (shmdt(array) < 0) {
    printf("Can't detach shared memory\n");
    exit(-1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x18(%rbp)
mov    0xe84(%rip),%eax
mov    %eax,-0x1e(%rbp)
mov    0xe7e(%rip),%ax
mov    %ax,-0x1a(%rbp)
lea    -0x1e(%rbp),%rdi
xor    %esi,%esi
call   1080 <ftok@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11b9 <main+0x59>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x24(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    0x2e1e(%rip),%rax
or     (%rax),%edx
mov    $0x18,%esi
call   1060 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    125b <main+0xfb>
mov    0x2ddf(%rip),%rax
mov    (%rax),%rax
mov    0x2ddd(%rip),%rcx
cmp    (%rcx),%rax
je     121c <main+0xbc>
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x24(%rbp),%edi
mov    $0xc,%esi
xor    %edx,%edx
call   1060 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    124f <main+0xef>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
movl   $0x0,-0x18(%rbp)
jmp    125b <main+0xfb>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1295 <main+0x135>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
je     12c5 <main+0x165>
mov    -0x10(%rbp),%rax
movl   $0x0,0xc(%rax)
mov    -0x10(%rbp),%rax
movl   $0x1,0x10(%rax)
mov    -0x10(%rbp),%rax
movl   $0x1,0x14(%rax)
jmp    135c <main+0x1fc>
mov    -0x10(%rbp),%rax
movl   $0x1,0x4(%rax)
mov    -0x10(%rbp),%rax
movl   $0x0,0x8(%rax)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmpl   $0x0,(%rcx)
mov    %al,-0x29(%rbp)
je     12fb <main+0x19b>
mov    -0x10(%rbp),%rax
cmpl   $0x0,0x8(%rax)
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    130b <main+0x1ab>
jmp    1310 <main+0x1b0>
jmp    12db <main+0x17b>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0x10(%rax)
movl   $0x0,-0x28(%rbp)
cmpl   $0x3b9aca00,-0x28(%rbp)
jge    1344 <main+0x1e4>
jmp    1336 <main+0x1d6>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1324 <main+0x1c4>
mov    -0x10(%rbp),%rax
mov    0x14(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0x14(%rax)
mov    -0x10(%rbp),%rax
movl   $0x0,0x4(%rax)
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%edx
mov    -0x10(%rbp),%rax
mov    0x14(%rax),%ecx
lea    0xcf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
jge    13aa <main+0x24a>
lea    0xd19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
movw   $0x37,0x4(%rsp)
movl   $0x706d6574,(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1090 <ftok@plt>
test   %eax,%eax
js     1294 <main+0x124>
mov    %eax,%ebx
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    0x2e4a(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    %ebx,%edi
mov    $0x18,%esi
call   1070 <shmget@plt>
test   %eax,%eax
js     1224 <main+0xb4>
xor    %r14d,%r14d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     129d <main+0x12d>
mov    %rax,%rbx
movabs $0x100000000,%rax
mov    %rax,0xc(%rbx)
movl   $0x1,0x14(%rbx)
mov    $0x1,%ecx
mov    $0x1,%edx
lea    0xe07(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
test   %rax,%rax
js     12a6 <main+0x136>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2da5(%rip),%rax
mov    (%rax),%rax
mov    0x2da3(%rip),%rcx
cmp    (%rcx),%rax
jne    12af <main+0x13f>
mov    %ebx,%edi
mov    $0xc,%esi
xor    %edx,%edx
call   1070 <shmget@plt>
test   %eax,%eax
js     12b8 <main+0x148>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     129d <main+0x12d>
mov    %rax,%rbx
movq   $0x1,0x4(%rax)
cmpl   $0x0,(%rax)
je     1272 <main+0x102>
nopl   (%rax)
jmp    1270 <main+0x100>
mov    0xc(%rbx),%r14d
mov    0x10(%rbx),%edx
add    $0x1,%edx
mov    %edx,0x10(%rbx)
mov    0x14(%rbx),%ecx
add    $0x1,%ecx
mov    %ecx,0x14(%rbx)
movl   $0x0,0x4(%rbx)
jmp    11f8 <main+0x88>
lea    0xe1b(%rip),%rdi
jmp    12bf <main+0x14f>
lea    0xdc3(%rip),%rdi
jmp    12bf <main+0x14f>
lea    0xd9f(%rip),%rdi
jmp    12bf <main+0x14f>
lea    0xde5(%rip),%rdi
jmp    12bf <main+0x14f>
lea    0xdc3(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.main.pathname = [6 x i8] c"temp7\00"
@.str = [20 x i8] c"Can't generate key\0A\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [28 x i8] c"Can't create shared memory\0A\00"
@.str.2 = [26 x i8] c"Can't find shared memory\0A\00"
@.str.3 = [28 x i8] c"Can't attach shared memory\0A\00"
@.str.4 = [70 x i8] c"Program 1 was spawn %d times, program 2 - %d times, total - %d times\0A\00"
@.str.5 = [28 x i8] c"Can't detach shared memory\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [6 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %4
%8 = bitcast [6 x i8]* %5 to i8*
%9 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%10 = call i32 @ftok(i8* %9, i32 0)
store i32 %10, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %0
%16 = load i32, i32* %6
%17 = load i32, i32* @IPC_CREAT
%18 = or i32 438, %17
%19 = load i32, i32* @IPC_EXCL
%20 = or i32 %18, %19
%21 = call i32 @shmget(i32 %16, i32 24, i32 %20)
store i32 %21, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %39
23: ; preds = %15
%24 = load i64, i64* @errno
%25 = load i64, i64* @EEXIST
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %23
%31 = load i32, i32* %6
%32 = call i32 @shmget(i32 %31, i32 12, i32 0)
store i32 %32, i32* %3
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %30
store i32 0, i32* %4
br label %38
38: ; preds = %37
br label %39
39: ; preds = %38, %15
%40 = load i32, i32* %3
%41 = call i64 @shmat(i32 %40, i32* null, i32 0)
%42 = inttoptr i64 %41 to i32*
store i32* %42, i32** %2
%43 = icmp eq i32* %42, inttoptr (i64 -1 to i32*)
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @exit(i32 -1)
unreachable
47: ; preds = %39
%48 = load i32, i32* %4
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %57
50: ; preds = %47
%51 = load i32*, i32** %2
%52 = getelementptr inbounds i32, i32* %51, i64 3
store i32 0, i32* %52
%53 = load i32*, i32** %2
%54 = getelementptr inbounds i32, i32* %53, i64 4
store i32 1, i32* %54
%55 = load i32*, i32** %2
%56 = getelementptr inbounds i32, i32* %55, i64 5
store i32 1, i32* %56
br label %94
57: ; preds = %47
%58 = load i32*, i32** %2
%59 = getelementptr inbounds i32, i32* %58, i64 1
store i32 1, i32* %59
%60 = load i32*, i32** %2
%61 = getelementptr inbounds i32, i32* %60, i64 2
store i32 0, i32* %61
br label %62
62: ; preds = %74, %57
%63 = load i32*, i32** %2
%64 = getelementptr inbounds i32, i32* %63, i64 0
%65 = load i32, i32* %64
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %72
67: ; preds = %62
%68 = load i32*, i32** %2
%69 = getelementptr inbounds i32, i32* %68, i64 2
%70 = load i32, i32* %69
%71 = icmp eq i32 %70, 0
br label %72
72: ; preds = %67, %62
%73 = phi i1 [ false, %62 ], [ %71, %67 ]
br i1 %73, label %74, label %75
74: ; preds = %72
br label %62
75: ; preds = %72
%76 = load i32*, i32** %2
%77 = getelementptr inbounds i32, i32* %76, i64 4
%78 = load i32, i32* %77
%79 = add nsw i32 %78, 1
store i32 %79, i32* %77
store i32 0, i32* %7
br label %80
80: ; preds = %84, %75
%81 = load i32, i32* %7
%82 = icmp slt i32 %81, 1000000000
br i1 %82, label %83, label %87
83: ; preds = %80
br label %84
84: ; preds = %83
%85 = load i32, i32* %7
%86 = add nsw i32 %85, 1
store i32 %86, i32* %7
br label %80
87: ; preds = %80
%88 = load i32*, i32** %2
%89 = getelementptr inbounds i32, i32* %88, i64 5
%90 = load i32, i32* %89
%91 = add nsw i32 %90, 1
store i32 %91, i32* %89
%92 = load i32*, i32** %2
%93 = getelementptr inbounds i32, i32* %92, i64 1
store i32 0, i32* %93
br label %94
94: ; preds = %87, %50
%95 = load i32*, i32** %2
%96 = getelementptr inbounds i32, i32* %95, i64 3
%97 = load i32, i32* %96
%98 = load i32*, i32** %2
%99 = getelementptr inbounds i32, i32* %98, i64 4
%100 = load i32, i32* %99
%101 = load i32*, i32** %2
%102 = getelementptr inbounds i32, i32* %101, i64 5
%103 = load i32, i32* %102
%104 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.4, i64 0, i64 0), i32 %97, i32 %100, i32 %103)
%105 = load i32*, i32** %2
%106 = call i64 @shmdt(i32* %105)
%107 = icmp slt i64 %106, 0
br i1 %107, label %108, label %111
108: ; preds = %94
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%110 = call i32 @exit(i32 -1)
unreachable
111: ; preds = %94
ret i32 0
}

LLVM IR (-O2):
@__const.main.pathname = [6 x i8] c"temp7\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.4 = [70 x i8] c"Program 1 was spawn %d times, program 2 - %d times, total - %d times\0A\00"
@str = [27 x i8] c"Can't detach shared memory\00"
@str.6 = [27 x i8] c"Can't attach shared memory\00"
@str.7 = [25 x i8] c"Can't find shared memory\00"
@str.8 = [27 x i8] c"Can't create shared memory\00"
@str.9 = [19 x i8] c"Can't generate key\00"
define i32 @main() {
%1 = alloca [6 x i8]
%2 = getelementptr inbounds [6 x i8], [6 x i8]* %1, i64 0, i64 0
%3 = call i32 @ftok(i8* %2, i32 0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.9, i64 0, i64 0))
%7 = call i32 @exit(i32 -1)
unreachable
8: ; preds = %0
%9 = load i32, i32* @IPC_CREAT
%10 = load i32, i32* @IPC_EXCL
%11 = or i32 %9, %10
%12 = or i32 %11, 438
%13 = call i32 @shmget(i32 %3, i32 24, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %8
%16 = load i64, i64* @errno
%17 = load i64, i64* @EEXIST
%18 = icmp eq i64 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %15
%23 = call i32 @shmget(i32 %3, i32 12, i32 0)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.7, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %22
%29 = call i64 @shmat(i32 %23, i32* null, i32 0)
%30 = inttoptr i64 %29 to i32*
%31 = icmp eq i32* %30, inttoptr (i64 -1 to i32*)
br i1 %31, label %36, label %43
32: ; preds = %8
%33 = call i64 @shmat(i32 %13, i32* null, i32 0)
%34 = inttoptr i64 %33 to i32*
%35 = icmp eq i32* %34, inttoptr (i64 -1 to i32*)
br i1 %35, label %36, label %39
36: ; preds = %32, %28
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %32
%40 = getelementptr inbounds i32, i32* %34, i64 3
store i32 0, i32* %40
%41 = getelementptr inbounds i32, i32* %34, i64 4
store i32 1, i32* %41
%42 = getelementptr inbounds i32, i32* %34, i64 5
store i32 1, i32* %42
br label %58
43: ; preds = %28
%44 = getelementptr inbounds i32, i32* %30, i64 1
store i32 1, i32* %44
%45 = getelementptr inbounds i32, i32* %30, i64 2
store i32 0, i32* %45
%46 = load i32, i32* %30
%47 = icmp eq i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %43, %48
br label %48
49: ; preds = %43
%50 = getelementptr inbounds i32, i32* %30, i64 4
%51 = load i32, i32* %50
%52 = add nsw i32 %51, 1
store i32 %52, i32* %50
%53 = getelementptr inbounds i32, i32* %30, i64 5
%54 = load i32, i32* %53
%55 = add nsw i32 %54, 1
store i32 %55, i32* %53
store i32 0, i32* %44
%56 = getelementptr inbounds i32, i32* %30, i64 3
%57 = load i32, i32* %56
br label %58
58: ; preds = %49, %39
%59 = phi i32 [ %55, %49 ], [ 1, %39 ]
%60 = phi i32 [ %52, %49 ], [ 1, %39 ]
%61 = phi i32 [ %57, %49 ], [ 0, %39 ]
%62 = phi i32* [ %30, %49 ], [ %34, %39 ]
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([70 x i8], [70 x i8]* @.str.4, i64 0, i64 0), i32 %61, i32 %60, i32 %59)
%64 = call i64 @shmdt(i32* %62)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %58
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%68 = call i32 @exit(i32 -1)
unreachable
69: ; preds = %58
ret i32 0
}