#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 */

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ IPC_RMID;
int SHM_R;
int SHM_W;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int shmdt(int *);
int shmget(int, int, int);
int /*<<< orphan*/ system(char *);
int main() {
  int shm;
  int *dir = NULL;
  int numero = 2;
  shm = shmget(1555, sizeof(int), IPC_CREAT | SHM_R | SHM_W | IPC_EXCL);
  if (shm == -1) {
    printf(">El segmento de memoria compartida ya ha sido creado\n");
    shm = shmget(1555, sizeof(int), SHM_R | SHM_W);
    if (shm == -1) {
      printf("Error al obtener el id de la memoria compartida\n");
      exit(-1);
    }
  } else
    printf(">El segmento de memoria compartida ha sido creado\n");
  system("ipcs -m");
  dir = (int *)shmat(shm, NULL, 0);
  if (dir == (int *)-1) {
    printf("Error al obtener la dirección de memoria compartida\n");
    exit(-1);
  }
  printf(">Direccion de memoria compartida obtenida\n");
  (*dir) = numero;
  if (shmdt(dir) == -1) {
    printf("Error al desvincular la memoria compartida\n");
    exit(-1);
  }
  printf(">Proceso desvinculado de la memoria compartida\n");
  if (shmctl(shm, IPC_RMID, NULL) == -1) {
    printf("Error al eliminar la memoria compartida\n");
    exit(-1);
  }
  printf(">Memoria compartida borrada\n");
  system("ipcs -m");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x2,-0x14(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
mov    0x2e42(%rip),%rax
or     (%rax),%edx
mov    0x2e19(%rip),%rax
or     (%rax),%edx
mov    0x2e40(%rip),%rax
or     (%rax),%edx
mov    $0x613,%edi
mov    $0x4,%esi
call   1080 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1223 <main+0xb3>
lea    0xe2b(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
mov    0x2dd6(%rip),%rax
or     (%rax),%edx
mov    $0x613,%edi
mov    $0x4,%esi
call   1080 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    121e <main+0xae>
lea    0xe27(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    122f <main+0xbf>
lea    0xe3d(%rip),%rdi
call   1050 <printf@plt>
lea    0xe64(%rip),%rdi
call   1040 <system@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    1274 <main+0x104>
lea    0xe3d(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe5d(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0xffffffff,%eax
jne    12b1 <main+0x141>
lea    0xe61(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe77(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2d09(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    12f1 <main+0x181>
lea    0xe7d(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe90(%rip),%rdi
call   1050 <printf@plt>
lea    0xd96(%rip),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e4d(%rip),%rax
mov    0x2e5e(%rip),%r14
mov    (%r14),%edx
or     (%rax),%edx
mov    0x2e32(%rip),%rbx
or     (%rbx),%edx
mov    0x2e59(%rip),%rax
or     (%rax),%edx
mov    $0x613,%edi
mov    $0x4,%esi
call   1080 <shmget@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x70>
lea    0xf83(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%edx
or     (%r14),%edx
mov    $0x613,%edi
mov    $0x4,%esi
call   1080 <shmget@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11ee <main+0x7e>
lea    0xf91(%rip),%rdi
jmp    1269 <main+0xf9>
mov    %eax,%ebp
lea    0xe1f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe0b(%rip),%rdi
call   1050 <system@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    1214 <main+0xa4>
lea    0xef0(%rip),%rdi
jmp    1269 <main+0xf9>
mov    %rax,%rbx
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
movl   $0x2,(%rbx)
mov    %rbx,%rdi
call   1040 <shmdt@plt>
cmp    $0xffffffff,%eax
jne    123f <main+0xcf>
lea    0xe9a(%rip),%rdi
jmp    1269 <main+0xf9>
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1060 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    1278 <main+0x108>
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe14(%rip),%rdi
call   1030 <puts@plt>
lea    0xd75(%rip),%rdi
call   1050 <system@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@SHM_R = global i32 0
@SHM_W = global i32 0
@IPC_EXCL = global i32 0
@.str = [54 x i8] c">El segmento de memoria compartida ya ha sido creado\0A\00"
@.str.1 = [49 x i8] c"Error al obtener el id de la memoria compartida\0A\00"
@.str.2 = [51 x i8] c">El segmento de memoria compartida ha sido creado\0A\00"
@.str.3 = [8 x i8] c"ipcs -m\00"
@.str.4 = [54 x i8] c"Error al obtener la direcci\C3\B3n de memoria compartida\0A\00"
@.str.5 = [43 x i8] c">Direccion de memoria compartida obtenida\0A\00"
@.str.6 = [44 x i8] c"Error al desvincular la memoria compartida\0A\00"
@.str.7 = [48 x i8] c">Proceso desvinculado de la memoria compartida\0A\00"
@IPC_RMID = global i32 0
@.str.8 = [41 x i8] c"Error al eliminar la memoria compartida\0A\00"
@.str.9 = [29 x i8] c">Memoria compartida borrada\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
store i32* null, i32** %3
store i32 2, i32* %4
%5 = load i32, i32* @IPC_CREAT
%6 = load i32, i32* @SHM_R
%7 = or i32 %5, %6
%8 = load i32, i32* @SHM_W
%9 = or i32 %7, %8
%10 = load i32, i32* @IPC_EXCL
%11 = or i32 %9, %10
%12 = call i32 @shmget(i32 1555, i32 4, i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %27
15: ; preds = %0
%16 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @SHM_R
%18 = load i32, i32* @SHM_W
%19 = or i32 %17, %18
%20 = call i32 @shmget(i32 1555, i32 4, i32 %19)
store i32 %20, i32* %2
%21 = load i32, i32* %2
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %15
br label %29
27: ; preds = %0
%28 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %26
%30 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* %2
%32 = call i64 @shmat(i32 %31, i32* null, i32 0)
%33 = inttoptr i64 %32 to i32*
store i32* %33, i32** %3
%34 = load i32*, i32** %3
%35 = icmp eq i32* %34, inttoptr (i64 -1 to i32*)
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %29
%40 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0))
%41 = load i32, i32* %4
%42 = load i32*, i32** %3
store i32 %41, i32* %42
%43 = load i32*, i32** %3
%44 = call i32 @shmdt(i32* %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0))
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %39
%50 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0))
%51 = load i32, i32* %2
%52 = load i32, i32* @IPC_RMID
%53 = call i32 @shmctl(i32 %51, i32 %52, i32* null)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0))
%57 = call i32 @exit(i32 -1)
unreachable
58: ; preds = %49
%59 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
%60 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@SHM_R = global i32 0
@SHM_W = global i32 0
@IPC_EXCL = global i32 0
@.str.3 = [8 x i8] c"ipcs -m\00"
@IPC_RMID = global i32 0
@str = [50 x i8] c">El segmento de memoria compartida ha sido creado\00"
@str.10 = [42 x i8] c">Direccion de memoria compartida obtenida\00"
@str.11 = [47 x i8] c">Proceso desvinculado de la memoria compartida\00"
@str.12 = [28 x i8] c">Memoria compartida borrada\00"
@str.13 = [40 x i8] c"Error al eliminar la memoria compartida\00"
@str.14 = [43 x i8] c"Error al desvincular la memoria compartida\00"
@str.15 = [53 x i8] c"Error al obtener la direcci\C3\B3n de memoria compartida\00"
@str.16 = [53 x i8] c">El segmento de memoria compartida ya ha sido creado\00"
@str.17 = [48 x i8] c"Error al obtener el id de la memoria compartida\00"
define i32 @main() {
%1 = load i32, i32* @IPC_CREAT
%2 = load i32, i32* @SHM_R
%3 = or i32 %2, %1
%4 = load i32, i32* @SHM_W
%5 = or i32 %3, %4
%6 = load i32, i32* @IPC_EXCL
%7 = or i32 %5, %6
%8 = tail call i32 @shmget(i32 1555, i32 4, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %20
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.16, i64 0, i64 0))
%12 = load i32, i32* @SHM_R
%13 = load i32, i32* @SHM_W
%14 = or i32 %13, %12
%15 = tail call i32 @shmget(i32 1555, i32 4, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %10
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.17, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %0
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %10, %20
%23 = phi i32 [ %15, %10 ], [ %8, %20 ]
%24 = tail call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i64 @shmat(i32 %23, i32* null, i32 0)
%26 = inttoptr i64 %25 to i32*
%27 = icmp eq i32* %26, inttoptr (i64 -1 to i32*)
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.15, i64 0, i64 0))
%30 = tail call i32 @exit(i32 -1)
unreachable
31: ; preds = %22
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.10, i64 0, i64 0))
store i32 2, i32* %26
%33 = tail call i32 @shmdt(i32* %26)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.14, i64 0, i64 0))
%37 = tail call i32 @exit(i32 -1)
unreachable
38: ; preds = %31
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.11, i64 0, i64 0))
%40 = load i32, i32* @IPC_RMID
%41 = tail call i32 @shmctl(i32 %23, i32 %40, i32* null)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.13, i64 0, i64 0))
%45 = tail call i32 @exit(i32 -1)
unreachable
46: ; preds = %38
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.12, i64 0, i64 0))
%48 = tail call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%49 = tail call i32 @exit(i32 0)
unreachable
}