// File: /workspace/github_c_makefile_star2_O1/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, -0x48(%rbp)
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, %r15
movl $0xc8, %edi
callq 0x21f0
movq %rax, %r14
cmpl $-0x1, %ebx
movl %ebx, -0x38(%rbp)
je 0x2b45
movq %r13, %rdi
xorl %esi, %esi
callq 0x22a0
testl %eax, %eax
je 0x2c5e
movups 0x151e(%rip), %xmm0     # 0x4049
movups %xmm0, 0x9(%r14)
movups 0x1509(%rip), %xmm0     # 0x4040
movups %xmm0, (%r14)
movb $0x1, %al
movl %eax, -0x2c(%rbp)
xorl %r12d, %r12d
jmp 0x2b66
movups 0x14e4(%rip), %xmm0     # 0x4030
movups %xmm0, 0xb(%r14)
movups 0x14cd(%rip), %xmm0     # 0x4025
movups %xmm0, (%r14)
xorl %r12d, %r12d
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0xc8, (%r15)
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 0x14e3(%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 0x14aa(%rip), %xmm0     # 0x40a8
movups %xmm0, (%r14,%rax)
movups 0x14ae(%rip), %xmm0     # 0x40b8
movups %xmm0, 0x10(%r14,%rax)
movq %r14, %rdi
callq 0x20e0
movups 0x14ae(%rip), %xmm0     # 0x40cd
movups %xmm0, (%r14,%rax)
movb $0x0, 0x10(%r14,%rax)
testb %r12b, %r12b
je 0x2c45
leaq -0x34(%rbp), %rsi
movq %r13, %rdi
callq 0x2a43
movq %rax, -0x40(%rbp)
movl -0x38(%rbp), %r12d
jmp 0x2cb6
movl -0x38(%rbp), %r12d
cmpl $-0x1, %r12d
je 0x2c9e
cmpb $0x0, -0x2c(%rbp)
je 0x2ca7
movl $0x32, -0x34(%rbp)
jmp 0x2cae
movq %r13, %rdi
movl $0x4, %esi
callq 0x22a0
testl %eax, %eax
je 0x2e88
movups 0x13e8(%rip), %xmm0     # 0x4062
movups %xmm0, 0x9(%r14)
movups 0x13d3(%rip), %xmm0     # 0x4059
movups %xmm0, (%r14)
movb $0x1, %al
movl %eax, -0x30(%rbp)
xorl %r12d, %r12d
movl $0x0, -0x2c(%rbp)
jmp 0x2b6d
movl $0x25, -0x34(%rbp)
jmp 0x2cae
movl $0x47, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
movl -0x34(%rbp), %r13d
movl %r13d, %edi
callq 0x26c6
movq %rax, %rbx
movq %r14, %rdi
movq %rax, %rsi
callq 0x22f0
movq %rbx, %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 0x13e2(%rip), %xmm0     # 0x40de
movups %xmm0, (%r14,%rax)
movups 0x13e0(%rip), %xmm0     # 0x40e8
movups %xmm0, 0xa(%r14,%rax)
movq %r14, %rdi
callq 0x20e0
movabsq $0xa0d0a0d646573, %rcx  # imm = 0xA0D0A0D646573
movq %rcx, 0xf(%r14,%rax)
movups 0x13cc(%rip), %xmm0     # 0x40f8
movups %xmm0, (%r14,%rax)
movq %r14, %rdi
callq 0x20e0
movq %rax, %rbx
movl %ebx, (%r15)
cmpl $-0x1, %r12d
je 0x2d9f
cmpb $0x0, -0x2c(%rbp)
je 0x2de8
leal 0x34(%rbx), %eax
movl %eax, (%r15)
movslq %eax, %rsi
movq %r14, %rdi
callq 0x2220
movq %rax, %r14
movq %rax, %rdi
callq 0x20e0
movups 0x13c2(%rip), %xmm0     # 0x4134
movups %xmm0, (%r14,%rax)
movups 0x13c6(%rip), %xmm0     # 0x4144
movups %xmm0, 0x10(%r14,%rax)
movups 0x13c9(%rip), %xmm0     # 0x4154
movups %xmm0, 0x20(%r14,%rax)
movl $0xa0d3e, 0x30(%r14,%rax) # imm = 0xA0D3E
jmp 0x2e6f
leal 0x25(%rbx), %eax
movl %eax, (%r15)
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 0x133e(%rip), %xmm0     # 0x410f
movups %xmm0, (%r14,%rax)
movups 0x1342(%rip), %xmm0     # 0x411f
movups %xmm0, 0x10(%r14,%rax)
jmp 0x2e6f
cmpb $0x0, -0x30(%rbp)
je 0x2e23
leal 0x49(%rbx), %eax
movl %eax, (%r15)
movslq %eax, %rsi
movq %r14, %rdi
callq 0x2220
movq %rax, %r14
movq %rax, %rdi
callq 0x20e0
movq %r14, %rdi
addq %rax, %rdi
leaq 0x1351(%rip), %rsi      # 0x4168
movl $0x49, %edx
callq 0x21b0
jmp 0x2e6f
movq -0x40(%rbp), %r12
movq %r12, %rdi
callq 0x20e0
addq %rbx, %rax
addq $0x3, %rax
movl %eax, (%r15)
movslq %eax, %rsi
movq %r14, %rdi
callq 0x2220
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
callq 0x22f0
movq %r14, %rdi
callq 0x20e0
movw $0xa0d, (%r14,%rax)     # imm = 0xA0D
movb $0x0, 0x2(%r14,%rax)
movq %r12, %rdi
callq 0x2030
movq -0x48(%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 0x11e3(%rip), %xmm0     # 0x4072
movups %xmm0, (%r14)
movw $0xa, 0x10(%r14)
movb $0x1, %r12b
jmp 0x2b5f