#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;
int /*<<< orphan*/ IPC_RMID;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftok(char *, char);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ shmdt(char *);
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ system(char *);
int main() {
  int shmid;
  char *shmadd;
  key_t key;
  key = ftok(".", 'a');
  if ((shmid = shmget(key, 2048, IPC_CREAT | IPC_EXCL | 0666)) < 0) {
    if (EEXIST == errno) {
      shmid = shmget(key, 2048, 0666);
    } else {
      perror("fail to shmget");
      exit(0);
    }
  }
  printf("created shared-memory : %d\n", shmid);
  system("ipcs -m");
  if ((shmadd = (char *)shmat(shmid, NULL, 0)) < (char *)0) {
    perror("fail to shmat");
    exit(-1);
  }
  printf("attached shared-memory\n");
  system("ipcs -m");
  if ((shmdt(shmadd)) < 0) {
    perror("fail to shmdt");
    exit(-1);
  }
  printf("detach shared-memory\n");
  system("ipcs -m");
  shmctl(shmid, IPC_RMID, NULL);
  system("ipcs  -m");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
mov    $0x61,%esi
call   10b0 <ftok@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    $0x800,%esi
call   1090 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1230 <main+0xa0>
mov    0x2ded(%rip),%rax
mov    (%rax),%rax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%rax
jne    1218 <main+0x88>
mov    -0x14(%rbp),%edi
mov    $0x800,%esi
mov    $0x1b6,%edx
call   1090 <shmget@plt>
mov    %eax,-0x8(%rbp)
jmp    122b <main+0x9b>
lea    0xde3(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    1230 <main+0xa0>
mov    -0x8(%rbp),%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xde5(%rip),%rdi
call   1040 <system@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jae    127f <main+0xef>
lea    0xdc5(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd99(%rip),%rdi
call   1040 <system@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
jge    12c2 <main+0x132>
lea    0xda8(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd56(%rip),%rdi
call   1040 <system@plt>
mov    -0x8(%rbp),%edi
mov    0x2ce2(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <shmctl@plt>
lea    0xd87(%rip),%rdi
call   1040 <system@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xe55(%rip),%rdi
mov    $0x61,%esi
call   10c0 <ftok@plt>
mov    %eax,%ebp
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
mov    0x2e29(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    %ebp,%edi
mov    $0x800,%esi
call   10a0 <shmget@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    120e <main+0x6e>
mov    0x2df0(%rip),%rax
mov    (%rax),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    12af <main+0x10f>
mov    %ebp,%edi
mov    $0x800,%esi
mov    $0x1b6,%edx
call   10a0 <shmget@plt>
mov    %eax,%ebx
lea    0xdfc(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe08(%rip),%r14
mov    %r14,%rdi
call   1050 <system@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,%rbp
lea    0xe0a(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1050 <system@plt>
mov    %rbp,%rdi
call   1040 <shmdt@plt>
test   %rax,%rax
js     1299 <main+0xf9>
lea    0xe00(%rip),%rdi
call   1030 <puts@plt>
lea    0xdbe(%rip),%rdi
call   1050 <system@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
lea    0xdb6(%rip),%rdi
call   1050 <system@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd95(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xd4c(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@EEXIST = global i64 0
@errno = global i64 0
@.str.1 = [15 x i8] c"fail to shmget\00"
@.str.2 = [28 x i8] c"created shared-memory : %d\0A\00"
@.str.3 = [8 x i8] c"ipcs -m\00"
@.str.4 = [14 x i8] c"fail to shmat\00"
@.str.5 = [24 x i8] c"attached shared-memory\0A\00"
@.str.6 = [14 x i8] c"fail to shmdt\00"
@.str.7 = [22 x i8] c"detach shared-memory\0A\00"
@IPC_RMID = global i32 0
@.str.8 = [9 x i8] c"ipcs -m\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @ftok(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8 signext 97)
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = load i32, i32* @IPC_CREAT
%8 = load i32, i32* @IPC_EXCL
%9 = or i32 %7, %8
%10 = or i32 %9, 438
%11 = call i32 @shmget(i32 %6, i32 2048, i32 %10)
store i32 %11, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %0
%14 = load i64, i64* @EEXIST
%15 = load i64, i64* @errno
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* %4
%19 = call i32 @shmget(i32 %18, i32 2048, i32 438)
store i32 %19, i32* %2
br label %23
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %17
br label %24
24: ; preds = %23, %0
%25 = load i32, i32* %2
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%28 = load i32, i32* %2
%29 = call i64 @shmat(i32 %28, i32* null, i32 0)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %3
%31 = icmp ult i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %24
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%37 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%38 = load i8*, i8** %3
%39 = call i64 @shmdt(i8* %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%43 = call i32 @exit(i32 -1)
unreachable
44: ; preds = %35
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%46 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* %2
%48 = load i32, i32* @IPC_RMID
%49 = call i32 @shmctl(i32 %47, i32 %48, i32* null)
%50 = call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@EEXIST = global i64 0
@errno = global i64 0
@.str.1 = [15 x i8] c"fail to shmget\00"
@.str.2 = [28 x i8] c"created shared-memory : %d\0A\00"
@.str.3 = [8 x i8] c"ipcs -m\00"
@.str.6 = [14 x i8] c"fail to shmdt\00"
@IPC_RMID = global i32 0
@.str.8 = [9 x i8] c"ipcs -m\00"
@str = [23 x i8] c"attached shared-memory\00"
@str.9 = [21 x i8] c"detach shared-memory\00"
define i32 @main() {
%1 = tail call i32 @ftok(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8 signext 97)
%2 = load i32, i32* @IPC_CREAT
%3 = load i32, i32* @IPC_EXCL
%4 = or i32 %2, %3
%5 = or i32 %4, 438
%6 = tail call i32 @shmget(i32 %1, i32 2048, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %0
%9 = load i64, i64* @EEXIST
%10 = load i64, i64* @errno
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @shmget(i32 %1, i32 2048, i32 438)
br label %17
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 0)
unreachable
17: ; preds = %12, %0
%18 = phi i32 [ %13, %12 ], [ %6, %0 ]
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = tail call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%21 = tail call i64 @shmat(i32 %18, i32* null, i32 0)
%22 = inttoptr i64 %21 to i8*
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%24 = tail call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i64 @shmdt(i8* %22)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%29 = tail call i32 @exit(i32 -1)
unreachable
30: ; preds = %17
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
%32 = tail call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @IPC_RMID
%34 = tail call i32 @shmctl(i32 %18, i32 %33, i32* null)
%35 = tail call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
ret i32 0
}