#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, long, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
char *getenv(char *);
int /*<<< orphan*/ printf(char *);
int sscanf(char *, char *, long *);
int /*<<< orphan*/ stdin;
int main(void) {
  char *lenstr;
  char input[80];
  long len;
  FILE *fout;
  int error;
  printf("Content-Type:text/html\n\n");
  printf("<html>\n<head>\n");
  printf("<title>Ejercicio 2: Formulario CGI y Leng. C</title>\n");
  printf("</head>\n<body>\n");
  lenstr = getenv("CONTENT_LENGTH");
  if (lenstr == NULL || sscanf(lenstr, "%ld", &len) != 1 || len > 80)
    printf("<p>Error en formulario.</p>");
  else {
    if ((fout = fopen("/home/afast/web/datos_alumno.txt", "w")) == NULL) {
      printf("<p>Error de escritura de datos.</p>\n");
      error = 1;
    } else {
      fgets(input, len + 1, stdin);
      fputs(input, fout);
      fclose(fout);
      printf("<p>Datos escritos correctamente.</p>\n");
      error = 0;
    }
  }
  printf("</body>\n");
  printf("</html>\n");
  return error;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
call   1050 <printf@plt>
lea    0xe84(%rip),%rdi
call   1050 <printf@plt>
lea    0xe87(%rip),%rdi
call   1050 <printf@plt>
lea    0xeb1(%rip),%rdi
call   1050 <printf@plt>
lea    0xeb5(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f5 <main+0x85>
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
lea    -0x68(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    11f5 <main+0x85>
cmpq   $0x50,-0x68(%rbp)
jle    1206 <main+0x96>
lea    0xe85(%rip),%rdi
call   1050 <printf@plt>
jmp    1287 <main+0x117>
lea    0xe90(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x70(%rbp)
cmp    $0x0,%rax
jne    123f <main+0xcf>
lea    0xe92(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x74(%rbp)
jmp    1282 <main+0x112>
lea    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rsi
add    $0x1,%rsi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
call   1070 <fgets@plt>
lea    -0x60(%rbp),%rdi
mov    -0x70(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x70(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe6f(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x74(%rbp)
jmp    1287 <main+0x117>
lea    0xe7d(%rip),%rdi
call   1050 <printf@plt>
lea    0xe7a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x74(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
lea    0xec4(%rip),%rdi
call   1040 <puts@plt>
lea    0xed0(%rip),%rdi
call   1040 <puts@plt>
lea    0xed2(%rip),%rdi
call   1040 <puts@plt>
lea    0xefb(%rip),%rdi
call   1040 <puts@plt>
lea    0xe42(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1246 <main+0xc6>
lea    0xe40(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1090 <sscanf@plt>
cmp    $0x1,%eax
jne    1246 <main+0xc6>
cmpq   $0x51,0x8(%rsp)
jge    1246 <main+0xc6>
lea    0xe3f(%rip),%rdi
lea    0xe59(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1274 <main+0xf4>
mov    %rax,%rbx
mov    0x8(%rsp),%rsi
add    $0x1,%rsi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1080 <fgets@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <fputs@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe7f(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
jmp    1252 <main+0xd2>
lea    0xdc6(%rip),%rdi
call   1060 <printf@plt>
lea    0xe88(%rip),%rdi
call   1040 <puts@plt>
lea    0xe84(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe76(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
jmp    1252 <main+0xd2>

LLVM IR (-O0):
@.str = [25 x i8] c"Content-Type:text/html\0A\0A\00"
@.str.1 = [15 x i8] c"<html>\0A<head>\0A\00"
@.str.2 = [54 x i8] c"<title>Ejercicio 2: Formulario CGI y Leng. C</title>\0A\00"
@.str.3 = [16 x i8] c"</head>\0A<body>\0A\00"
@.str.4 = [15 x i8] c"CONTENT_LENGTH\00"
@.str.5 = [4 x i8] c"%ld\00"
@.str.6 = [28 x i8] c"<p>Error en formulario.</p>\00"
@.str.7 = [33 x i8] c"/home/afast/web/datos_alumno.txt\00"
@.str.8 = [2 x i8] c"w\00"
@.str.9 = [37 x i8] c"<p>Error de escritura de datos.</p>\0A\00"
@stdin = global i32 0
@.str.10 = [38 x i8] c"<p>Datos escritos correctamente.</p>\0A\00"
@.str.11 = [9 x i8] c"</body>\0A\00"
@.str.12 = [9 x i8] c"</html>\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [80 x i8]
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%11 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
store i8* %11, i8** %2
%12 = load i8*, i8** %2
%13 = icmp eq i8* %12, null
br i1 %13, label %21, label %14
14: ; preds = %0
%15 = load i8*, i8** %2
%16 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i64* %4)
%17 = icmp ne i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i64, i64* %4
%20 = icmp sgt i64 %19, 80
br i1 %20, label %21, label %23
21: ; preds = %18, %14, %0
%22 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
br label %41
23: ; preds = %18
%24 = call i32* @fopen(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
store i32* %24, i32** %5
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %6
br label %40
28: ; preds = %23
%29 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%30 = load i64, i64* %4
%31 = add nsw i64 %30, 1
%32 = load i32, i32* @stdin
%33 = call i32 @fgets(i8* %29, i64 %31, i32 %32)
%34 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%35 = load i32*, i32** %5
%36 = call i32 @fputs(i8* %34, i32* %35)
%37 = load i32*, i32** %5
%38 = call i32 @fclose(i32* %37)
%39 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.10, i64 0, i64 0))
store i32 0, i32* %6
br label %40
40: ; preds = %28, %26
br label %41
41: ; preds = %40, %21
%42 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.11, i64 0, i64 0))
%43 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.12, i64 0, i64 0))
%44 = load i32, i32* %6
ret i32 %44
}

LLVM IR (-O2):
@.str.4 = [15 x i8] c"CONTENT_LENGTH\00"
@.str.5 = [4 x i8] c"%ld\00"
@.str.6 = [28 x i8] c"<p>Error en formulario.</p>\00"
@.str.7 = [33 x i8] c"/home/afast/web/datos_alumno.txt\00"
@.str.8 = [2 x i8] c"w\00"
@stdin = global i32 0
@str = [24 x i8] c"Content-Type:text/html\0A\00"
@str.13 = [14 x i8] c"<html>\0A<head>\00"
@str.14 = [53 x i8] c"<title>Ejercicio 2: Formulario CGI y Leng. C</title>\00"
@str.15 = [15 x i8] c"</head>\0A<body>\00"
@str.16 = [37 x i8] c"<p>Datos escritos correctamente.</p>\00"
@str.17 = [8 x i8] c"</body>\00"
@str.18 = [8 x i8] c"</html>\00"
@str.19 = [36 x i8] c"<p>Error de escritura de datos.</p>\00"
define i32 @main() {
%1 = alloca [80 x i8]
%2 = alloca i64
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%4 = bitcast i64* %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.13, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.14, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.15, i64 0, i64 0))
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %17, label %11
11: ; preds = %0
%12 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i64* %2)
%13 = icmp ne i32 %12, 1
%14 = load i64, i64* %2
%15 = icmp sgt i64 %14, 80
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %17, label %19
17: ; preds = %11, %0
%18 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
br label %32
19: ; preds = %11
%20 = call i32* @fopen(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.19, i64 0, i64 0))
br label %32
24: ; preds = %19
%25 = load i64, i64* %2
%26 = add nsw i64 %25, 1
%27 = load i32, i32* @stdin
%28 = call i32 @fgets(i8* %3, i64 %26, i32 %27)
%29 = call i32 @fputs(i8* %3, i32* %20)
%30 = call i32 @fclose(i32* %20)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.16, i64 0, i64 0))
br label %32
32: ; preds = %22, %24, %17
%33 = phi i32 [ undef, %17 ], [ 1, %22 ], [ 0, %24 ]
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.17, i64 0, i64 0))
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.18, i64 0, i64 0))
ret i32 %33
}