#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, 6 * 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] = 1;
    array[4] = 0;
    array[5] = 1;
  } else {
    array[0] = 1;
    array[2] = 1;
    while (array[1] && array[2] == 1)
      ;
    array[3] += 1;
    for (int i = 0; i < 1000000000; i++)
      ;
    array[5] += 1;
    array[0] = 0;
  }
  for (int i = 0; i < 1000000000; i++)
    ;
  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    $0x18,%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   $0x1,0xc(%rax)
mov    -0x10(%rbp),%rax
movl   $0x0,0x10(%rax)
mov    -0x10(%rbp),%rax
movl   $0x1,0x14(%rax)
jmp    135b <main+0x1fb>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x10(%rbp),%rax
movl   $0x1,0x8(%rax)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmpl   $0x0,0x4(%rcx)
mov    %al,-0x2d(%rbp)
je     12fb <main+0x19b>
mov    -0x10(%rbp),%rax
cmpl   $0x1,0x8(%rax)
sete   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    130b <main+0x1ab>
jmp    1310 <main+0x1b0>
jmp    12da <main+0x17a>
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0xc(%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,(%rax)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3b9aca00,-0x2c(%rbp)
jge    1382 <main+0x222>
jmp    1374 <main+0x214>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1362 <main+0x202>
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    0xcce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
jge    13d0 <main+0x270>
lea    0xcf3(%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     12a3 <main+0x133>
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     121e <main+0xae>
xor    %r14d,%r14d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     12ac <main+0x13c>
mov    %rax,%rbx
movq   $0x1,0xc(%rax)
movl   $0x1,0x14(%rax)
mov    $0x1,%ecx
mov    $0x1,%esi
lea    0xe0d(%rip),%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
test   %rax,%rax
js     12b5 <main+0x145>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dab(%rip),%rax
mov    (%rax),%rax
mov    0x2da9(%rip),%rcx
cmp    (%rcx),%rax
jne    12be <main+0x14e>
mov    %ebx,%edi
mov    $0x18,%esi
xor    %edx,%edx
call   1070 <shmget@plt>
test   %eax,%eax
js     12c7 <main+0x157>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     12ac <main+0x13c>
mov    %rax,%rbx
movl   $0x1,(%rax)
movl   $0x1,0x8(%rax)
cmpl   $0x0,0x4(%rax)
je     1282 <main+0x112>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
jmp    1280 <main+0x110>
mov    0xc(%rbx),%esi
mov    0x10(%rbx),%r14d
add    $0x1,%esi
mov    %esi,0xc(%rbx)
mov    0x14(%rbx),%ecx
add    $0x1,%ecx
mov    %ecx,0x14(%rbx)
movl   $0x0,(%rbx)
jmp    11f2 <main+0x82>
lea    0xe0c(%rip),%rdi
jmp    12ce <main+0x15e>
lea    0xdb4(%rip),%rdi
jmp    12ce <main+0x15e>
lea    0xd90(%rip),%rdi
jmp    12ce <main+0x15e>
lea    0xdd6(%rip),%rdi
jmp    12ce <main+0x15e>
lea    0xdb4(%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
%8 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %4
%9 = bitcast [6 x i8]* %5 to i8*
%10 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%11 = call i32 @ftok(i8* %10, i32 0)
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %0
%17 = load i32, i32* %6
%18 = load i32, i32* @IPC_CREAT
%19 = or i32 438, %18
%20 = load i32, i32* @IPC_EXCL
%21 = or i32 %19, %20
%22 = call i32 @shmget(i32 %17, i32 24, i32 %21)
store i32 %22, i32* %3
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %40
24: ; preds = %16
%25 = load i64, i64* @errno
%26 = load i64, i64* @EEXIST
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %24
%32 = load i32, i32* %6
%33 = call i32 @shmget(i32 %32, i32 24, i32 0)
store i32 %33, i32* %3
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %31
store i32 0, i32* %4
br label %39
39: ; preds = %38
br label %40
40: ; preds = %39, %16
%41 = load i32, i32* %3
%42 = call i64 @shmat(i32 %41, i32* null, i32 0)
%43 = inttoptr i64 %42 to i32*
store i32* %43, i32** %2
%44 = icmp eq i32* %43, inttoptr (i64 -1 to i32*)
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %40
%49 = load i32, i32* %4
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %48
%52 = load i32*, i32** %2
%53 = getelementptr inbounds i32, i32* %52, i64 3
store i32 1, i32* %53
%54 = load i32*, i32** %2
%55 = getelementptr inbounds i32, i32* %54, i64 4
store i32 0, i32* %55
%56 = load i32*, i32** %2
%57 = getelementptr inbounds i32, i32* %56, i64 5
store i32 1, i32* %57
br label %95
58: ; preds = %48
%59 = load i32*, i32** %2
%60 = getelementptr inbounds i32, i32* %59, i64 0
store i32 1, i32* %60
%61 = load i32*, i32** %2
%62 = getelementptr inbounds i32, i32* %61, i64 2
store i32 1, i32* %62
br label %63
63: ; preds = %75, %58
%64 = load i32*, i32** %2
%65 = getelementptr inbounds i32, i32* %64, i64 1
%66 = load i32, i32* %65
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %63
%69 = load i32*, i32** %2
%70 = getelementptr inbounds i32, i32* %69, i64 2
%71 = load i32, i32* %70
%72 = icmp eq i32 %71, 1
br label %73
73: ; preds = %68, %63
%74 = phi i1 [ false, %63 ], [ %72, %68 ]
br i1 %74, label %75, label %76
75: ; preds = %73
br label %63
76: ; preds = %73
%77 = load i32*, i32** %2
%78 = getelementptr inbounds i32, i32* %77, i64 3
%79 = load i32, i32* %78
%80 = add nsw i32 %79, 1
store i32 %80, i32* %78
store i32 0, i32* %7
br label %81
81: ; preds = %85, %76
%82 = load i32, i32* %7
%83 = icmp slt i32 %82, 1000000000
br i1 %83, label %84, label %88
84: ; preds = %81
br label %85
85: ; preds = %84
%86 = load i32, i32* %7
%87 = add nsw i32 %86, 1
store i32 %87, i32* %7
br label %81
88: ; preds = %81
%89 = load i32*, i32** %2
%90 = getelementptr inbounds i32, i32* %89, i64 5
%91 = load i32, i32* %90
%92 = add nsw i32 %91, 1
store i32 %92, i32* %90
%93 = load i32*, i32** %2
%94 = getelementptr inbounds i32, i32* %93, i64 0
store i32 0, i32* %94
br label %95
95: ; preds = %88, %51
store i32 0, i32* %8
br label %96
96: ; preds = %100, %95
%97 = load i32, i32* %8
%98 = icmp slt i32 %97, 1000000000
br i1 %98, label %99, label %103
99: ; preds = %96
br label %100
100: ; preds = %99
%101 = load i32, i32* %8
%102 = add nsw i32 %101, 1
store i32 %102, i32* %8
br label %96
103: ; preds = %96
%104 = load i32*, i32** %2
%105 = getelementptr inbounds i32, i32* %104, i64 3
%106 = load i32, i32* %105
%107 = load i32*, i32** %2
%108 = getelementptr inbounds i32, i32* %107, i64 4
%109 = load i32, i32* %108
%110 = load i32*, i32** %2
%111 = getelementptr inbounds i32, i32* %110, i64 5
%112 = load i32, i32* %111
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.4, i64 0, i64 0), i32 %106, i32 %109, i32 %112)
%114 = load i32*, i32** %2
%115 = call i64 @shmdt(i32* %114)
%116 = icmp slt i64 %115, 0
br i1 %116, label %117, label %120
117: ; preds = %103
%118 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%119 = call i32 @exit(i32 -1)
unreachable
120: ; preds = %103
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 24, 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 1, i32* %40
%41 = getelementptr inbounds i32, i32* %34, i64 4
store i32 0, i32* %41
%42 = getelementptr inbounds i32, i32* %34, i64 5
store i32 1, i32* %42
br label %58
43: ; preds = %28
store i32 1, i32* %30
%44 = getelementptr inbounds i32, i32* %30, i64 2
store i32 1, i32* %44
%45 = getelementptr inbounds i32, i32* %30, i64 1
%46 = load i32, i32* %45
%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 3
%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* %30
%56 = getelementptr inbounds i32, i32* %30, i64 4
%57 = load i32, i32* %56
br label %58
58: ; preds = %49, %39
%59 = phi i32 [ %55, %49 ], [ 1, %39 ]
%60 = phi i32 [ %57, %49 ], [ 0, %39 ]
%61 = phi i32 [ %52, %49 ], [ 1, %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
}