#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 */
char EOF;
int EXIT_SUCCESS;
int atoi(int /*<<< orphan*/);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ copyWithInject(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                  char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getUserInfo(char *, char *, char *, int);
char getchar();
int /*<<< orphan*/ getenv(char *);
int /*<<< orphan*/ printRedirect(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stdout;
char validatePassword(char *, char *);
int /*<<< orphan*/ writeCSVRow(int /*<<< orphan*/ *, char **, int);
int main() {
  int n = atoi(getenv("CONTENT_LENGTH"));
  char *content = (char *)calloc(n + 1, sizeof(char));
  int i = 0;
  for (i = 0; i < n; i++) {
    char c = getchar();
    if (c == EOF) {
      break;
    }
    content[i] = c;
  }
  content[i] = '\0';
  char username[201];
  char password[201];
  getUserInfo(content, username, password, 201);
  char goodPassword = validatePassword(username, password);
  printf("Content-Type:text/html\n\n");
  if (goodPassword) {
    FILE *loggedIn = fopen("database/LoggedIn.csv", "a");
    char *usernamePointer = username;
    writeCSVRow(loggedIn, &usernamePointer, 1);
    fclose(loggedIn);
    FILE *catalogue = fopen("catalogue.html", "r");
    char *injectFormat = "<input type='hidden' name='username' value=\"%s\"/>";
    char inject[260];
    sprintf(inject, injectFormat, username);
    copyWithInject(catalogue, stdout, "<!--INPUT_USERNAME-->", inject);
    fclose(catalogue);
  } else {
    printRedirect("login_error.html");
  }
  free(content);
  fflush(stdout);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2e0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xdf7(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
call   10e0 <atoi@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1287 <main+0x97>
mov    $0x0,%al
call   1090 <getchar@plt>
mov    %al,-0x15(%rbp)
movsbl -0x15(%rbp),%eax
mov    0x2d8d(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    126b <main+0x7b>
jmp    1287 <main+0x97>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    123a <main+0x4a>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
lea    -0xe0(%rbp),%rsi
lea    -0x1b0(%rbp),%rdx
mov    $0xc9,%ecx
call   1110 <getUserInfo@plt>
lea    -0xe0(%rbp),%rdi
lea    -0x1b0(%rbp),%rsi
call   10b0 <validatePassword@plt>
mov    %al,-0x1b1(%rbp)
lea    0xd40(%rip),%rdi
call   1060 <printf@plt>
cmpb   $0x0,-0x1b1(%rbp)
je     13a3 <main+0x1b3>
lea    0xd40(%rip),%rdi
lea    0xd4f(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x1c0(%rbp)
lea    -0xe0(%rbp),%rax
mov    %rax,-0x1c8(%rbp)
mov    -0x1c0(%rbp),%rdi
lea    -0x1c8(%rbp),%rsi
mov    $0x1,%edx
call   10f0 <writeCSVRow@plt>
mov    -0x1c0(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xd0c(%rip),%rdi
lea    0xd14(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x1d0(%rbp)
lea    0xd03(%rip),%rax
mov    %rax,-0x1d8(%rbp)
lea    -0x2e0(%rbp),%rdi
mov    -0x1d8(%rbp),%rsi
lea    -0xe0(%rbp),%rdx
call   1100 <sprintf@plt>
mov    -0x1d0(%rbp),%rdi
mov    0x2c5b(%rip),%rax
mov    (%rax),%esi
lea    -0x2e0(%rbp),%rcx
lea    0xcf6(%rip),%rdx
call   10a0 <copyWithInject@plt>
mov    -0x1d0(%rbp),%rdi
call   1050 <fclose@plt>
jmp    13af <main+0x1bf>
lea    0xcef(%rip),%rdi
call   1070 <printRedirect@plt>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
mov    0x2c19(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    0x2c03(%rip),%rax
mov    (%rax),%eax
add    $0x2e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2b8,%rsp
lea    0xdfb(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%edi
call   10e0 <atoi@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%r15
test   %ebx,%ebx
jle    125f <main+0x6f>
mov    %ebx,%r14d
mov    0x2db7(%rip),%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1090 <getchar@plt>
cmp    (%r12),%al
je     1261 <main+0x71>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1240 <main+0x50>
mov    %r14d,%ebx
jmp    1261 <main+0x71>
xor    %ebx,%ebx
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
lea    0xe0(%rsp),%r14
lea    0x10(%rsp),%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    $0xc9,%ecx
call   1110 <getUserInfo@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10b0 <validatePassword@plt>
mov    %eax,%ebx
lea    0xdf5(%rip),%rdi
call   1050 <puts@plt>
test   %bl,%bl
je     1333 <main+0x143>
lea    0xd5f(%rip),%rdi
lea    0xd6e(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%rbx
mov    %r14,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    $0x1,%edx
call   10f0 <writeCSVRow@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xd42(%rip),%rdi
lea    0xd4a(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%rbx
lea    0xd3d(%rip),%rsi
lea    0x1b0(%rsp),%r12
mov    %r12,%rdi
mov    %r14,%rdx
call   1100 <sprintf@plt>
mov    0x2cc3(%rip),%rax
mov    (%rax),%esi
lea    0xd4c(%rip),%rdx
mov    %rbx,%rdi
mov    %r12,%rcx
call   10a0 <copyWithInject@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    133f <main+0x14f>
lea    0xd46(%rip),%rdi
call   1070 <printRedirect@plt>
mov    %r15,%rdi
call   1040 <free@plt>
mov    0x2c8a(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    0x2c74(%rip),%rax
mov    (%rax),%eax
add    $0x2b8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [15 x i8] c"CONTENT_LENGTH\00"
@EOF = global i8 0
@.str.1 = [25 x i8] c"Content-Type:text/html\0A\0A\00"
@.str.2 = [22 x i8] c"database/LoggedIn.csv\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [15 x i8] c"catalogue.html\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [50 x i8] c"<input type='hidden' name='username' value=\22%s\22/>\00"
@stdout = global i32 0
@.str.7 = [22 x i8] c"<!--INPUT_USERNAME-->\00"
@.str.8 = [17 x i8] c"login_error.html\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca [201 x i8]
%7 = alloca [201 x i8]
%8 = alloca i8
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca [260 x i8]
store i32 0, i32* %1
%14 = call i32 @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @atoi(i32 %14)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
%18 = call i64 @calloc(i32 %17, i32 1)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %3
store i32 0, i32* %4
store i32 0, i32* %4
br label %20
20: ; preds = %38, %0
%21 = load i32, i32* %4
%22 = load i32, i32* %2
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %41
24: ; preds = %20
%25 = call signext i8 (...) @getchar()
store i8 %25, i8* %5
%26 = load i8, i8* %5
%27 = sext i8 %26 to i32
%28 = load i8, i8* @EOF
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %27, %29
br i1 %30, label %31, label %32
31: ; preds = %24
br label %41
32: ; preds = %24
%33 = load i8, i8* %5
%34 = load i8*, i8** %3
%35 = load i32, i32* %4
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 %33, i8* %37
br label %38
38: ; preds = %32
%39 = load i32, i32* %4
%40 = add nsw i32 %39, 1
store i32 %40, i32* %4
br label %20
41: ; preds = %31, %20
%42 = load i8*, i8** %3
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %3
%47 = getelementptr inbounds [201 x i8], [201 x i8]* %6, i64 0, i64 0
%48 = getelementptr inbounds [201 x i8], [201 x i8]* %7, i64 0, i64 0
%49 = call i32 @getUserInfo(i8* %46, i8* %47, i8* %48, i32 201)
%50 = getelementptr inbounds [201 x i8], [201 x i8]* %6, i64 0, i64 0
%51 = getelementptr inbounds [201 x i8], [201 x i8]* %7, i64 0, i64 0
%52 = call signext i8 @validatePassword(i8* %50, i8* %51)
store i8 %52, i8* %8
%53 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%54 = load i8, i8* %8
%55 = icmp ne i8 %54, 0
br i1 %55, label %56, label %74
56: ; preds = %41
%57 = call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %57, i32** %9
%58 = getelementptr inbounds [201 x i8], [201 x i8]* %6, i64 0, i64 0
store i8* %58, i8** %10
%59 = load i32*, i32** %9
%60 = call i32 @writeCSVRow(i32* %59, i8** %10, i32 1)
%61 = load i32*, i32** %9
%62 = call i32 @fclose(i32* %61)
%63 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %63, i32** %11
store i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.6, i64 0, i64 0), i8** %12
%64 = getelementptr inbounds [260 x i8], [260 x i8]* %13, i64 0, i64 0
%65 = load i8*, i8** %12
%66 = getelementptr inbounds [201 x i8], [201 x i8]* %6, i64 0, i64 0
%67 = call i32 @sprintf(i8* %64, i8* %65, i8* %66)
%68 = load i32*, i32** %11
%69 = load i32, i32* @stdout
%70 = getelementptr inbounds [260 x i8], [260 x i8]* %13, i64 0, i64 0
%71 = call i32 @copyWithInject(i32* %68, i32 %69, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %70)
%72 = load i32*, i32** %11
%73 = call i32 @fclose(i32* %72)
br label %76
74: ; preds = %41
%75 = call i32 @printRedirect(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
br label %76
76: ; preds = %74, %56
%77 = load i8*, i8** %3
%78 = call i32 @free(i8* %77)
%79 = load i32, i32* @stdout
%80 = call i32 @fflush(i32 %79)
%81 = load i32, i32* @EXIT_SUCCESS
ret i32 %81
}

LLVM IR (-O2):
@.str = [15 x i8] c"CONTENT_LENGTH\00"
@EOF = global i8 0
@.str.2 = [22 x i8] c"database/LoggedIn.csv\00"
@.str.3 = [2 x i8] c"a\00"
@.str.4 = [15 x i8] c"catalogue.html\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [50 x i8] c"<input type='hidden' name='username' value=\22%s\22/>\00"
@stdout = global i32 0
@.str.7 = [22 x i8] c"<!--INPUT_USERNAME-->\00"
@.str.8 = [17 x i8] c"login_error.html\00"
@EXIT_SUCCESS = global i32 0
@str = [24 x i8] c"Content-Type:text/html\0A\00"
define i32 @main() {
%1 = alloca [201 x i8]
%2 = alloca [201 x i8]
%3 = alloca i8*
%4 = alloca [260 x i8]
%5 = tail call i32 @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @atoi(i32 %5)
%7 = add nsw i32 %6, 1
%8 = tail call i64 @calloc(i32 %7, i32 1)
%9 = inttoptr i64 %8 to i8*
%10 = icmp sgt i32 %6, 0
br i1 %10, label %11, label %24
11: ; preds = %0
%12 = zext i32 %6 to i64
br label %13
13: ; preds = %11, %18
%14 = phi i64 [ 0, %11 ], [ %20, %18 ]
%15 = tail call signext i8 (...) @getchar()
%16 = load i8, i8* @EOF
%17 = icmp eq i8 %15, %16
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = getelementptr inbounds i8, i8* %9, i64 %14
store i8 %15, i8* %19
%20 = add nuw nsw i64 %14, 1
%21 = icmp eq i64 %20, %12
br i1 %21, label %24, label %13
22: ; preds = %13
%23 = trunc i64 %14 to i32
br label %24
24: ; preds = %18, %22, %0
%25 = phi i32 [ 0, %0 ], [ %23, %22 ], [ %6, %18 ]
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %9, i64 %26
store i8 0, i8* %27
%28 = getelementptr inbounds [201 x i8], [201 x i8]* %1, i64 0, i64 0
%29 = getelementptr inbounds [201 x i8], [201 x i8]* %2, i64 0, i64 0
%30 = call i32 @getUserInfo(i8* %9, i8* %28, i8* %29, i32 201)
%31 = call signext i8 @validatePassword(i8* %28, i8* %29)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%33 = icmp eq i8 %31, 0
br i1 %33, label %45, label %34
34: ; preds = %24
%35 = call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%36 = bitcast i8** %3 to i8*
store i8* %28, i8** %3
%37 = call i32 @writeCSVRow(i32* %35, i8** %3, i32 1)
%38 = call i32 @fclose(i32* %35)
%39 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%40 = getelementptr inbounds [260 x i8], [260 x i8]* %4, i64 0, i64 0
%41 = call i32 @sprintf(i8* %40, i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.6, i64 0, i64 0), i8* %28)
%42 = load i32, i32* @stdout
%43 = call i32 @copyWithInject(i32* %39, i32 %42, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %40)
%44 = call i32 @fclose(i32* %39)
br label %47
45: ; preds = %24
%46 = call i32 @printRedirect(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
br label %47
47: ; preds = %45, %34
%48 = call i32 @free(i8* %9)
%49 = load i32, i32* @stdout
%50 = call i32 @fflush(i32 %49)
%51 = load i32, i32* @EXIT_SUCCESS
ret i32 %51
}