// File: /workspace/github_c_makefile_star2_O2/stefpant[P]TCPserver/webserver/functions.c
// Function: create_answer
char* create_answer(int *curlen,char *page,int err,int* pagebytes){
	time_t timer;
	char timebuffer[26];
	struct tm* tm_info;
	char* ans = malloc(200*sizeof(char));
	int flag=0;

	if( err == -1){
		strcpy(ans,"HTTP/1.1 400 Bad Request\r\n");
		flag=1;
	}
	else if(0 != access(page,F_OK)){
		strcpy(ans,"HTTP/1.1 404 Not Found\r\n");
		flag=2;
	}
	else if(0 != access(page,R_OK)){
		strcpy(ans,"HTTP/1.1 403 Forbidden\r\n");
		flag=3;
	}
	else
		strcpy(ans,"HTTP/1.1 200 OK\r\n");
	*curlen = 200;
	int bytes;
	//char* line = NULL;
	char* charbytes = NULL;

	strcat(ans,"Date: ");

	time(&timer);
	tm_info = localtime(&timer);
	strftime(timebuffer, 26, "%a, %d-%m-%Y %H:%M:%S", tm_info);

	strcat(ans,timebuffer);
	strcat(ans," GMT\r\n");
	strcat(ans,"Server: webserver/1.0.0 (Ubuntu64)\r\n");
	strcat(ans,"Content-Length: ");
	char* cppage = NULL;
	if( !flag )
		cppage = copy_page(page,&bytes);
	else if(flag == 1)
		bytes = 37;
	else if(flag == 2)
		bytes = 50;
	else
		bytes = 71;
	charbytes = myitoa(bytes);
	strcat(ans,charbytes);
	free(charbytes);
	strcat(ans,"\r\n");
	strcat(ans,"Content-Type: text/html\r\n");
	strcat(ans,"Connection: Closed\r\n\r\n");
	*curlen = strlen(ans);
	if(flag == 1){
		*curlen += 37;
		ans = (char *)realloc(ans,(*curlen)*sizeof(char));
		strcat(ans,"<html>Bad request received!</html>\r\n");
	}
	else if(flag == 2){
		*curlen += 52;
		ans = (char *)realloc(ans,(*curlen)*sizeof(char));
		strcat(ans,"<html>Sorry dude, couldn't find this file!</html>\r\n");
	}
	else if(flag == 3){
		*curlen += 73;
		ans = (char *)realloc(ans,(*curlen)*sizeof(char));
		strcat(ans,"<html>Trying to access this file but don't think I can make it!</html>\r\n");
	}
	else{

		*curlen += strlen(cppage) + 3;//for \0 and in the end \r\n
		ans = (char *)realloc(ans,(*curlen)*sizeof(char));
		strcat(ans,cppage);
		strcat(ans,"\r\n");
		free(cppage);
	}
	*pagebytes = bytes;
	return ans;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, -0x40(%rbp)
movl %edx, %r12d
movq %rsi, %r13
movq %rdi, %rbx
movl $0xc8, %edi
callq 0x21e0
movq %rax, %r14
cmpl $-0x1, %r12d
je 0x29a9
movq %r13, %rdi
xorl %esi, %esi
callq 0x2290
testl %eax, %eax
je 0x2c39
movups 0x16ba(%rip), %xmm0     # 0x4049
movups %xmm0, 0x9(%r14)
movups 0x16a5(%rip), %xmm0     # 0x4040
movups %xmm0, (%r14)
movb $0x1, %al
movl %eax, -0x2c(%rbp)
xorl %r15d, %r15d
jmp 0x29ca
movups 0x1680(%rip), %xmm0     # 0x4030
movups %xmm0, 0xb(%r14)
movups 0x1669(%rip), %xmm0     # 0x4025
movups %xmm0, (%r14)
xorl %r15d, %r15d
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq %rbx, -0x38(%rbp)
movl $0xc8, (%rbx)
movq %r14, %rdi
callq 0x20e0
movl $0x65746144, (%r14,%rax) # imm = 0x65746144
movl $0x203a65, 0x3(%r14,%rax) # imm = 0x203A65
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x21b0
movq %rbx, %rdi
callq 0x2040
leaq 0x167c(%rip), %rdx      # 0x408b
leaq -0x70(%rbp), %rbx
pushq $0x1a
popq %rsi
movq %rbx, %rdi
movq %rax, %rcx
callq 0x2260
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22e0
movq %r14, %rdi
callq 0x20e0
movl $0x544d4720, (%r14,%rax) # imm = 0x544D4720
movl $0xa0d54, 0x3(%r14,%rax) # imm = 0xA0D54
movq %r14, %rdi
callq 0x20e0
movabsq $0xa0d2934367574, %rcx  # imm = 0xA0D2934367574
movq %rcx, 0x1d(%r14,%rax)
movups 0x1645(%rip), %xmm0     # 0x40a8
movups %xmm0, (%r14,%rax)
movups 0x1649(%rip), %xmm0     # 0x40b8
movups %xmm0, 0x10(%r14,%rax)
movq %r14, %rdi
callq 0x20e0
movups 0x1649(%rip), %xmm0     # 0x40cd
movups %xmm0, (%r14,%rax)
movb $0x0, 0x10(%r14,%rax)
testb %r15b, %r15b
je 0x2aab
leaq -0x44(%rbp), %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x28a2
movq %rax, %rbx
movl (%r15), %r13d
jmp 0x2ac6
cmpb $0x0, -0x2c(%rbp)
pushq $0x32
popq %rax
pushq $0x47
popq %rcx
cmovnel %eax, %ecx
cmpl $-0x1, %r12d
pushq $0x25
popq %r13
cmovnel %ecx, %r13d
xorl %ebx, %ebx
movl %r13d, %edi
callq 0x25b7
movq %rax, %r15
movq %r14, %rdi
movq %rax, %rsi
callq 0x22e0
movq %r15, %rdi
callq 0x2030
movq %r14, %rdi
callq 0x20e0
movw $0xa0d, (%r14,%rax)     # imm = 0xA0D
movb $0x0, 0x2(%r14,%rax)
movq %r14, %rdi
callq 0x20e0
movups 0x15d6(%rip), %xmm0     # 0x40de
movups %xmm0, (%r14,%rax)
movups 0x15d4(%rip), %xmm0     # 0x40e8
movups %xmm0, 0xa(%r14,%rax)
movq %r14, %rdi
callq 0x20e0
movabsq $0xa0d0a0d646573, %rcx  # imm = 0xA0D0A0D646573
movq %rcx, 0xf(%r14,%rax)
movups 0x15c0(%rip), %xmm0     # 0x40f8
movups %xmm0, (%r14,%rax)
movq %r14, %rdi
callq 0x20e0
movq %rax, %r15
cmpl $-0x1, %r12d
je 0x2bac
cmpb $0x0, -0x2c(%rbp)
je 0x2bf9
leal 0x34(%r15), %eax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movslq %eax, %rsi
movq %r14, %rdi
callq 0x2210
movq %rax, %r14
movq %rax, %rdi
callq 0x20e0
movups 0x15b5(%rip), %xmm0     # 0x4134
movups %xmm0, (%r14,%rax)
movups 0x15b9(%rip), %xmm0     # 0x4144
movups %xmm0, 0x10(%r14,%rax)
movups 0x15bc(%rip), %xmm0     # 0x4154
movups %xmm0, 0x20(%r14,%rax)
movl $0xa0d3e, 0x30(%r14,%rax) # imm = 0xA0D3E
jmp 0x2cc2
leal 0x25(%r15), %eax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movslq %eax, %rsi
movq %r14, %rdi
callq 0x2210
movq %rax, %r14
movq %rax, %rdi
callq 0x20e0
movabsq $0xa0d3e6c6d7468, %rcx  # imm = 0xA0D3E6C6D7468
movq %rcx, 0x1d(%r14,%rax)
movups 0x152d(%rip), %xmm0     # 0x410f
movups %xmm0, (%r14,%rax)
movups 0x1531(%rip), %xmm0     # 0x411f
movups %xmm0, 0x10(%r14,%rax)
jmp 0x2cc2
cmpb $0x0, -0x30(%rbp)
je 0x2c77
leal 0x49(%r15), %eax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movslq %eax, %rsi
movq %r14, %rdi
callq 0x2210
movq %rax, %r14
movq %rax, %rdi
callq 0x20e0
movq %r14, %rdi
addq %rax, %rdi
leaq 0x153c(%rip), %rsi      # 0x4168
pushq $0x49
popq %rdx
callq 0x21a0
jmp 0x2cc2
pushq $0x4
popq %rsi
movq %r13, %rdi
callq 0x2290
testl %eax, %eax
je 0x2cdb
movups 0x140f(%rip), %xmm0     # 0x4062
movups %xmm0, 0x9(%r14)
movups 0x13fa(%rip), %xmm0     # 0x4059
movups %xmm0, (%r14)
movb $0x1, %al
movl %eax, -0x30(%rbp)
xorl %r15d, %r15d
movl $0x0, -0x2c(%rbp)
jmp 0x29d1
movq %rbx, %rdi
callq 0x20e0
addq %r15, %rax
addq $0x3, %rax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movslq %eax, %rsi
movq %r14, %rdi
callq 0x2210
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22e0
movq %r14, %rdi
callq 0x20e0
movw $0xa0d, (%r14,%rax)     # imm = 0xA0D
movb $0x0, 0x2(%r14,%rax)
movq %rbx, %rdi
callq 0x2030
movq -0x40(%rbp), %rax
movl %r13d, (%rax)
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movups 0x1390(%rip), %xmm0     # 0x4072
movups %xmm0, (%r14)
movw $0xa, 0x10(%r14)
movb $0x1, %r15b
jmp 0x29c3