#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ *stdin;
int strlen(char *);
main() {
  FILE *pf = NULL;
  char buffer[81], nomfi[13];
  printf("Nombre del fichero: ");
  fgets(nomfi, 13, stdin);
  fflush(stdin);
  if (nomfi[strlen(nomfi) - 1] == '\n')
    nomfi[strlen(nomfi) - 1] = 0;
  if ((pf = fopen(nomfi, "w+")) == NULL) {
    printf("El fichero %s no puede abrirse.", nomfi);
    exit(1);
  }
  printf("Fichero %s abierto\n", nomfi);
  printf("Introducir datos. Finalizar cada línea con <Entrar>\n");
  printf("Para terminar introduzca la marca eof\n\n");
  while (fgets(buffer, 81, stdin) != NULL) {
    fputs(buffer, pf);
    if (ferror(pf)) {
      perror("Error al escribir");
      exit(2);
    }
  }
  rewind(pf);
  fgets(buffer, 81, pf);
  while (!ferror(pf) && !feof(pf)) {
    printf("%s", buffer);
    fgets(buffer, 81, pf);
  }
  if (ferror(pf))
    perror("Error durante la lectura");
  fclose(pf);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x7d(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%rdx
mov    $0xd,%esi
call   1090 <fgets@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%rdi
call   10b0 <fflush@plt>
lea    -0x7d(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x7d(%rbp,%rax,1),%eax
cmp    $0xa,%eax
jne    123e <main+0x7e>
lea    -0x7d(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x7d(%rbp,%rax,1)
lea    -0x7d(%rbp),%rdi
lea    0xdcc(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1278 <main+0xb8>
lea    -0x7d(%rbp),%rsi
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x7d(%rbp),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2d37(%rip),%rax
mov    (%rax),%rdx
mov    $0x51,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     1303 <main+0x143>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <fputs@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12fe <main+0x13e>
lea    0xdbb(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
jmp    12a6 <main+0xe6>
mov    -0x10(%rbp),%rdi
call   1070 <rewind@plt>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x51,%esi
call   1090 <fgets@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x7e(%rbp)
jne    134d <main+0x18d>
mov    -0x10(%rbp),%rdi
call   10a0 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x7e(%rbp)
mov    -0x7e(%rbp),%al
test   $0x1,%al
jne    135d <main+0x19d>
jmp    1386 <main+0x1c6>
lea    -0x70(%rbp),%rsi
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x51,%esi
call   1090 <fgets@plt>
jmp    131e <main+0x15e>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     13a5 <main+0x1e5>
lea    0xd1f(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x68,%rsp
lea    0xe1e(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2df8(%rip),%r14
mov    (%r14),%rdx
lea    0x3(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xd,%esi
call   10a0 <fgets@plt>
mov    (%r14),%rdi
call   10c0 <fflush@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    122d <main+0x5d>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xde1(%rip),%rsi
lea    0x3(%rsp),%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1367 <main+0x197>
mov    %rax,%r12
lea    0xde7(%rip),%rdi
lea    0x3(%rsp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe16(%rip),%rdi
call   1030 <puts@plt>
lea    0xe3f(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r14),%rdx
mov    %rbx,%rdi
mov    $0x51,%esi
call   10a0 <fgets@plt>
test   %rax,%rax
je     12c3 <main+0xf3>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <fputs@plt>
mov    %r12,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     1280 <main+0xb0>
lea    0xd98(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x2,%edi
call   10f0 <exit@plt>
mov    %r12,%rdi
call   1080 <rewind@plt>
lea    0x10(%rsp),%rdi
mov    $0x51,%esi
mov    %r12,%rdx
call   10a0 <fgets@plt>
mov    %r12,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    1336 <main+0x166>
lea    0xd6d(%rip),%r14
lea    0x10(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
call   10b0 <feof@plt>
test   %eax,%eax
jne    1336 <main+0x166>
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
mov    $0x51,%esi
mov    %r12,%rdx
call   10a0 <fgets@plt>
mov    %r12,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     1300 <main+0x130>
mov    %r12,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    1359 <main+0x189>
mov    %r12,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd01(%rip),%rdi
call   10e0 <perror@plt>
jmp    1343 <main+0x173>
lea    0xcaa(%rip),%rdi
lea    0x3(%rsp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"Nombre del fichero: \00"
@stdin = global i32* null
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [32 x i8] c"El fichero %s no puede abrirse.\00"
@.str.3 = [20 x i8] c"Fichero %s abierto\0A\00"
@.str.4 = [54 x i8] c"Introducir datos. Finalizar cada l\C3\ADnea con <Entrar>\0A\00"
@.str.5 = [40 x i8] c"Para terminar introduzca la marca eof\0A\0A\00"
@.str.6 = [18 x i8] c"Error al escribir\00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [25 x i8] c"Error durante la lectura\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [81 x i8]
%4 = alloca [13 x i8]
store i32 0, i32* %1
store i32* null, i32** %2
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%7 = load i32*, i32** @stdin
%8 = call i32* @fgets(i8* %6, i32 13, i32* %7)
%9 = load i32*, i32** @stdin
%10 = call i32 @fflush(i32* %9)
%11 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%12 = call i32 @strlen(i8* %11)
%13 = sub nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 10
br i1 %18, label %19, label %25
19: ; preds = %0
%20 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%21 = call i32 @strlen(i8* %20)
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %19, %0
%26 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %2
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %25
%34 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
br label %38
38: ; preds = %53, %33
%39 = getelementptr inbounds [81 x i8], [81 x i8]* %3, i64 0, i64 0
%40 = load i32*, i32** @stdin
%41 = call i32* @fgets(i8* %39, i32 81, i32* %40)
%42 = icmp ne i32* %41, null
br i1 %42, label %43, label %54
43: ; preds = %38
%44 = getelementptr inbounds [81 x i8], [81 x i8]* %3, i64 0, i64 0
%45 = load i32*, i32** %2
%46 = call i32 @fputs(i8* %44, i32* %45)
%47 = load i32*, i32** %2
%48 = call i64 @ferror(i32* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %43
%51 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%52 = call i32 @exit(i32 2)
unreachable
53: ; preds = %43
br label %38
54: ; preds = %38
%55 = load i32*, i32** %2
%56 = call i32 @rewind(i32* %55)
%57 = getelementptr inbounds [81 x i8], [81 x i8]* %3, i64 0, i64 0
%58 = load i32*, i32** %2
%59 = call i32* @fgets(i8* %57, i32 81, i32* %58)
br label %60
60: ; preds = %71, %54
%61 = load i32*, i32** %2
%62 = call i64 @ferror(i32* %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %69, label %64
64: ; preds = %60
%65 = load i32*, i32** %2
%66 = call i32 @feof(i32* %65)
%67 = icmp ne i32 %66, 0
%68 = xor i1 %67, true
br label %69
69: ; preds = %64, %60
%70 = phi i1 [ false, %60 ], [ %68, %64 ]
br i1 %70, label %71, label %77
71: ; preds = %69
%72 = getelementptr inbounds [81 x i8], [81 x i8]* %3, i64 0, i64 0
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %72)
%74 = getelementptr inbounds [81 x i8], [81 x i8]* %3, i64 0, i64 0
%75 = load i32*, i32** %2
%76 = call i32* @fgets(i8* %74, i32 81, i32* %75)
br label %60
77: ; preds = %69
%78 = load i32*, i32** %2
%79 = call i64 @ferror(i32* %78)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %77
%82 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
br label %83
83: ; preds = %81, %77
%84 = load i32*, i32** %2
%85 = call i32 @fclose(i32* %84)
%86 = load i32, i32* %1
ret i32 %86
}

LLVM IR (-O2):
@.str = [21 x i8] c"Nombre del fichero: \00"
@stdin = global i32* null
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [32 x i8] c"El fichero %s no puede abrirse.\00"
@.str.3 = [20 x i8] c"Fichero %s abierto\0A\00"
@.str.6 = [18 x i8] c"Error al escribir\00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [25 x i8] c"Error durante la lectura\00"
@str = [53 x i8] c"Introducir datos. Finalizar cada l\C3\ADnea con <Entrar>\00"
@str.9 = [39 x i8] c"Para terminar introduzca la marca eof\0A\00"
define i32 @main() {
%1 = alloca [81 x i8]
%2 = alloca [13 x i8]
%3 = getelementptr inbounds [81 x i8], [81 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%6 = load i32*, i32** @stdin
%7 = call i32* @fgets(i8* %4, i32 13, i32* %6)
%8 = load i32*, i32** @stdin
%9 = call i32 @fflush(i32* %8)
%10 = call i32 @strlen(i8* %4)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
br i1 %15, label %16, label %21
16: ; preds = %0
%17 = call i32 @strlen(i8* %4)
%18 = add nsw i32 %17, -1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %16, %0
%22 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.9, i64 0, i64 0))
br label %31
31: ; preds = %35, %27
%32 = load i32*, i32** @stdin
%33 = call i32* @fgets(i8* %3, i32 81, i32* %32)
%34 = icmp eq i32* %33, null
br i1 %34, label %42, label %35
35: ; preds = %31
%36 = call i32 @fputs(i8* %3, i32* %22)
%37 = call i64 @ferror(i32* %22)
%38 = icmp eq i64 %37, 0
br i1 %38, label %31, label %39
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%41 = call i32 @exit(i32 2)
unreachable
42: ; preds = %31
%43 = call i32 @rewind(i32* %22)
%44 = call i32* @fgets(i8* %3, i32 81, i32* %22)
%45 = call i64 @ferror(i32* %22)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %55
47: ; preds = %42, %50
%48 = call i32 @feof(i32* %22)
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %3)
%52 = call i32* @fgets(i8* %3, i32 81, i32* %22)
%53 = call i64 @ferror(i32* %22)
%54 = icmp eq i64 %53, 0
br i1 %54, label %47, label %55
55: ; preds = %47, %50, %42
%56 = call i64 @ferror(i32* %22)
%57 = icmp eq i64 %56, 0
br i1 %57, label %60, label %58
58: ; preds = %55
%59 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
%61 = call i32 @fclose(i32* %22)
ret i32 0
}