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

/* Variables and functions */
int /*<<< orphan*/ __fpurge(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ system(char *);
int main() {
  FILE *puntero;
  int opcion;
  char linea[100];
  while (1) {
    do {
      system("clear");
      printf("1) - Añadir contenido al fichero\n");
      printf("2) - Leer el contenido del fichero\n");
      printf("0) - Salir del programa\n");
      printf("\n¿Que quieres hacer?\t");
      scanf("%d", &opcion);
      __fpurge(stdin);
    } while (opcion > 2);
    switch (opcion) {
    case 0:
      printf("\n\nSaliendo del programa.");
      exit(0);
      break;
    case 1:
      if ((puntero = fopen("fichero_test.txt", "a+")) == NULL) {
        printf("Se ha producido un error al intentar abrir o crear el fichero "
               "en modo escritura\n");
        exit(1);
      } else {
        printf("\nEscribe lo que quieras y pulsa la tencla ENTER para añadir "
               "al fichero\n");
        scanf("%[^\n]s", linea);
        __fpurge(stdin);
        fputs(linea, puntero);
        fclose(puntero);
      }
      break;
    case 2:
      if ((puntero = fopen("fichero_test.txt", "r")) == NULL) {
        printf("Se ha producido un error al intentar leer el fichero\n");
        exit(1);
      } else {
        system("clear");
        printf("Esto es lo que continene el archivo:\n\n");
        while (!feof(puntero) || ferror(puntero)) {
          fputc(fgetc(puntero), stdout);
        }
        getchar();
        fclose(puntero);
      }
      break;
    default:
      fclose(puntero);
      printf("\n\nSe ha producido un error en las opciones.");
      exit(1);
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
jmp    11e7 <main+0x17>
lea    0xe12(%rip),%rdi
call   1060 <system@plt>
lea    0xe0c(%rip),%rdi
call   1070 <printf@plt>
lea    0xe23(%rip),%rdi
call   1070 <printf@plt>
lea    0xe3b(%rip),%rdi
call   1070 <printf@plt>
lea    0xe48(%rip),%rdi
call   1070 <printf@plt>
lea    0xe53(%rip),%rdi
lea    -0x14(%rbp),%rsi
mov    $0x0,%al
call   10d0 <scanf@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1040 <__fpurge@plt>
cmpl   $0x2,-0x14(%rbp)
jg     11e7 <main+0x17>
mov    -0x14(%rbp),%eax
mov    %eax,-0x84(%rbp)
test   %eax,%eax
je     128b <main+0xbb>
jmp    1263 <main+0x93>
mov    -0x84(%rbp),%eax
sub    $0x1,%eax
je     129e <main+0xce>
jmp    1277 <main+0xa7>
mov    -0x84(%rbp),%eax
sub    $0x2,%eax
je     131c <main+0x14c>
jmp    13e2 <main+0x212>
lea    0xdee(%rip),%rdi
call   1070 <printf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
lea    0xdf4(%rip),%rdi
lea    0xdfe(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    12d5 <main+0x105>
lea    0xde7(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xe22(%rip),%rdi
call   1070 <printf@plt>
lea    -0x80(%rbp),%rsi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   10d0 <scanf@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
call   1040 <__fpurge@plt>
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <fputs@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
jmp    1401 <main+0x231>
lea    0xd76(%rip),%rdi
lea    0xcda(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1353 <main+0x183>
lea    0xe0a(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xca6(%rip),%rdi
call   1060 <system@plt>
lea    0xe1e(%rip),%rdi
call   1070 <printf@plt>
mov    -0x10(%rbp),%rdi
call   10c0 <feof@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x85(%rbp)
je     139c <main+0x1cc>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
setne  %al
mov    %al,-0x85(%rbp)
mov    -0x85(%rbp),%al
test   $0x1,%al
jne    13af <main+0x1df>
jmp    13cd <main+0x1fd>
mov    -0x10(%rbp),%rdi
call   1090 <fgetc@plt>
mov    %eax,%edi
mov    0x2c17(%rip),%rax
mov    (%rax),%esi
call   10a0 <fputc@plt>
jmp    136b <main+0x19b>
mov    $0x0,%al
call   10b0 <getchar@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
jmp    1401 <main+0x231>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xdb9(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
jmp    11e2 <main+0x12>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
lea    0xe0b(%rip),%rcx
jmp    120b <main+0x2b>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %r13,%rcx
lea    0xdf4(%rip),%r12
lea    0xe04(%rip),%rbx
lea    0xc(%rsp),%rbp
mov    0x2dc3(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rcx,%r13
mov    %rcx,%rdi
call   1070 <system@plt>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
call   1080 <printf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   10e0 <scanf@plt>
mov    (%r14),%edi
call   1050 <__fpurge@plt>
mov    %r13,%rcx
mov    0xc(%rsp),%eax
cmp    $0x2,%eax
jg     1230 <main+0x50>
cmp    $0x1,%eax
je     1320 <main+0x140>
cmp    $0x2,%eax
jne    137b <main+0x19b>
lea    0xd98(%rip),%rdi
lea    0xd5c(%rip),%rsi
call   10f0 <fopen@plt>
test   %rax,%rax
je     1392 <main+0x1b2>
mov    %rax,%r15
mov    %r13,%rdi
call   1070 <system@plt>
lea    0xe15(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d04(%rip),%rbx
jmp    12f1 <main+0x111>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   10a0 <fgetc@plt>
mov    (%rbx),%esi
mov    %eax,%edi
call   10b0 <fputc@plt>
mov    %r15,%rdi
call   10d0 <feof@plt>
test   %eax,%eax
je     12e0 <main+0x100>
mov    %r15,%rdi
call   1040 <ferror@plt>
test   %eax,%eax
jne    12e0 <main+0x100>
xor    %eax,%eax
call   10c0 <getchar@plt>
jmp    1200 <main+0x20>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0xd12(%rip),%rdi
lea    0xd1c(%rip),%rsi
call   10f0 <fopen@plt>
test   %rax,%rax
mov    0x2cab(%rip),%rbx
je     139b <main+0x1bb>
mov    %rax,%r15
lea    0xdef(%rip),%rdi
call   1030 <puts@plt>
lea    0xcf8(%rip),%rdi
lea    0x10(%rsp),%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   10e0 <scanf@plt>
mov    (%rbx),%edi
call   1050 <__fpurge@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1090 <fputs@plt>
jmp    1200 <main+0x20>
test   %eax,%eax
jne    13b1 <main+0x1d1>
lea    0xc9a(%rip),%rdi
call   1080 <printf@plt>
xor    %edi,%edi
call   1100 <exit@plt>
lea    0xd6a(%rip),%rdi
jmp    13a2 <main+0x1c2>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
lea    0xc94(%rip),%rdi
call   1080 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"clear\00"
@.str.1 = [35 x i8] c"1) - A\C3\B1adir contenido al fichero\0A\00"
@.str.2 = [36 x i8] c"2) - Leer el contenido del fichero\0A\00"
@.str.3 = [25 x i8] c"0) - Salir del programa\0A\00"
@.str.4 = [23 x i8] c"\0A\C2\BFQue quieres hacer?\09\00"
@.str.5 = [3 x i8] c"%d\00"
@stdin = global i32 0
@.str.6 = [25 x i8] c"\0A\0ASaliendo del programa.\00"
@.str.7 = [17 x i8] c"fichero_test.txt\00"
@.str.8 = [3 x i8] c"a+\00"
@.str.9 = [81 x i8] c"Se ha producido un error al intentar abrir o crear el fichero en modo escritura\0A\00"
@.str.10 = [73 x i8] c"\0AEscribe lo que quieras y pulsa la tencla ENTER para a\C3\B1adir al fichero\0A\00"
@.str.11 = [7 x i8] c"%[^\0A]s\00"
@.str.12 = [2 x i8] c"r\00"
@.str.13 = [54 x i8] c"Se ha producido un error al intentar leer el fichero\0A\00"
@.str.14 = [39 x i8] c"Esto es lo que continene el archivo:\0A\0A\00"
@stdout = global i32 0
@.str.15 = [44 x i8] c"\0A\0ASe ha producido un error en las opciones.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [100 x i8]
store i32 0, i32* %1
br label %5
5: ; preds = %0, %75
br label %6
6: ; preds = %15, %5
%7 = call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%11 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%12 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%13 = load i32, i32* @stdin
%14 = call i32 @__fpurge(i32 %13)
br label %15
15: ; preds = %6
%16 = load i32, i32* %3
%17 = icmp sgt i32 %16, 2
br i1 %17, label %6, label %18
18: ; preds = %15
%19 = load i32, i32* %3
switch i32 %19, label %70 [
i32 0, label %20
i32 1, label %23
i32 2, label %41
]
20: ; preds = %18
%21 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %18
%24 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
store i32* %24, i32** %2
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.9, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %23
%30 = call i32 @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.10, i64 0, i64 0))
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%32 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @stdin
%34 = call i32 @__fpurge(i32 %33)
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%36 = load i32*, i32** %2
%37 = call i32 @fputs(i8* %35, i32* %36)
%38 = load i32*, i32** %2
%39 = call i32 @fclose(i32* %38)
br label %40
40: ; preds = %29
br label %75
41: ; preds = %18
%42 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
store i32* %42, i32** %2
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.13, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %41
%48 = call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%49 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.14, i64 0, i64 0))
br label %50
50: ; preds = %60, %47
%51 = load i32*, i32** %2
%52 = call i32 @feof(i32* %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = load i32*, i32** %2
%56 = call i32 @ferror(i32* %55)
%57 = icmp ne i32 %56, 0
br label %58
58: ; preds = %54, %50
%59 = phi i1 [ true, %50 ], [ %57, %54 ]
br i1 %59, label %60, label %65
60: ; preds = %58
%61 = load i32*, i32** %2
%62 = call i32 @fgetc(i32* %61)
%63 = load i32, i32* @stdout
%64 = call i32 @fputc(i32 %62, i32 %63)
br label %50
65: ; preds = %58
%66 = call i32 (...) @getchar()
%67 = load i32*, i32** %2
%68 = call i32 @fclose(i32* %67)
br label %69
69: ; preds = %65
br label %75
70: ; preds = %18
%71 = load i32*, i32** %2
%72 = call i32 @fclose(i32* %71)
%73 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.15, i64 0, i64 0))
%74 = call i32 @exit(i32 1)
unreachable
75: ; preds = %69, %40
br label %5
}

LLVM IR (-O2):
@.str = [6 x i8] c"clear\00"
@.str.4 = [23 x i8] c"\0A\C2\BFQue quieres hacer?\09\00"
@.str.5 = [3 x i8] c"%d\00"
@stdin = global i32 0
@.str.6 = [25 x i8] c"\0A\0ASaliendo del programa.\00"
@.str.7 = [17 x i8] c"fichero_test.txt\00"
@.str.8 = [3 x i8] c"a+\00"
@.str.11 = [7 x i8] c"%[^\0A]s\00"
@.str.12 = [2 x i8] c"r\00"
@stdout = global i32 0
@.str.15 = [44 x i8] c"\0A\0ASe ha producido un error en las opciones.\00"
@str = [34 x i8] c"1) - A\C3\B1adir contenido al fichero\00"
@str.16 = [35 x i8] c"2) - Leer el contenido del fichero\00"
@str.17 = [24 x i8] c"0) - Salir del programa\00"
@str.18 = [38 x i8] c"Esto es lo que continene el archivo:\0A\00"
@str.19 = [53 x i8] c"Se ha producido un error al intentar leer el fichero\00"
@str.20 = [72 x i8] c"\0AEscribe lo que quieras y pulsa la tencla ENTER para a\C3\B1adir al fichero\00"
@str.21 = [80 x i8] c"Se ha producido un error al intentar abrir o crear el fichero en modo escritura\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
br label %5
5: ; preds = %59, %0
%6 = phi i32* [ undef, %0 ], [ %60, %59 ]
br label %7
7: ; preds = %7, %5
%8 = call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.16, i64 0, i64 0))
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.17, i64 0, i64 0))
%12 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%13 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %1)
%14 = load i32, i32* @stdin
%15 = call i32 @__fpurge(i32 %14)
%16 = load i32, i32* %1
%17 = icmp sgt i32 %16, 2
br i1 %17, label %7, label %18
18: ; preds = %7
switch i32 %16, label %55 [
i32 0, label %19
i32 1, label %22
i32 2, label %34
]
19: ; preds = %18
%20 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %18
%23 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([80 x i8], [80 x i8]* @str.21, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @str.20, i64 0, i64 0))
%30 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0), i8* %4)
%31 = load i32, i32* @stdin
%32 = call i32 @__fpurge(i32 %31)
%33 = call i32 @fputs(i8* %4, i32* %23)
br label %59
34: ; preds = %18
%35 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.19, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %34
%41 = call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.18, i64 0, i64 0))
br label %43
43: ; preds = %49, %40
%44 = call i32 @feof(i32* %35)
%45 = icmp eq i32 %44, 0
br i1 %45, label %49, label %46
46: ; preds = %43
%47 = call i32 @ferror(i32* %35)
%48 = icmp eq i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %43, %46
%50 = call i32 @fgetc(i32* %35)
%51 = load i32, i32* @stdout
%52 = call i32 @fputc(i32 %50, i32 %51)
br label %43
53: ; preds = %46
%54 = call i32 (...) @getchar()
br label %59
55: ; preds = %18
%56 = call i32 @fclose(i32* %6)
%57 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.15, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %53, %28
%60 = phi i32* [ %35, %53 ], [ %23, %28 ]
%61 = call i32 @fclose(i32* %60)
br label %5
}