#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 new = 1;
  char pathname[] = "1a.c";
  key_t key;
  if ((key = ftok(pathname, 0)) < 0) {
    printf("Can\'t generate key\n");
    exit(-1);
  }
  if ((shmid = shmget(key, 3 * 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);
      }
      new = 0;
    }
  }
  if ((array = (int *)shmat(shmid, NULL, 0)) == (int *)(-1)) {
    printf("Can't attach shared memory\n");
    exit(-1);
  }
  if (new) {
    array[0] = 1;
    array[1] = 0;
    array[2] = 1;
  } else {
    array[0] += 1;
    array[2] += 1;
  }
  printf(
      "Program 1 was spawn %d times, program 2 - %d times, total - %d times\n",
      array[0], array[1], array[2]);
  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,-0x1d(%rbp)
mov    0xe7f(%rip),%al
mov    %al,-0x19(%rbp)
lea    -0x1d(%rbp),%rdi
xor    %esi,%esi
call   1080 <ftok@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11b7 <main+0x57>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x24(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    0x2e20(%rip),%rax
or     (%rax),%edx
mov    $0xc,%esi
call   1060 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1259 <main+0xf9>
mov    0x2de1(%rip),%rax
mov    (%rax),%rax
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%rax
je     121a <main+0xba>
lea    0xe10(%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    124d <main+0xed>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
movl   $0x0,-0x18(%rbp)
jmp    1259 <main+0xf9>
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    1293 <main+0x133>
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
je     12c2 <main+0x162>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x10(%rbp),%rax
movl   $0x0,0x4(%rax)
mov    -0x10(%rbp),%rax
movl   $0x1,0x8(%rax)
jmp    12da <main+0x17a>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%ecx
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
jge    1327 <main+0x1c7>
lea    0xd9b(%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
movb   $0x0,0x4(%rsp)
movl   $0x632e6131,(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1090 <ftok@plt>
test   %eax,%eax
js     1269 <main+0xf9>
mov    %eax,%ebx
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    0x2e4c(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    %ebx,%edi
mov    $0xc,%esi
call   1070 <shmget@plt>
test   %eax,%eax
js     11f1 <main+0x81>
xor    %r14d,%r14d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1272 <main+0x102>
mov    %rax,%rbx
movq   $0x1,(%rax)
movl   $0x1,0x8(%rax)
mov    $0x1,%ecx
mov    $0x1,%esi
jmp    1241 <main+0xd1>
mov    0x2dd8(%rip),%rax
mov    (%rax),%rax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
jne    1284 <main+0x114>
mov    %ebx,%edi
mov    $0xc,%esi
xor    %edx,%edx
call   1070 <shmget@plt>
test   %eax,%eax
js     128d <main+0x11d>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1272 <main+0x102>
mov    %rax,%rbx
mov    (%rax),%esi
mov    0x4(%rax),%r14d
add    $0x1,%esi
mov    %esi,(%rax)
mov    0x8(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0x8(%rax)
lea    0xdbd(%rip),%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
test   %rax,%rax
js     127b <main+0x10b>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe45(%rip),%rdi
jmp    1294 <main+0x124>
lea    0xded(%rip),%rdi
jmp    1294 <main+0x124>
lea    0xdc9(%rip),%rdi
jmp    1294 <main+0x124>
lea    0xe0f(%rip),%rdi
jmp    1294 <main+0x124>
lea    0xded(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.main.pathname = [5 x i8] c"1a.c\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 [5 x i8]
%6 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %4
%7 = bitcast [5 x i8]* %5 to i8*
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%9 = call i32 @ftok(i8* %8, i32 0)
store i32 %9, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %6
%16 = load i32, i32* @IPC_CREAT
%17 = or i32 438, %16
%18 = load i32, i32* @IPC_EXCL
%19 = or i32 %17, %18
%20 = call i32 @shmget(i32 %15, i32 12, i32 %19)
store i32 %20, i32* %3
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %38
22: ; preds = %14
%23 = load i64, i64* @errno
%24 = load i64, i64* @EEXIST
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %22
%30 = load i32, i32* %6
%31 = call i32 @shmget(i32 %30, i32 12, i32 0)
store i32 %31, i32* %3
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %29
store i32 0, i32* %4
br label %37
37: ; preds = %36
br label %38
38: ; preds = %37, %14
%39 = load i32, i32* %3
%40 = call i64 @shmat(i32 %39, i32* null, i32 0)
%41 = inttoptr i64 %40 to i32*
store i32* %41, i32** %2
%42 = icmp eq i32* %41, inttoptr (i64 -1 to i32*)
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @exit(i32 -1)
unreachable
46: ; preds = %38
%47 = load i32, i32* %4
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %46
%50 = load i32*, i32** %2
%51 = getelementptr inbounds i32, i32* %50, i64 0
store i32 1, i32* %51
%52 = load i32*, i32** %2
%53 = getelementptr inbounds i32, i32* %52, i64 1
store i32 0, i32* %53
%54 = load i32*, i32** %2
%55 = getelementptr inbounds i32, i32* %54, i64 2
store i32 1, i32* %55
br label %65
56: ; preds = %46
%57 = load i32*, i32** %2
%58 = getelementptr inbounds i32, i32* %57, i64 0
%59 = load i32, i32* %58
%60 = add nsw i32 %59, 1
store i32 %60, i32* %58
%61 = load i32*, i32** %2
%62 = getelementptr inbounds i32, i32* %61, i64 2
%63 = load i32, i32* %62
%64 = add nsw i32 %63, 1
store i32 %64, i32* %62
br label %65
65: ; preds = %56, %49
%66 = load i32*, i32** %2
%67 = getelementptr inbounds i32, i32* %66, i64 0
%68 = load i32, i32* %67
%69 = load i32*, i32** %2
%70 = getelementptr inbounds i32, i32* %69, i64 1
%71 = load i32, i32* %70
%72 = load i32*, i32** %2
%73 = getelementptr inbounds i32, i32* %72, i64 2
%74 = load i32, i32* %73
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.4, i64 0, i64 0), i32 %68, i32 %71, i32 %74)
%76 = load i32*, i32** %2
%77 = call i64 @shmdt(i32* %76)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %65
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%81 = call i32 @exit(i32 -1)
unreachable
82: ; preds = %65
ret i32 0
}

LLVM IR (-O2):
@__const.main.pathname = [5 x i8] c"1a.c\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 [5 x i8]
%2 = getelementptr inbounds [5 x i8], [5 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 12, 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 %42
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
store i32 1, i32* %34
%40 = getelementptr inbounds i32, i32* %34, i64 1
store i32 0, i32* %40
%41 = getelementptr inbounds i32, i32* %34, i64 2
store i32 1, i32* %41
br label %50
42: ; preds = %28
%43 = load i32, i32* %30
%44 = add nsw i32 %43, 1
store i32 %44, i32* %30
%45 = getelementptr inbounds i32, i32* %30, i64 2
%46 = load i32, i32* %45
%47 = add nsw i32 %46, 1
store i32 %47, i32* %45
%48 = getelementptr inbounds i32, i32* %30, i64 1
%49 = load i32, i32* %48
br label %50
50: ; preds = %42, %39
%51 = phi i32 [ %47, %42 ], [ 1, %39 ]
%52 = phi i32 [ %49, %42 ], [ 0, %39 ]
%53 = phi i32 [ %44, %42 ], [ 1, %39 ]
%54 = phi i32* [ %30, %42 ], [ %34, %39 ]
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([70 x i8], [70 x i8]* @.str.4, i64 0, i64 0), i32 %53, i32 %52, i32 %51)
%56 = call i64 @shmdt(i32* %54)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %50
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%60 = call i32 @exit(i32 -1)
unreachable
61: ; preds = %50
ret i32 0
}