#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 errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, long, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int fprintf(int /*<<< orphan*/ *, char *, ...);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int sscanf(char *, char *, long *);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ stdin;
char *strerror(int);
int /*<<< orphan*/ unencode(char *, char *, char *);
int main(void) {
  char *lenstr;
  char input[80 + 5 + 2], data[80 + 5 + 2];
  long len;
  int ret;
  int errnum;
  char *str_error;
  printf("%s%c%c\n", "Content-Type:text/html;charset=iso-8859-1", 13, 10);
  printf("<TITLE>Response</TITLE>\n");
  lenstr = getenv("CONTENT_LENGTH");
  if (lenstr == NULL || sscanf(lenstr, "%ld", &len) != 1 || len > 80)
    printf("<P>Error in invocation - wrong FORM probably.");
  else {
    FILE *f;
    fgets(input, len + 1, stdin);
    unencode(input + 5, input + len, data);
    f = fopen("./www/cgi/data.txt", "a");
    if (f == NULL)
      printf("<P>Sorry, cannot store your data.");
    else {
      ret = fprintf(f, "%s", data);
    }
    if (ret > 0)
      printf("Write to file successful with length %d<br>", ret);
    else {
      printf("Write to file failed<br>");
      errnum = errno;
      fprintf(stderr, "Value of errno: %d\n", errno);
      perror("Error printed by perror");
      str_error = strerror(errnum);
      fprintf(stderr, "Error opening file: %s\n", str_error);
    }
    fclose(f);
    printf("<P>Thank you! The following contribution of yours has been "
           "stored:<BR>%s",
           data);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe47(%rip),%rdi
lea    0xe48(%rip),%rsi
mov    $0xd,%edx
mov    $0xa,%ecx
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe65(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1228 <main+0x88>
mov    -0x10(%rbp),%rdi
lea    0xe55(%rip),%rsi
lea    -0xd8(%rbp),%rdx
call   1090 <sscanf@plt>
cmp    $0x1,%eax
jne    1228 <main+0x88>
cmpq   $0x50,-0xd8(%rbp)
50
jle    123b <main+0x9b>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1396 <main+0x1f6>
lea    -0x70(%rbp),%rdi
mov    -0xd8(%rbp),%rsi
add    $0x1,%rsi
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
call   1070 <fgets@plt>
lea    -0x70(%rbp),%rdi
add    $0x5,%rdi
lea    -0x70(%rbp),%rsi
add    -0xd8(%rbp),%rsi
lea    -0xd0(%rbp),%rdx
call   1040 <unencode@plt>
lea    0xe0e(%rip),%rdi
lea    0xe1a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0xf0(%rbp)
cmpq   $0x0,-0xf0(%rbp)
jne    12b2 <main+0x112>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12d4 <main+0x134>
mov    -0xf0(%rbp),%rdi
lea    -0xd0(%rbp),%rdx
lea    0xecb(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    %eax,-0xdc(%rbp)
cmpl   $0x0,-0xdc(%rbp)
jle    12fa <main+0x15a>
mov    -0xdc(%rbp),%esi
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1375 <main+0x1d5>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2cc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xe0(%rbp)
mov    0x2cba(%rip),%rax
mov    (%rax),%rdi
mov    0x2ca8(%rip),%rax
mov    (%rax),%edx
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    0xddd(%rip),%rdi
call   10b0 <perror@plt>
mov    -0xe0(%rbp),%edi
call   10c0 <strerror@plt>
mov    %rax,-0xe8(%rbp)
mov    0x2c7b(%rip),%rax
mov    (%rax),%rdi
mov    -0xe8(%rbp),%rdx
lea    0xdc6(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0xf0(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0xd0(%rbp),%rsi
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xd0,%rsp
lea    0xe3e(%rip),%rdi
lea    0xe3f(%rip),%rsi
mov    $0xd,%edx
mov    $0xa,%ecx
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xf9b(%rip),%rdi
call   1040 <puts@plt>
lea    0xe45(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     12ba <main+0x10a>
lea    0xe3f(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   10a0 <sscanf@plt>
cmp    $0x1,%eax
jne    12ba <main+0x10a>
mov    0x8(%rsp),%rsi
cmp    $0x51,%rsi
jge    12ba <main+0x10a>
add    $0x1,%rsi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
lea    0x70(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <fgets@plt>
lea    0x75(%rsp),%rdi
add    0x8(%rsp),%rbx
lea    0x10(%rsp),%rdx
mov    %rbx,%rsi
call   1050 <unencode@plt>
lea    0xe14(%rip),%rdi
lea    0xe20(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12d6 <main+0x126>
lea    0xeff(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <fprintf@plt>
test   %eax,%eax
jle    12e4 <main+0x134>
lea    0xe16(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xe87(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12c8 <main+0x118>
lea    0xd84(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
add    $0xd0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdab(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdeb(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%ebp
mov    0x2cd6(%rip),%r14
mov    (%r14),%rdi
lea    0xde3(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
lea    0xde7(%rip),%rdi
call   10c0 <perror@plt>
mov    %ebp,%edi
call   10d0 <strerror@plt>
mov    (%r14),%rdi
lea    0xde9(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    129d <main+0xed>

LLVM IR (-O0):
@.str = [8 x i8] c"%s%c%c\0A\00"
@.str.1 = [42 x i8] c"Content-Type:text/html;charset=iso-8859-1\00"
@.str.2 = [25 x i8] c"<TITLE>Response</TITLE>\0A\00"
@.str.3 = [15 x i8] c"CONTENT_LENGTH\00"
@.str.4 = [4 x i8] c"%ld\00"
@.str.5 = [46 x i8] c"<P>Error in invocation - wrong FORM probably.\00"
@stdin = global i32 0
@.str.6 = [19 x i8] c"./www/cgi/data.txt\00"
@.str.7 = [2 x i8] c"a\00"
@.str.8 = [34 x i8] c"<P>Sorry, cannot store your data.\00"
@.str.9 = [3 x i8] c"%s\00"
@.str.10 = [44 x i8] c"Write to file successful with length %d<br>\00"
@.str.11 = [25 x i8] c"Write to file failed<br>\00"
@errno = global i32 0
@stderr = global i32* null
@.str.12 = [20 x i8] c"Value of errno: %d\0A\00"
@.str.13 = [24 x i8] c"Error printed by perror\00"
@.str.14 = [24 x i8] c"Error opening file: %s\0A\00"
@.str.15 = [73 x i8] c"<P>Thank you! The following contribution of yours has been stored:<BR>%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [87 x i8]
%4 = alloca [87 x i8]
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i32 0, i32* %1
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 13, i32 10)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%12 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i8* %12, i8** %2
%13 = load i8*, i8** %2
%14 = icmp eq i8* %13, null
br i1 %14, label %22, label %15
15: ; preds = %0
%16 = load i8*, i8** %2
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64* %5)
%18 = icmp ne i32 %17, 1
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load i64, i64* %5
%21 = icmp sgt i64 %20, 80
br i1 %21, label %22, label %24
22: ; preds = %19, %15, %0
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0))
br label %69
24: ; preds = %19
%25 = getelementptr inbounds [87 x i8], [87 x i8]* %3, i64 0, i64 0
%26 = load i64, i64* %5
%27 = add nsw i64 %26, 1
%28 = load i32, i32* @stdin
%29 = call i32 @fgets(i8* %25, i64 %27, i32 %28)
%30 = getelementptr inbounds [87 x i8], [87 x i8]* %3, i64 0, i64 0
%31 = getelementptr inbounds i8, i8* %30, i64 5
%32 = getelementptr inbounds [87 x i8], [87 x i8]* %3, i64 0, i64 0
%33 = load i64, i64* %5
%34 = getelementptr inbounds i8, i8* %32, i64 %33
%35 = getelementptr inbounds [87 x i8], [87 x i8]* %4, i64 0, i64 0
%36 = call i32 @unencode(i8* %31, i8* %34, i8* %35)
%37 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32* %37, i32** %9
%38 = load i32*, i32** %9
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %42
40: ; preds = %24
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
br label %46
42: ; preds = %24
%43 = load i32*, i32** %9
%44 = getelementptr inbounds [87 x i8], [87 x i8]* %4, i64 0, i64 0
%45 = call i32 (i32*, i8*, ...) @fprintf(i32* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8* %44)
store i32 %45, i32* %6
br label %46
46: ; preds = %42, %40
%47 = load i32, i32* %6
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* %6
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.10, i64 0, i64 0), i32 %50)
br label %64
52: ; preds = %46
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.11, i64 0, i64 0))
%54 = load i32, i32* @errno
store i32 %54, i32* %7
%55 = load i32*, i32** @stderr
%56 = load i32, i32* @errno
%57 = call i32 (i32*, i8*, ...) @fprintf(i32* %55, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0), i32 %56)
%58 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.13, i64 0, i64 0))
%59 = load i32, i32* %7
%60 = call i8* @strerror(i32 %59)
store i8* %60, i8** %8
%61 = load i32*, i32** @stderr
%62 = load i8*, i8** %8
%63 = call i32 (i32*, i8*, ...) @fprintf(i32* %61, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.14, i64 0, i64 0), i8* %62)
br label %64
64: ; preds = %52, %49
%65 = load i32*, i32** %9
%66 = call i32 @fclose(i32* %65)
%67 = getelementptr inbounds [87 x i8], [87 x i8]* %4, i64 0, i64 0
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.15, i64 0, i64 0), i8* %67)
br label %69
69: ; preds = %64, %22
ret i32 0
}

LLVM IR (-O2):
@.str = [8 x i8] c"%s%c%c\0A\00"
@.str.1 = [42 x i8] c"Content-Type:text/html;charset=iso-8859-1\00"
@.str.3 = [15 x i8] c"CONTENT_LENGTH\00"
@.str.4 = [4 x i8] c"%ld\00"
@.str.5 = [46 x i8] c"<P>Error in invocation - wrong FORM probably.\00"
@stdin = global i32 0
@.str.6 = [19 x i8] c"./www/cgi/data.txt\00"
@.str.7 = [2 x i8] c"a\00"
@.str.8 = [34 x i8] c"<P>Sorry, cannot store your data.\00"
@.str.9 = [3 x i8] c"%s\00"
@.str.10 = [44 x i8] c"Write to file successful with length %d<br>\00"
@.str.11 = [25 x i8] c"Write to file failed<br>\00"
@errno = global i32 0
@stderr = global i32* null
@.str.12 = [20 x i8] c"Value of errno: %d\0A\00"
@.str.13 = [24 x i8] c"Error printed by perror\00"
@.str.14 = [24 x i8] c"Error opening file: %s\0A\00"
@.str.15 = [73 x i8] c"<P>Thank you! The following contribution of yours has been stored:<BR>%s\00"
@str = [24 x i8] c"<TITLE>Response</TITLE>\00"
define i32 @main() {
%1 = alloca [87 x i8]
%2 = alloca [87 x i8]
%3 = alloca i64
%4 = getelementptr inbounds [87 x i8], [87 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [87 x i8], [87 x i8]* %2, i64 0, i64 0
%6 = bitcast i64* %3 to i8*
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 13, i32 10)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, 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.4, i64 0, i64 0), i64* %3)
%13 = icmp ne i32 %12, 1
%14 = load i64, i64* %3
%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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0))
br label %48
19: ; preds = %11
%20 = add nsw i64 %14, 1
%21 = load i32, i32* @stdin
%22 = call i32 @fgets(i8* %4, i64 %20, i32 %21)
%23 = getelementptr inbounds [87 x i8], [87 x i8]* %1, i64 0, i64 5
%24 = load i64, i64* %3
%25 = getelementptr inbounds [87 x i8], [87 x i8]* %1, i64 0, i64 %24
%26 = call i32 @unencode(i8* %23, i8* %25, i8* %5)
%27 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
br label %36
31: ; preds = %19
%32 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8* %5)
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.10, i64 0, i64 0), i32 %32)
br label %45
36: ; preds = %29, %31
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.11, i64 0, i64 0))
%38 = load i32, i32* @errno
%39 = load i32*, i32** @stderr
%40 = call i32 (i32*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0), i32 %38)
%41 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.13, i64 0, i64 0))
%42 = call i8* @strerror(i32 %38)
%43 = load i32*, i32** @stderr
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %43, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.14, i64 0, i64 0), i8* %42)
br label %45
45: ; preds = %36, %34
%46 = call i32 @fclose(i32* %27)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([73 x i8], [73 x i8]* @.str.15, i64 0, i64 0), i8* %5)
br label %48
48: ; preds = %45, %17
ret i32 0
}