#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*/ ftok(char *, int);
int /*<<< orphan*/ printf(char *, int, int, int);
char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ shmdt(char *);
int shmget(int /*<<< orphan*/, int, int);
int main(int argc, char **argv) {
  int mem_size = 3;
  key_t key = ftok("shared.mem", 108);
  int shmget_result = shmget(key, mem_size, 0666 | IPC_CREAT | IPC_EXCL);
  int f = 1;
  if (errno == EEXIST) {
    shmget_result = shmget(key, mem_size, 0);
    f = 0;
  }
  if (shmget_result == -1)
    return -1;
  char *ch = shmat(shmget_result, 0, 0);
  if (f)
    ch[0] = ch[1] = ch[2] = 0;
  ch[0]++;
  ch[2]++;
  printf("%d %d %d\n", (int)ch[0], (int)ch[1], (int)ch[2]);
  if (shmdt(ch) != 0)
    return -2;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x3,-0x14(%rbp)
lea    0xe8c(%rip),%rdi
mov    $0x6c,%esi
call   1070 <ftok@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    0x2e53(%rip),%rax
or     (%rax),%edx
call   1060 <shmget@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%rax
mov    0x2e19(%rip),%rcx
cmp    (%rcx),%rax
jne    11df <main+0x8f>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1060 <shmget@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f5 <main+0xa5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <main+0x137>
mov    -0x1c(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <shmat@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1226 <main+0xd6>
mov    -0x28(%rbp),%rax
movb   $0x0,0x2(%rax)
mov    -0x28(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%cl
add    $0x1,%cl
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
mov    0x2(%rax),%cl
add    $0x1,%cl
mov    %cl,0x2(%rax)
mov    -0x28(%rbp),%rax
movsbl (%rax),%esi
mov    -0x28(%rbp),%rax
movsbl 0x1(%rax),%edx
mov    -0x28(%rbp),%rax
movsbl 0x2(%rax),%ecx
lea    0xdaf(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
je     1280 <main+0x130>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1287 <main+0x137>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xea6(%rip),%rdi
mov    $0x6c,%esi
call   1070 <ftok@plt>
mov    %eax,%ebx
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
mov    0x2e7a(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    %ebx,%edi
mov    $0x3,%esi
call   1060 <shmget@plt>
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e3d(%rip),%rdx
cmp    (%rdx),%rcx
jne    11d1 <main+0x81>
mov    %ebx,%edi
mov    $0x3,%esi
xor    %edx,%edx
call   1060 <shmget@plt>
cmp    $0xffffffff,%eax
je     1224 <main+0xd4>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,%rbx
mov    (%rax),%cl
mov    0x2(%rax),%al
movsbl 0x1(%rbx),%ebp
add    $0x1,%cl
add    $0x1,%al
jmp    11f3 <main+0xa3>
cmp    $0xffffffff,%eax
je     1224 <main+0xd4>
xor    %ebp,%ebp
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,%rbx
movb   $0x0,0x2(%rax)
movw   $0x0,(%rax)
mov    $0x1,%al
mov    $0x1,%cl
mov    %cl,(%rbx)
mov    %al,0x2(%rbx)
movsbl %cl,%esi
movsbl %al,%ecx
lea    0xe06(%rip),%rdi
mov    %ebp,%edx
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    (%rcx,%rcx,1),%eax
add    $0xfffffffe,%eax
jmp    1229 <main+0xd9>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [11 x i8] c"shared.mem\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [10 x i8] c"%d %d %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @ftok(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 108)
%4 = load i32, i32* @IPC_CREAT
%5 = load i32, i32* @IPC_EXCL
%6 = or i32 %4, %5
%7 = or i32 %6, 438
%8 = tail call i32 @shmget(i32 %3, i32 3, i32 %7)
%9 = load i64, i64* @errno
%10 = load i64, i64* @EEXIST
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = tail call i32 @shmget(i32 %3, i32 3, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %43, label %17
15: ; preds = %2
%16 = icmp eq i32 %8, -1
br i1 %16, label %43, label %27
17: ; preds = %12
%18 = tail call i8* @shmat(i32 %13, i32 0, i32 0)
%19 = load i8, i8* %18
%20 = getelementptr inbounds i8, i8* %18, i64 2
%21 = load i8, i8* %20
%22 = getelementptr inbounds i8, i8* %18, i64 1
%23 = load i8, i8* %22
%24 = add i8 %19, 1
%25 = add i8 %21, 1
%26 = sext i8 %23 to i32
br label %31
27: ; preds = %15
%28 = tail call i8* @shmat(i32 %8, i32 0, i32 0)
%29 = getelementptr inbounds i8, i8* %28, i64 2
store i8 0, i8* %29
%30 = getelementptr inbounds i8, i8* %28, i64 1
store i8 0, i8* %30
store i8 0, i8* %28
br label %31
31: ; preds = %17, %27
%32 = phi i32 [ 0, %27 ], [ %26, %17 ]
%33 = phi i8 [ 1, %27 ], [ %25, %17 ]
%34 = phi i8 [ 1, %27 ], [ %24, %17 ]
%35 = phi i8* [ %28, %27 ], [ %18, %17 ]
store i8 %34, i8* %35
%36 = getelementptr inbounds i8, i8* %35, i64 2
store i8 %33, i8* %36
%37 = sext i8 %34 to i32
%38 = sext i8 %33 to i32
%39 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %37, i32 %32, i32 %38)
%40 = tail call i64 @shmdt(i8* %35)
%41 = icmp eq i64 %40, 0
%42 = select i1 %41, i32 0, i32 -2
br label %43
43: ; preds = %15, %12, %31
%44 = phi i32 [ %42, %31 ], [ -1, %12 ], [ -1, %15 ]
ret i32 %44
}