#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 */
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftok(char *, char);
int leer_car();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int /*<<< orphan*/, int, int);
int main(int argc, char *argv[]) {
  int shmid, *variable;
  key_t llave;
  llave = ftok(argv[0], 'K');
  if ((shmid = shmget(llave, sizeof(int), IPC_CREAT | 0600)) == -1) {
    perror("Error en shmget");
    exit(-1);
  }
  if ((variable = (int *)shmat(shmid, NULL, 0)) == (int *)(-1)) {
    perror("Fallo shmat");
    exit(-1);
  }
  while (1) {
    printf("\nIntroduzca m para modificar el valor de la variable, v para "
           "visualizarla y t para terminar:\n ");
    switch (leer_car()) {
    case 't':
      shmctl(shmid, IPC_RMID, 0);
      exit(0);
    case 'v':
      printf("variable = %d\n", *variable);
      break;
    case 'm':
      printf("Nuevo valor de la variable en memoria compartida: \n");
      scanf("%d", variable);
      break;
    default:
      printf("Se introdujo una letra incorrecta\n");
      break;
    }
  }
}


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)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x4b,%esi
call   10b0 <ftok@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0x4,%esi
call   1090 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11f8 <main+0x68>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1230 <main+0xa0>
lea    0xdef(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    1235 <main+0xa5>
lea    0xde0(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x29(%rbp)
call   1030 <printf@plt>
mov    -0x29(%rbp),%al
call   1040 <leer_car@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0x6d,%eax
je     12b6 <main+0x126>
jmp    1261 <main+0xd1>
mov    -0x28(%rbp),%eax
sub    $0x74,%eax
je     1283 <main+0xf3>
jmp    1272 <main+0xe2>
mov    -0x28(%rbp),%eax
sub    $0x76,%eax
je     129d <main+0x10d>
jmp    12d9 <main+0x149>
mov    -0x14(%rbp),%edi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <shmctl@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12e7 <main+0x157>
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xdef(%rip),%rdi
call   1050 <scanf@plt>
jmp    12e7 <main+0x157>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1235 <main+0xa5>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    (%rsi),%rdi
mov    $0x4b,%esi
call   10c0 <ftok@plt>
mov    0x2e19(%rip),%rcx
mov    $0x180,%edx
or     (%rcx),%edx
mov    %eax,%edi
mov    $0x4,%esi
call   10a0 <shmget@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x40>
lea    0xe22(%rip),%rdi
jmp    11fa <main+0x5a>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    1209 <main+0x69>
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    %rax,%rbx
mov    %ebp,0x4(%rsp)
lea    0xe05(%rip),%rbp
lea    0xe6f(%rip),%r15
lea    0xe65(%rip),%r12
lea    0xe4f(%rip),%r13
lea    0xe8d(%rip),%r14
jmp    124c <main+0xac>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rbx),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <leer_car@plt>
cmp    $0x6d,%eax
je     1280 <main+0xe0>
cmp    $0x76,%eax
je     1240 <main+0xa0>
cmp    $0x74,%eax
je     1295 <main+0xf5>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    124c <main+0xac>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <scanf@plt>
jmp    124c <main+0xac>
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
mov    0x4(%rsp),%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [16 x i8] c"Error en shmget\00"
@.str.1 = [12 x i8] c"Fallo shmat\00"
@.str.2 = [95 x i8] c"\0AIntroduzca m para modificar el valor de la variable, v para visualizarla y t para terminar:\0A \00"
@IPC_RMID = global i32 0
@.str.3 = [15 x i8] c"variable = %d\0A\00"
@.str.4 = [52 x i8] c"Nuevo valor de la variable en memoria compartida: \0A\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [35 x i8] c"Se introdujo una letra incorrecta\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 0
%11 = load i8*, i8** %10
%12 = call i32 @ftok(i8* %11, i8 signext 75)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = load i32, i32* @IPC_CREAT
%15 = or i32 %14, 384
%16 = call i32 @shmget(i32 %13, i32 4, i32 %15)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %2
%22 = load i32, i32* %6
%23 = call i64 @shmat(i32 %22, i32* null, i32 0)
%24 = inttoptr i64 %23 to i32*
store i32* %24, i32** %7
%25 = icmp eq i32* %24, inttoptr (i64 -1 to i32*)
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %21
br label %30
30: ; preds = %29, %48
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 (...) @leer_car()
switch i32 %32, label %46 [
i32 116, label %33
i32 118, label %38
i32 109, label %42
]
33: ; preds = %30
%34 = load i32, i32* %6
%35 = load i32, i32* @IPC_RMID
%36 = call i32 @shmctl(i32 %34, i32 %35, i32 0)
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %30
%39 = load i32*, i32** %7
%40 = load i32, i32* %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %40)
br label %48
42: ; preds = %30
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0))
%44 = load i32*, i32** %7
%45 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %44)
br label %48
46: ; preds = %30
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
br label %48
48: ; preds = %46, %42, %38
br label %30
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [16 x i8] c"Error en shmget\00"
@.str.1 = [12 x i8] c"Fallo shmat\00"
@.str.2 = [95 x i8] c"\0AIntroduzca m para modificar el valor de la variable, v para visualizarla y t para terminar:\0A \00"
@IPC_RMID = global i32 0
@.str.3 = [15 x i8] c"variable = %d\0A\00"
@.str.5 = [3 x i8] c"%d\00"
@str = [51 x i8] c"Nuevo valor de la variable en memoria compartida: \00"
@str.7 = [34 x i8] c"Se introdujo una letra incorrecta\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i8*, i8** %1
%4 = tail call i32 @ftok(i8* %3, i8 signext 75)
%5 = load i32, i32* @IPC_CREAT
%6 = or i32 %5, 384
%7 = tail call i32 @shmget(i32 %4, i32 4, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = tail call i64 @shmat(i32 %7, i32* null, i32 0)
%14 = inttoptr i64 %13 to i32*
%15 = icmp eq i32* %14, inttoptr (i64 -1 to i32*)
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %12, %34
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([95 x i8], [95 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 (...) @leer_car()
switch i32 %21, label %32 [
i32 116, label %22
i32 118, label %26
i32 109, label %29
]
22: ; preds = %19
%23 = load i32, i32* @IPC_RMID
%24 = tail call i32 @shmctl(i32 %7, i32 %23, i32 0)
%25 = tail call i32 @exit(i32 0)
unreachable
26: ; preds = %19
%27 = load i32, i32* %14
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %27)
br label %34
29: ; preds = %19
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
%31 = tail call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %14)
br label %34
32: ; preds = %19
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.7, i64 0, i64 0))
br label %34
34: ; preds = %32, %29, %26
br label %19
}