// File: /workspace/github_c_makefile_star2_O3/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 0x21f0
movq %rax, %r14
cmpl $-0x1, %r12d
je 0x2b51
movq %r13, %rdi
xorl %esi, %esi
callq 0x22a0
testl %eax, %eax
je 0x2deb
movups 0x1512(%rip), %xmm0     # 0x4049
movups %xmm0, 0x9(%r14)
movups 0x14fd(%rip), %xmm0     # 0x4040
movups %xmm0, (%r14)
movb $0x1, %al
movl %eax, -0x2c(%rbp)
xorl %r15d, %r15d
jmp 0x2b72
movups 0x14d8(%rip), %xmm0     # 0x4030
movups %xmm0, 0xb(%r14)
movups 0x14c1(%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 0x21c0
movq %rbx, %rdi
callq 0x2040
leaq 0x14d4(%rip), %rdx      # 0x408b
leaq -0x70(%rbp), %rbx
movl $0x1a, %esi
movq %rbx, %rdi
movq %rax, %rcx
callq 0x2270
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22f0
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 0x149b(%rip), %xmm0     # 0x40a8
movups %xmm0, (%r14,%rax)
movups 0x149f(%rip), %xmm0     # 0x40b8
movups %xmm0, 0x10(%r14,%rax)
movq %r14, %rdi
callq 0x20e0
movups 0x149f(%rip), %xmm0     # 0x40cd
movups %xmm0, (%r14,%rax)
movb $0x0, 0x10(%r14,%rax)
testb %r15b, %r15b
je 0x2c55
leaq -0x44(%rbp), %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x2a43
movq %rax, %rbx
movl (%r15), %r13d
jmp 0x2c76
cmpb $0x0, -0x2c(%rbp)
movl $0x32, %eax
movl $0x47, %ecx
cmovnel %eax, %ecx
cmpl $-0x1, %r12d
movl $0x25, %r13d
cmovnel %ecx, %r13d
xorl %ebx, %ebx
movl %r13d, %edi
callq 0x26c7
movq %rax, %r15
movq %r14, %rdi
movq %rax, %rsi
callq 0x22f0
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 0x1426(%rip), %xmm0     # 0x40de
movups %xmm0, (%r14,%rax)
movups 0x1424(%rip), %xmm0     # 0x40e8
movups %xmm0, 0xa(%r14,%rax)
movq %r14, %rdi
callq 0x20e0
movabsq $0xa0d0a0d646573, %rcx  # imm = 0xA0D0A0D646573
movq %rcx, 0xf(%r14,%rax)
movups 0x1410(%rip), %xmm0     # 0x40f8
movups %xmm0, (%r14,%rax)
movq %r14, %rdi
callq 0x20e0
movq %rax, %r15
cmpl $-0x1, %r12d
je 0x2d5c
cmpb $0x0, -0x2c(%rbp)
je 0x2da9
leal 0x34(%r15), %eax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movslq %eax, %rsi
movq %r14, %rdi
callq 0x2220
movq %rax, %r14
movq %rax, %rdi
callq 0x20e0
movups 0x1405(%rip), %xmm0     # 0x4134
movups %xmm0, (%r14,%rax)
movups 0x1409(%rip), %xmm0     # 0x4144
movups %xmm0, 0x10(%r14,%rax)
movups 0x140c(%rip), %xmm0     # 0x4154
movups %xmm0, 0x20(%r14,%rax)
movl $0xa0d3e, 0x30(%r14,%rax) # imm = 0xA0D3E
jmp 0x2e76
leal 0x25(%r15), %eax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movslq %eax, %rsi
movq %r14, %rdi
callq 0x2220
movq %rax, %r14
movq %rax, %rdi
callq 0x20e0
movabsq $0xa0d3e6c6d7468, %rcx  # imm = 0xA0D3E6C6D7468
movq %rcx, 0x1d(%r14,%rax)
movups 0x137d(%rip), %xmm0     # 0x410f
movups %xmm0, (%r14,%rax)
movups 0x1381(%rip), %xmm0     # 0x411f
movups %xmm0, 0x10(%r14,%rax)
jmp 0x2e76
cmpb $0x0, -0x30(%rbp)
je 0x2e2b
leal 0x49(%r15), %eax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movslq %eax, %rsi
movq %r14, %rdi
callq 0x2220
movq %rax, %r14
movq %rax, %rdi
callq 0x20e0
movq %r14, %rdi
addq %rax, %rdi
leaq 0x138c(%rip), %rsi      # 0x4168
movl $0x49, %edx
callq 0x21b0
jmp 0x2e76
movq %r13, %rdi
movl $0x4, %esi
callq 0x22a0
testl %eax, %eax
je 0x2e8f
movups 0x125b(%rip), %xmm0     # 0x4062
movups %xmm0, 0x9(%r14)
movups 0x1246(%rip), %xmm0     # 0x4059
movups %xmm0, (%r14)
movb $0x1, %al
movl %eax, -0x30(%rbp)
xorl %r15d, %r15d
movl $0x0, -0x2c(%rbp)
jmp 0x2b79
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 0x2220
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22f0
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 0x11dc(%rip), %xmm0     # 0x4072
movups %xmm0, (%r14)
movw $0xa, 0x10(%r14)
movb $0x1, %r15b
jmp 0x2b6b